Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- program: add delegate stake if ([#1859](https://github.com/drift-labs/protocol-v2/pull/1859))

### Fixes

### Breaking
Expand Down
69 changes: 64 additions & 5 deletions programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ use crate::instructions::optional_accounts::{load_maps, AccountMaps};
use crate::math::casting::Cast;
use crate::math::constants::{
AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO, DEFAULT_LIQUIDATION_MARGIN_BUFFER_RATIO,
FEE_POOL_TO_REVENUE_POOL_THRESHOLD, IF_FACTOR_PRECISION, INSURANCE_A_MAX, INSURANCE_B_MAX,
INSURANCE_C_MAX, INSURANCE_SPECULATIVE_MAX, LIQUIDATION_FEE_PRECISION,
MAX_CONCENTRATION_COEFFICIENT, MAX_SQRT_K, MAX_UPDATE_K_PRICE_CHANGE, PERCENTAGE_PRECISION,
PERCENTAGE_PRECISION_I64, QUOTE_SPOT_MARKET_INDEX, SPOT_CUMULATIVE_INTEREST_PRECISION,
SPOT_IMF_PRECISION, SPOT_WEIGHT_PRECISION, THIRTEEN_DAY, TWENTY_FOUR_HOUR,
FEE_POOL_TO_REVENUE_POOL_THRESHOLD, GOV_SPOT_MARKET_INDEX, IF_FACTOR_PRECISION,
INSURANCE_A_MAX, INSURANCE_B_MAX, INSURANCE_C_MAX, INSURANCE_SPECULATIVE_MAX,
LIQUIDATION_FEE_PRECISION, MAX_CONCENTRATION_COEFFICIENT, MAX_SQRT_K,
MAX_UPDATE_K_PRICE_CHANGE, PERCENTAGE_PRECISION, PERCENTAGE_PRECISION_I64,
QUOTE_SPOT_MARKET_INDEX, SPOT_CUMULATIVE_INTEREST_PRECISION, SPOT_IMF_PRECISION,
SPOT_WEIGHT_PRECISION, THIRTEEN_DAY, TWENTY_FOUR_HOUR,
};
use crate::math::cp_curve::get_update_k_result;
use crate::math::helpers::get_proportion_u128;
Expand All @@ -45,6 +46,7 @@ use crate::state::fulfillment_params::serum::SerumContext;
use crate::state::fulfillment_params::serum::SerumV3FulfillmentConfig;
use crate::state::high_leverage_mode_config::HighLeverageModeConfig;
use crate::state::if_rebalance_config::{IfRebalanceConfig, IfRebalanceConfigParams};
use crate::state::insurance_fund_stake::InsuranceFundStake;
use crate::state::insurance_fund_stake::ProtocolIfSharesTransferConfig;
use crate::state::oracle::get_sb_on_demand_price;
use crate::state::oracle::{
Expand Down Expand Up @@ -4915,6 +4917,39 @@ pub fn handle_update_feature_bit_flags_median_trigger_price(
Ok(())
}

pub fn handle_update_delegate_user_gov_token_insurance_stake(
ctx: Context<UpdateDelegateUserGovTokenInsuranceStake>,
) -> Result<()> {
let insurance_fund_stake = &mut load_mut!(ctx.accounts.insurance_fund_stake)?;
let user_stats = &mut load_mut!(ctx.accounts.user_stats)?;
let spot_market = &mut load_mut!(ctx.accounts.spot_market)?;

validate!(
insurance_fund_stake.market_index == GOV_SPOT_MARKET_INDEX,
ErrorCode::IncorrectSpotMarketAccountPassed,
"insurance_fund_stake is not for governance market index = {}",
GOV_SPOT_MARKET_INDEX
)?;

if insurance_fund_stake.market_index == GOV_SPOT_MARKET_INDEX
&& spot_market.market_index == GOV_SPOT_MARKET_INDEX
{
let clock = Clock::get()?;
let now = clock.unix_timestamp;

crate::controller::insurance::update_user_stats_if_stake_amount(
0,
ctx.accounts.insurance_fund_vault.amount,
insurance_fund_stake,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do you turn off the the delegated stake?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

permissionless crank of user vs their existing stake handle_update_user_gov_token_insurance_stake

user_stats,
spot_market,
now,
)?;
}

Ok(())
}

#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(mut)]
Expand Down Expand Up @@ -5759,3 +5794,27 @@ pub struct UpdateIfRebalanceConfig<'info> {
)]
pub state: Box<Account<'info, State>>,
}

