Skip to content

Commit

Permalink
sdk; move getInsuranceFuelBonnus to userStats (#1352)
Browse files Browse the repository at this point in the history
* sdk; move getInsuranceFuelBonnus to userStats

* sdk: fix imports
  • Loading branch information
evanpipta authored Dec 4, 2024
1 parent 3e18eed commit d22aceb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 62 deletions.
81 changes: 20 additions & 61 deletions sdk/src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
QUOTE_PRECISION_EXP,
QUOTE_SPOT_MARKET_INDEX,
SPOT_MARKET_WEIGHT_PRECISION,
GOV_SPOT_MARKET_INDEX,
TEN,
TEN_THOUSAND,
TWO,
Expand Down Expand Up @@ -98,11 +97,7 @@ import { calculateLiveOracleTwap } from './math/oracles';
import { getPerpMarketTierNumber, getSpotMarketTierNumber } from './math/tiers';
import { StrictOraclePrice } from './oracles/strictOraclePrice';

import {
calculateSpotFuelBonus,
calculatePerpFuelBonus,
calculateInsuranceFuelBonus,
} from './math/fuel';
import { calculateSpotFuelBonus, calculatePerpFuelBonus } from './math/fuel';
import { grpcUserAccountSubscriber } from './accounts/grpcUserAccountSubscriber';

export class User {
Expand Down Expand Up @@ -931,21 +926,24 @@ export class User {
positionFuel: ZERO,
};

const userStats = this.driftClient.getUserStats();
const userStatsAccount: UserStatsAccount = userStats.getAccount();

if (includeSettled) {
const userStats: UserStatsAccount = this.driftClient
.getUserStats()
.getAccount();
result.insuranceFuel = result.insuranceFuel.add(
new BN(userStats.fuelInsurance)
result.takerFuel = result.takerFuel.add(
new BN(userStatsAccount.fuelTaker)
);
result.makerFuel = result.makerFuel.add(
new BN(userStatsAccount.fuelMaker)
);
result.takerFuel = result.takerFuel.add(new BN(userStats.fuelTaker));
result.makerFuel = result.makerFuel.add(new BN(userStats.fuelMaker));
result.depositFuel = result.depositFuel.add(
new BN(userStats.fuelDeposits)
new BN(userStatsAccount.fuelDeposits)
);
result.borrowFuel = result.borrowFuel.add(
new BN(userStatsAccount.fuelBorrows)
);
result.borrowFuel = result.borrowFuel.add(new BN(userStats.fuelBorrows));
result.positionFuel = result.positionFuel.add(
new BN(userStats.fuelPositions)
new BN(userStatsAccount.fuelPositions)
);
}

Expand Down Expand Up @@ -1027,53 +1025,14 @@ export class User {
);
}
}

const userStats: UserStatsAccount = this.driftClient
.getUserStats()
.getAccount();

// todo: get real time ifStakedGovTokenAmount using ifStakeAccount
if (userStats.ifStakedGovTokenAmount.gt(ZERO)) {
const spotMarketAccount: SpotMarketAccount =
this.driftClient.getSpotMarketAccount(GOV_SPOT_MARKET_INDEX);

const fuelBonusNumeratorUserStats = BN.max(
now.sub(
BN.max(new BN(userStats.lastFuelIfBonusUpdateTs), FUEL_START_TS)
),
ZERO
);

result.insuranceFuel = result.insuranceFuel.add(
calculateInsuranceFuelBonus(
spotMarketAccount,
userStats.ifStakedGovTokenAmount,
fuelBonusNumeratorUserStats
)
);
}

if (userStats.ifStakedQuoteAssetAmount.gt(ZERO)) {
const spotMarketAccount: SpotMarketAccount =
this.driftClient.getSpotMarketAccount(QUOTE_SPOT_MARKET_INDEX);

const fuelBonusNumeratorUserStats = BN.max(
now.sub(
BN.max(new BN(userStats.lastFuelIfBonusUpdateTs), FUEL_START_TS)
),
ZERO
);

result.insuranceFuel = result.insuranceFuel.add(
calculateInsuranceFuelBonus(
spotMarketAccount,
userStats.ifStakedQuoteAssetAmount,
fuelBonusNumeratorUserStats
)
);
}
}

