Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from './clearingHouseUser';
export * from './clearingHouse';
export * from './factory/clearingHouse';
export * from './factory/clearingHouseUser';
export * from './math/collateral';
export * from './math/conversion';
export * from './math/funding';
export * from './math/insuranceFund';
Expand Down
27 changes: 27 additions & 0 deletions sdk/src/math/collateral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { MarketsAccount, StateAccount } from '../types';
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 calculateUserCollateralSize(
connection: Connection,
state: StateAccount,
marketsAccount: MarketsAccount
): Promise<BN> {
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);
}, collateralVaultAmount);
}