#[derive(Accounts)]
pub struct UpdateDelegateUserGovTokenInsuranceStake<'info> {
#[account(
mut,
seeds = [b"spot_market", 15_u16.to_le_bytes().as_ref()],
bump
)]
pub spot_market: AccountLoader<'info, SpotMarket>,
pub insurance_fund_stake: AccountLoader<'info, InsuranceFundStake>,
#[account(mut)]
pub user_stats: AccountLoader<'info, UserStats>,
pub admin: Signer<'info>,
#[account(
mut,
seeds = [b"insurance_fund_vault".as_ref(), 15_u16.to_le_bytes().as_ref()],
bump,
)]
pub insurance_fund_vault: Box<InterfaceAccount<'info, TokenAccount>>,
#[account(
has_one = admin
)]
pub state: Box<Account<'info, State>>,
}
7 changes: 6 additions & 1 deletion programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ pub mod drift {
handle_update_spot_market_expiry(ctx, expiry_ts)
}

// IF stakers
pub fn update_user_quote_asset_insurance_stake(
ctx: Context<UpdateUserQuoteAssetInsuranceStake>,
) -> Result<()> {
Expand All @@ -737,7 +738,11 @@ pub mod drift {
handle_update_user_gov_token_insurance_stake(ctx)
}

// IF stakers
pub fn update_delegate_user_gov_token_insurance_stake(
ctx: Context<UpdateDelegateUserGovTokenInsuranceStake>,
) -> Result<()> {
handle_update_delegate_user_gov_token_insurance_stake(ctx)
}

pub fn initialize_insurance_fund_stake(
ctx: Context<InitializeInsuranceFundStake>,
Expand Down
51 changes: 51 additions & 0 deletions sdk/src/adminClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
getFuelOverflowAccountPublicKey,
getTokenProgramForSpotMarket,
getIfRebalanceConfigPublicKey,
getInsuranceFundStakeAccountPublicKey,
} from './addresses/pda';
import { squareRootBN } from './math/utils';
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
Expand All @@ -50,6 +51,7 @@ import {
ONE,
BASE_PRECISION,
PRICE_PRECISION,
GOV_SPOT_MARKET_INDEX,
} from './constants/numericConstants';
import { calculateTargetPriceTrade } from './math/trade';
import { calculateAmmReservesAfterSwap, getSwapDirection } from './math/amm';
Expand Down Expand Up @@ -4649,4 +4651,53 @@ export class AdminClient extends DriftClient {
}
);
}

public async updateDelegateUserGovTokenInsuranceStake(
authority: PublicKey,
delegate: PublicKey
): Promise<TransactionSignature> {
const updateDelegateUserGovTokenInsuranceStakeIx =
await this.getUpdateDelegateUserGovTokenInsuranceStakeIx(
authority,
delegate
);

const tx = await this.buildTransaction(
updateDelegateUserGovTokenInsuranceStakeIx
);
const { txSig } = await this.sendTransaction(tx, [], this.opts);

return txSig;
}

public async getUpdateDelegateUserGovTokenInsuranceStakeIx(
authority: PublicKey,
delegate: PublicKey
): Promise<TransactionInstruction> {
const marketIndex = GOV_SPOT_MARKET_INDEX;
const spotMarket = this.getSpotMarketAccount(marketIndex);
const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
this.program.programId,
delegate,
marketIndex
);
const userStatsPublicKey = getUserStatsAccountPublicKey(
this.program.programId,
authority
);

const ix =
this.program.instruction.getUpdateDelegateUserGovTokenInsuranceStakeIx({
accounts: {
state: await this.getStatePublicKey(),
spotMarket: spotMarket.pubkey,
insuranceFundStake: ifStakeAccountPublicKey,
userStats: userStatsPublicKey,
signer: this.wallet.publicKey,
insuranceFundVault: spotMarket.insuranceFund.vault,
},
});

return ix;
}
}
Loading