From c21c240a8104f18b933a5249a9a4125669b49f46 Mon Sep 17 00:00:00 2001 From: "Justin R. Evans" Date: Thu, 8 Feb 2024 07:42:27 -0500 Subject: [PATCH] fix: initialize chain and query native token properly --- .../src/background/chains/service.ts | 79 ++++++++----------- 1 file changed, 35 insertions(+), 44 deletions(-) diff --git a/apps/extension/src/background/chains/service.ts b/apps/extension/src/background/chains/service.ts index d83710f32f..1a3ca03297 100644 --- a/apps/extension/src/background/chains/service.ts +++ b/apps/extension/src/background/chains/service.ts @@ -4,70 +4,61 @@ import { KVStore } from "@namada/storage"; import { Chain } from "@namada/types"; import { ExtensionBroadcaster } from "extension"; -const { - NAMADA_INTERFACE_NAMADA_TOKEN: - tokenAddress = "tnam1qxgfw7myv4dh0qna4hq0xdg6lx77fzl7dcem8h7e", -} = process.env; - export const CHAINS_KEY = "chains"; export class ChainsService { constructor( protected readonly chainsStore: KVStore, protected readonly broadcaster: ExtensionBroadcaster - ) { } + ) { + this._initChain(); + } - async getChain(): Promise { - const chain = await this.chainsStore.get(CHAINS_KEY); - if (!chain) { - // Initialize default chain in storage - const defaultChain = chains.namada; - const { - currency: { address }, - } = defaultChain; - if (!address) { - const query = new Query(defaultChain.rpc); - try { - const nativeToken = await query.query_native_token(); - defaultChain.currency.address = nativeToken || tokenAddress; - } catch (e) { - console.warn(`Chain is unreachable: ${e}`); - } + private async _queryNativeToken(chain: Chain): Promise { + const query = new Query(chain.rpc); + try { + const nativeToken = await query.query_native_token(); + if (nativeToken) { + chain.currency.address = nativeToken; } + } catch (e) { + console.warn(`Chain is unreachable: ${e}`); + } + + await this.chainsStore.set(CHAINS_KEY, chain); + return chain; + } - await this.chainsStore.set(CHAINS_KEY, defaultChain); - return defaultChain; + private async _initChain(): Promise { + // Initialize default chain in storage + const chain = (await this.chainsStore.get(CHAINS_KEY)) || chains.namada; + // Default chain config does not have a token address, so we query: + if (!chain.currency.address) { + this._queryNativeToken(chain); + } + } + + async getChain(): Promise { + let chain = (await this.chainsStore.get(CHAINS_KEY)) || chains.namada; + // If a previous query for native token failed, attempt again: + if (!chain.currency.address) { + chain = await this._queryNativeToken(chain); } return chain; } async updateChain(chainId: string, url: string): Promise { - const chain = await this.getChain(); + let chain = await this.getChain(); if (!chain) { throw new Error("No chain found!"); } - - // If query fails, leave address undefined so it will be caught when chain is available - let address: string | undefined; - - // Attempt to fetch native token, fallback to env - try { - const query = new Query(chain.rpc); - const nativeToken = await query.query_native_token(); - address = nativeToken || tokenAddress; - } catch (e) { - console.warn(`Chain is unreachable: ${e}`); - } - - await this.chainsStore.set(CHAINS_KEY, { + // Update RPC & Chain ID, then query for native token + chain = { ...chain, chainId, rpc: url, - currency: { - ...chain.currency, - address, - }, - }); + }; + await this.chainsStore.set(CHAINS_KEY, await this._queryNativeToken(chain)); this.broadcaster.updateNetwork(); } }