CryptoCurrency NODE.js Web3 Ethereum - Kann keine Verbindung zu TESTRPC herstellen

Ich versuche, von einer meiner web3-Anwendung aus eine Verbindung zu meiner testrpc-Instanz herzustellen, aber ich kann anscheinend keine Verbindung herstellen. Der folgende Code sollte ein Array aller Konten in der testrpc-Kette zurückgeben.

Code:

Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
console.log(web3.eth.accounts);

Es gibt Folgendes zurück und innerhalb des Objekts intelliJ ist das Objekt currentprovider wie folgt.Geben Sie hier die Bildbeschreibung ein

Es scheint nur das Objekt zu drucken, denke ich:

Accounts {
  eth: 
   Eth {
     currentProvider: [Getter/Setter],
     _requestManager: RequestManager { provider: [Object], providers: [Object], subscriptions: {} },
     givenProvider: null,
     providers: 
      { WebsocketProvider: [Function: WebsocketProvider],
        HttpProvider: [Function: HttpProvider],
        IpcProvider: [Function: IpcProvider] },
     _provider: HttpProvider { host: 'http://localhost:8545', timeout: 0, connected: false },
     setProvider: [Function],
     BatchRequest: [Function: bound Batch],
     extend: 
      { [Function: ex]
        formatters: [Object],
        utils: [Object],
        Method: [Function: Method] },
     clearSubscriptions: [Function],
     getProtocolVersion: { [Function: send] request: [Function: bound ], call: 'eth_protocolVersion' },
     getCoinbase: { [Function: send] request: [Function: bound ], call: 'eth_coinbase' },
     isMining: { [Function: send] request: [Function: bound ], call: 'eth_mining' },
     getHashrate: { [Function: send] request: [Function: bound ], call: 'eth_hashrate' },
     isSyncing: { [Function: send] request: [Function: bound ], call: 'eth_syncing' },
     getGasPrice: { [Function: send] request: [Function: bound ], call: 'eth_gasPrice' },
     getAccounts: { [Function: send] request: [Function: bound ], call: 'eth_accounts' },
     getBlockNumber: { [Function: send] request: [Function: bound ], call: 'eth_blockNumber' },
     getBalance: { [Function: send] request: [Function: bound ], call: 'eth_getBalance' },
     getStorageAt: { [Function: send] request: [Function: bound ], call: 'eth_getStorageAt' },
     getCode: { [Function: send] request: [Function: bound ], call: 'eth_getCode' },
     getBlock: { [Function: send] request: [Function: bound ], call: [Function: blockCall] },
     getUncle: { [Function: send] request: [Function: bound ], call: [Function: uncleCall] },
     getCompilers: { [Function: send] request: [Function: bound ], call: 'eth_getCompilers' },
     getBlockTransactionCount: 
      { [Function: send]
        request: [Function: bound ],
        call: [Function: getBlockTransactionCountCall] },
     getBlockUncleCount: 
      { [Function: send]
        request: [Function: bound ],
        call: [Function: uncleCountCall] },
     getTransaction: 
      { [Function: send]
        request: [Function: bound ],
        call: 'eth_getTransactionByHash' },
     getTransactionFromBlock: 
      { [Function: send]
        request: [Function: bound ],
        call: [Function: transactionFromBlockCall] },
     getTransactionReceipt: 
      { [Function: send]
        request: [Function: bound ],
        call: 'eth_getTransactionReceipt' },
     getTransactionCount: { [Function: send] request: [Function: bound ], call: 'eth_getTransactionCount' },
     call: { [Function: send] request: [Function: bound ], call: 'eth_call' },
     estimateGas: { [Function: send] request: [Function: bound ], call: 'eth_estimateGas' },
     sendSignedTransaction: { [Function: send] request: [Function: bound ], call: 'eth_sendRawTransaction' },
     signTransaction: { [Function: send] request: [Function: bound ], call: 'eth_signTransaction' },
     sendTransaction: { [Function: send] request: [Function: bound ], call: 'eth_sendTransaction' },
     sign: { [Function: send] request: [Function: bound ], call: 'eth_sign' },
     compile: { solidity: [Object], lll: [Object], serpent: [Object] },
     submitWork: { [Function: send] request: [Function: bound ], call: 'eth_submitWork' },
     getWork: { [Function: send] request: [Function: bound ], call: 'eth_getWork' },
     getPastLogs: { [Function: send] request: [Function: bound ], call: 'eth_getLogs' },
     subscribe: { [Function] call: undefined },
     net: 
      Net {
        currentProvider: [Getter/Setter],
        _requestManager: [Object],
        givenProvider: null,
        providers: [Object],
        _provider: [Object],
        setProvider: [Function],
        BatchRequest: [Function: bound Batch],
        extend: [Object],
        getId: [Object],
        isListening: [Object],
        getPeerCount: [Object],
        getNetworkType: [Function: bound getNetworkType] },
     accounts: [Circular],
     personal: 
      Personal {
        currentProvider: [Getter/Setter],
        _requestManager: [Object],
        givenProvider: null,
        providers: [Object],
        _provider: [Object],
        setProvider: [Function],
        BatchRequest: [Function: bound Batch],
        extend: [Object],
        getAccounts: [Object],
        newAccount: [Object],
        unlockAccount: [Object],
        sendTransaction: [Object],
        lockAccount: [Object],
        importRawKey: [Object],
        sign: [Object],
        ecRecover: [Object],
        net: [Object] },
     Contract: [Function: Contract],
     Iban: 
      { [Function: Iban]
        toAddress: [Function],
        toIban: [Function],
        fromAddress: [Function],
        fromBban: [Function],
        createIndirect: [Function],
        isValid: [Function] },
     abi: ABICoder { _types: [Array] } },
  wallet: 
   Wallet {
     length: 0,
     _accounts: [Circular],
     defaultKeyName: 'web3js_wallet' } }