result.insuranceFuel = userStats.getInsuranceFuelBonus(
now,
includeSettled,
includeUnsettled
);

return result;
}

Expand Down
69 changes: 68 additions & 1 deletion sdk/src/userStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ import { DataAndSlot, UserStatsAccountSubscriber } from './accounts/types';
import { UserStatsConfig } from './userStatsConfig';
import { PollingUserStatsAccountSubscriber } from './accounts/pollingUserStatsAccountSubscriber';
import { WebSocketUserStatsAccountSubscriber } from './accounts/webSocketUserStatsAccountSubsriber';
import { ReferrerInfo, UserStatsAccount } from './types';
import { ReferrerInfo, SpotMarketAccount, UserStatsAccount } from './types';
import {
getUserAccountPublicKeySync,
getUserStatsAccountPublicKey,
} from './addresses/pda';
import { grpcUserStatsAccountSubscriber } from './accounts/grpcUserStatsAccountSubscriber';
import { FUEL_START_TS } from './constants/numericConstants';
import { ZERO } from './constants/numericConstants';
import {
GOV_SPOT_MARKET_INDEX,
QUOTE_SPOT_MARKET_INDEX,
} from './constants/numericConstants';
import { BN } from '@coral-xyz/anchor';
import { calculateInsuranceFuelBonus } from './math/fuel';

export class UserStats {
driftClient: DriftClient;
Expand Down Expand Up @@ -79,6 +87,65 @@ export class UserStats {
return this.accountSubscriber.getUserStatsAccountAndSlot().data;
}

public getInsuranceFuelBonus(
now: BN,
includeSettled = true,
includeUnsettled = true
): BN {
const userStats: UserStatsAccount = this.getAccount();

let insuranceFuel = ZERO;

if (includeSettled) {
insuranceFuel = insuranceFuel.add(new BN(userStats.fuelInsurance));
}

if (includeUnsettled) {
// todo: get real time ifStakedGovTokenAmount using ifStakeAccount
if (userStats.ifStakedGovTokenAmount.gt(ZERO)) {
const spotMarketAccount: SpotMarketAccount =
this.driftClient.getSpotMarketAccount(GOV_SPOT_MARKET_INDEX);

const fuelBonusNumeratorUserStats = BN.max(
now.sub(
BN.max(new BN(userStats.lastFuelIfBonusUpdateTs), FUEL_START_TS)
),
ZERO
);

insuranceFuel = insuranceFuel.add(
calculateInsuranceFuelBonus(
spotMarketAccount,
userStats.ifStakedGovTokenAmount,
fuelBonusNumeratorUserStats
)
);
}

if (userStats.ifStakedQuoteAssetAmount.gt(ZERO)) {
const spotMarketAccount: SpotMarketAccount =
this.driftClient.getSpotMarketAccount(QUOTE_SPOT_MARKET_INDEX);

const fuelBonusNumeratorUserStats = BN.max(
now.sub(
BN.max(new BN(userStats.lastFuelIfBonusUpdateTs), FUEL_START_TS)
),
ZERO
);

insuranceFuel = insuranceFuel.add(
calculateInsuranceFuelBonus(
spotMarketAccount,
userStats.ifStakedQuoteAssetAmount,
fuelBonusNumeratorUserStats
)
);
}
}

return insuranceFuel;
}

public getReferrerInfo(): ReferrerInfo | undefined {
if (this.getAccount().referrer.equals(PublicKey.default)) {
return undefined;
Expand Down

0 comments on commit d22aceb

Please sign in to comment.