Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

program: add-spot-insurance-activity-tracking #1127

Merged
merged 14 commits into from
Jul 15, 2024
Merged
144 changes: 91 additions & 53 deletions programs/drift/src/controller/insurance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use crate::error::ErrorCode;
use crate::math::amm::calculate_net_user_pnl;
use crate::math::casting::Cast;
use crate::math::constants::{
MAX_APR_PER_REVENUE_SETTLE_TO_INSURANCE_FUND_VAULT, ONE_YEAR, PERCENTAGE_PRECISION,
FUEL_WINDOW_U128, MAX_APR_PER_REVENUE_SETTLE_TO_INSURANCE_FUND_VAULT, ONE_YEAR,
PERCENTAGE_PRECISION, QUOTE_PRECISION_U64,
SHARE_OF_REVENUE_ALLOCATED_TO_INSURANCE_FUND_VAULT_DENOMINATOR,
SHARE_OF_REVENUE_ALLOCATED_TO_INSURANCE_FUND_VAULT_NUMERATOR,
};
Expand All @@ -35,6 +36,63 @@ use crate::{emit, validate, GOV_SPOT_MARKET_INDEX, QUOTE_SPOT_MARKET_INDEX};
#[cfg(test)]
mod tests;

pub fn update_user_stats_if_stake_amount(
0xbigz marked this conversation as resolved.
Show resolved Hide resolved
amount: i64,
insurance_vault_amount: u64,
insurance_fund_stake: &mut InsuranceFundStake,
user_stats: &mut UserStats,
spot_market: &mut SpotMarket,
now: i64,
) -> DriftResult {
if spot_market.market_index == QUOTE_SPOT_MARKET_INDEX
0xbigz marked this conversation as resolved.
Show resolved Hide resolved
|| spot_market.market_index == GOV_SPOT_MARKET_INDEX
|| spot_market.fuel_boost_insurance != 0
{
let if_stake_amount = if amount >= 0 {
if_shares_to_vault_amount(
insurance_fund_stake.checked_if_shares(spot_market)?,
spot_market.insurance_fund.total_shares,
insurance_vault_amount.safe_add(amount.unsigned_abs())?,
)?
} else {
if_shares_to_vault_amount(
insurance_fund_stake.checked_if_shares(spot_market)?,
spot_market.insurance_fund.total_shares,
insurance_vault_amount.safe_sub(amount.unsigned_abs())?,
)?
};

if spot_market.market_index == QUOTE_SPOT_MARKET_INDEX {
user_stats.if_staked_quote_asset_amount = if_stake_amount;
} else if spot_market.market_index == GOV_SPOT_MARKET_INDEX {
user_stats.if_staked_gov_token_amount = if_stake_amount;
}

if spot_market.fuel_boost_insurance != 0 {
let now_u32: u32 = now.cast()?;
let since_last = user_stats
.last_fuel_bonus_update_ts
.max(now_u32)
.safe_sub(now_u32)?;

let fuel_bonus_insurance = if_stake_amount
.cast::<u128>()?
.safe_mul(since_last.cast()?)?
.safe_mul(spot_market.fuel_boost_deposits.cast()?)?
0xbigz marked this conversation as resolved.
Show resolved Hide resolved
.safe_div(FUEL_WINDOW_U128)?
.cast::<u64>()?
/ (QUOTE_PRECISION_U64 / 10);

user_stats.fuel_insurance = user_stats
.fuel_insurance
.saturating_add(fuel_bonus_insurance.cast()?);
user_stats.last_fuel_bonus_update_ts = now_u32;
}
}

Ok(())
}

