0

I have the contract

pragma solidity ^0.4.11;

contract UserBasic {
    struct Record {
        bytes32 _id;
        address _addedBy;
        uint _dateAdded;
        bytes32 _transactionHash;
        bytes32 _type;
        bytes32 _hash;
        bytes32 _signature;
    }
    // Type to records array
    mapping(bytes32 => bytes32[]) typeRecords;
    // Record ID to record
    mapping(bytes32 => Record) idRecord;
    // Search for record
    function searchRecord(bytes32 _id) constant returns (bytes32, address, uint, bytes32, bytes32, bytes32, bytes32) {
        Record storage temp = idRecord[_id];
        return (temp._id, temp._addedBy, temp._dateAdded, temp._transactionHash, temp._type, temp._hash, temp._signature);
    }
    // Add a record
    function addRecord(bytes32 _type, bytes32 _id) {
        typeRecords[_type].push(_id);
        var _new = Record(_id, tx.origin, now, "", _type, "", "");
        idRecord[_id] = _new;
    }
    // Get all records of a given type
    function getRecordsByType(bytes32 _type) constant returns(bytes32[]) {
        return typeRecords[_type];
    }
}

My addRecord method isn't working (I figure because of gas costs). I wanted to estimate how much gas that method costs but not sure how. This answer explains a way for a function with one parameter but it seems too complex of a process, I was wondering if there is a simpler way.

I'm calling it from node with

function addRecord(publicAddress, contractAddress, _type, _id) {
    const contract = contractInstance("UserBasic", contractAddress);
    // Interaction with the contract
    contract.addRecord(_type, _id, {from: publicAddress}, (err, res) => {
        // Log transaction to explore
        if (err) {
            console.log(err);
        } else {
            console.log('tx: ' + res);
            helpers.addTransaction(publicAddress, res);
        }
    });
}

I read the docs but the only part I don't know is how to convert multiple parameters to data.

1

If you have the contract address and abi you can estimate the method call directly

const Web3 = require('web3');

const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

const contractAbi = [.....] ;// Contract abi goes here
const contractAddress = "0x....."; // Contract address goes here

const contract = new web3.eth.Contract(contractAbi, contractAddress);


// Estimate gas for:
//     contractInstance.methods.MethodToCalll(param1, param2, param3);
const gas = contract.methods.MethodToCall.estimateGas(param1, param2, param3);

console.log(`Gas estimated: ${gas}`);

Will output something like

Gas estimation: 26818

0

Data param is a signature of method, In geth, you can run the following command to find the signature of the addRecord(bytes32,bytes32) function:

> web3.sha3("addRecord(bytes32,bytes32)")
"0x3193195597c87745d51912ca6cbef014b04b9d925f94235ee2ed5c1819afb2cb"

And take the first 4 bytes to create the data parameter:

0x319319550000000000000000000000000000000000000000000000000000000000000001

Now you have a data param. Hope it helps~

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.