我对 Ethereum/Solidity/Web3.js 很陌生。我正在尝试使用 web3.js web.eth.sendsendTransaction() 方法来在私有链上的已部署合约中运行一个函数。
我尝试执行的功能是:
contract Matematicas{
uint256 ultimaSuma;
uint256 ultimaMultiplicacion;
uint256 contador;
uint256 factorA;
uint256 factorB;
uint256 sumandoA;
uint256 sumandoB;
bytes datosMensaje;...
function multiplica(uint256 a, uint256 b) public{
datosMensaje=msg.data;
factorA=a;
factorB=b;
ultimaMultiplicacion=(a*b);
}
...
}
我从运行以下 JavaScript 代码的 Mist 浏览器中调用 multiplica:
var contracAddress="0xXXXXXXXX";
var contractABI=[{"constant":false,"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"name":"multiplica","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},...];
var functionABI=$.grep(contractABITercero,function(metodo,index){ return metodo.name=='multiplica';});
functionABI=abiDelaFuncion[0];
var abiByteCode= web3.eth.abi.encodeFunctionCall(functionABI,[document.getElementById('firstNumber').value,document.getElementById('secondNumber').value]);
var transactionObject={from:"0xxxxxxxxxx",to:contractAddress,data:abiByteCode, gas:10000000};
web3.eth.sendTransaction(transactionObject, function(error,hash){......});
如果我设置 firstNumber=1000 和 secondNumber=2000 那么 abiByteCodes 恰好是:
0x38e836df00000000000000000000000000000000000000000000000003e8000000000000000000000000000000000000000000000000000000000000000000
0x38e836df是函数签名的sha,正确;
03e8 是 1000(firstNumber) 的十六进制数
07d0 是 2000(secondNumber) 的十六进制数
但是存储在区块链中的数据是:
datosmensaje:0x38e836df00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,0000 9E0 7D0 7D0 7D0 7D0 7D0 7D0 7D0
因素A:8520680(0x8203E8)
因子B:8521680(0x8207D0)
我究竟做错了什么?
我在 Windows 10 64 位桌面上使用 geth 1.7.3 和 Mist 0.9.2。
谢谢
PS 我知道还有其他方法可以调用合约函数,例如通过 new web3.eth.Contract(contractABI,contractAddress) 实例化合约,但我正在考虑一个需要使用 sendTransaction() 方法的项目