iconation/icon-sdk-php

与 ICON 区块链通信的 SDK

v1.2.2 2021-11-03 17:54 UTC

This package is auto-updated.

Last update: 2024-09-24 03:18:20 UTC


README

ICONation logo

PHP 版本的 ICON SDK

License: MIT

这是一个用于与 ICON 区块链通信的 SDK,专为 PHP 构建而成。

免责声明:我不能保证该软件的最佳性能。它按原样提供,不提供任何保证。请自行承担使用风险。

特性

完全或部分支持所有 Iconservice 功能、IRC-2 代币和 IISS 调用。

要求和安装

请确保您正在使用 >=php7.2。然后检查您是否已经安装或安装所需的 PHP 扩展。

apt install php-curl php-xml php-gmp php-bcmath

在您的项目中的 composer.json 文件中添加此包

composer require iconation/icon-sdk-php --no-dev

测试

apt install php-mbstring
composer install
composer test

用法

Iconservice

  • icx_getLastBlock
use iconation\IconSDK\IconService\IconService;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');

$res = $iconservice->getLastBlock();
  • icx_getBlockByHeight
use iconation\IconSDK\IconService\IconService;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');

$res = $iconservice->getBlockByHeight('0x3');
  • icx_getBlockByHash
use iconation\IconSDK\IconService\IconService;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');

$res = $iconservice->getBlockByHash('0x123986e1c834632f6e65915c249d81cd01453ec915e3370d364d6df7be5e6c03');
  • icx_call
use iconation\IconSDK\IconService\IconService;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');

$score = "cx9ab3078e72c8d9017194d17b34b1a47b661945ca";
$params = new stdClass();
$params->method = "balanceOf";
$params->params = new stdClass();
$params->params->_owner = "hx70e8eeb5d23ab18a828ec95f769db6d953e5f0fd";

$res = $iconservice->call($score, $params);
  • icx_getBalance
use iconation\IconSDK\IconService\IconService;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');

$res = $iconservice->getBalance('hx70e8eeb5d23ab18a828ec95f769db6d953e5f0fd');
  • icx_getScoreApi
use iconation\IconSDK\IconService\IconService;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');

$score = "cx9ab3078e72c8d9017194d17b34b1a47b661945ca";
$res = $iconservice->getBalance($score);
  • icx_getTotalSupply
use iconation\IconSDK\IconService\IconService;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');

$res = $iconservice->getTotalSupply();
  • icx_getTransactionResult
use iconation\IconSDK\IconService\IconService;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');

$txHash = '0xb89690b7598e07c286db87f05c1ee4cfc1cf915bf061007ac3404a42dc4979e9';

$res = $iconservice->getTransactionResult($txHash);
  • icx_getTransactionByHash
use iconation\IconSDK\IconService\IconService;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');

$txHash = '0xb89690b7598e07c286db87f05c1ee4cfc1cf915bf061007ac3404a42dc4979e9';

$res = $iconservice->getTransactionByHash($txHash);
  • ise_getStatus
use iconation\IconSDK\IconService\IconService;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');

$keys = ['lastBlock'];

$res = $iconservice->getStatus($keys);
  • icx_sendTransaction
use iconation\IconSDK\IconService\IconService;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');

$private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key
$from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160";
$to = "hxf8689d6c4c8f333651469fdea2ac59a18f6c242d";
$value = "0x2386f26fc10000"; // = 0.01 ICX

$res = $iconservice->send($from, $to, $value, $private_key);
  • 信息
use iconation\IconSDK\IconService\IconService;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');

$private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key
$from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160";
$to = "hxf8689d6c4c8f333651469fdea2ac59a18f6c242d";
$message = "Your message goes here"; // = 0.01 ICX

$res = $iconservice->message($from, $to, $private_key, $message);

IRC-2

  • 名称
use iconation\IconSDK\IconService\IconService;
use iconation\IconSDK\IconService\IRC2;

$contract = 'cx8901ee4f6df58bd437de0e66c9dd3385ba4c2328';
$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');
$irc2 = new IRC2($contract, $iconservice);

