Created
March 27, 2023 18:40
-
-
Save nheingit/2205191fba02aadc159b08b9934350f1 to your computer and use it in GitHub Desktop.
Phantom Polygon Switch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const addMaticToMetamask: () => void = () => { | |
const { ethereum, phantom } = window as any; | |
if (ethereum) { | |
ethereum | |
.request({ | |
method: 'wallet_addEthereumChain', | |
params: [ | |
{ | |
chainId: '0x89', | |
chainName: 'Polygon Network', | |
rpcUrls: ['https://polygon-rpc.com/'], | |
iconUrls: [ | |
'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0/logo.png', | |
], | |
blockExplorerUrls: ['https://polygonscan.com/'], | |
nativeCurrency: { | |
name: 'Matic Token', | |
symbol: 'MATIC', | |
decimals: 18, | |
}, | |
}, | |
], // you must have access to the specified account | |
}) | |
.catch((error: any) => { | |
if (error.code === 4001) { | |
console.log('We can encrypt anything without the key.'); | |
} else { | |
console.error(error); | |
} | |
}); | |
} | |
if (phantom) { | |
phantom.ethereum | |
.request({ | |
method: 'wallet_switchEthereumChain', | |
params: [ | |
{ | |
chainId: '0x89', | |
} | |
], // you must have access to the specified account | |
}) | |
.catch((error: any) => { | |
if (error.code === 4001) { | |
console.log('We can encrypt anything without the key.'); | |
} else { | |
console.error(error); | |
} | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment