Skip to content

Commit 6cce7e5

Browse files
0xbigzcrispheaney
andauthored
program: init-delegated-if-stake (#1859)
* program: init-delegated-if-stake * add sdk * CHANGELOG --------- Co-authored-by: Chris Heaney <[email protected]>
1 parent e91800e commit 6cce7e5

File tree

4 files changed

+123
-6
lines changed

4 files changed

+123
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Features
1111

12+
- program: add delegate stake if ([#1859](https://github.com/drift-labs/protocol-v2/pull/1859))
13+
1214
### Fixes
1315

1416
### Breaking

programs/drift/src/instructions/admin.rs

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ use crate::instructions::optional_accounts::{load_maps, AccountMaps};
1818
use crate::math::casting::Cast;
1919
use crate::math::constants::{
2020
AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO, DEFAULT_LIQUIDATION_MARGIN_BUFFER_RATIO,
21-
FEE_POOL_TO_REVENUE_POOL_THRESHOLD, IF_FACTOR_PRECISION, INSURANCE_A_MAX, INSURANCE_B_MAX,
22-
INSURANCE_C_MAX, INSURANCE_SPECULATIVE_MAX, LIQUIDATION_FEE_PRECISION,
23-
MAX_CONCENTRATION_COEFFICIENT, MAX_SQRT_K, MAX_UPDATE_K_PRICE_CHANGE, PERCENTAGE_PRECISION,
24-
PERCENTAGE_PRECISION_I64, QUOTE_SPOT_MARKET_INDEX, SPOT_CUMULATIVE_INTEREST_PRECISION,
25-
SPOT_IMF_PRECISION, SPOT_WEIGHT_PRECISION, THIRTEEN_DAY, TWENTY_FOUR_HOUR,
21+
FEE_POOL_TO_REVENUE_POOL_THRESHOLD, GOV_SPOT_MARKET_INDEX, IF_FACTOR_PRECISION,
22+
INSURANCE_A_MAX, INSURANCE_B_MAX, INSURANCE_C_MAX, INSURANCE_SPECULATIVE_MAX,
23+
LIQUIDATION_FEE_PRECISION, MAX_CONCENTRATION_COEFFICIENT, MAX_SQRT_K,
24+
MAX_UPDATE_K_PRICE_CHANGE, PERCENTAGE_PRECISION, PERCENTAGE_PRECISION_I64,
25+
QUOTE_SPOT_MARKET_INDEX, SPOT_CUMULATIVE_INTEREST_PRECISION, SPOT_IMF_PRECISION,
26+
SPOT_WEIGHT_PRECISION, THIRTEEN_DAY, TWENTY_FOUR_HOUR,
2627
};
2728
use crate::math::cp_curve::get_update_k_result;
2829
use crate::math::helpers::get_proportion_u128;
@@ -45,6 +46,7 @@ use crate::state::fulfillment_params::serum::SerumContext;
4546
use crate::state::fulfillment_params::serum::SerumV3FulfillmentConfig;
4647
use crate::state::high_leverage_mode_config::HighLeverageModeConfig;
4748
use crate::state::if_rebalance_config::{IfRebalanceConfig, IfRebalanceConfigParams};
49+
use crate::state::insurance_fund_stake::InsuranceFundStake;
4850
use crate::state::insurance_fund_stake::ProtocolIfSharesTransferConfig;
4951
use crate::state::oracle::get_sb_on_demand_price;
5052
use crate::state::oracle::{
@@ -4915,6 +4917,39 @@ pub fn handle_update_feature_bit_flags_median_trigger_price(
49154917
Ok(())
49164918
}
49174919

4920+
pub fn handle_update_delegate_user_gov_token_insurance_stake(
4921+
ctx: Context<UpdateDelegateUserGovTokenInsuranceStake>,
4922+
) -> Result<()> {
4923+
let insurance_fund_stake = &mut load_mut!(ctx.accounts.insurance_fund_stake)?;
4924+
let user_stats = &mut load_mut!(ctx.accounts.user_stats)?;
4925+
let spot_market = &mut load_mut!(ctx.accounts.spot_market)?;
4926+
4927+
validate!(
4928+
insurance_fund_stake.market_index == GOV_SPOT_MARKET_INDEX,
4929+
ErrorCode::IncorrectSpotMarketAccountPassed,
4930+
"insurance_fund_stake is not for governance market index = {}",
4931+
GOV_SPOT_MARKET_INDEX
4932+
)?;
4933+
4934+
if insurance_fund_stake.market_index == GOV_SPOT_MARKET_INDEX
4935+
&& spot_market.market_index == GOV_SPOT_MARKET_INDEX
4936+
{
4937+
let clock = Clock::get()?;
4938+
let now = clock.unix_timestamp;
4939+
4940+
crate::controller::insurance::update_user_stats_if_stake_amount(
4941+
0,
4942+
ctx.accounts.insurance_fund_vault.amount,
4943+
insurance_fund_stake,
4944+
user_stats,
4945+
spot_market,
4946+
now,
4947+
)?;
4948+
}
4949+
4950+
Ok(())
4951+
}
4952+
49184953
#[derive(Accounts)]
49194954
pub struct Initialize<'info> {
49204955
#[account(mut)]
@@ -5759,3 +5794,27 @@ pub struct UpdateIfRebalanceConfig<'info> {
57595794
)]
57605795
pub state: Box<Account<'info, State>>,
57615796
}
5797+
5798+
#[derive(Accounts)]
5799+
pub struct UpdateDelegateUserGovTokenInsuranceStake<'info> {
5800+
#[account(
5801+
mut,
5802+
seeds = [b"spot_market", 15_u16.to_le_bytes().as_ref()],
5803+
bump
5804+
)]
5805+
pub spot_market: AccountLoader<'info, SpotMarket>,
5806+
pub insurance_fund_stake: AccountLoader<'info, InsuranceFundStake>,
5807+
#[account(mut)]
5808+
pub user_stats: AccountLoader<'info, UserStats>,
5809+
pub admin: Signer<'info>,
5810+
#[account(
5811+
mut,
5812+
seeds = [b"insurance_fund_vault".as_ref(), 15_u16.to_le_bytes().as_ref()],
5813+
bump,
5814+
)]
5815+
pub insurance_fund_vault: Box<InterfaceAccount<'info, TokenAccount>>,
5816+
#[account(
5817+
has_one = admin
5818+
)]
5819+
pub state: Box<Account<'info, State>>,
5820+
}

programs/drift/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ pub mod drift {
725725
handle_update_spot_market_expiry(ctx, expiry_ts)
726726
}
727727

728+
// IF stakers
728729
pub fn update_user_quote_asset_insurance_stake(
729730
ctx: Context<UpdateUserQuoteAssetInsuranceStake>,
730731
) -> Result<()> {
@@ -737,7 +738,11 @@ pub mod drift {
737738
handle_update_user_gov_token_insurance_stake(ctx)
738739
}
739740

740-
// IF stakers
741+
pub fn update_delegate_user_gov_token_insurance_stake(
742+
ctx: Context<UpdateDelegateUserGovTokenInsuranceStake>,
743+
) -> Result<()> {
744+
handle_update_delegate_user_gov_token_insurance_stake(ctx)
745+
}
741746

742747
pub fn initialize_insurance_fund_stake(
743748
ctx: Context<InitializeInsuranceFundStake>,

sdk/src/adminClient.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
getFuelOverflowAccountPublicKey,
4040
getTokenProgramForSpotMarket,
4141
getIfRebalanceConfigPublicKey,
42+
getInsuranceFundStakeAccountPublicKey,
4243
} from './addresses/pda';
4344
import { squareRootBN } from './math/utils';
4445
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
@@ -50,6 +51,7 @@ import {
5051
ONE,
5152
BASE_PRECISION,
5253
PRICE_PRECISION,
54+
GOV_SPOT_MARKET_INDEX,
5355
} from './constants/numericConstants';
5456
import { calculateTargetPriceTrade } from './math/trade';
5557
import { calculateAmmReservesAfterSwap, getSwapDirection } from './math/amm';
@@ -4649,4 +4651,53 @@ export class AdminClient extends DriftClient {
46494651
}
46504652
);
46514653
}
4654+
4655+
public async updateDelegateUserGovTokenInsuranceStake(
4656+
authority: PublicKey,
4657+
delegate: PublicKey
4658+
): Promise<TransactionSignature> {
4659+
const updateDelegateUserGovTokenInsuranceStakeIx =
4660+
await this.getUpdateDelegateUserGovTokenInsuranceStakeIx(
4661+
authority,
4662+
delegate
4663+
);
4664+
4665+
const tx = await this.buildTransaction(
4666+
updateDelegateUserGovTokenInsuranceStakeIx
4667+
);
4668+
const { txSig } = await this.sendTransaction(tx, [], this.opts);
4669+
4670+
return txSig;
4671+
}
4672+
4673+
public async getUpdateDelegateUserGovTokenInsuranceStakeIx(
4674+
authority: PublicKey,
4675+
delegate: PublicKey
4676+
): Promise<TransactionInstruction> {
4677+
const marketIndex = GOV_SPOT_MARKET_INDEX;
4678+
const spotMarket = this.getSpotMarketAccount(marketIndex);
4679+
const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
4680+
this.program.programId,
4681+
delegate,
4682+
marketIndex
4683+
);
4684+
const userStatsPublicKey = getUserStatsAccountPublicKey(
4685+
this.program.programId,
4686+
authority
4687+
);
4688+
4689+
const ix =
4690+
this.program.instruction.getUpdateDelegateUserGovTokenInsuranceStakeIx({
4691+
accounts: {
4692+
state: await this.getStatePublicKey(),
4693+
spotMarket: spotMarket.pubkey,
4694+
insuranceFundStake: ifStakeAccountPublicKey,
4695+
userStats: userStatsPublicKey,
4696+
signer: this.wallet.publicKey,
4697+
insuranceFundVault: spotMarket.insuranceFund.vault,
4698+
},
4699+
});
4700+
4701+
return ix;
4702+
}
46524703
}

0 commit comments

Comments
 (0)