-
Notifications
You must be signed in to change notification settings - Fork 17
frontend: Migrate to use the new MetaMask API #339
frontend: Migrate to use the new MetaMask API #339
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #339 +/- ##
============================================
- Coverage 89.57% 64.61% -24.96%
============================================
Files 22 22
Lines 1775 1775
============================================
- Hits 1590 1147 -443
- Misses 185 628 +443
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good as far as I can tell. Added some questions, but most of them are out of curiosity.
Please ignore if the answer is obvious.
I believe the 1 missing error handling should be implemented though.
resources/static/js/account.js
Outdated
async function connectWeb3() { | ||
let accounts = []; | ||
try { | ||
accounts = await provider.request({ method: 'eth_accounts' }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you even need this method, and is there a scenario that this will not return an empty list?
I undertstood it that way that the eth_requestAccounts
should be generally used.
This is ok for the user, since there will only be a UI-prompt when access is requested the first time (?)
try {
// Request account access if needed
const accounts = await ethereum.send('eth_requestAccounts');
// Accounts now exposed, use them
ethereum.send('eth_sendTransaction', { from: accounts[0], /* ... */ })
} catch (error) {
// User denied account access
}
(from EIP-1102)
I could be wrong - so I'm mainly just curious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user may already have given the allowance to access the accounts, when the button is clicked. For example in case the button is clicked a second time after canceling the first transaction.
In my understanding, every call to eth_requestAccounts
may trigger a user interface, depending on the RPC Provider. Since this is not the case with MetaMask, I think we can just use the call to eth_requestAccounts
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok - I couldn't find anything about eth_accounts
being unlocked by the eth_requestAccounts
as well,
and the example code from the metamask docs was using eth_requestAccounts
just before a transaction.
- Handle errors from requesting `eth_chainId` - Remove `eth_accounts` request and only use `eth_requestAccounts`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good
Fixes #338
MetaMask removed the injected
window.web3
object recently, which we relied on. This broke our integration of MetaMask.MetaMask should now be used through the
window.ethereum
API, which is specified by EIP 1193. This PR migrates the frontend to use this API to make the transaction request. The only place where we send a transaction from the frontend is for funding the user account.This uses a small library (
@metamask/detect-provider
) to detect the rpc provider as recommended by MetaMask.