From 4d8885f3b2e224cba438730b062ee20f0456931e Mon Sep 17 00:00:00 2001 From: jinoosss Date: Fri, 20 Oct 2023 14:56:54 +0900 Subject: [PATCH] [GSW-470] fix: Resolve wallet instance initialization errors --- .../wallet-client/adena/adena-client.ts | 3 +++ .../src/common/errors/common/common-error.ts | 4 +++ packages/web/src/hooks/wallet/use-wallet.ts | 27 ++++++++++++------- .../account/account-repository-impl.ts | 17 ++++++++++-- .../repositories/pool/pool-repository-impl.ts | 8 ++++-- .../repositories/swap/swap-repository-impl.ts | 7 +++-- packages/web/src/states/wallet.ts | 3 +-- 7 files changed, 52 insertions(+), 17 deletions(-) diff --git a/packages/web/src/common/clients/wallet-client/adena/adena-client.ts b/packages/web/src/common/clients/wallet-client/adena/adena-client.ts index 32ac27689..5e255d141 100644 --- a/packages/web/src/common/clients/wallet-client/adena/adena-client.ts +++ b/packages/web/src/common/clients/wallet-client/adena/adena-client.ts @@ -73,6 +73,9 @@ export class AdenaClient implements WalletClient { }; public static createAdenaClient() { + if (typeof window !== "undefined" && typeof window.adena !== "undefined") { + return null; + } return new AdenaClient(); } } diff --git a/packages/web/src/common/errors/common/common-error.ts b/packages/web/src/common/errors/common/common-error.ts index bffc75666..65916b209 100644 --- a/packages/web/src/common/errors/common/common-error.ts +++ b/packages/web/src/common/errors/common/common-error.ts @@ -9,6 +9,10 @@ const ERROR_VALUE = { status: 401, type: "Failed to initialize Gno Provider", }, + FAILED_INITIALIZE_WALLET: { + status: 402, + type: "Failed to initialize Wallet", + }, }; type ErrorType = keyof typeof ERROR_VALUE; diff --git a/packages/web/src/hooks/wallet/use-wallet.ts b/packages/web/src/hooks/wallet/use-wallet.ts index ec86ff693..9f141bf47 100644 --- a/packages/web/src/hooks/wallet/use-wallet.ts +++ b/packages/web/src/hooks/wallet/use-wallet.ts @@ -48,14 +48,21 @@ export const useWallet = () => { } const connectAdenaClient = useCallback(() => { - const adena = new AdenaClient(); - adena.initAdena(); + const adena = AdenaClient.createAdenaClient(); + if (adena !== null) { + adena.initAdena(); + } setWalletClient(adena); - connectAccount(); }, [setWalletClient]); - async function connectAccount() { - const established = await accountRepository.addEstablishedSite(); + const connectAccount = useCallback(async () => { + const established = await accountRepository + .addEstablishedSite() + .catch(() => null); + + if (established === null) { + return; + } if (established.code === 0 || established.code === 4001) { const account = await accountRepository.getAccount(); @@ -64,7 +71,7 @@ export const useWallet = () => { } else { accountRepository.setConnectedWallet(false); } - } + }, [accountRepository, setWalletAccount]); const disconnectWallet = useCallback(() => { setWalletAccount(null); @@ -78,11 +85,13 @@ export const useWallet = () => { try { walletClient.addEventChangedAccount(connectAdenaClient); walletClient.addEventChangedNetwork(connectAdenaClient); - } catch { - setWalletClient(new AdenaClient()); - } + } catch {} } + useEffect(() => { + if (walletClient) connectAccount(); + }, [connectAccount, walletClient]); + return { wallet, account: walletAccount, diff --git a/packages/web/src/repositories/account/account-repository-impl.ts b/packages/web/src/repositories/account/account-repository-impl.ts index 1ed175341..b0bf9b903 100644 --- a/packages/web/src/repositories/account/account-repository-impl.ts +++ b/packages/web/src/repositories/account/account-repository-impl.ts @@ -12,15 +12,16 @@ import { SendTransactionRequestParam } from "@common/clients/wallet-client/proto import { AccountMapper } from "@models/account/mapper/account-mapper"; import { NetworkClient } from "@common/clients/network-client"; import { AccountBalanceModel } from "@models/account/account-balance-model"; +import { CommonError } from "@common/errors"; export class AccountRepositoryImpl implements AccountRepository { - private walletClient: WalletClient; + private walletClient: WalletClient | null; private networkClient: NetworkClient; private localStorageClient: StorageClient; private sessionStorageClient: StorageClient; constructor( - walletClient: WalletClient, + walletClient: WalletClient | null, networkClient: NetworkClient, localStorageClient: StorageClient, sessionStorageClient: StorageClient, @@ -45,6 +46,9 @@ export class AccountRepositoryImpl implements AccountRepository { }; public getAccount = async () => { + if (this.walletClient === null) { + throw new CommonError("FAILED_INITIALIZE_WALLET"); + } const response = await this.walletClient.getAccount(); AdenaError.valdiate(response); return AccountMapper.fromResponse(response); @@ -60,17 +64,26 @@ export class AccountRepositoryImpl implements AccountRepository { }; public existsWallet = () => { + if (this.walletClient === null) { + throw new CommonError("FAILED_INITIALIZE_WALLET"); + } const response = this.walletClient.existsWallet(); return response; }; public addEstablishedSite = async () => { + if (this.walletClient === null) { + throw new CommonError("FAILED_INITIALIZE_WALLET"); + } const SITE_NAME = "Gnoswap"; const response = await this.walletClient.addEstablishedSite(SITE_NAME); return response; }; public sendTransaction = async (request: SendTransactionRequestParam) => { + if (this.walletClient === null) { + throw new CommonError("FAILED_INITIALIZE_WALLET"); + } const response = await this.walletClient.sendTransaction(request); return response; }; diff --git a/packages/web/src/repositories/pool/pool-repository-impl.ts b/packages/web/src/repositories/pool/pool-repository-impl.ts index e2e96500d..165876282 100644 --- a/packages/web/src/repositories/pool/pool-repository-impl.ts +++ b/packages/web/src/repositories/pool/pool-repository-impl.ts @@ -8,6 +8,7 @@ import { SwapFeeTierType, } from "@constants/option.constant"; import { SendTransactionSuccessResponse } from "@common/clients/wallet-client/protocols"; +import { CommonError } from "@common/errors"; const POOL_PATH = process.env.NEXT_PUBLIC_PACKAGE_POOL_PATH || ""; const POSITION_PATH = process.env.NEXT_PUBLIC_PACKAGE_POSITION_PATH || ""; @@ -15,9 +16,9 @@ const POOL_ADDRESS = process.env.NEXT_PUBLIC_PACKAGE_POOL_ADDRESS || ""; export class PoolRepositoryImpl implements PoolRepository { private networkClient: NetworkClient; - private walletClient: WalletClient; + private walletClient: WalletClient | null; - constructor(networkClient: NetworkClient, walletClient: WalletClient) { + constructor(networkClient: NetworkClient, walletClient: WalletClient | null) { this.networkClient = networkClient; this.walletClient = walletClient; } @@ -42,6 +43,9 @@ export class PoolRepositoryImpl implements PoolRepository { }; createPool = async (request: CreatePoolRequest): Promise => { + if (this.walletClient === null) { + throw new CommonError("FAILED_INITIALIZE_WALLET"); + } const { tokenA, tokenB, diff --git a/packages/web/src/repositories/swap/swap-repository-impl.ts b/packages/web/src/repositories/swap/swap-repository-impl.ts index d23748b3e..71d5b95a2 100644 --- a/packages/web/src/repositories/swap/swap-repository-impl.ts +++ b/packages/web/src/repositories/swap/swap-repository-impl.ts @@ -29,10 +29,10 @@ const POOL_ADDRESS = process.env.NEXT_PUBLIC_PACKAGE_POOL_ADDRESS || ""; export class SwapRepositoryImpl implements SwapRepository { private localStorageClient: StorageClient; private rpcProvider: GnoProvider | null; - private walletClient: WalletClient; + private walletClient: WalletClient | null; constructor( - walletClient: WalletClient, + walletClient: WalletClient | null, rpcProvider: GnoProvider | null, localStorageClient: StorageClient, ) { @@ -155,6 +155,9 @@ export class SwapRepositoryImpl implements SwapRepository { }; public swap = async (swapRequest: SwapRequest): Promise => { + if (this.walletClient === null) { + throw new CommonError("FAILED_INITIALIZE_WALLET"); + } const poolPackagePath = process.env.NEXT_PUBLIC_PACKAGE_POOL_PATH; const account = await this.walletClient.getAccount(); if (!account.data || !poolPackagePath) { diff --git a/packages/web/src/states/wallet.ts b/packages/web/src/states/wallet.ts index 6d9901bf8..5a03dd2dd 100644 --- a/packages/web/src/states/wallet.ts +++ b/packages/web/src/states/wallet.ts @@ -1,8 +1,7 @@ import { WalletClient } from "@common/clients/wallet-client"; -import { AdenaClient } from "@common/clients/wallet-client/adena"; import { AccountModel } from "@models/account/account-model"; import { atom } from "jotai"; -export const client = atom(new AdenaClient()); +export const client = atom(null); export const account = atom(null);