Skip to content

Commit

Permalink
refactor(sdk): handle error
Browse files Browse the repository at this point in the history
  • Loading branch information
ChesterSim committed Aug 5, 2024
1 parent 22675e6 commit a3d9c84
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions ts/sdk/src/vaultClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -194,22 +195,28 @@ export class VaultClient {
address?: PublicKey;
vault?: Vault;
}): Promise<BN> {
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;
}
}

/**
Expand Down

0 comments on commit a3d9c84

Please sign in to comment.