Skip to content

Commit

Permalink
fix: fallback to retry to add chain when unable to get eth provider
Browse files Browse the repository at this point in the history
  • Loading branch information
hthieu1110 committed Oct 8, 2024
1 parent 2cbb12a commit a933aae
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
6 changes: 3 additions & 3 deletions networks.json
Original file line number Diff line number Diff line change
Expand Up @@ -9226,18 +9226,18 @@
{
"id": "polygon",
"kind": "Ethereum",
"displayName": "Polygon",
"displayName": "Polygon Mainnet",
"icon": "polygon.svg",
"features": [
"RiotP2E"
],
"currencies": [
{
"denom": "0x0000000000000000000000000000000000000000",
"displayName": "MATIC",
"displayName": "POL",
"decimals": 18,
"variant": "ethereum",
"coingeckoId": "matic",
"coingeckoId": "pol-ex-matic",
"icon": "polygon.svg",
"kind": "native",
"color": "#232731"
Expand Down
4 changes: 2 additions & 2 deletions packages/networks/polygon/currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { CurrencyInfo } from "../types";
export const polygonCurrencies: CurrencyInfo[] = [
{
denom: "0x0000000000000000000000000000000000000000", // native currency: wei
displayName: "MATIC",
displayName: "POL",
decimals: 18,
variant: "ethereum",
coingeckoId: "matic",
coingeckoId: "pol-ex-matic",
icon: "polygon.svg",
kind: "native",
color: currencyETHcolor,
Expand Down
2 changes: 1 addition & 1 deletion packages/networks/polygon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EthereumNetworkInfo, NetworkFeature, NetworkKind } from "../types";
export const polygonNetwork: EthereumNetworkInfo = {
id: "polygon",
kind: NetworkKind.Ethereum,
displayName: "Polygon",
displayName: "Polygon Mainnet",
icon: "polygon.svg",
features: [NetworkFeature.RiotP2E],
currencies: polygonCurrencies,
Expand Down
51 changes: 49 additions & 2 deletions packages/utils/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import {
parseNetworkObjectId,
parseNftId,
NetworkKind,
NativeCurrencyInfo,
} from "../networks";

const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));

// this is used to block all calls to the provider getter while we wait for network switch auth
const proms: {
[key: string]: Promise<ethers.providers.Web3Provider | null> | undefined;
Expand Down Expand Up @@ -103,6 +106,39 @@ export const getEthereumProvider = async (
return alchemyProviders[cacheKey];
};

const addEthereumChain = async (network: EthereumNetworkInfo) => {
try {
const ethereum = (window as any).ethereum;
if (!ethereum) {
return null;
}

const currency = network.currencies[0] as NativeCurrencyInfo;

await ethereum.request({
method: "wallet_addEthereumChain",
params: [
{
chainId: "0x" + network.chainId.toString(16),
chainName: network.displayName,
rpcUrls: [network.endpoint],
nativeCurrency: {
name: currency.displayName,
symbol: currency.displayName,
decimals: currency.decimals,
},
blockExplorerUrls: [network.txExplorer],
},
],
});

return true;
} catch (err) {
console.error(err);
return null;
}
};

export const getMetaMaskEthereumSigner = async (
network: EthereumNetworkInfo | undefined,
address: string | undefined,
Expand All @@ -111,8 +147,19 @@ export const getMetaMaskEthereumSigner = async (
return null;
}

const provider = await getMetaMaskEthereumProvider(network.chainId);
if (!provider) return null;
let provider = await getMetaMaskEthereumProvider(network.chainId);

// If unable to switch to expected chain then try to
// add that chain to metamask then retry to get provider again
if (!provider) {
const isOk = await addEthereumChain(network);
if (!isOk) return null;

await sleep(1000);
provider = await getMetaMaskEthereumProvider(network.chainId);

if (!provider) return null;
}

return provider.getSigner(address);
};
Expand Down

0 comments on commit a933aae

Please sign in to comment.