在VueX中部署web3时出现以下问题:
TypeError:重复的方法构造函数。此方法被定义为RPC调用和Object方法。
我推断问题出在setupWeb3 Action中的web3声明中(见下文)。
我猜是打字的问题吧?
我在main.ts中声明了一个全局名称空间,如下所示:
declare global {
interface Window {
ethereum:any;
web3:any;
}
}
以下是我的行动:
export const actions: ActionTree<Network, RootState> = {
setupWeb3(context: ActionContext<Network, RootState>) {
let web3;
if (window.ethereum) {
web3 = new Web3(window.ethereum);
window.ethereum.enable().then(enabled => console.log(enabled));
} else if (window.web3) {
web3 = new Web3(window.web3.currentProvider);
} else {
// TODO better handle of metamask
window.alert(
'Non-Ethereum browser detected. You should consider trying MetaMask!',
);
}
context.commit('SET_WEB3', web3);
},
async getNetworkData(context: ActionContext<Network, RootState>) {
const { Web3 } = context.getters;
const network = await Web3.eth.net.getNetworkType();
const networkId = await Web3.eth.net.getId();
const currentBlock = await Web3.eth.getBlockNumber();
context.commit('SET_NETWORK_DATA', {
network,
networkId,
currentBlock,
});
},
getAddress(context: ActionContext<Network, RootState>) {
const { Web3 } = context.getters;
Web3.eth.getAccounts().then((account: string[]) =>
context.commit("SET_ADDRESS", account[0]));
},
bootstrapContracts(context: ActionContext<Network, RootState>) {
const setupWeb3 = context.dispatch('setupWeb3');
const network = context.dispatch('getNetworkData');
const address = context.dispatch('getAddress');
Promise.all([setupWeb3, network, address,])
.then(() => {
context.dispatch('setupLootControls');
});
},
};
1条答案
按热度按时间ee7vknir1#
原来我是在一个测试版本,并通过升级到
第一个月
帮我修正了错误。