Skip to content

Commit

Permalink
Send it
Browse files Browse the repository at this point in the history
  • Loading branch information
guibescos committed Nov 9, 2023
1 parent deb1121 commit 2fa4a11
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 237 deletions.
10 changes: 0 additions & 10 deletions staking/programs/staking/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,6 @@ pub struct UpdatePdaAuthority<'info> {
pub config: Account<'info, global_config::GlobalConfig>,
}


#[derive(Accounts)]
#[instruction(freeze : bool)]
pub struct UpdateFreeze<'info> {
#[account(address = config.governance_authority)]
pub governance_signer: Signer<'info>,
#[account(mut, seeds = [CONFIG_SEED.as_bytes()], bump = config.bump)]
pub config: Account<'info, global_config::GlobalConfig>,
}

#[derive(Accounts)]
#[instruction(token_list_time : Option<i64>)]
pub struct UpdateTokenListTime<'info> {
Expand Down
12 changes: 2 additions & 10 deletions staking/programs/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,6 @@ pub mod staking {
Ok(())
}

pub fn update_freeze(ctx: Context<UpdateFreeze>, freeze: bool) -> Result<()> {
let config = &mut ctx.accounts.config;
config.freeze = freeze;
Ok(())
}

pub fn update_token_list_time(
ctx: Context<UpdateTokenListTime>,
token_list_time: Option<i64>,
Expand All @@ -121,7 +115,7 @@ pub mod staking {
lock: VestingSchedule,
) -> Result<()> {
let config = &ctx.accounts.config;
config.check_frozen()?;


let stake_account_metadata = &mut ctx.accounts.stake_account_metadata;
stake_account_metadata.metadata_bump = *ctx.bumps.get("stake_account_metadata").unwrap();
Expand Down Expand Up @@ -167,7 +161,7 @@ pub mod staking {
let current_epoch = get_current_epoch(config)?;
let target_account = &mut ctx.accounts.target_account;

config.check_frozen()?;

ctx.accounts
.stake_account_metadata
.check_is_llc_member(&config.agreement_hash)?;
Expand Down Expand Up @@ -223,7 +217,6 @@ pub mod staking {
let config = &ctx.accounts.config;
let current_epoch = get_current_epoch(config)?;

config.check_frozen()?;

let mut current_position: Position = stake_account_positions
.read_position(i)?
Expand Down Expand Up @@ -334,7 +327,6 @@ pub mod staking {
let config = &ctx.accounts.config;
let current_epoch = get_current_epoch(config).unwrap();

config.check_frozen()?;

let unvested_balance = ctx
.accounts
Expand Down
11 changes: 0 additions & 11 deletions staking/programs/staking/src/state/global_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ impl GlobalConfig {
pub const LEN: usize = 10240;
}

impl GlobalConfig {
// Checks freeze flag and raises error
pub fn check_frozen(&self) -> Result<()> {
if self.freeze {
Err(error!(ErrorCode::Frozen))
} else {
Ok(())
}
}
}

#[cfg(test)]
pub mod tests {
use {
Expand Down
30 changes: 0 additions & 30 deletions staking/target/idl/staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,36 +104,6 @@
}
]
},
{
"name": "updateFreeze",
"accounts": [
{
"name": "governanceSigner",
"isMut": false,
"isSigner": true
},
{
"name": "config",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "const",
"type": "string",
"value": "config"
}
]
}
}
],
"args": [
{
"name": "freeze",
"type": "bool"
}
]
},
{
"name": "updateTokenListTime",
"accounts": [
Expand Down
60 changes: 0 additions & 60 deletions staking/target/types/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,36 +104,6 @@ export type Staking = {
}
]
},
{
"name": "updateFreeze",
"accounts": [
{
"name": "governanceSigner",
"isMut": false,
"isSigner": true
},
{
"name": "config",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "const",
"type": "string",
"value": "config"
}
]
}
}
],
"args": [
{
"name": "freeze",
"type": "bool"
}
]
},
{
"name": "updateTokenListTime",
"accounts": [
Expand Down Expand Up @@ -2041,36 +2011,6 @@ export const IDL: Staking = {
}
]
},
{
"name": "updateFreeze",
"accounts": [
{
"name": "governanceSigner",
"isMut": false,
"isSigner": true
},
{
"name": "config",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "const",
"type": "string",
"value": "config"
}
]
}
}
],
"args": [
{
"name": "freeze",
"type": "bool"
}
]
},
{
"name": "updateTokenListTime",
"accounts": [
Expand Down
125 changes: 9 additions & 116 deletions staking/tests/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe("config", async () => {
pythTokenMint: pythMintAccount.publicKey,
unlockingDuration: 2,
epochDuration: new BN(3600),
freeze: true,
freeze: false,
pdaAuthority: pdaAuthority,
governanceProgram: governanceProgram,
pythTokenListTime: null,
Expand Down Expand Up @@ -124,7 +124,7 @@ describe("config", async () => {
pythGovernanceRealm: zeroPubkey,
unlockingDuration: 2,
epochDuration: new BN(3600),
freeze: true,
freeze: false,
pdaAuthority: pdaAuthority,
governanceProgram: governanceProgram,
pythTokenListTime: null,
Expand All @@ -150,7 +150,7 @@ describe("config", async () => {
pythGovernanceRealm: zeroPubkey,
unlockingDuration: 2,
epochDuration: new BN(3600),
freeze: true,
freeze: false,
pdaAuthority: pdaAuthority,
governanceProgram: governanceProgram,
pythTokenListTime: null,
Expand All @@ -174,7 +174,7 @@ describe("config", async () => {
pythGovernanceRealm: zeroPubkey,
unlockingDuration: 2,
epochDuration: new BN(3600),
freeze: true,
freeze: false,
pdaAuthority: pdaAuthority,
governanceProgram: governanceProgram,
pythTokenListTime: null,
Expand Down Expand Up @@ -202,7 +202,7 @@ describe("config", async () => {
pythGovernanceRealm: zeroPubkey,
unlockingDuration: 2,
epochDuration: new BN(3600),
freeze: true,
freeze: false,
pdaAuthority: pdaAuthority,
governanceProgram: governanceProgram,
pythTokenListTime: new BN(5),
Expand All @@ -226,7 +226,7 @@ describe("config", async () => {
pythGovernanceRealm: zeroPubkey,
unlockingDuration: 2,
epochDuration: new BN(3600),
freeze: true,
freeze: false,
pdaAuthority: pdaAuthority,
governanceProgram: governanceProgram,
pythTokenListTime: null,
Expand All @@ -236,59 +236,7 @@ describe("config", async () => {
);
});

it("freeze", async () => {
await program.methods.updateFreeze(true).rpc({ skipPreflight: DEBUG });

const configAccountData = await program.account.globalConfig.fetch(
configAccount
);

assert.equal(
JSON.stringify(configAccountData),
JSON.stringify({
bump,
governanceAuthority: program.provider.wallet.publicKey,
pythTokenMint: pythMintAccount.publicKey,
pythGovernanceRealm: zeroPubkey,
unlockingDuration: 2,
epochDuration: new BN(3600),
freeze: true,
pdaAuthority: pdaAuthority,
governanceProgram: governanceProgram,
pythTokenListTime: null,
agreementHash: getDummyAgreementHash(),
mockClockTime: new BN(30),
})
);

const owner = program.provider.wallet.publicKey;
const stakeAccountKeypair = new Keypair();
const instructions: TransactionInstruction[] = [];

instructions.push(
await program.account.positionData.createInstruction(
stakeAccountKeypair,
wasm.Constants.POSITIONS_ACCOUNT_SIZE()
)
);

await expectFail(
program.methods
.createStakeAccount(owner, { fullyVested: {} })
.preInstructions(instructions)
.accounts({
stakeAccountPositions: stakeAccountKeypair.publicKey,
mint: pythMintAccount.publicKey,
})
.signers([stakeAccountKeypair]),
"Protocol is frozen",
errMap
);
});

it("unfreeze, create account", async () => {
await program.methods.updateFreeze(false).rpc({ skipPreflight: DEBUG });

it("create account", async () => {
const configAccountData = await program.account.globalConfig.fetch(
configAccount
);
Expand Down Expand Up @@ -335,56 +283,6 @@ describe("config", async () => {
stakeAccountAddress = stakeAccountKeypair.publicKey;
});

it("freeze again try other instructions", async () => {
await program.methods.updateFreeze(true).rpc({ skipPreflight: DEBUG });

const configAccountData = await program.account.globalConfig.fetch(
configAccount
);

assert(configAccountData.freeze);

await expectFail(
program.methods
.createPosition(votingProduct, new BN(1))
.accounts({
stakeAccountPositions: stakeAccountAddress,
targetAccount: votingProductMetadataAccount,
})
.signers([]),
"Protocol is frozen",
errMap
);

await expectFail(
program.methods
.closePosition(0, new BN(1), votingProduct)
.accounts({
stakeAccountPositions: stakeAccountAddress,
targetAccount: votingProductMetadataAccount,
})
.signers([]),
"Protocol is frozen",
errMap
);

const toAccount = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
pythMintAccount.publicKey,
program.provider.wallet.publicKey
);

await expectFail(
program.methods.withdrawStake(new BN(0)).accounts({
stakeAccountPositions: stakeAccountAddress,
destination: toAccount,
}),
"Protocol is frozen",
errMap
);
});

it("someone else tries to access admin methods", async () => {
const sam = new Keypair();
const samConnection = await StakeConnection.createStakeConnection(
Expand All @@ -401,11 +299,6 @@ describe("config", async () => {
// Airdrops are not instant unfortunately, wait
await new Promise((resolve) => setTimeout(resolve, 2000));

await expectFail(
samConnection.program.methods.updateFreeze(true),
"An address constraint was violated",
errMap
);
await expectFail(
samConnection.program.methods.updateGovernanceAuthority(new PublicKey(0)),
"An address constraint was violated",
Expand Down Expand Up @@ -458,7 +351,7 @@ describe("config", async () => {
pythGovernanceRealm: zeroPubkey,
unlockingDuration: 2,
epochDuration: new BN(3600),
freeze: true,
freeze: false,
pdaAuthority: program.provider.wallet.publicKey,
governanceProgram,
pythTokenListTime: null,
Expand All @@ -481,7 +374,7 @@ describe("config", async () => {
pythGovernanceRealm: zeroPubkey,
unlockingDuration: 2,
epochDuration: new BN(3600),
freeze: true,
freeze: false,
pdaAuthority: pdaAuthority,
governanceProgram,
pythTokenListTime: null,
Expand Down

0 comments on commit 2fa4a11

Please sign in to comment.