[英]How to detect an account change in MetaMask using Meteor and web3?
I am creating a DApp using meteor in which a templateVar displays an account address. 我正在使用流星创建DApp,其中templateVar显示一个帐户地址。
Using a web3
function in JavaScript I defined the updateInterface()
method as seen below as updateInterface() 在JavaScript中使用
web3
函数,我定义了updateInterface()
方法,如下所示:updateInterface()
However, since the method is a loop I can't seem to trigger the event inside Template.foo.events
. 但是,由于该方法是一个循环,我似乎无法在
Template.foo.events
内部触发事件。 The method works as I am able to alert(res)
alert the result onto the screen. 该方法的工作,因为我能够
alert(res)
的结果通知到屏幕上。
Could somebody point me in the right direction? 有人能指出我正确的方向吗? cheers
干杯
EDIT: added the template.oncreated
function and also the function updateInterface()
is placed outside the oncreated
, helper and events methods. 编辑:添加了
template.oncreated
函数,并且函数updateInterface()
置于oncreated
,helper和events方法之外。
Template.foo.onCreated(function fooOnCreated() {
this.account= new ReactiveVar(0);
});
Template.foo.helpers({
account() {
var template = Template.instance();
myContract = web3.eth.contract(ABIArray).at(contractAddress);
var result = web3.eth.getCoinbase(function(err, res) {
TemplateVar.set(template, "person", res);
});
},
});
Template.foo.events({
// not sure what to place here
});
var account = web3.eth.accounts[0];
var accountInterval = setInterval(function() {
if (web3.eth.accounts[0] !== account) {
account = web3.eth.accounts[0];
updateInterface();
}
}, 100);
function updateInterface() {
var template = Template.instance();
var result = web3.eth.getCoinbase(function(err, res) {
alert(res);
TemplateVar.set(template, "account", res);
});
}
The problem with events is that it requires an event, whereas the metamask detection function detects a change in account every 100 milliseconds , I am not sure where to place the updateInterface()
method. 事件的问题在于它需要一个事件,而metamask检测功能每100毫秒检测一次帐户更改,但我不确定将
updateInterface()
方法放在何处。
TemplateVar points here. TemplateVar指向此处。
<head>
<body>
<div id="section">
{{>foo}}
</div>
</body>
</head>
<template name="foo">
<div id="location">
<p>Currently logged in as: {{account}} {{TemplateVar.get
"account"}} </p>
</div>
</template>
I think you should put the interval loop inside of the onCreated
and instead of checking account !== web3.eth.accounts[0]
you should check self.account.get() = web3.eth.accounts[0]
我认为您应该将间隔循环放在
onCreated
,而不是检查account !== web3.eth.accounts[0]
,而应检查self.account.get() = web3.eth.accounts[0]
like this 像这样
Template.foo.onCreated(function fooOnCreated() {
this.account= new ReactiveVar(0);
this.account.set(web3.eth.accounts[0]);
var self = this;
var accountInterval = setInterval(function() {
if (web3.eth.accounts[0] !== self.account.get()) {
self.account.set(web3.eth.accounts[0]);
}
}, 500);
});
and because it is a ReactiveVar
you don't have to call the update function. 并且由于它是
ReactiveVar
您不必调用更新函数。 The template should update automatically as the value is used in it. 模板将随着使用的值而自动更新。
I have not tested it but I think it should work like this 我没有测试过,但我认为它应该像这样工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.