Skip to content

Commit

Permalink
(HAL-01) PAUSED STATUS CHECK MISSING IN PAUSE INSTRUCTIONS
Browse files Browse the repository at this point in the history
  • Loading branch information
makarychev committed Nov 15, 2024
1 parent 8f4a03f commit 9125693
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/src/types/dividends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ export type Dividends = {
code: 6010;
name: "invalidIpfsHashSize";
msg: "Invalid IPFS hash size";
},
{
code: 6011;
name: "valueUnchanged";
msg: "The provided value is already set. No changes were made";
}
];
types: [
Expand Down
5 changes: 5 additions & 0 deletions app/src/types/transfer_restrictions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1706,6 +1706,11 @@ export type TransferRestrictions = {
code: 6020;
name: "zeroGroupHolderGroupMaxCannotBeNonZero";
msg: "Zero group holder group max cannot be non-zero";
},
{
code: 6021;
name: "valueUnchanged";
msg: "The provided value is already set. No changes were made";
}
];
types: [
Expand Down
2 changes: 2 additions & 0 deletions programs/dividends/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ pub enum DividendsErrorCode {
DistributorNotReadyToClaim,
#[msg("Invalid IPFS hash size")]
InvalidIPFSHashSize,
#[msg("The provided value is already set. No changes were made")]
ValueUnchanged,
}
6 changes: 5 additions & 1 deletion programs/dividends/src/instructions/pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Pause<'info> {

/// Authority wallet role to pause the distributor.
#[account(
constraint = authority_wallet_role.owner == authority.key(),
constraint = authority_wallet_role.owner == authority.key(),
constraint = authority_wallet_role.has_role(access_control::Roles::ContractAdmin) @ DividendsErrorCode::Unauthorized,
constraint = authority_wallet_role.access_control == access_control.key(),
)]
Expand All @@ -29,6 +29,10 @@ pub struct Pause<'info> {
}

pub fn pause(ctx: Context<Pause>, paused: bool) -> Result<()> {
if paused == ctx.accounts.distributor.paused {
return Err(DividendsErrorCode::ValueUnchanged.into());
}

let distributor = &mut ctx.accounts.distributor;
distributor.paused = paused;

Expand Down
2 changes: 2 additions & 0 deletions programs/transfer-restrictions/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ pub enum TransferRestrictionsError {
NewHolderGroupMaxMustExceedCurrentHolderGroupCount,
#[msg("Zero group holder group max cannot be non-zero")]
ZeroGroupHolderGroupMaxCannotBeNonZero,
#[msg("The provided value is already set. No changes were made")]
ValueUnchanged,
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub fn pause(ctx: Context<Pause>, paused: bool) -> Result<()> {
{
return Err(TransferRestrictionsError::Unauthorized.into());
}
if paused == ctx.accounts.transfer_restriction_data.paused {
return Err(TransferRestrictionsError::ValueUnchanged.into());
}

let transfer_restriction_data = &mut ctx.accounts.transfer_restriction_data;
transfer_restriction_data.paused = paused;
Expand Down
39 changes: 39 additions & 0 deletions tests/dividends/pause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,43 @@ describe(`pause distribution`, () => {
);
assert.equal(distributorData.paused, false);
});

it("fails to pause when already paused", async () => {
await dividendsProgram.methods
.pause(true)
.accountsStrict({
distributor,
accessControl: testEnvironment.accessControlHelper.accessControlPubkey,
authorityWalletRole: testEnvironment.accessControlHelper.walletRolePDA(
signer.publicKey
)[0],
authority: signer.publicKey,
})
.signers([signer])
.rpc({ commitment });

try {
await dividendsProgram.methods
.pause(true)
.accountsStrict({
distributor,
accessControl:
testEnvironment.accessControlHelper.accessControlPubkey,
authorityWalletRole:
testEnvironment.accessControlHelper.walletRolePDA(
signer.publicKey
)[0],
authority: signer.publicKey,
})
.signers([signer])
.rpc({ commitment });
assert.fail("Expected an error");
} catch ({ error }) {
assert.equal(error.errorCode.code, "ValueUnchanged");
assert.equal(
error.errorMessage,
"The provided value is already set. No changes were made"
);
}
});
});
30 changes: 30 additions & 0 deletions tests/transfer_restrictions/pause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,34 @@ describe("Pause transfers", () => {
(reserveAmountBeforeTransfer + BigInt(transferAmount)).toString()
);
});

it("fails to pause with the same value as it is on-chain", async () => {
const signer = testEnvironment.transferAdmin;
const [authorityWalletRolePubkey] =
testEnvironment.accessControlHelper.walletRolePDA(signer.publicKey);

try {
await testEnvironment.transferRestrictionsHelper.program.methods
.pause(false)
.accountsStrict({
securityMint: testEnvironment.mintKeypair.publicKey,
transferRestrictionData:
testEnvironment.transferRestrictionsHelper
.transferRestrictionDataPubkey,
accessControlAccount:
testEnvironment.accessControlHelper.accessControlPubkey,
authorityWalletRole: authorityWalletRolePubkey,
payer: signer.publicKey,
})
.signers([signer])
.rpc({ commitment: testEnvironment.commitment });
assert.fail("Expect an error");
} catch ({ error }) {
assert.equal(error.errorCode.code, "ValueUnchanged");
assert.equal(
error.errorMessage,
"The provided value is already set. No changes were made"
);
}
});
});

0 comments on commit 9125693

Please sign in to comment.