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

bigz/state-max-number-of-subaccounts #710

Merged
merged 5 commits into from
Nov 22, 2023
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
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 @@
lp_cooldown_time: 0,
liquidation_duration: 0,
initial_pct_to_liquidate: 0,
padding: [0; 14],
max_number_of_sub_accounts: 0,
padding: [0; 12],

Check warning on line 87 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L87

Added line #L87 was not covered by tests
};

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

pub fn handle_update_state_max_number_of_sub_accounts(

Check warning on line 1965 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L1965

Added line #L1965 was not covered by tests
ctx: Context<AdminUpdateState>,
max_number_of_sub_accounts: u16,
) -> Result<()> {
ctx.accounts.state.max_number_of_sub_accounts = max_number_of_sub_accounts;
Ok(())

Check warning on line 1970 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L1969-L1970

Added lines #L1969 - L1970 were not covered by tests
}

#[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 @@
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(),

Check warning on line 141 in programs/drift/src/instructions/user.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/user.rs#L141

Added line #L141 was not covered by tests
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 @@
handle_update_state_settlement_duration(ctx, settlement_duration)
}

pub fn update_state_max_number_of_sub_accounts(

Check warning on line 957 in programs/drift/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/lib.rs#L957

Added line #L957 was not covered by tests
ctx: Context<AdminUpdateState>,
max_number_of_sub_accounts: u16,
) -> Result<()> {
handle_update_state_max_number_of_sub_accounts(ctx, max_number_of_sub_accounts)

Check warning on line 961 in programs/drift/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/lib.rs#L961

Added line #L961 was not covered by tests
}

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 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 @@
.get_exchange_status()?
.contains(ExchangeStatus::FundingPaused))
}

pub fn max_number_of_sub_accounts(&self) -> u64 {
(self.max_number_of_sub_accounts as u64)

Check warning on line 79 in programs/drift/src/state/state.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/state.rs#L78-L79

Added lines #L78 - L79 were not covered by tests
.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
Loading