Skip to content

Commit

Permalink
program: make max sub accounts a state param (#710)
Browse files Browse the repository at this point in the history
* bigz/state-max-number-of-subaccounts

* update CHANGELOG.md

* use u32 for max_number_of_sub_accounts

* use u16

* idl

---------

Co-authored-by: Chris Heaney <[email protected]>
  • Loading branch information
0xbigz and crispheaney committed Nov 22, 2023
1 parent dd5607f commit 033defe
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixes
- sdk: fix vamm L2asks by using askAmm ([#708](https://github.com/drift-labs/protocol-v2/pull/708))
- program: add max_number_of_sub_accounts onto state account ([#710](https://github.com/drift-labs/protocol-v2/pull/710))

### Breaking

Expand Down
11 changes: 10 additions & 1 deletion programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ pub fn handle_initialize(ctx: Context<Initialize>) -> Result<()> {
lp_cooldown_time: 0,
liquidation_duration: 0,
initial_pct_to_liquidate: 0,
padding: [0; 14],
max_number_of_sub_accounts: 0,
padding: [0; 12],
};

Ok(())
Expand Down Expand Up @@ -1961,6 +1962,14 @@ pub fn handle_update_state_settlement_duration(
Ok(())
}

pub fn handle_update_state_max_number_of_sub_accounts(
ctx: Context<AdminUpdateState>,
max_number_of_sub_accounts: u16,
) -> Result<()> {
ctx.accounts.state.max_number_of_sub_accounts = max_number_of_sub_accounts;
Ok(())
}

#[access_control(
perp_market_valid(&ctx.accounts.perp_market)
)]
Expand Down
2 changes: 1 addition & 1 deletion programs/drift/src/instructions/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub fn handle_initialize_user(
safe_increment!(state.number_of_sub_accounts, 1);

validate!(
state.number_of_sub_accounts <= 20000,
state.number_of_sub_accounts <= state.max_number_of_sub_accounts(),
ErrorCode::MaxNumberOfUsers
)?;

Expand Down
7 changes: 7 additions & 0 deletions programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,13 @@ pub mod drift {
handle_update_state_settlement_duration(ctx, settlement_duration)
}

pub fn update_state_max_number_of_sub_accounts(
ctx: Context<AdminUpdateState>,
max_number_of_sub_accounts: u16,
) -> Result<()> {
handle_update_state_max_number_of_sub_accounts(ctx, max_number_of_sub_accounts)
}

pub fn update_perp_market_oracle(
ctx: Context<RepegCurve>,
oracle: Pubkey,
Expand Down
9 changes: 8 additions & 1 deletion programs/drift/src/state/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ pub struct State {
pub exchange_status: u8,
pub liquidation_duration: u8,
pub initial_pct_to_liquidate: u16,
pub padding: [u8; 14],
pub max_number_of_sub_accounts: u16,
pub padding: [u8; 12],
}

#[derive(BitFlags, Clone, Copy, PartialEq, Debug, Eq)]
Expand Down Expand Up @@ -73,6 +74,12 @@ impl State {
.get_exchange_status()?
.contains(ExchangeStatus::FundingPaused))
}

pub fn max_number_of_sub_accounts(&self) -> u64 {
(self.max_number_of_sub_accounts as u64)
.saturating_mul(100)
.max(25000)
}
}

impl Size for State {
Expand Down
14 changes: 14 additions & 0 deletions sdk/src/adminClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,20 @@ export class AdminClient extends DriftClient {
);
}

public async updateStateMaxNumberOfSubAccounts(
maxNumberOfSubAccounts: number
): Promise<TransactionSignature> {
return await this.program.rpc.updateStateMaxNumberOfSubAccounts(
maxNumberOfSubAccounts,
{
accounts: {
admin: this.wallet.publicKey,
state: await this.getStatePublicKey(),
},
}
);
}

public async updateWithdrawGuardThreshold(
spotMarketIndex: number,
withdrawGuardThreshold: BN
Expand Down
27 changes: 26 additions & 1 deletion sdk/src/idl/drift.json
Original file line number Diff line number Diff line change
Expand Up @@ -4163,6 +4163,27 @@
}
]
},
{
"name": "updateStateMaxNumberOfSubAccounts",
"accounts": [
{
"name": "admin",
"isMut": false,
"isSigner": true
},
{
"name": "state",
"isMut": true,
"isSigner": false
}
],
"args": [
{
"name": "maxNumberOfSubAccounts",
"type": "u16"
}
]
},
{
"name": "updatePerpMarketOracle",
"accounts": [
Expand Down Expand Up @@ -5722,12 +5743,16 @@
"name": "initialPctToLiquidate",
"type": "u16"
},
{
"name": "maxNumberOfSubAccounts",
"type": "u16"
},
{
"name": "padding",
"type": {
"array": [
"u8",
14
12
]
}
}
Expand Down
1 change: 1 addition & 0 deletions sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ export type StateAccount = {
defaultSpotAuctionDuration: number;
liquidationMarginBufferRatio: number;
settlementDuration: number;
maxNumberOfSubAccounts: number;
signer: PublicKey;
signerNonce: number;
srmVault: PublicKey;
Expand Down
1 change: 1 addition & 0 deletions sdk/tests/dlob/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ export const mockStateAccount: StateAccount = {
},
srmVault: PublicKey.default,
whitelistMint: PublicKey.default,
maxNumberOfSubAccounts: 0,
};

export class MockUserMap implements UserMapInterface {
Expand Down

0 comments on commit 033defe

Please sign in to comment.