簡體   English   中英

web3.eth.getAccounts 不適用於 infura

[英]web3.eth.getAccounts not working with infura

PS:我正在使用 web3 beta-37(因為每個版本都有自己的問題)部署函數中 web3.eth.getAccounts 行下的任何內容都不起作用。 當我運行代碼時,它什么也沒顯示! 這是代碼:

const HDWalletProvider = require('truffle-hdwallet-provider');
const Web3 = require('web3');
const {interface , bytecode} = require('./compile');

const provider = new HDWalletProvider(mnemonic,
'https://rinkeby.infura.io/v3/my_project_id');

const web3 = new Web3(provider);

const deploy = async() => {
    const accounts = await web3.eth.getAccounts();
    console.log('Attempting to deploy from account', accounts[0]);
    const result = await new web3.eth.Contract(JSON.parse(interface))
                             .deploy({data: bytecode, arguments: ['Hi There!']})
                             .send({'from': accounts[0], 'gas': '1000000'});

    console.log('Contract deployed to: ', result.options.address);
};
deploy();

此外,在使用 ganache-cli 時,在 mocha 中測試它會顯示錯誤

const assert = require('assert');
const HDWalletProvider = require('truffle-hdwallet-provider');
const Web3 = require('web3');
const {bytecode , interface} = require('../compile');

const provider = new HDWalletProvider(mnemonic,
'https://rinkeby.infura.io/v3/project_id');



let accounts;

beforeEach(async () => {
  //Get a list of all accounts
  accounts = await web3.eth.getAccounts();

});

describe('Inbox', () => {
  it('address', () => {
    assert.ok(accounts);
  })

以下是不同版本的結果: beta-46:無法讀取未定義的屬性 map() stackexchange 上的一個答案說使用 beta-36 來解決這個問題

beta-36 和 beta-37:在 getAccounts() 語句執行后沒有任何內容,空白屏幕。

beta-26:core.addProviders 不是函數

我也有同樣的問題。

嘗試從 infura 網址中刪除“/v3”部分:

const provider = new HDWalletProvider(mnemonic,
'https://rinkeby.infura.io/my_project_id');

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM