Betrachten Sie den folgenden Vertrag:
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;
}
}
Ich verwende Solc 0.4.4 und Web3-Aufrufe, um mit dem Vertrag zu interagieren. Wenn ich f1 aufrufe, egal wie oft ich es versuche, erhalte ich einen Fehler (unter dem web3-Snippet); Beim Aufrufen von f1 tritt dieses Problem jedoch nicht auf - der tx_hash kann abgerufen werden.
testContractInstance.f2(1,"random",{from:accounts[0],gas:1000000}function(err,tx) {
if (err) {console.log(err);}
console.log(tx)});
Alle Vorschläge wären sehr hilfreich. Fehler unten:
BigNumber Fehler: new BigNumber() keine Zahl: neu
from:accounts[0]
durch from: web3.eth.accounts[0]
(oder from: eth.accounts[0]
), fügen Sie ein ,
Before hinzu function
und Ihr Code sollte funktionieren.Ihr Code funktioniert perfekt in Browser Solidity:
geth
Bereitstellung und AusführungIota:Homebrew bok$ solc --version
Version: 0.4.4+commit.4633f3de.Darwin.appleclang
Iota:Homebrew bok$ geth version
Version: 1.4.18-stable-c72f5459
geth
in einer privaten Kette eingerichtet, wie in https://ethereum.stackexchange.com/a/9181/1268 beschrieben .
Ich habe Ihren Code mithilfe stripCrLf
von So laden Sie die Solidity-Quelldatei in Geth , indem Sie den folgenden Code in Test.sol speichern, vereinfacht:
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;
}
}
Und
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; }}'
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
Alles scheint mit den richtigen Funktionsparametern zu funktionieren.
Ich habe versucht, accounts[0]
anstelle von eth.accounts[0]
in meinem Funktionsaufruf zu verwenden, und habe die folgende Meldung erhalten:
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
Nicht die gleiche Fehlermeldung, die Sie erhalten haben.
Ich habe versucht, das vor der Funktion zu entfernen ,
, aber es gibt nicht die gleiche Fehlermeldung wie Sie:
> 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)
Dies scheint zu passieren, wenn Ihr Knoten nicht vollständig synchronisiert wurde . Vielleicht versuchen Sie, mit einem Vertrag zu kommunizieren, der noch nicht synchronisiert ist. Versuchen Sie also, mit der Blockchain zu synchronisieren, und versuchen Sie es erneut.
lesen: https://github.com/ethereum/web3.js/issues/434
Ich habe Ihren Vertrag und das folgende Snipet in web3js ausprobiert und es funktioniert gut:
contractInstance.f2(1,"random",{from:web3.eth.accounts[0],gas:1000000},function(err,tx) {
if (err) {console.log(err);}
console.log(tx)});
es gibt den Transaktions-Hash zurück