3

Consider the following contract:

contract Test{  
    uint public id;  
    address public addr;  
    bytes32 public name;  

    struct t {
     bytes32 name;
     uint id;
    address addr;  
   }  
   mapping (address => t) addr_map;
    function f1 (uint param_1, bytes32 param_2) returns (bool) {  
       id = param_1;  
       name = param_2;  
    }

   function f2 (uint param_1, bytes32 param_2) returns (bool) {  
      addr_map[msg.sender].name = param_2;  
      addr_map[msg.sender].id = param_1;  
   }  
}

I am using solc 0.4.4 and web3 calls to interact with the contract. Calling f1 no matter how much I try gives me an error (below the web3 snippet); however calling f1 doesnt have this issue - able to get the tx_hash.

testContractInstance.f2(1,"random",{from:accounts[0],gas:1000000}function(err,tx) {
    if (err) {console.log(err);} 
    console.log(tx)});  

Any suggestions would be greatly helpful. Error below:

BigNumber Error: new BigNumber() not a number: new

2

Summary

  • Replace from:accounts[0] with from: web3.eth.accounts[0] (or from: eth.accounts[0]), add a , before function and your code should work.



Quick Code Check

Your code works perfectly in Browser Solidity:

enter image description here



geth Deployment And Execution

Environment

Iota:Homebrew bok$ solc --version
Version: 0.4.4+commit.4633f3de.Darwin.appleclang
Iota:Homebrew bok$ geth version
Version: 1.4.18-stable-c72f5459

geth set up on a private chain as described in https://ethereum.stackexchange.com/a/9181/1268 .

Flatten your solidity code

I've flattened your code using stripCrLf from How to load Solidity source file into geth by saving the following code into Test.sol:

pragma solidity ^0.4.4;

contract Test {  
    uint public id;  
    address public addr;
    bytes32 public name;

    struct t {
        bytes32 name;
        uint id;
        address addr;
    }  

    mapping (address => t) addr_map;

    function f1 (uint param_1, bytes32 param_2) returns (bool) {
       id = param_1;
       name = param_2;
    }

   function f2 (uint param_1, bytes32 param_2) returns (bool) {
      addr_map[msg.sender].name = param_2;
      addr_map[msg.sender].id = param_1;
   }
}

And

Iota:BigNumberError bok$ echo "var myTestSource='`stripCrLf Test.sol`'"
var myTestSource='pragma solidity ^0.4.4;contract Test {  uint public id;  address public addr; bytes32 public name; struct t { bytes32 name; uint id; address addr; }   mapping (address => t) addr_map;  function f1 (uint param_1, bytes32 param_2) returns (bool) { id = param_1; name = param_2; } function f2 (uint param_1, bytes32 param_2) returns (bool) { addr_map[msg.sender].name = param_2; addr_map[msg.sender].id = param_1; }}'

Run Your Code In geth

> var myTestSource='pragma solidity ^0.4.4;contract Test {  uint public id;  address public addr; bytes32 public name; struct t { bytes32 name; uint id; address addr; }   mapping (address => t) addr_map;  function f1 (uint param_1, bytes32 param_2) returns (bool) { id = param_1; name = param_2; } function f2 (uint param_1, bytes32 param_2) returns (bool) { addr_map[msg.sender].name = param_2; addr_map[msg.sender].id = param_1; }}'
undefined
> var myTestCompiled = web3.eth.compile.solidity(myTestSource);
undefined
> personal.unlockAccount(eth.accounts[0], "aaaargh");
true
> var myTestContract = web3.eth.contract(myTestCompiled.Test.info.abiDefinition);
undefined
var myTest = myTestContract.new({from:web3.eth.accounts[0], data: myTestCompiled.Test.code, gas: 1000000}, 
  function(e, contract) {
    if (!e) {
      if (!contract.address) {
        console.log("Contract transaction send: TransactionHash: " + 
          contract.transactionHash + " waiting to be mined...");
      } else {
        console.log("Contract mined! Address: " + contract.address);
        console.log(contract);
      }
    }
  }
)
Contract transaction send: TransactionHash: 0xc1516f0ab41f20ac705df2645b361fc0438f94470942cf6c14381f1d6ffe9148 waiting to be mined...
undefined
> Contract mined! Address: 0x073cc4149857145d1898dd91ef24c3a767929f5f
[object Object]
> myTest.f1(1, "random", {from: eth.accounts[0], gas: 1000000}, function(err, tx) {
    if (err) {
        console.log(err);
    } 
    console.log(tx)
});
> myTest.id()
1
> web3.toAscii(myTest.name().toString());
"random\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
>     myTest.f2(2, "random 2", {from: eth.accounts[0], gas: 1000000}, function(err, tx) {
    if (err) {
        console.log(err);
    } 
    console.log(tx)
});
0xf8814a2e1466b8c55041b6df95a55495616491e035e50dfd3a0039d367678de4
undefined
> myTest.f2(2, "random 2", {from: eth.accounts[0], gas: 1000000}, function(err, tx) {
    if (err) {
        console.log(err);
    } 
    console.log(tx)
});
0xf8814a2e1466b8c55041b6df95a55495616491e035e50dfd3a0039d367678de4
undefined

All seems to work with the correct function parameters.

Looking For Your Error Message

I tried using accounts[0] instead of eth.accounts[0] in my function call and got the following message:

myTest.f2(2, "random 2", {from: accounts[0], gas: 1000000}, function(err, tx) {
    if (err) {
        console.log(err);
    } 
    console.log(tx)
});
ReferenceError: 'accounts' is not defined
    at <anonymous>:1:37

Not the same error message as you got.

I tried removing the , before the function but it does not give the same error message as you got:

> myTest.f2(2, "random 2", {from: accounts[0], gas: 1000000} function(err, tx) {
......     if (err) {
.........         console.log(err);
.........     } 
......     console.log(tx)
......     });
(anonymous): Line 1:64 Unexpected token function (and 2 more errors)
> myTest.f2(2, "random 2", {from: eth.accounts[0], gas: 1000000} function(err, tx) {
......     if (err) {
.........     console.log(err);
.........    } 
......     console.log(tx)
...... });
(anonymous): Line 1:68 Unexpected token function (and 2 more errors)
1

This seems to happen if your node wasn't fully synced. perhaps you try to communicate with a contract which is not synced yet. so try to sync with the blockchain and retry.

read :https://github.com/ethereum/web3.js/issues/434

I've tried your contract and the following snipet in web3js and it works fine :

 contractInstance.f2(1,"random",{from:web3.eth.accounts[0],gas:1000000},function(err,tx) {
    if (err) {console.log(err);} 
    console.log(tx)}); 

it returns the transaction hash

enter image description here

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.