ERC20
composer 引入
composer require kgs/web3 'dev-master'
合约操作
//查询代币余额
$coin_info = [
'host'=>'节点',
'abi'=>'对应合约abi',
'address'=>'对应合约地址',
];
$web = new Web3($coin_info['host']);
$contract = Contract::at($web,$coin_info['abi'],$coin_info['address']);
$s = $contract->call('balanceOf',[$address]);
//代币转账
$web = new Web3($coin_info['host']);
$wallet = Wallet::createByPrivate($keyPrivate); //私钥
$contract = Contract::at($web,$coin_info['abi'],$coin_info['address']);
$reStr = $contract->send($wallet,'transfer',[$to,$money]);//转账地址 和 数量 注意数量单位
//查询hash状态
$web3 = new Web3($host);
$e = $web3->getTransactionReceipt($hash);
TRC20
composer require fenguoz/tron-php 'dev-master'
生成地址 generateAddress()
验证地址 validateAddress(Address $address)
根据私钥得到地址 privateKeyToAddress(string $privateKeyHex)
查询余额 balance(Address $address)
交易转账(离线签名) transfer(Address $from, Address $to, float $amount)
查询最新区块 blockNumber()
根据区块链查询信息 blockByNumber(int $blockID)
根据交易哈希查询信息 transactionReceipt(string $txHash)
use GuzzleHttp\Client;
$uri = 'https://api.shasta.trongrid.io';// shasta testnet
$api = new \Tron\Api(new Client(['base_uri' => $uri]));
$trxWallet = new \Tron\TRX($api);
$addressData = $trxWallet->generateAddress();
// $addressData->privateKey
// $addressData->address
$config = [
'contract_address' => 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',// USDT TRC20
'decimals' => 6,
];
$trc20Wallet = new \Tron\TRC20($api, $this->config);
$addressData = $trc20Wallet->generateAddress();