1

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?

  • See here a similar question on how to deal with bytes32 in Javascript. – goodvibration Nov 14 '18 at 9:57
  • Thank you @goodvibration but that does not seem to work on the string containing numeric characters as mentioned in the question – Apar Adhikari Nov 14 '18 at 10:37
  • The fact that the characters are numeric is not supposed to have any impact on the behavior. How exactly are you sending parameter to the contract, and how exactly are you parsing the returned event? – goodvibration Nov 14 '18 at 11:35
  • 1
    You should try to convert the string into bytes32 before sending, and you can do that with web3 utils. – Kaki Master Of Time Nov 14 '18 at 12:48
  • @goodvibration It is all mentioned in the code – Apar Adhikari Nov 15 '18 at 9:55
1

In order to pass byte32 parameter properly you need to use 0x prefix in value (e.g. '0x1234')

or take a look how to convert string to bytes32 https://ethereum.stackexchange.com/a/23110/30889

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.