Accessing accounts in metamask through web3.js 1.xBetter pattern to detect web3 default account when using metamaskHow to detect MetaMask loginIssue with getting web3.eth.accounts (MetaMask)Using web3 from MetaMask in ReactHow to get reference to Metamask account from Node.jsmetamask web3 is undefinedProblems with metamask/web3 connectionHow to handle user login in Ethereum DApps?Use local Metamask account with Infura nodeUse Web3 and Metamask in React
Are those flyers about apartment purchase a scam?
Why is tert-butoxide often used in elimination reactions when it is not necessary?
What unique challenges/limitations will I face if I start a career as a pilot at 45 years old?
How would armour (and combat) change if the fighter didn't need to actually wear it?
How do I call a 6-digit Australian phone number with a US-based mobile phone?
Why command hierarchy, if the chain of command is standing next to each other?
"Table of Astronomy's" depiction of the solar system models
Do I have to cite common CS algorithms?
Shifting tenses in the middle of narration
If "more guns less crime", how do gun advocates explain that the EU has less crime than the US?
Scam? Phone call from "Department of Social Security" asking me to call back
(A room / an office) where an artist works
Why is the result of ('b'+'a'+ + 'a' + 'a').toLowerCase() 'banana'?
How far did Gandalf and the Balrog drop from the bridge in Moria?
What is a "soap"?
Causal Diagrams using Wolfram?
Why did IBM make the PC BIOS source code public?
Help, I cannot decide when to start the story
Can lodestones be used to magnetize crude iron weapons?
An array battle with odd secret powers
How did Arecibo detect methane lakes on Titan, and image Saturn's rings?
Word for an event that will likely never happen again
What is the status of this patent?
How do some PhD students get 10+ papers? Is that what I need for landing good faculty position?
Accessing accounts in metamask through web3.js 1.x
Better pattern to detect web3 default account when using metamaskHow to detect MetaMask loginIssue with getting web3.eth.accounts (MetaMask)Using web3 from MetaMask in ReactHow to get reference to Metamask account from Node.jsmetamask web3 is undefinedProblems with metamask/web3 connectionHow to handle user login in Ethereum DApps?Use local Metamask account with Infura nodeUse Web3 and Metamask in React
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I want to retrieve the currently selected account in my metamask plugin through web3.js. And I want to do it dynamically, so when switched to another account, it should be printed to the UI.
I'm importing the library (beta.37) via:<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.37/dist/web3.min.js"></script>"
To make things more complicated:
1) In Brave web3.eth.accounts[0] would log my current address in any other site than my dApp, yet here, it returns "undefined".
2) in Chrome (same build) it would always returns undefined.
To me it is inexplicable how it can return undefined, when other dApps that are built on web3.js 0.x use that exact same code.
Consequently, I can't use the following function, to dynamically print the current address:
var accountInterval = setInterval(function()
if (web3.eth.accounts[0] !== userAccount)
userAccount = web3.eth.accounts[0];
, 100);
web3js metamask
add a comment |
I want to retrieve the currently selected account in my metamask plugin through web3.js. And I want to do it dynamically, so when switched to another account, it should be printed to the UI.
I'm importing the library (beta.37) via:<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.37/dist/web3.min.js"></script>"
To make things more complicated:
1) In Brave web3.eth.accounts[0] would log my current address in any other site than my dApp, yet here, it returns "undefined".
2) in Chrome (same build) it would always returns undefined.
To me it is inexplicable how it can return undefined, when other dApps that are built on web3.js 0.x use that exact same code.
Consequently, I can't use the following function, to dynamically print the current address:
var accountInterval = setInterval(function()
if (web3.eth.accounts[0] !== userAccount)
userAccount = web3.eth.accounts[0];
, 100);
web3js metamask
add a comment |
I want to retrieve the currently selected account in my metamask plugin through web3.js. And I want to do it dynamically, so when switched to another account, it should be printed to the UI.
I'm importing the library (beta.37) via:<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.37/dist/web3.min.js"></script>"
To make things more complicated:
1) In Brave web3.eth.accounts[0] would log my current address in any other site than my dApp, yet here, it returns "undefined".
2) in Chrome (same build) it would always returns undefined.
To me it is inexplicable how it can return undefined, when other dApps that are built on web3.js 0.x use that exact same code.
Consequently, I can't use the following function, to dynamically print the current address:
var accountInterval = setInterval(function()
if (web3.eth.accounts[0] !== userAccount)
userAccount = web3.eth.accounts[0];
, 100);
web3js metamask
I want to retrieve the currently selected account in my metamask plugin through web3.js. And I want to do it dynamically, so when switched to another account, it should be printed to the UI.
I'm importing the library (beta.37) via:<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.37/dist/web3.min.js"></script>"
To make things more complicated:
1) In Brave web3.eth.accounts[0] would log my current address in any other site than my dApp, yet here, it returns "undefined".
2) in Chrome (same build) it would always returns undefined.
To me it is inexplicable how it can return undefined, when other dApps that are built on web3.js 0.x use that exact same code.
Consequently, I can't use the following function, to dynamically print the current address:
var accountInterval = setInterval(function()
if (web3.eth.accounts[0] !== userAccount)
userAccount = web3.eth.accounts[0];
, 100);
web3js metamask
web3js metamask
edited Mar 28 at 9:15
Marcellvs
asked Mar 27 at 10:31
MarcellvsMarcellvs
114 bronze badges
114 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
In web3.js 1.x you must use getAccounts() async method, e.g. as follows:
var accounts = await web3.eth.getAccounts();
var userAccount =accounts[0]
PS: web3.eth.accounts[0] in other Dapps shows your account because they are still using the old web3.js version, very likely the one injected by Metamask
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55375031%2faccessing-accounts-in-metamask-through-web3-js-1-x%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In web3.js 1.x you must use getAccounts() async method, e.g. as follows:
var accounts = await web3.eth.getAccounts();
var userAccount =accounts[0]
PS: web3.eth.accounts[0] in other Dapps shows your account because they are still using the old web3.js version, very likely the one injected by Metamask
add a comment |
In web3.js 1.x you must use getAccounts() async method, e.g. as follows:
var accounts = await web3.eth.getAccounts();
var userAccount =accounts[0]
PS: web3.eth.accounts[0] in other Dapps shows your account because they are still using the old web3.js version, very likely the one injected by Metamask
add a comment |
In web3.js 1.x you must use getAccounts() async method, e.g. as follows:
var accounts = await web3.eth.getAccounts();
var userAccount =accounts[0]
PS: web3.eth.accounts[0] in other Dapps shows your account because they are still using the old web3.js version, very likely the one injected by Metamask
In web3.js 1.x you must use getAccounts() async method, e.g. as follows:
var accounts = await web3.eth.getAccounts();
var userAccount =accounts[0]
PS: web3.eth.accounts[0] in other Dapps shows your account because they are still using the old web3.js version, very likely the one injected by Metamask
answered Jun 28 at 12:19
saccoddsaccodd
1222 silver badges11 bronze badges
1222 silver badges11 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55375031%2faccessing-accounts-in-metamask-through-web3-js-1-x%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown