Skip to content

Commit

Permalink
Merge branch 'master' into crispheaney/modify-order-enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Dec 5, 2024
2 parents 99398ef + d3134a6 commit 9801f99
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 68 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixes

- program: fix force delete user for token 2022 ([#1358](https://github.com/drift-labs/protocol-v2/pull/1358))

### Breaking

## [2.102.0] - 2024-11-21
Expand Down
10 changes: 8 additions & 2 deletions programs/drift/src/instructions/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::cell::RefMut;
use std::convert::{TryFrom, TryInto};

use anchor_lang::prelude::*;
use anchor_spl::associated_token::get_associated_token_address;
use anchor_spl::associated_token::{
get_associated_token_address, get_associated_token_address_with_program_id,
};
use anchor_spl::token::spl_token;
use anchor_spl::token_2022::spl_token_2022;
use anchor_spl::token_interface::{TokenAccount, TokenInterface};
Expand Down Expand Up @@ -2245,7 +2247,11 @@ pub fn handle_force_delete_user<'c: 'info, 'info>(
.find(|acc| acc.key() == spot_market_mint.key())
.map(|acc| InterfaceAccount::try_from(acc).unwrap());

let keeper_vault = get_associated_token_address(&keeper_key, spot_market_mint);
let keeper_vault = get_associated_token_address_with_program_id(
&keeper_key,
spot_market_mint,
&token_program_pubkey,
);
let keeper_vault_account_info = ctx
.remaining_accounts
.iter()
Expand Down
2 changes: 1 addition & 1 deletion sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.103.0-beta.9
2.103.0-beta.10
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@drift-labs/sdk",
"version": "2.103.0-beta.9",
"version": "2.103.0-beta.10",
"main": "lib/node/index.js",
"types": "lib/node/index.d.ts",
"browser": "./lib/browser/index.js",
Expand Down
5 changes: 3 additions & 2 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1658,16 +1658,17 @@ export class DriftClient {
isWritable: true,
pubkey: spotMarket.vault,
});
const tokenProgram = this.getTokenProgramForSpotMarket(spotMarket);
const keeperVault = await this.getAssociatedTokenAccount(
spotPosition.marketIndex,
false
false,
tokenProgram
);
remainingAccounts.push({
isSigner: false,
isWritable: true,
pubkey: keeperVault,
});
const tokenProgram = this.getTokenProgramForSpotMarket(spotMarket);
tokenPrograms.add(tokenProgram.toBase58());
}

Expand Down
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 9801f99

Please sign in to comment.