Ich versuche, dem folgenden Beispiel zu folgen.
http://hypernephelist.com/2016/12/13/compile-deploy-ethereum-smart-contract-web3-solc.html
Aber ich bekomme den folgenden Fehler. Kann mir jemand zeigen, was in meinem Code falsch ist? Danke.
$ 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.
Sie analysieren die ABI zweimal.
const abi = JSON.parse(output.contracts[':HelloWorld'].interface);
und hier
const testContract = web3.eth.contract(JSON.parse(abi));
Ändern Sie einfach die zweite Zeile und entfernen JSON.parse
Sie den Fehler, der behoben wird.
Bedenken Sie jedoch, dass dieser Code nicht richtig funktionieren würde, wenn Sie web3 1.0 verwenden (was standardmäßig mitgeliefert wird, wenn Sie das Paket jetzt installieren). Die Syntax ist anders und Sie müssen sie möglicherweise richtig ändern:
const Web3 = require('web3') const web3 = new Web3("http://localhost:8545"); web3.eth.getAccounts().then((Konten) => { const testContract = new web3.eth.Contract(abi); testContract.deploy({ Daten: "0x" + Bytecode, }) .send({von: Konten[0], Gas: 4700000}) .then((Instanz) => { console.log(`Adresse: ${instance.options.address}`); }) .catch(console.log); });
'ws://localhost:8546'
dies die richtige Adresse / der richtige Port ist und auf Websocket lauscht? Wenn nicht, wird es nicht funktionieren. Wenn Sie ganache-cli verwenden, ist die Standardadresse die in meinem Beitrag erwähnte$ node test.js Address: 0x4F5818ffD21b1eb122Fb1517117bC15a5c4F5EC8
. Warum wird das Programm nicht beendet? Was kann ich von diesem Programm erwarten?web3
Ihren Vertrag aufrufen.web3
sich auf den Vertrag berufen. Bitte lesen Sie die Dokumentation web3js.readthedocs.io/en/1.0/web3-eth-contract.html und wenn Sie ein spezifisches Problem haben, stellen Sie eine andere Frage.This documentation is work in progress and web3.js 1.0 is not yet released!
). Ich weiß nicht, was ich lernen soll und was nicht.
kherwa