Estoy tratando de seguir el siguiente ejemplo.
http://hypernephelist.com/2016/12/13/compile-deploy-ethereum-smart-contract-web3-solc.html
Pero me sale el siguiente error. ¿Alguien puede mostrarme qué está mal en mi código? Gracias.
$ cat contracts/HelloWorld.sol
pragma solidity ^0.4.16;
contract HelloWorld {
function get() public pure returns (string retVal) {
return "HelloWorld!!";
}
}
$ cat test.js
const fs = require('fs')
const input = fs.readFileSync('contracts/HelloWorld.sol', 'utf8');
const solc = require('solc')
const output = solc.compile(input, 1);
const bytecode = output.contracts[':HelloWorld'].bytecode;
const abi = JSON.parse(output.contracts[':HelloWorld'].interface);
const Web3 = require('web3')
const web3 = new Web3('ws://localhost:8546');
web3.eth.getAccounts().then((accounts) => {
const testContract = new web3.eth.Contract(abi);
testContract.deploy({
data: "0x" + bytecode,
})
.send({from: accounts[0], gas: 4700000})
.then((instance) => {
console.log(`Address: ${instance.options.address}`);
})
.catch(console.log);
});
$ node test.js
connection not open on send()
(node:11309) UnhandledPromiseRejectionWarning: Error: connection not open
at WebsocketProvider.send (/Users/pengy/linux/test/gnu/geth/bin/Hello-Ethereum/node_modules/web3-providers-ws/src/index.js:211:18)
at Timeout._onTimeout (/Users/pengy/linux/test/gnu/geth/bin/Hello-Ethereum/node_modules/web3-providers-ws/src/index.js:196:19)
at ontimeout (timers.js:466:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:267:5)
(node:11309) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:11309) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Estás analizando el ABI dos veces.
const abi = JSON.parse(output.contracts[':HelloWorld'].interface);
y aquí
const testContract = web3.eth.contract(JSON.parse(abi));
simplemente cambie la segunda línea y elimine JSON.parse
el error que se resolverá.
Pero considere que si está usando web3 1.0 (que viene por defecto si instala el paquete ahora) ese código no funcionará correctamente. La sintaxis es diferente y es posible que deba cambiarla correctamente:
const Web3 = require('web3') const web3 = nuevo Web3("http://localhost:8545"); web3.eth.getAccounts().then((cuentas) => { const testContract = new web3.eth.Contract(abi); pruebaContract.deploy({ datos: "0x" + código de bytes, }) .send({de: cuentas[0], gasolina: 4700000}) .then((instancia) => { console.log(`Dirección: ${instancia.opciones.dirección}`); }) .catch(consola.log); });
'ws://localhost:8546'
es la dirección/puerto correcto y está escuchando en websocket? Si no, no funcionará. Si usa ganache-cli, la dirección predeterminada es la mencionada en mi publicación$ node test.js Address: 0x4F5818ffD21b1eb122Fb1517117bC15a5c4F5EC8
. ¿Por qué el programa no termina? ¿Qué debo esperar de este programa?web3
para invocar su contrato.web3
para invocar el contrato. Lea la documentación web3js.readthedocs.io/en/1.0/web3-eth-contract.html y si tiene un problema específico, haga otra pregunta.This documentation is work in progress and web3.js 1.0 is not yet released!
). No sé qué aprender y qué no aprender.
kherwa