I have a number as a string which I pass to smart contract function as a parameter. The function takes byte32 as a parameter.
Here is the node.js code for executing the contract function:
const app = require("express")();
const config = require("./config");
const Web3 = require("web3");
const web3EndPoint = config.web3EndPoint;
const web3 = new Web3(new Web3.providers.HttpProvider(web3EndPoint));
const ethTx = require('./eth-transaction');
const abi = config.abi;
const MyContract = web3.eth.contract(abi);
const myContractInstance = MyContract.at(config.contractAddress);
txnCount = web3.eth.getTransactionCount(config.defaultAccount);
const parameter = '1234';
const data = myContractInstance.setID.getData(parameter);
myContractInstance.setID.estimateGas(parameter, (error , value) => {
if (!error) {
console.log("gasLimit" , value);
const rawTx = {
nonce: web3.toHex(txnCount) ,
gasPrice: web3.toHex(100000000000) ,
gasLimit: value ,
to: config.contractAddress ,
value: web3.toHex(0) ,
data: data ,
};
ethTx.sendRawTransaction(config.priKey , rawTx , web3 ).then((result) => {
console.log(result);
}).catch(transactionFailedErr => {
console.error(transactionFailedErr);
});
} else {
console.error(error);
}
});
myContractInstance.LogTestCase().watch((error, result) => {
console.log(web3.toUtf8(result.args._id)); // Its logs 'M'
});
Here is the solidity code:
pragma solidity ^0.4.21;
contract test2 {
bytes32 public _id;
event LogTestCase(
bytes32 _id
);
function setID(bytes32 cId) returns (bool){
_id = cId;
emit LogTestCase(_id);
return true;
}
}
Now when I put '1234' as a parameter, fetching it from the contract event results in 'M'. But when I put any other string containing alphabets, I get the correct initial string
So passing any number as a string to the contract function with byte32 type cannot be retrieved. Is there any way to get exactly what I passed?
bytes32
in Javascript.parameter
to the contract, and how exactly are you parsing the returned event?