Skip to content

Commit

Permalink
sdk: dlp updates (#822)
Browse files Browse the repository at this point in the history
* dlp updates

* add optional custom margin ratio param to initAndDeposit

* prettify
  • Loading branch information
lowkeynicc authored Jan 17, 2024
1 parent 87fba60 commit d8b18b4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 14 additions & 3 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,8 @@ export class DriftClient {
}

public async updateUserCustomMarginRatio(
updates: { marginRatio: number; subAccountId: number }[]
updates: { marginRatio: number; subAccountId: number }[],
txParams?: TxParams
): Promise<TransactionSignature> {
const ixs = await Promise.all(
updates.map(async ({ marginRatio, subAccountId }) => {
Expand All @@ -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;
Expand Down Expand Up @@ -1877,7 +1878,8 @@ export class DriftClient {
fromSubAccountId?: number,
referrerInfo?: ReferrerInfo,
donateAmount?: BN,
txParams?: TxParams
txParams?: TxParams,
customMaxMarginRatio?: number
): Promise<[TransactionSignature, PublicKey]> {
const ixs = [];

Expand Down Expand Up @@ -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(
Expand Down
16 changes: 8 additions & 8 deletions sdk/src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand All @@ -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];
}
Expand Down

0 comments on commit d8b18b4

Please sign in to comment.