web3Client.eth.call不会在区块链上生成交易
web3Client.eth.sendSignedTransaction会在区块链上生成交易
1. 利用call向以太坊发送交易
function rawTransCall(contractName,functionName,functionParams) { let rawTx = {}, usingContract, usingContractJson, funcABI, encodefunc; if(contractName === lotteryGameEngineName){ usingContract = lotteryGameEngineInst; usingContractJson = lotteryGameEngineJSON; }else if(contractName === lottery3DNumberName){ usingContract = lottery3DNumberInst; usingContractJson = lottery3DNumberJSON; } funcABI = usingContractJson['abi'].filter(per=>{ //返回数组 return per.name === functionName; }); encodefunc = web3Client.eth.abi.encodeFunctionCall(funcABI[0],functionParams); rawTx['to'] = usingContract.address; rawTx['data'] = encodefunc; logger.debug("final call rawTx:",rawTx); return web3Client.eth.call(rawTx).then(callValue => { logger.debug("funcABI[0]['outputs']:",funcABI[0]['outputs']); logger.debug("called Value result:",JSON.stringify(callValue)); return web3Client.eth.abi.decodeParameters(funcABI[0]['outputs'],callValue);//funcABI[0]['outputs'] }); }
2. 利用sendSignedTransaction向以太坊发送交易
function rawTransSend (callUserInfo,contractName,functionName,functionParams) { let rawTx = { from: '', gasLimit: '0x52080',//7a1200=>800,0000 ('21000'=>'0x5208') gasPrice: '',//web3.eth.getGasPrice nonce: 0, //web3.eth.getTransactionCount(address, 'pending') to: '',//contract address value: '0x00', //0 for contact, data: '' //contract method and parameters }, usingContract, usingContractJson, caller, callerPrivatekey, callerValue = '0x00', privatekey, funcABI, encodefunc, tx, serializedTx ; logger.debug("functionParams:",functionParams); callerPrivatekey = callUserInfo.privatekey; caller = callUserInfo.account; if(callUserInfo.value !== undefined){ callerValue = web3Client.utils.toHex(web3Client.utils.toWei(callUserInfo.value, 'ether')); } logger.debug("callerValue:",callerValue); if(contractName === lotteryGameEngineName){ usingContract = lotteryGameEngineInst; usingContractJson = lotteryGameEngineJSON; }else if(contractName === lottery3DNumberName){ usingContract = lottery3DNumberInst; usingContractJson = lottery3DNumberJSON; } privatekey = new Buffer(callerPrivatekey.slice(2),'hex'); rawTx.from = caller; rawTx.to = usingContract.address; rawTx.value = callerValue; funcABI = usingContractJson['abi'].filter(per=>{ //返回数组 return per.name === functionName; }); logger.debug("functionParams encoded result:",functionParams); return web3Client.eth.getTransactionCount(caller).then(transCounts => { rawTx.nonce = transCounts; return web3Client.eth.getGasPrice(); // 当前gasPrice }).then(gasPrice => { // wei rawTx.gasPrice = web3Client.utils.toHex(gasPrice); // paramsm默认是Array ,...functionParams encodefunc = web3Client.eth.abi.encodeFunctionCall(funcABI[0],functionParams); logger.debug("encodefunc:",encodefunc); rawTx.data = encodefunc; logger.debug("rawTx final is:",rawTx); tx= new Tx(rawTx); tx.sign(privatekey); serializedTx = tx.serialize(); logger.debug("raw data:",'0x' + serializedTx.toString('hex')); return web3Client.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')) .on('receipt', function (recepit) { console.log("transaction receipt received:"+JSON.stringify(recepit)); return new Promise((resolve,reject) => { resolve(recepit); }); }); });// then }
当使用web3Client.eth.sendSignedTransaction发送交易时,rawTx 为
{ from: '0x554d38376bfe981747588be3af1be303a6a2f7f3', gasLimit: '0x52080', gasPrice: '0x4a817c800', nonce: 1, to: '0x92b1dcc19a59de3b913398eeb3974cd155512ca2', value: '0x2c68af0bb140000', data: '0x4324c69900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001' }
然后根据hash拿到交易信息:
{ blockHash: "0xcb89942f9d24e93d2325514e1b5abc103a178926d5a42b5a8f84f0388c02c326", blockNumber: 22, from: "0x554d38376bfe981747588be3af1be303a6a2f7f3", gas: 336000, gasPrice: 20000000000, hash: "0xa6e1de6768b211b37b4f336c2535bd10a1d5cdfd35428eacf5441ad6d0cd7ea6", input: "0x4324c69900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001", nonce: 1, r: "0xbd6bd93ef02d39c5d3214f30a372a8262253dafe9d938320f1ba8a48d39db8d7", s: "0x23fd20f1da4f9551ae50cdfef8cb99b20247468262bf403b6c537673dc27baa2", to: "0x92b1dcc19a59de3b913398eeb3974cd155512ca2", transactionIndex: 0, v: "0x1b", value: 200000000000000000 }
发现rawTx中data字段和getTransaction中Input字段完全相同。