pub fn add_insurance_fund_stake(
amount: u64,
insurance_vault_amount: u64,
Expand Down Expand Up @@ -77,19 +135,14 @@ pub fn add_insurance_fund_stake(
spot_market.insurance_fund.user_shares =
spot_market.insurance_fund.user_shares.safe_add(n_shares)?;

if spot_market.market_index == QUOTE_SPOT_MARKET_INDEX {
user_stats.if_staked_quote_asset_amount = if_shares_to_vault_amount(
insurance_fund_stake.checked_if_shares(spot_market)?,
spot_market.insurance_fund.total_shares,
insurance_vault_amount.safe_add(amount)?,
)?;
} else if spot_market.market_index == GOV_SPOT_MARKET_INDEX {
user_stats.if_staked_gov_token_amount = if_shares_to_vault_amount(
insurance_fund_stake.checked_if_shares(spot_market)?,
spot_market.insurance_fund.total_shares,
insurance_vault_amount.safe_add(amount)?,
)?;
}
update_user_stats_if_stake_amount(
amount.cast()?,
insurance_vault_amount,
insurance_fund_stake,
user_stats,
spot_market,
now,
)?;

let if_shares_after = insurance_fund_stake.checked_if_shares(spot_market)?;

Expand Down Expand Up @@ -237,19 +290,14 @@ pub fn request_remove_insurance_fund_stake(

let if_shares_after = insurance_fund_stake.checked_if_shares(spot_market)?;

if spot_market.market_index == QUOTE_SPOT_MARKET_INDEX {
user_stats.if_staked_quote_asset_amount = if_shares_to_vault_amount(
insurance_fund_stake.checked_if_shares(spot_market)?,
spot_market.insurance_fund.total_shares,
insurance_vault_amount,
)?;
} else if spot_market.market_index == GOV_SPOT_MARKET_INDEX {
user_stats.if_staked_gov_token_amount = if_shares_to_vault_amount(
insurance_fund_stake.checked_if_shares(spot_market)?,
spot_market.insurance_fund.total_shares,
insurance_vault_amount,
)?;
}
update_user_stats_if_stake_amount(
0,
insurance_vault_amount,
insurance_fund_stake,
user_stats,
spot_market,
now,
)?;

emit!(InsuranceFundStakeRecord {
ts: now,
Expand Down Expand Up @@ -314,19 +362,14 @@ pub fn cancel_request_remove_insurance_fund_stake(

let if_shares_after = insurance_fund_stake.checked_if_shares(spot_market)?;

if spot_market.market_index == 0 {
user_stats.if_staked_quote_asset_amount = if_shares_to_vault_amount(
if_shares_after,
spot_market.insurance_fund.total_shares,
insurance_vault_amount,
)?;
} else if spot_market.market_index == GOV_SPOT_MARKET_INDEX {
user_stats.if_staked_gov_token_amount = if_shares_to_vault_amount(
insurance_fund_stake.checked_if_shares(spot_market)?,
spot_market.insurance_fund.total_shares,
insurance_vault_amount,
)?;
}
update_user_stats_if_stake_amount(
0,
insurance_vault_amount,
insurance_fund_stake,
user_stats,
spot_market,
now,
)?;

emit!(InsuranceFundStakeRecord {
ts: now,
Expand Down Expand Up @@ -415,19 +458,14 @@ pub fn remove_insurance_fund_stake(

let if_shares_after = insurance_fund_stake.checked_if_shares(spot_market)?;

if spot_market.market_index == QUOTE_SPOT_MARKET_INDEX {
user_stats.if_staked_quote_asset_amount = if_shares_to_vault_amount(
if_shares_after,
spot_market.insurance_fund.total_shares,
insurance_vault_amount.safe_sub(amount)?,
)?;
} else if spot_market.market_index == GOV_SPOT_MARKET_INDEX {
user_stats.if_staked_gov_token_amount = if_shares_to_vault_amount(
if_shares_after,
spot_market.insurance_fund.total_shares,
insurance_vault_amount.safe_sub(amount)?,
)?;
}
update_user_stats_if_stake_amount(
-(amount.cast()?),
insurance_vault_amount,
insurance_fund_stake,
user_stats,
spot_market,
now,
)?;

emit!(InsuranceFundStakeRecord {
ts: now,
Expand Down
15 changes: 14 additions & 1 deletion programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ pub fn handle_initialize_spot_market(
fuel_boost_borrows: 0,
fuel_boost_taker: 0,
fuel_boost_maker: 0,
padding: [0; 43],
fuel_boost_insurance: 0,
padding: [0; 42],
insurance_fund: InsuranceFund {
vault: *ctx.accounts.insurance_fund_vault.to_account_info().key,
unstaking_period: THIRTEEN_DAY,
Expand Down Expand Up @@ -3283,6 +3284,7 @@ pub fn handle_update_spot_market_fuel(
fuel_boost_borrows: Option<u8>,
fuel_boost_taker: Option<u8>,
fuel_boost_maker: Option<u8>,
fuel_boost_insurance: Option<u8>,
) -> Result<()> {
let spot_market = &mut load_mut!(ctx.accounts.spot_market)?;

Expand Down Expand Up @@ -3330,6 +3332,17 @@ pub fn handle_update_spot_market_fuel(
msg!("perp_market.fuel_boost_borrows: unchanged");
}

if let Some(fuel_boost_insurance) = fuel_boost_insurance {
msg!(
"perp_market.fuel_boost_insurance: {:?} -> {:?}",
spot_market.fuel_boost_insurance,
fuel_boost_insurance
);
spot_market.fuel_boost_insurance = fuel_boost_insurance;
} else {
msg!("perp_market.fuel_boost_insurance: unchanged");
}

Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions programs/drift/src/instructions/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ pub fn handle_initialize_user<'c: 'info, 'info>(
let now_ts = Clock::get()?.unix_timestamp;

user.last_fuel_bonus_update_ts = now_ts;
user_stats.last_fuel_bonus_update_ts = now_ts.cast()?;
0xbigz marked this conversation as resolved.
Show resolved Hide resolved

emit!(NewUserRecord {
ts: now_ts,
Expand Down
2 changes: 2 additions & 0 deletions programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,13 +1264,15 @@ pub mod drift {
fuel_boost_borrows: Option<u8>,
fuel_boost_taker: Option<u8>,
fuel_boost_maker: Option<u8>,
fuel_boost_insurance: Option<u8>,
) -> Result<()> {
handle_update_spot_market_fuel(
ctx,
fuel_boost_deposits,
fuel_boost_borrows,
fuel_boost_taker,
fuel_boost_maker,
fuel_boost_insurance,
)
}

Expand Down
8 changes: 6 additions & 2 deletions programs/drift/src/state/spot_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ pub struct SpotMarket {
/// fuel multiplier for spot maker
/// precision: 10
pub fuel_boost_maker: u8,
pub padding: [u8; 43],
/// fuel multiplier for spot insurance stake
/// precision: 10
pub fuel_boost_insurance: u8,
pub padding: [u8; 42],
}

impl Default for SpotMarket {
Expand Down Expand Up @@ -263,7 +266,8 @@ impl Default for SpotMarket {
fuel_boost_borrows: 0,
fuel_boost_taker: 0,
fuel_boost_maker: 0,
padding: [0; 43],
fuel_boost_insurance: 0,
padding: [0; 42],
}
}
}
Expand Down
23 changes: 15 additions & 8 deletions programs/drift/src/state/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1541,22 +1541,27 @@ pub struct UserStats {
/// Whether the user is a referrer. Sub account 0 can not be deleted if user is a referrer
pub is_referrer: bool,
pub disable_update_perp_bid_ask_twap: bool,
pub padding1: [u8; 6],
/// sub account id for spot deposit, borrow fuel tracking
pub padding1: [u8; 2],
/// accumulated fuel for token amounts of insurance
pub fuel_insurance: u32,
/// accumulated fuel for notional of deposits
pub fuel_deposits: u32,
/// accumulate fuel bonus for epoch
/// accumulate fuel bonus for notional of borrows
pub fuel_borrows: u32,
/// accumulated fuel for perp open interest
pub fuel_positions: u32,
/// accumulate fuel bonus for epoch
/// accumulate fuel bonus for taker volume
pub fuel_taker: u32,
/// accumulate fuel bonus for epoch
/// accumulate fuel bonus for maker volume
pub fuel_maker: u32,

/// The amount of tokens staked in the governance spot markets if
pub if_staked_gov_token_amount: u64,

pub padding: [u8; 16],
/// last unix ts user stats data was used to update fuel (u32 to save space)
pub last_fuel_bonus_update_ts: u32,
0xbigz marked this conversation as resolved.
Show resolved Hide resolved

pub padding: [u8; 12],
}

impl Default for UserStats {
Expand All @@ -1577,14 +1582,16 @@ impl Default for UserStats {
number_of_sub_accounts_created: 0,
is_referrer: false,
disable_update_perp_bid_ask_twap: false,
padding1: [0; 6],
padding1: [0; 2],
fuel_insurance: 0,
fuel_deposits: 0,
fuel_borrows: 0,
fuel_taker: 0,
fuel_maker: 0,
fuel_positions: 0,
if_staked_gov_token_amount: 0,
padding: [0; 16],
last_fuel_bonus_update_ts: 0,
padding: [0; 12],
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions sdk/src/adminClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3538,14 +3538,16 @@ export class AdminClient extends DriftClient {
fuelBoostDeposits?: number,
fuelBoostBorrows?: number,
fuelBoostTaker?: number,
fuelBoostMaker?: number
fuelBoostMaker?: number,
fuelBoostInsurance?: number
): Promise<TransactionSignature> {
const updateSpotMarketFuelIx = await this.getUpdateSpotMarketFuelIx(
spotMarketIndex,
fuelBoostDeposits || null,
fuelBoostBorrows || null,
fuelBoostTaker || null,
fuelBoostMaker || null
fuelBoostMaker || null,
fuelBoostInsurance || null
);

const tx = await this.buildTransaction(updateSpotMarketFuelIx);
Expand All @@ -3559,7 +3561,8 @@ export class AdminClient extends DriftClient {
fuelBoostDeposits?: number,
fuelBoostBorrows?: number,
fuelBoostTaker?: number,
fuelBoostMaker?: number
fuelBoostMaker?: number,
fuelBoostInsurance?: number
): Promise<TransactionInstruction> {
const spotMarketPublicKey = await getSpotMarketPublicKey(
this.program.programId,
Expand All @@ -3571,6 +3574,7 @@ export class AdminClient extends DriftClient {
fuelBoostBorrows || null,
fuelBoostTaker || null,
fuelBoostMaker || null,
fuelBoostInsurance || null,
{
accounts: {
admin: this.isSubscribed
Expand Down
1 change: 1 addition & 0 deletions sdk/src/constants/numericConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const ONE_HOUR = new BN(60 * 60);
export const ONE_YEAR = new BN(31536000);

export const QUOTE_SPOT_MARKET_INDEX = 0;
export const GOV_SPOT_MARKET_INDEX = 15;

export const LAMPORTS_PRECISION = new BN(LAMPORTS_PER_SOL);
export const LAMPORTS_EXP = new BN(Math.log10(LAMPORTS_PER_SOL));
Expand Down
Loading
Loading