Skip to content

Instantly share code, notes, and snippets.

@Pana
Forked from rekmarks/newProvider.js
Last active August 31, 2020 14:57
Show Gist options
  • Save Pana/0839e442c006d556e696c02ca0770e2f to your computer and use it in GitHub Desktop.
Save Pana/0839e442c006d556e696c02ca0770e2f to your computer and use it in GitHub Desktop.
Using the New MetaMask Inpage Provider
// Running on the page, in the browser
// This API will go live in early 2020
// It will be the only API available after a 6-week deprecation period
if (!conflux || !conflux.isConfluxPortal) {
throw new Error('Please install MetaMask.')
}
/*********************************************************/
/* Handle chain (network) and chainChanged, per EIP 1193 */
/*********************************************************/
let currentChainId = null
conflux.send('cfx_getStatus')
.then(handleChainChanged)
.catch(err => console.error(err)) // This should never happen
conflux.on('chainChanged', handleChainChanged)
function handleChainChanged (status) {
if (currentChainId !== status.chainId) {
currentChainId = chainId
// Run any other necessary logic...
}
}
/**********************************************************/
/* Handle user accounts and accountsChanged, per EIP 1193 */
/**********************************************************/
let currentAccount = null
conflux.send('cfx_accounts')
.then(handleAccountsChanged)
.catch(err => {
// In the future, maybe in 2020, this will return a 4100 error if
// the user has yet to connect
if (err.code === 4100) { // EIP 1193 unauthorized error
console.log('Please connect to MetaMask.')
} else {
console.error(err)
}
})
// Note that this event is emitted on page load.
// If the array of accounts is non-empty, you're already
// connected.
conflux.on('accountsChanged', handleAccountsChanged)
// For now, 'eth_accounts' will continue to always return an array
function handleAccountsChanged (accounts) {
if (accounts.length === 0) {
// MetaMask is locked or the user has not connected any accounts
console.log('Please connect to MetaMask.')
} else if (accounts[0] !== currentAccount) {
currentAccount = accounts[0]
// Run any other necessary logic...
}
}
/***********************************/
/* Handle connecting, per EIP 1102 */
/***********************************/
// You should only attempt to connect in response to user interaction,
// such as a button click. Otherwise, you're popup-spamming the user
// like it's 1999.
// If you can't retrieve the user's account(s), you should encourage the user
// to initiate a connection attempt.
document.getElementById('connectButton', connect)
function connect () {
// This is equivalent to ethereum.enable()
conflux.send('eth_requestAccounts')
.then(handleAccountsChanged)
.catch(err => {
if (err.code === 4001) { // EIP 1193 userRejectedRequest error
console.log('Please connect to MetaMask.')
} else {
console.error(err)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment