Considere el siguiente contrato:
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;
}
}
Estoy usando llamadas solc 0.4.4 y web3 para interactuar con el contrato. Llamar a f1 sin importar cuánto lo intente me da un error (debajo del fragmento web3); sin embargo, llamar a f1 no tiene este problema: puede obtener el tx_hash.
testContractInstance.f2(1,"random",{from:accounts[0],gas:1000000}function(err,tx) {
if (err) {console.log(err);}
console.log(tx)});
Cualquier sugerencia sería de gran ayuda. Error a continuación:
BigNumber Error: nuevo BigNumber() no es un número: nuevo
from:accounts[0]
con from: web3.eth.accounts[0]
(o from: eth.accounts[0]
), agregue un ,
antes function
y su código debería funcionar.Su código funciona perfectamente en Browser Solidity:
geth
Despliegue y EjecuciónIota:Homebrew bok$ solc --version
Version: 0.4.4+commit.4633f3de.Darwin.appleclang
Iota:Homebrew bok$ geth version
Version: 1.4.18-stable-c72f5459
geth
configurado en una cadena privada como se describe en https://ethereum.stackexchange.com/a/9181/1268 .
He aplanado su código usando stripCrLf
How to load Solidity source file into geth guardando el siguiente código en 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;
}
}
Y
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
Todo parece funcionar con los parámetros de función correctos.
Intenté usar accounts[0]
en lugar de eth.accounts[0]
en mi llamada de función y recibí el siguiente mensaje:
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
No es el mismo mensaje de error que recibiste.
Intenté eliminar ,
antes de la función, pero no da el mismo mensaje de error que recibió:
> 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)
Esto parece suceder si su nodo no estaba completamente sincronizado . quizás intente comunicarse con un contrato que aún no está sincronizado. así que intente sincronizar con la cadena de bloques y vuelva a intentarlo.
leer: https://github.com/ethereum/web3.js/issues/434
Probé su contrato y el siguiente fragmento en web3js y funciona bien:
contractInstance.f2(1,"random",{from:web3.eth.accounts[0],gas:1000000},function(err,tx) {
if (err) {console.log(err);}
console.log(tx)});
devuelve el hash de la transacción