Skip to content

Commit

Permalink
enforce mainnet in metamask (#9689)
Browse files Browse the repository at this point in the history
  • Loading branch information
chibie authored Nov 16, 2021
1 parent 9358b93 commit 28defb1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
23 changes: 21 additions & 2 deletions app/assets/v2/js/cart-ethereum-zksync.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ Vue.component('grantsCartEthereumZksync', {
this.zksync.showModal = false; // hide checkout modal if visible
this.resetZkSyncModal(); // reset modal settings

_alert('There is an insufficient balance to complete checkout. Please load funds and try again.', 'danger');
this.handleError(new Error('Insufficient balance to complete checkout')); // throw error and show to user
this.handleError(new Error('There is an insufficient balance to complete checkout. Please load funds and try again.')); // throw error and show to user
},

// Reset zkSync modal status after a checkout failure
Expand Down Expand Up @@ -217,6 +216,26 @@ Vue.component('grantsCartEthereumZksync', {
throw new Error('Please connect a wallet');
}

let networkId = String(Number(web3.eth.currentProvider.chainId));

if (networkId !== '1' && networkId !== '4') {
// User MetaMask must be connected to Ethereum mainnet
try {
await ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x1' }]
});
} catch (switchError) {
if (switchError.code === 4001) {
throw new Error('Please connect MetaMask to Ethereum network.');
} else if (switchError.code === -32002) {
throw new Error('Please respond to a pending MetaMask request.');
} else {
console.error(switchError);
}
}
}

// Make sure setup is completed properly
const isCorrectNetwork = this.zksync.checkoutManager && this.zksync.checkoutManager.network === this.network;

Expand Down
2 changes: 1 addition & 1 deletion app/assets/v2/js/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ Vue.component('grants-cart', {
let networkName = getDataChains(networkId, 'chainId')[0] && getDataChains(networkId, 'chainId')[0].network;

if (networkName == 'mainnet' && networkId !== '1') {
// User MetaMask must be connected to Ethereum mainnet or a supported testnet
// User MetaMask must be connected to Ethereum mainnet
try {
await ethereum.request({
method: 'wallet_switchEthereumChain',
Expand Down

0 comments on commit 28defb1

Please sign in to comment.