Ethereum web3.eth.getAccounts不使用infura
PS:我在使用web3 beta-37(因为每个版本都有自己的问题) 部署函数中web3.eth.getAccounts行下的任何内容都不起作用。 当我运行代码时,它什么也不显示! 代码如下:Ethereum web3.eth.getAccounts不使用infura,ethereum,solidity,web3,web3js,Ethereum,Solidity,Web3,Web3js,PS:我在使用web3 beta-37(因为每个版本都有自己的问题) 部署函数中web3.eth.getAccounts行下的任何内容都不起作用。 当我运行代码时,它什么也不显示! 代码如下: const HDWalletProvider = require('truffle-hdwallet-provider'); const Web3 = require('web3'); const {interface , bytecode} = require('./compile'); const
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();
此外,在mocha中测试此选项会在使用ganache cli时显示错误
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:无法读取未定义的属性映射()
stackexchange上的一个答案是使用beta-36修复此问题
beta-36和beta-37:在执行getAccounts()语句之后,没有任何内容,在空白屏幕上
beta-26:core.addProviders不是一个函数我也有同样的问题 尝试从infura url中删除“/v3”部分:
const provider = new HDWalletProvider(mnemonic,
'https://rinkeby.infura.io/my_project_id');
希望这有帮助。代码对我来说很好。我不应该这么说,但是你实际上没有Infura链接中的
我的项目id
,对吗?除此之外,它不起作用似乎很奇怪?您使用什么版本的web3
?您能否验证提供程序设置是否正确(可能是通过调试)?@nikosfotiadis on console.log(提供程序)它向我显示了一个带有10个地址数组(带有一个地址)的大对象。所以我猜提供者设置正确,我没有使用我的项目id。另外web3版本是beta-37。你能分享你的compile.js
文件吗?尝试从帐户部署的行是否记录了控制台日志?@IFIFartaz可以确定…我已经在摩卡运行了测试。。。这件事和ganache配合得很好。。。不。。。const accounts=wait web3.eth.getAccounts()之后没有任何内容代码>获取控制台日志编辑工作:)谢谢!我无法告诉你我已经尝试了多少事情来让它工作。我甚至在web3 Github上发布了这个问题,我已经被困了3天了,我很高兴能帮助你们!