Skip to content

Commit

Permalink
update calculateFeeForQuoteAmount and prettify (#777)
Browse files Browse the repository at this point in the history
* update calculateFeeForQuoteAmount and prettify

* move down feeTier

* fix import
  • Loading branch information
lowkeynicc authored Dec 18, 2023
1 parent c8fa720 commit 6268f6c
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions sdk/src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
OPEN_ORDER_MARGIN_REQUIREMENT,
PRICE_PRECISION,
QUOTE_PRECISION,
QUOTE_PRECISION_EXP,
QUOTE_SPOT_MARKET_INDEX,
SPOT_MARKET_WEIGHT_PRECISION,
TEN,
Expand All @@ -40,6 +41,7 @@ import {
UserAccountSubscriber,
} from './accounts/types';
import {
BigNum,
BN,
calculateBaseAssetValue,
calculateMarketMarginRatio,
Expand Down Expand Up @@ -3013,7 +3015,7 @@ export class User {
let makerFee =
feeTier.makerRebateNumerator / feeTier.makerRebateDenominator;

if (marketIndex && isVariant(marketType, 'perp')) {
if (marketIndex !== undefined && isVariant(marketType, 'perp')) {
const marketAccount = this.driftClient.getPerpMarketAccount(marketIndex);
takerFee += (takerFee * marketAccount.feeAdjustment) / 100;
makerFee += (makerFee * marketAccount.feeAdjustment) / 100;
Expand All @@ -3030,11 +3032,22 @@ export class User {
* @param quoteAmount
* @returns feeForQuote : Precision QUOTE_PRECISION
*/
public calculateFeeForQuoteAmount(quoteAmount: BN): BN {
const feeTier = this.getUserFeeTier(MarketType.PERP);
return quoteAmount
.mul(new BN(feeTier.feeNumerator))
.div(new BN(feeTier.feeDenominator));
public calculateFeeForQuoteAmount(quoteAmount: BN, marketIndex?: number): BN {
if (marketIndex !== undefined) {
const takerFeeMultiplier = this.getMarketFees(
MarketType.PERP,
marketIndex
).takerFee;
const feeAmountNum =
BigNum.from(quoteAmount, QUOTE_PRECISION_EXP).toNum() *
takerFeeMultiplier;
return BigNum.fromPrint(feeAmountNum.toString(), QUOTE_PRECISION_EXP).val;
} else {
const feeTier = this.getUserFeeTier(MarketType.PERP);
return quoteAmount
.mul(new BN(feeTier.feeNumerator))
.div(new BN(feeTier.feeDenominator));
}
}

/**
Expand Down

0 comments on commit 6268f6c

Please sign in to comment.