Stellen Sie Smart-Contract mit nodejs bereit

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 parsen abi zweimal!

Antworten (1)

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.parseSie 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);
});
Ich sehe immer noch Fehler. Siehe die neue Bearbeitung. Weißt du, was falsch ist? Danke.
der Fehler ist ziemlich offensichtlich. Die Verbindung ist nicht geöffnet. Sind Sie sicher, dass '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
OK. Ich habe 8546 auf 8545 geändert. Dann scheint es zu funktionieren $ node test.js Address: 0x4F5818ffD21b1eb122Fb1517117bC15a5c4F5EC8. Warum wird das Programm nicht beendet? Was kann ich von diesem Programm erwarten?
Die Erwartung ist die Vertragsadresse, und Sie bekommen sie. Es bedeutet also, dass es korrekt bereitgestellt wurde und Sie die Adresse zurückerhalten haben. Mit der können Sie web3Ihren Vertrag aufrufen.
Wenn ich den Vertrag ausführen möchte, wie mache ich das?
Ich habe Ihnen bereits gesagt, dass Sie web3sich 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.
Die Dokumentation dort ist veraltet (es heißt 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.
ist nicht veraltet! Die web3 1.0 ist die Beta-Version und wird im Moment standardmäßig installiert und ist diejenige, auf die sich der Beitrag bezieht. Wenn Sie die vorherige Version ( 0.2xx ) verwenden möchten, gehen Sie hier: github.com/ethereum/wiki/wiki/JavaScript-API