2

I have an array of json objects, which contain an id and 5 fields. I have a function to register these fields against a unique identifier within a contract.

contract Test {  
    struct Product {  
        uint p_id,  
        uint qty
    }

    mapping(bytes32 => Product) ProdMap;

    function registerProduct (bytes32 id, uint p, uint q) returns (bool) {
        ProdMap[id].p_id = p;
        ProdMap[id].qty = q;
    }
}  

I am able to interact with a contract get a transaction hash out of the executed transaction in a contract:

contractInstance.registerProduct.sendTransaction(
    web3.fromAscii("0abc123"),
    items[0].Product_ID,
    items[0].Quantity,
    {
        from:web3.eth.accounts[0],
        gas:470000
    }, 
    function(err,txid) {
        if(err) { console.log(err); }
        var hash = txid;
        console.log('Transaction registered '+'and transaction hash is: ' + hash);
        callback(null,hash);
    });  

Here items is an array if json objects. But when I loop through the entire array, I am getting an out of gas error.

contractInstance.registerProduct.sendTransaction(
    web3.fromAscii(item.Line_Item_ID),
    item.Product_ID,
    item.Quantity,
    {
        from:web3.eth.accounts[0],
        gas:1000000
    }, 
    function(err,txid) {
        if(err) {console.log(err);}
        var hash = txid;
        console.log('Transaction registered '+'and transaction hash is: ' + hash);
        callback(null,hash);
    });  

Here I have listed only 2 attributes, but I have 5 in all while registering the product. I have been using testrpc and when I set the limit to a really high amount 4500000000000, I am able to execute the transactions. But when I see the logs for testrpc, the gas used is 27608. Is it possible to determine the gas needed to execute a transaction? Any suggestions?

1

There should be a estimateGas() method, you can invoke like this:

contractInstance.registerProduct.sendTransaction.estimateGas(
    web3.fromAscii(item.Line_Item_ID),
    item.Product_ID,
    item.Quantity,
    {
        from:web3.eth.accounts[0]
   }, 
   function(err,txid) {
        if(err) {console.log(err);}
        var hash = txid;
        console.log('Transaction registered '+'and transaction hash is: ' + hash);
        callback(null,hash);
   });  
| improve this answer | |
  • You have an extra sendTransaction in your example that should not be needed. – Ismael Jan 19 '18 at 21:34

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.