使用web3js编写脚本,实现以太的自动交易

注:必须在安装有web3的环境下运行脚本,若未安装web3,请参照以下博文

 CentOS 7 环境 web3安装 及 对象的创建_m0_47233175的博客-CSDN博客icon-default.png?t=LA92https://blog.csdn.net/m0_47233175/article/details/121960931脚本代码如下

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

var arguments = process.argv.splice(2);

if(!arguments || arguments.length != 2){
    console.log("Parameter error!");
    return;
}

var _from = arguments[0];
var _to = arguments[1];
var _value = arguments[2];

web3.eth.sendTransaction({from: _from, to: _to, value: _value}, (err, res)=>{
    if(err)
        console.log("Error: ", err);
    else
        console.log("Tx: ", res);
});

  将以上脚本代码拷贝进一个新建的 transfer_script.js 文件 

运行脚本

node transfer_script.js 0x6903269a98f6ef7ea7190fc0122d5e15ddd25526 0x456982ded2a3eb889b3c3774e48de7fd867c0e5a 10000000

  注:js文件名后的第一个参数为以太的发出地址第二个参数为以太的接收地址第三个参数为以太的交易数量

请确保私链以rpc模式启动

请确保交易账户已经解锁

猜你喜欢

转载自blog.csdn.net/m0_47233175/article/details/121978770