diff --git a/CHANGELOG.md b/CHANGELOG.md index 6edd0432b..a15f5a399 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,11 +20,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - sdk: add ability to do placeAndTake order with bracket orders attached - sdk: add option to cancel existing orders in market for place and take order - sdk: add option to get signed settlePnl tx back from a market order - - program: auto derisk lp positions in settle pnl ([#766](https://github.com/drift-labs/protocol-v2/pull/766)) - program: increase full perp liquidation threshold ([#807](https://github.com/drift-labs/protocol-v2/pull/807)) - program: remove spot fee pool transfer ([#800](https://github.com/drift-labs/protocol-v2/pull/800)) - program: increase insurance tier max ([#784](https://github.com/drift-labs/protocol-v2/pull/784)) +- sdk: can specify max custom margin ratio to initialize a new account with ### Fixes diff --git a/sdk/src/driftClient.ts b/sdk/src/driftClient.ts index e9b8fc026..5502dd7b6 100644 --- a/sdk/src/driftClient.ts +++ b/sdk/src/driftClient.ts @@ -904,7 +904,8 @@ export class DriftClient { } public async updateUserCustomMarginRatio( - updates: { marginRatio: number; subAccountId: number }[] + updates: { marginRatio: number; subAccountId: number }[], + txParams?: TxParams ): Promise { const ixs = await Promise.all( updates.map(async ({ marginRatio, subAccountId }) => { @@ -916,7 +917,7 @@ export class DriftClient { }) ); - const tx = await this.buildTransaction(ixs, this.txParams); + const tx = await this.buildTransaction(ixs, txParams ?? this.txParams); const { txSig } = await this.sendTransaction(tx, [], this.opts); return txSig; @@ -1877,7 +1878,8 @@ export class DriftClient { fromSubAccountId?: number, referrerInfo?: ReferrerInfo, donateAmount?: BN, - txParams?: TxParams + txParams?: TxParams, + customMaxMarginRatio?: number ): Promise<[TransactionSignature, PublicKey]> { const ixs = []; @@ -1971,6 +1973,15 @@ export class DriftClient { ixs.push(donateIx); } + // Set the max margin ratio to initialize account with if passed + if (customMaxMarginRatio) { + const customMarginRatioIx = await this.getUpdateUserCustomMarginRatioIx( + customMaxMarginRatio, + subAccountId + ); + ixs.push(customMarginRatioIx); + } + // Close the wrapped sol account at the end of the transaction if (createWSOLTokenAccount) { ixs.push( diff --git a/sdk/src/user.ts b/sdk/src/user.ts index 5b4a9a7c6..4516ad489 100644 --- a/sdk/src/user.ts +++ b/sdk/src/user.ts @@ -388,12 +388,16 @@ export class User { /** * calculates the open bids and asks for an lp + * optionally pass in lpShares to see what bid/asks a user *would* take on * @returns : lp open bids * @returns : lp open asks */ - public getLPBidAsks(marketIndex: number): [BN, BN] { + public getLPBidAsks(marketIndex: number, lpShares?: BN): [BN, BN] { const position = this.getPerpPosition(marketIndex); - if (position === undefined || position.lpShares.eq(ZERO)) { + + const lpSharesToCalc = lpShares ?? position?.lpShares; + + if (!lpSharesToCalc || lpSharesToCalc.eq(ZERO)) { return [ZERO, ZERO]; } @@ -405,12 +409,8 @@ export class User { market.amm.orderStepSize ); - const lpOpenBids = marketOpenBids - .mul(position.lpShares) - .div(market.amm.sqrtK); - const lpOpenAsks = marketOpenAsks - .mul(position.lpShares) - .div(market.amm.sqrtK); + const lpOpenBids = marketOpenBids.mul(lpSharesToCalc).div(market.amm.sqrtK); + const lpOpenAsks = marketOpenAsks.mul(lpSharesToCalc).div(market.amm.sqrtK); return [lpOpenBids, lpOpenAsks]; }