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:
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)