From d22aceb423a28b06fe1e08eda6041155907363f1 Mon Sep 17 00:00:00 2001 From: Evan Pipta <3pipta@gmail.com> Date: Wed, 4 Dec 2024 14:56:50 -0500 Subject: [PATCH 1/3] sdk; move getInsuranceFuelBonnus to userStats (#1352) * sdk; move getInsuranceFuelBonnus to userStats * sdk: fix imports --- sdk/src/user.ts | 81 +++++++++++--------------------------------- sdk/src/userStats.ts | 69 ++++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 62 deletions(-) diff --git a/sdk/src/user.ts b/sdk/src/user.ts index 4c60b0530..2ecae8b8d 100644 --- a/sdk/src/user.ts +++ b/sdk/src/user.ts @@ -36,7 +36,6 @@ import { QUOTE_PRECISION_EXP, QUOTE_SPOT_MARKET_INDEX, SPOT_MARKET_WEIGHT_PRECISION, - GOV_SPOT_MARKET_INDEX, TEN, TEN_THOUSAND, TWO, @@ -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 { @@ -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) ); } @@ -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; } diff --git a/sdk/src/userStats.ts b/sdk/src/userStats.ts index 1a343d605..8fdb30d2b 100644 --- a/sdk/src/userStats.ts +++ b/sdk/src/userStats.ts @@ -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; @@ -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; From f48937886572d607a4897414f18237083c70bbb6 Mon Sep 17 00:00:00 2001 From: lil perp Date: Wed, 4 Dec 2024 19:24:22 -0500 Subject: [PATCH 2/3] program: fix force delete user for token 22 (#1358) * program: fix force delete user for token 22 * cargo fmt -- * CHANGELOG --- CHANGELOG.md | 2 ++ programs/drift/src/instructions/keeper.rs | 10 ++++++++-- programs/drift/src/math/spot_balance.rs | 1 - programs/drift/src/math/spot_balance/tests.rs | 6 ++++-- sdk/src/driftClient.ts | 5 +++-- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44b0cb3ef..d456a1f41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/programs/drift/src/instructions/keeper.rs b/programs/drift/src/instructions/keeper.rs index b79d8b6ed..64c5fe381 100644 --- a/programs/drift/src/instructions/keeper.rs +++ b/programs/drift/src/instructions/keeper.rs @@ -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}; @@ -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() diff --git a/programs/drift/src/math/spot_balance.rs b/programs/drift/src/math/spot_balance.rs index 5005b2a14..5af43ef48 100644 --- a/programs/drift/src/math/spot_balance.rs +++ b/programs/drift/src/math/spot_balance.rs @@ -8,7 +8,6 @@ use crate::state::oracle::{OraclePriceData, StrictOraclePrice}; use crate::state::spot_market::{SpotBalanceType, SpotMarket}; use crate::state::user::SpotPosition; - #[cfg(test)] mod tests; diff --git a/programs/drift/src/math/spot_balance/tests.rs b/programs/drift/src/math/spot_balance/tests.rs index 725313bc0..5fbe7427a 100644 --- a/programs/drift/src/math/spot_balance/tests.rs +++ b/programs/drift/src/math/spot_balance/tests.rs @@ -14,9 +14,11 @@ mod test { let one_bonk = 10_u128.pow(spot_market.decimals); - let balance = get_spot_balance(one_bonk, &spot_market, &SpotBalanceType::Deposit, false).unwrap(); + let balance = + get_spot_balance(one_bonk, &spot_market, &SpotBalanceType::Deposit, false).unwrap(); - let token_amount = get_token_amount(balance, &spot_market, &SpotBalanceType::Deposit).unwrap(); + let token_amount = + get_token_amount(balance, &spot_market, &SpotBalanceType::Deposit).unwrap(); assert_eq!(token_amount, one_bonk); } } diff --git a/sdk/src/driftClient.ts b/sdk/src/driftClient.ts index fb1305992..69a7519fd 100644 --- a/sdk/src/driftClient.ts +++ b/sdk/src/driftClient.ts @@ -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()); } From d3134a6b0fe235a2f29c3fbf6d7e34b1f0f9335c Mon Sep 17 00:00:00 2001 From: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 5 Dec 2024 00:26:44 +0000 Subject: [PATCH 3/3] sdk: release v2.103.0-beta.10 --- sdk/VERSION | 2 +- sdk/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/VERSION b/sdk/VERSION index 853eeb9d5..62fa59710 100644 --- a/sdk/VERSION +++ b/sdk/VERSION @@ -1 +1 @@ -2.103.0-beta.9 \ No newline at end of file +2.103.0-beta.10 \ No newline at end of file diff --git a/sdk/package.json b/sdk/package.json index dbbf1e48e..ad5d62fe8 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -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",