$res = $irc2->name();
  • 符号
use iconation\IconSDK\IconService\IconService;
use iconation\IconSDK\IconService\IRC2;

$contract = 'cx8901ee4f6df58bd437de0e66c9dd3385ba4c2328';
$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');
$irc2 = new IRC2($contract, $iconservice);

$res = $irc2->symbol();
  • 小数位
use iconation\IconSDK\IconService\IconService;
use iconation\IconSDK\IconService\IRC2;

$contract = 'cx8901ee4f6df58bd437de0e66c9dd3385ba4c2328';
$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');
$irc2 = new IRC2($contract, $iconservice);

$res = $irc2->decimals();
  • 总供应量
use iconation\IconSDK\IconService\IconService;
use iconation\IconSDK\IconService\IRC2;

$contract = 'cx8901ee4f6df58bd437de0e66c9dd3385ba4c2328';
$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');
$irc2 = new IRC2($contract, $iconservice);

$res = $irc2->totalSupply();
  • 余额
use iconation\IconSDK\IconService\IconService;
use iconation\IconSDK\IconService\IRC2;

$contract = 'cx8901ee4f6df58bd437de0e66c9dd3385ba4c2328';
$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');
$irc2 = new IRC2($contract, $iconservice);

$account = 'hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160';

$res = $irc2->balanceOf($account);
  • 转账
use iconation\IconSDK\IconService\IconService;
use iconation\IconSDK\IconService\IRC2;

$contract = 'cx8901ee4f6df58bd437de0e66c9dd3385ba4c2328';
$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');
$irc2 = new IRC2($contract, $iconservice);

$from = 'hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160';
$to = 'hxf8689d6c4c8f333651469fdea2ac59a18f6c242d';
$value = '1';
$privateKey = '3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5';

$res = $irc2->transfer($from, $to, $value, $privateKey);

IISS

  • 设置抵押
use iconation\IconSDK\IconService\IconService;
use iconation\IconSDK\IISS\IISS;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');
$iiss = new IISS($iconservice);

$value = 0.5; //Stake 0.5 ICX
$from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160";
$private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Staker's private key

$res = $iiss->setStake($value, $from, $private_key);
  • 获取抵押
use iconation\IconSDK\IconService\IconService;
use iconation\IconSDK\IISS\IISS;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');
$iiss = new IISS($iconservice);

$address = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160";

$res = $iiss->getStake($address);
  • 设置委托
use iconation\IconSDK\IconService\IconService;
use iconation\IconSDK\IISS\IISS;
use iconation\IconSDK\IISS\Delegation;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');
$iiss = new IISS($iconservice);

$delegation1 = new Delegation("hxec79e9c1c882632688f8c8f9a07832bcabe8be8f", "0x2c68af0bb140000");
$delegation1 = $delegation1->getDelegationObject();

$delegation2 = new Delegation("hxd3be921dfe193cd49ed7494a53743044e3376cd3", "0x2c68af0bb140000");
$delegation2 = $delegation2->getDelegationObject();

$delegations = array(
                $delegation1, 
                $delegation2
               );
$private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Staker's private key
$from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160";

$res = $iiss->setDelegation($delegations, $from, $private_key);
  • 获取委托
use iconation\IconSDK\IconService\IconService;
use iconation\IconSDK\IISS\IISS;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');
$iiss = new IISS($iconservice);

$address = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160";

$res = $iiss->getDelegation($address);
  • 领取 IScore
use iconation\IconSDK\IconService\IconService;
use iconation\IconSDK\IISS\IISS;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');
$iiss = new IISS($iconservice);

$private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Staker's private key
$from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160";

$res = $iiss->claimIScore($from, $private_key);
  • 查询 IScore
use iconation\IconSDK\IconService\IconService;
use iconation\IconSDK\IISS\IISS;

$iconservice = new IconService('https://ctz.solidwallet.io/api/v3');
$iiss = new IISS($iconservice);

$address = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160";

$res = $iiss->queryIScore($address);