如何使用 sendRawTransaction 呼叫已部署的智能合約
使用此程式碼,我想呼叫我的令牌智能合約的傳輸功能。據我所知,我必須將此“.transfer(address,amount)”添加到數據欄位中。我對麼?我也在使用 web3 0.20。在這個版本中,我必須使用 getData 對嗎?所以這將是var dataforTx = mycontract.tranfer.getData(address,amount); 然後將該 dataforTx 添加到數據欄位?
var Tx = require(’ethereumjs-tx’); var privateKey = new Buffer(’e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109’, ‘hex’)
VAR rawTx = {隨機數:‘0×00’,gasPrice:‘0x09184e72a000’,
gasLimit:‘0x2710’,到:“0x0000000000000000000000000000000000000000’,值:‘0×00’,
**數據:‘0x7f7465737432000000000000000000000000000000000000000000000000000000600057’} ** //如何獲取我的 dataforTx 的十六進制值??????????
var tx = 新的 Tx(rawTx); tx.sign(privateKey);
var serializedTx = tx.serialize();
//console.log(serializedTx.toString(‘hex’));
web3.eth.sendRawTransaction(‘0x’ + serializedTx.toString(‘hex’), function(err, hash) { if (!err) console.log(hash); // “0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385” });
我仍然無法轉移令牌。它返回雜湊值但不反映其他帳戶中的任何餘額。如果我使用 balanceOf 那麼如何在 sendRawTransaction 中擷取 balanceOf 函式的輸出
var abi=[//your abi array]; var contractAddress = "//your contract address"; var contract = web3.eth.contract(abi).at(contractAddress); let send_add = data.send_public_address; let recv_add = rec_public_address; let amount = web3.toHex(transfer_amount), let nonce = web3.toHex(web3.eth.getTransactionCount(send_public_address)); var private_key = privatekey_sender.slice(2); let gas = web3.toHex(data.gas_limit); let gasPrice = web3.toHex(data.gas_price); var rawTx = { from: send_add, nonce: nonce, gasLimit: gas, gasPrice: gasPrice, amount: web3.toHex(transfer_amount), to: recv_add, }; var transaction = new tx(rawTx); var txData = new Buffer(private_key, 'hex'); transaction.sign(txData); var serializedTx = transaction.serialize().toString('hex'); contract.transfer.sendTransaction(recv_add , amount , {'0x' + serializedTx}, function (err, txHash) { if (txHash) { next(null, txHash); } else if (err && err.message) { next(err.message, null); } else { next('Unable to sendTransaction', null); } });
在這裡我為這個問題做了一個教程https://github.com/pubkey/eth-crypto/blob/master/tutorials/signed-data.md#deploy-the-contract