You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
async function getSignedData(publicAddress, jsonData) {
|
|
const signedData = await window.ethereum.request({
|
|
method: 'eth_signTypedData_v3',
|
|
params: [publicAddress, JSON.stringify(jsonData)]
|
|
});
|
|
console.log(signedData);
|
|
return signedData
|
|
}
|
|
|
|
async function confirmAvalanche(){
|
|
let debug = true;
|
|
let chainId;
|
|
let rpcUrl;
|
|
let explorerUrl;
|
|
let name;
|
|
if (debug) {
|
|
name = 'Avalance Testnet';
|
|
chainId = '0xA869';
|
|
rpcUrl = 'https://api.avax-test.network/ext/bc/C/rpc';
|
|
explorerUrl = 'https://testnet.snowtrace.io';
|
|
} else {
|
|
name = 'Avalanche Mainnet';
|
|
chainId = '0xA86A';
|
|
rpcUrl = 'https://api.avax.network/ext/bc/C/rpc';
|
|
explorerUrl = 'https://snowtrace.io';
|
|
}
|
|
try {
|
|
await ethereum.request({
|
|
method: 'wallet_switchEthereumChain',
|
|
params: [{ chainId: chainId }],
|
|
});
|
|
} catch (switchError) {
|
|
// This error code indicates that the chain has not been added to MetaMask.
|
|
if (switchError.code === 4902) {
|
|
try {
|
|
await ethereum.request({
|
|
method: 'wallet_addEthereumChain',
|
|
params: [{
|
|
chainId: chainId,
|
|
chainName: name,
|
|
nativeCurrency: {
|
|
name: 'Avalanche',
|
|
symbol: 'AVAX',
|
|
decimals: 18,
|
|
},
|
|
rpcUrls: [rpcUrl],
|
|
blockExplorerUrls: [explorerUrl],
|
|
}],
|
|
});
|
|
} catch (addError) {
|
|
// handle "add" error
|
|
}
|
|
}
|
|
// handle other "switch" errors
|
|
}
|
|
}
|