Der testRPC-Server startet ebenfalls erfolgreich:Geben Sie hier die Bildbeschreibung ein

Haben Sie versucht, ob andere Befehle funktionieren?

Antworten (4)

Wenn Sie das neue web3.js 1.0.0-beta verwenden, versuchen Sie dies (die gesamte Syntax hat sich geändert, seufz). Getestet mit testrpc -d.

> var Web3 = require('web3');
> var web3 = new Web3('http://localhost:8545');
> web3.version;
'1.0.0-beta.11'
> web3.eth.getAccounts().then(console.log);
Promise { ... }
> [ '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1',
  '0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0',
  '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',
  '0xE11BA2b4D45Eaed5996Cd0823791E0C93114882d',
  '0xd03ea8624C8C5987235048901fB614fDcA89b117',
  '0x95cED938F7991cd0dFcb48F0a06a40FA1aF46EBC',
  '0x3E5e9111Ae8eB78Fe1CC3bb8915d5D461F3Ef9A9',
  '0x28a8746e75304c0780E011BEd21C72cD78cd535E',
  '0xACa94ef8bD5ffEE41947b4585a84BdA5a3d3DA6E',
  '0x1dF62f291b2E969fB0849d99D9Ce41e2F137006e' ]

Absolut alles ist jetzt asynchron, daher das Herumspielen mit Versprechungen mit ähnlichen .thenMethoden. Ich habe den letzten Tag damit verbracht, mich damit auseinanderzusetzen - es ist anfangs sehr seltsam, wird aber mit der Vertrautheit besser.

Wenn Sie die Konten in einer Variablen haben möchten, können Sie es so machen, aber achten Sie darauf, die Variable nicht zu verwenden, bis das Promise aufgelöst ist.

> var foo;
> web3.eth.getAccounts().then(accts => {foo = accts});
> foo
[ '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1',
  '0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0',
  '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',
  '0xE11BA2b4D45Eaed5996Cd0823791E0C93114882d',
  '0xd03ea8624C8C5987235048901fB614fDcA89b117',
  '0x95cED938F7991cd0dFcb48F0a06a40FA1aF46EBC',
  '0x3E5e9111Ae8eB78Fe1CC3bb8915d5D461F3Ef9A9',
  '0x28a8746e75304c0780E011BEd21C72cD78cd535E',
  '0xACa94ef8bD5ffEE41947b4585a84BdA5a3d3DA6E',
  '0x1dF62f291b2E969fB0849d99D9Ce41e2F137006e' ]
wann wurde das geändert?
Ich weiß es nicht genau - ich glaube, v.1.0.0 wurde npmin den letzten Wochen zur Standardinstallation. Sie können die alte stabile Version mit installieren npm install web3@0.20.0, aber es sieht so aus, als ob hier die Zukunft liegt, also denke ich, dass es sich lohnt, sich damit auseinanderzusetzen.
Juhu, das hat mich gerettet! Versuche das schon seit einiger Zeit herauszufinden. Sie müssen ihre Dokumente wirklich aktualisieren, wenn die Syncronous-Funktionen nicht mehr funktionieren. github.com/ethereum/wiki/wiki/JavaScript-API
Für normales JS:web3.eth.getAccounts().then(function(accts) { console.log(accts); })
Das funktioniert auch und ist kürzer, was beim Arbeiten an der Konsole praktisch ist: web3.eth.getAccounts().then(console.log).

Ich habe deinen Code getestet und er scheint zu funktionieren. Ich habe auch testrpc verwendet.

yourscript.js

var Web3 = require('web3')
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
console.log(web3.eth.accounts);

if(web3.isConnected())
    console.log("ok");

Die Konten werden bei der Ausgabe korrekt protokolliert ok. Hast du versucht den Befehl auszuführen node yourscript.js?

Es scheint einen Fehler in der neuesten Version von web3 zu geben. npm install greift auf 1.0.0-beta zu, was nicht die stabile Version (0.20.0) ist.

Mit neu installieren

npm uninstall web3

npm install web3@0.20.1 --save

Dann versuchen Sie es erneut. Es sollte funktionieren.

Haben Sie versucht, mit zu überprüfen truffle console?

Überprüfen Sie auch, ob nach der Ausführung etwas in der testrpc-Konsole protokolliert wird.

Ich würde vorschlagen, Ihren testrpc neu zu starten, da Ihr Code in nichts falsch zu sein scheint und er auch für mich perfekt funktioniert.