From a3d9c843d15772ca0d39fe72b12db745af554592 Mon Sep 17 00:00:00 2001 From: Chester Sim Date: Mon, 5 Aug 2024 17:17:30 +0800 Subject: [PATCH] refactor(sdk): handle error --- ts/sdk/src/vaultClient.ts | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/ts/sdk/src/vaultClient.ts b/ts/sdk/src/vaultClient.ts index 14dd858b..b5fc5076 100644 --- a/ts/sdk/src/vaultClient.ts +++ b/ts/sdk/src/vaultClient.ts @@ -8,6 +8,7 @@ import { getUserStatsAccountPublicKey, TEN, UserMap, + ZERO, } from '@drift-labs/sdk'; import { BorshAccountsCoder, Program, ProgramAccount } from '@coral-xyz/anchor'; import { DriftVaults } from './types/drift_vaults'; @@ -194,22 +195,28 @@ export class VaultClient { address?: PublicKey; vault?: Vault; }): Promise { - let vaultAccount: Vault; - if (params.address !== undefined) { - // @ts-ignore - vaultAccount = await this.program.account.vault.fetch(params.address); - } else if (params.vault !== undefined) { - vaultAccount = params.vault; - } else { - throw new Error('Must supply address or vault'); - } - - const user = await this.getSubscribedVaultUser(vaultAccount.user); - - const netSpotValue = user.getNetSpotMarketValue(); - const unrealizedPnl = user.getUnrealizedPNL(true, undefined, undefined); + try { - return netSpotValue.add(unrealizedPnl); + let vaultAccount: Vault; + if (params.address !== undefined) { + // @ts-ignore + vaultAccount = await this.program.account.vault.fetch(params.address); + } else if (params.vault !== undefined) { + vaultAccount = params.vault; + } else { + throw new Error('Must supply address or vault'); + } + + const user = await this.getSubscribedVaultUser(vaultAccount.user); + + const netSpotValue = user.getNetSpotMarketValue(); + const unrealizedPnl = user.getUnrealizedPNL(true, undefined, undefined); + + return netSpotValue.add(unrealizedPnl); + } catch (err) { + console.error("VaultClient ~ err:", err); + return ZERO; + } } /**