我是以太坊/ 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);
}
...
}
我从Mist浏览器调用multiplica运行以下JavaScript代码:
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恰好是:
0x38e836df00000000000000000000000000000000000000000000000000000000000003e800000000000000000000000000000000000000000000000000000000000007d0
0x38e836df是功能签名的sha,这是正确的;
03e8是1000(firstNumber)右边的十六进制
07d0是2000的十六进制(secondNumber)确定
但是存储在区块链中的数据是:
datosMensaje:0x38e836df0000000000000000000000000000000000000000000000000000000000 9e0 3e80000000000000000000000000000000000000000000000000000000000 9e0 7d0
factorA: 8520680(0x8203E8)
factorB: 8521680(0x8207D0)
我做错了什么?
我在Windows 10 64位桌面上使用geth 1.7.3和Mist 0.9.2。
谢谢
P.S。我知道还有其他方法来调用契约函数,比如通过新的web3.eth.Contract(contractABI,contractAddress)实例化合同,但我正在考虑一个需要使用sendTransaction()方法的项目
答案 0 :(得分:0)
经过无数个小时后,我意识到这是由于混音IDE在调试选项卡中显示值的错误。如果我使用web3.js ver 1.0方法getStorageAt(address,key)从区块链中恢复数据,我会得到预期的值。
谢谢Adam