0
votes

I'm mew to Ethereum/Solidity/Web3.js. I'm trying to use web3.js web.eth.sendsendTransaction() method in order to run a function in a deployed contract on a private chain.

The function I try to execute is:

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);
    }
... 
}

I call multiplica from Mist browser runnig the following JavaScript code:

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){......});

If I set firstNumber=1000 and secondNumber=2000 then abiByteCodes happens to be:

0x38e836df00000000000000000000000000000000000000000000000000000000000003e800000000000000000000000000000000000000000000000000000000000007d0

0x38e836df is the sha of the fuction signature, that is correct;

03e8 is hexadecimal for 1000(firstNumber) right

07d0 is hex for 2000(secondNumber) ok

But data stored in the block chain is:

datosMensaje: 0x38e836df00000000000000000000000000000000000000000000000000000000009e03e800000000000000000000000000000000000000000000000000000000009e07d0

factorA: 8520680 (0x8203E8)

factorB: 8521680 (0x8207D0)

What am I doing wrong?

I'm using geth 1.7.3 and Mist 0.9.2 on a Windows 10 64 bit desktop.

Thank you

P.S. I know there are other ways to call contract functions like instantiating the contract via new web3.eth.Contract(contractABI,contractAddress) but I'm thinking about a project that would require to use the sendTransaction() method

1
Assuming you copy and pasted your code, you misspelled "contractAddress" on the first line: var contracAddress="0xXXXXXXXX"; Your transactionObject has an empty to field as a result. - Adam Kipnis
Thank hoy Adam. In the actual Code the true address is especified I just wrote here 0xXxxx... to avoid copying the hole number - absoluteBeginner
I wasn’t referring to the value. I was referring to the variable name. You’re missing a t in “contractAddress”. It is spelled correctly (and, hence, a different variable) when set in the transactionObject. - Adam Kipnis

1 Answers

0
votes

After countless hours I've realized this is due to an error in how remix IDE shows values in the debug tab. If I recover the data from the blockchain using web3.js ver 1.0 method getStorageAt(address,key) I get the expected values.

Thank you Adam