Skip to content

Commit

Permalink
Merge pull request #218 from gnoswap-labs/GSW-470-resolve-wallet-inst…
Browse files Browse the repository at this point in the history
…ance-initialization-errors

[GSW-470] fix: Resolve wallet instance initialization errors
  • Loading branch information
jinoosss authored Oct 20, 2023
2 parents 96ebdd7 + 4d8885f commit f8bf236
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
4 changes: 4 additions & 0 deletions packages/web/src/common/errors/common/common-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 18 additions & 9 deletions packages/web/src/hooks/wallet/use-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -64,7 +71,7 @@ export const useWallet = () => {
} else {
accountRepository.setConnectedWallet(false);
}
}
}, [accountRepository, setWalletAccount]);

const disconnectWallet = useCallback(() => {
setWalletAccount(null);
Expand All @@ -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,
Expand Down
17 changes: 15 additions & 2 deletions packages/web/src/repositories/account/account-repository-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<StorageKeyType>;
private sessionStorageClient: StorageClient<SessionStorageKeyType>;

constructor(
walletClient: WalletClient,
walletClient: WalletClient | null,
networkClient: NetworkClient,
localStorageClient: StorageClient,
sessionStorageClient: StorageClient,
Expand All @@ -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);
Expand All @@ -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;
};
Expand Down
8 changes: 6 additions & 2 deletions packages/web/src/repositories/pool/pool-repository-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ 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 || "";
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;
}
Expand All @@ -42,6 +43,9 @@ export class PoolRepositoryImpl implements PoolRepository {
};

createPool = async (request: CreatePoolRequest): Promise<string | null> => {
if (this.walletClient === null) {
throw new CommonError("FAILED_INITIALIZE_WALLET");
}
const {
tokenA,
tokenB,
Expand Down
7 changes: 5 additions & 2 deletions packages/web/src/repositories/swap/swap-repository-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand Down Expand Up @@ -155,6 +155,9 @@ export class SwapRepositoryImpl implements SwapRepository {
};

public swap = async (swapRequest: SwapRequest): Promise<SwapResponse> => {
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) {
Expand Down
3 changes: 1 addition & 2 deletions packages/web/src/states/wallet.ts
Original file line number Diff line number Diff line change
@@ -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<WalletClient>(new AdenaClient());
export const client = atom<WalletClient | null>(null);

export const account = atom<AccountModel | null>(null);

0 comments on commit f8bf236

Please sign in to comment.