以太坊轻钱包开发笔录02

ETH转账

Credentials 加载采用秘钥形式,安全方面还是建议采用离线签名交易Keystore形式,由于业务需要我采用的是秘钥形式加载凭证->签名打包->转账广播

composer require myxtype/ethereum-client:dev-master
use xtype\Ethereum\Client as EthereumClient;
use xtype\Ethereum\Utils;

        $client = new EthereumClient([
            'base_uri' => 'http://xxxxxxxxx',
            'timeout' => 10,
        ]);

        $client->addPrivateKeys(["秘钥xxxx"]);

        // 2. 组装交易
        $trans = [
            "from" => '0x47755a9631c6cd54a23809da8a596005fcc15f54',
            "to" => '0x94e7E2bE1707e665FAD2bf86d5f5687774060ebd',
            "value" => Utils::ethToWei('10', true),
            "data" => '0x',
        ];
        // 设定Gas,nonce,gasprice
        $trans['gas'] = dechex(hexdec($client->eth_estimateGas($trans)) * 1.5);
        $trans['gasPrice'] = $client->eth_gasPrice();
        $trans['nonce'] = $client->eth_getTransactionCount('0x47755a9631c6cd54a23809da8a596005fcc15f54', 'pending');
        // 3. 发送您的交易
        // 如果需要服务器,也可以使用eth_sendTransaction
        $txid = $client->sendTransaction($trans);
       
         //4.得到交易hash
        var_dump($txid);

        //查询到账情况
        var_dump($client->eth_getTransactionReceipt($txid));

你可能感兴趣的:(以太坊轻钱包开发笔录02)