Use blockchain info provided by server to add network to user's wallet

This commit is contained in:
silverpill 2022-06-22 21:54:13 +00:00
parent 8d140ff95f
commit 01979e9fa8
2 changed files with 24 additions and 3 deletions

View file

@ -1,6 +1,14 @@
import { BACKEND_URL } from "@/constants"
import { http } from "./common"
interface ChainInfo {
chain_name: string;
public_api_url: string;
currency_name: string;
currency_symbol: string;
currency_decimals: string;
}
export interface InstanceInfo {
uri: string;
title: string;
@ -12,6 +20,7 @@ export interface InstanceInfo {
blockchain_id: string | null;
blockchain_explorer_url: string | null;
blockchain_contract_address: string | null;
blockchain_info: ChainInfo | null;
ipfs_gateway_url: string | null;
}

View file

@ -38,12 +38,24 @@ async function connectWallet(): Promise<void> {
)
} catch (switchError: any) {
// This error code indicates that the chain has not been added to MetaMask
if (switchError.code === 4902) {
if (switchError.code === 4902 && instance.value.blockchain_info) {
const { blockchain_explorer_url, blockchain_info } = instance.value
// https://eips.ethereum.org/EIPS/eip-3085
const ethereumChainParams = {
chainId: instanceChainId,
chainName: blockchain_info.chain_name,
rpcUrls: [blockchain_info.public_api_url],
blockExplorerUrls: blockchain_explorer_url ? [blockchain_explorer_url] : [],
nativeCurrency: {
name: blockchain_info.currency_name,
symbol: blockchain_info.currency_symbol,
decimals: parseInt(blockchain_info.currency_decimals),
},
}
try {
// https://eips.ethereum.org/EIPS/eip-3085
await provider.send(
"wallet_addEthereumChain",
[{ chainId: instanceChainId }],
[ethereumChainParams],
)
} catch (addError) {
walletError.value = "Incorrect network"