From d376bac1b696730f3f921f932e4bb8692a68b7c0 Mon Sep 17 00:00:00 2001 From: DaemonWAGMI Date: Tue, 1 Feb 2022 00:11:23 -0500 Subject: [PATCH 1/3] sdk: add calculateClientCollateralSize --- sdk/src/index.ts | 1 + sdk/src/math/collateral.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 sdk/src/math/collateral.ts diff --git a/sdk/src/index.ts b/sdk/src/index.ts index ec31522b..0508160d 100644 --- a/sdk/src/index.ts +++ b/sdk/src/index.ts @@ -10,6 +10,7 @@ export * from './addresses'; export * from './admin'; export * from './clearingHouseUser'; export * from './clearingHouse'; +export * from './math/collateral'; export * from './math/conversion'; export * from './math/funding'; export * from './math/insuranceFund'; diff --git a/sdk/src/math/collateral.ts b/sdk/src/math/collateral.ts new file mode 100644 index 00000000..ff96af51 --- /dev/null +++ b/sdk/src/math/collateral.ts @@ -0,0 +1,28 @@ +import { MarketsAccount, StateAccount } from '../types'; +import BN from 'bn.js'; +import { Connection } from '@solana/web3.js'; + +/** + * Client collateral is represented by the balance of the collateral wallet, as specified in the state, minus the sum of + * each markets undistributed fees. + * + * @param connection + * @param state + * @param marketsAccount + * @returns Precision : QUOTE_ASSET_PRECISION + */ +export async function calculateClientCollateralSize( + connection: Connection, + state: StateAccount, + marketsAccount: MarketsAccount +): Promise { + const collateralVaultPublicKey = state.collateralVault; + const collateralVaultAmount = new BN( + ( + await connection.getTokenAccountBalance(collateralVaultPublicKey) + ).value.amount + ); + return marketsAccount.markets.reduce((collateralVaultAmount, market) => { + return collateralVaultAmount.sub(market.amm.totalFee.div(new BN(2))); + }, collateralVaultAmount); +} From 26358ff87e0ee997cc9c601b4bcf5d7bb0895856 Mon Sep 17 00:00:00 2001 From: DaemonWAGMI Date: Thu, 3 Feb 2022 23:45:11 -0500 Subject: [PATCH 2/3] sdk: rename calculateClientCollateralSize --- sdk/src/math/collateral.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/math/collateral.ts b/sdk/src/math/collateral.ts index ff96af51..67665286 100644 --- a/sdk/src/math/collateral.ts +++ b/sdk/src/math/collateral.ts @@ -11,7 +11,7 @@ import { Connection } from '@solana/web3.js'; * @param marketsAccount * @returns Precision : QUOTE_ASSET_PRECISION */ -export async function calculateClientCollateralSize( +export async function calculateUserCollateralSize( connection: Connection, state: StateAccount, marketsAccount: MarketsAccount From 468d20629a7b785427118559763bfada19bd4e4e Mon Sep 17 00:00:00 2001 From: DaemonWAGMI Date: Tue, 15 Feb 2022 20:21:29 -0500 Subject: [PATCH 3/3] sdk: adjust calculateClientCollateralSize logic --- sdk/src/math/collateral.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sdk/src/math/collateral.ts b/sdk/src/math/collateral.ts index 67665286..b480d0cb 100644 --- a/sdk/src/math/collateral.ts +++ b/sdk/src/math/collateral.ts @@ -1,5 +1,4 @@ import { MarketsAccount, StateAccount } from '../types'; -import BN from 'bn.js'; import { Connection } from '@solana/web3.js'; /** @@ -23,6 +22,6 @@ export async function calculateUserCollateralSize( ).value.amount ); return marketsAccount.markets.reduce((collateralVaultAmount, market) => { - return collateralVaultAmount.sub(market.amm.totalFee.div(new BN(2))); + return collateralVaultAmount.sub(market.amm.totalFee); }, collateralVaultAmount); }