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”部分 url:
const provider = new HDWalletProvider(mnemonic,
'https://rinkeby.infura.io/my_project_id');
希望对您有所帮助。
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”部分 url:
const provider = new HDWalletProvider(mnemonic,
'https://rinkeby.infura.io/my_project_id');
希望对您有所帮助。