ethereumjs-tx推荐版本1.3.7 上面是单纯的转账交易,下面是把调用合约函数打包成交易发送出去
以太坊发送签名交易
直接上代码:const Web3 = require('web3'); const Tx = require('ethereumjs-tx'); const web3 = new Web3(new Web3.providers.WebsocketProvider("ws://localhost:8546") ) ; var _from = "签名以太坊账户"; var privateKey1 = Buffer.from('对应私钥','hex');//process.env.PRIVATE_KEY_1 web3.eth.getTransactionCount(_from,(err,txcount)=>{ var txObject ={ nonce: web3.utils.toHex(txcount), gasPrice: web3.utils.toHex(web3.utils.toWei('10','gwei')), gasLimit: web3.utils.toHex(21000), to: '对方账户', value:web3.utils.toHex(web3.utils.toWei('1','ether')), } var tx = new Tx(txObject); tx.sign(privateKey1); var serializedTx = tx.serialize(); web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function(err, hash) { if (!err){ console.log(hash); }else{ console.log(err); } });
无论哪一个 ethereumjs-tx版本是重点!!!不然会报错const Web3 = require('web3'); const EthereumTx = require('ethereumjs-tx');//因为ethereum-tx2.0版本实例化需要带未知参数,目前私有链好像行不通,所以npm install ethereum-tx@1.3.7 const web3 = new Web3(new Web3.providers.WebsocketProvider("ws://localhost:8546") ) ; //智能合约地址 const registryAddress = "0xb77065c8adf97e94ca2924c602ac7c3fd92ecdaa" //智能合约对应Abi文件 var contractAbi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"burn","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"initialSupply","type":"uint256"},{"name":"tokenName","type":"string"},{"name":"tokenSymbol","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Burn","type":"event"}]; //私钥转换为Buffer const privateKey = Buffer.from('你的私钥',"hex")//推荐使用cmd set命令然后env.process导出来 //私钥转换为账号 const account = web3.eth.accounts.privateKeyToAccount("0x"+"你的私钥"); //私钥对应的账号地地址 const address = account.address console.log("address: ",address) //获取合约实例 var myContract = new web3.eth.Contract(contractAbi,registryAddress) //获取nonce,使用本地私钥发送交易 web3.eth.getTransactionCount(address).then( nonce => { console.log("nonce: ",nonce) const txParams = { nonce: nonce, gasLimit: '0x271000', to: registryAddress, data: myContract.methods.transfer('0x198b2feE780F944F4b9D80e87C59AAe5ee8460bd',1000).encodeABI(), //ERC20转账 } const tx = new EthereumTx(txParams) tx.sign(privateKey) const serializedTx = tx.serialize() web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')) .on('receipt', console.log); }, e => console.log(e) )
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算