通过Web3大家可以很方便的使用js,java,python 库转ERC20代币。作为世界上最好语言的php的web3库 sc0vu/web3.php只支持私有节点转账,无法使用公共节点转账。因此我们要使用另外一个库进行开发以太坊相关的。
安装
在composer项目中安装kgs/web3
composer require kgs/web3
Web3操作
kgs/web3封装了常用的json_rpc函数
$web3 = new Web3('https://kovan.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161');
var_dump($web3->accounts());
echo $web3->gasPrice();
$address = "0xdB7D1B76D262D31c51d740C6fb98047B8498D851";
echo Utils::hexToDec($web3->getBalance($address));
钱包操作
$key = "e872122c04df93040ede8996c0e738f35a0ea44e77642d97eb5c3deedbdd4201";
$wallet =Wallet::createByPrivate($key);
echo $wallet->getAddress();
$wallet = Wallet::create();
echo $wallet->getAddress();
echo $wallet->getPrivateKey();
合约操作
$key = "e872122c04df93040ede8996c0e738f35a0ea44e77642d97eb5c3deedbdd4201";
$abi = '[{"inputs":[{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"symbol","type":"string"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]';
$wallet =Wallet::createByPrivate($key);
$web3 = new Web3('https://kovan.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161');
$contractAddress='0x7ef08Db1E4121b71177B828d5b5ff7a1BCB8305D';
$contract = Contract::at($web3,$abi,$contractAddress);
$toAccount = "0x6aba7cd6750225f9d732a256F0f334916C866264";
$amount =\Web3\Utils::ethToWei(1);
$res =$contract->send($wallet,'transfer',[$toAccount,]);
echo $res;
$res =$contract->call('balanceOf',[$toAccount]);
|