Skip to content

Commit

Permalink
fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
makarychev committed Aug 1, 2024
1 parent c8d66e0 commit 46da4cd
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 144 deletions.
66 changes: 45 additions & 21 deletions tests/access_control/burn-securities.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import * as anchor from "@coral-xyz/anchor";
import { assert } from "chai";
import {
Keypair,
PublicKey
} from "@solana/web3.js";
import { Keypair, PublicKey } from "@solana/web3.js";

import {
TestEnvironment,
Expand Down Expand Up @@ -31,24 +28,31 @@ describe("Access Control burn securities", () => {
testEnvironment = new TestEnvironment(testEnvironmentParams);
await testEnvironment.setup();

[reserveAdminWalletRole] = testEnvironment.accessControlHelper.walletRolePDA(testEnvironment.reserveAdmin.publicKey);
reserveAdminTokenAccountPubkey = testEnvironment.mintHelper.getAssocciatedTokenAddress(testEnvironment.reserveAdmin.publicKey);
[reserveAdminWalletRole] =
testEnvironment.accessControlHelper.walletRolePDA(
testEnvironment.reserveAdmin.publicKey
);
reserveAdminTokenAccountPubkey =
testEnvironment.mintHelper.getAssocciatedTokenAddress(
testEnvironment.reserveAdmin.publicKey
);
});

it("fails to burn more than maxTotalSupply", async () => {
const { supply: currentSupply } = await testEnvironment.mintHelper.getMint();
const { supply: currentSupply } =
await testEnvironment.mintHelper.getMint();
const amount = new anchor.BN(currentSupply.toString()).addn(1);
try {
await testEnvironment.accessControlHelper.burnSecurities(
amount,
testEnvironment.reserveAdmin.publicKey,
reserveAdminTokenAccountPubkey,
testEnvironment.reserveAdmin,
testEnvironment.reserveAdmin
);
assert.fail("Expected an error");
} catch (error) {
const res = error.logs.some((log: string) =>
log === "Program log: Error: insufficient funds"
const res = error.logs.some(
(log: string) => log === "Program log: Error: insufficient funds"
);
assert.isTrue(res);
}
Expand All @@ -61,7 +65,7 @@ describe("Access Control burn securities", () => {
amount,
testEnvironment.reserveAdmin.publicKey,
reserveAdminTokenAccountPubkey,
testEnvironment.walletsAdmin,
testEnvironment.walletsAdmin
);
assert.fail("Expected an error");
} catch ({ error }) {
Expand All @@ -73,15 +77,20 @@ describe("Access Control burn securities", () => {
it("fails when signer is not the authority", async () => {
const amount = new anchor.BN(1_000_000);
const reserveAdminPretender = new Keypair();
await topUpWallet(testEnvironment.connection, reserveAdminPretender.publicKey, solToLamports(1));
await topUpWallet(
testEnvironment.connection,
reserveAdminPretender.publicKey,
solToLamports(1)
);

try {
await testEnvironment.accessControlHelper.program.methods
.burnSecurities(amount)
.accountsStrict({
authority: testEnvironment.reserveAdmin.publicKey,
authorityWalletRole: reserveAdminWalletRole,
accessControl: testEnvironment.accessControlHelper.accessControlPubkey,
accessControl:
testEnvironment.accessControlHelper.accessControlPubkey,
securityMint: testEnvironment.mintKeypair.publicKey,
targetAccount: reserveAdminTokenAccountPubkey,
targetAuthority: testEnvironment.reserveAdmin.publicKey,
Expand Down Expand Up @@ -113,14 +122,18 @@ describe("Access Control burn securities", () => {
attackerEnvironment = new TestEnvironment(attackerTestEnvironmentParams);
await attackerEnvironment.setup();
const amount = new anchor.BN(1_000_000);
const [attackerReserveAdminWalletRole] = attackerEnvironment.accessControlHelper.walletRolePDA(attackerEnvironment.reserveAdmin.publicKey);
const [attackerReserveAdminWalletRole] =
attackerEnvironment.accessControlHelper.walletRolePDA(
attackerEnvironment.reserveAdmin.publicKey
);
try {
await testEnvironment.accessControlHelper.program.methods
.burnSecurities(amount)
.accountsStrict({
authority: attackerEnvironment.reserveAdmin.publicKey,
authorityWalletRole: attackerReserveAdminWalletRole,
accessControl: testEnvironment.accessControlHelper.accessControlPubkey,
accessControl:
testEnvironment.accessControlHelper.accessControlPubkey,
securityMint: testEnvironment.mintKeypair.publicKey,
targetAccount: reserveAdminTokenAccountPubkey,
targetAuthority: testEnvironment.reserveAdmin.publicKey,
Expand All @@ -137,18 +150,29 @@ describe("Access Control burn securities", () => {

it("burn securities", async () => {
const amount = new anchor.BN(1_000_000);
const { supply: supplyBeforeBurn } = await testEnvironment.mintHelper.getMint();
const { amount: reserveAdminAmountBefore } = await testEnvironment.mintHelper.getAccount(reserveAdminTokenAccountPubkey);
const { supply: supplyBeforeBurn } =
await testEnvironment.mintHelper.getMint();
const { amount: reserveAdminAmountBefore } =
await testEnvironment.mintHelper.getAccount(
reserveAdminTokenAccountPubkey
);
await testEnvironment.accessControlHelper.burnSecurities(
amount,
testEnvironment.reserveAdmin.publicKey,
reserveAdminTokenAccountPubkey,
testEnvironment.reserveAdmin,
testEnvironment.reserveAdmin
);

const { supply: supplyAfterBurn } = await testEnvironment.mintHelper.getMint();
const { amount: reserveAdminAmountAfter } = await testEnvironment.mintHelper.getAccount(reserveAdminTokenAccountPubkey);
assert.equal(reserveAdminAmountAfter, reserveAdminAmountBefore - BigInt(amount.toString()));
const { supply: supplyAfterBurn } =
await testEnvironment.mintHelper.getMint();
const { amount: reserveAdminAmountAfter } =
await testEnvironment.mintHelper.getAccount(
reserveAdminTokenAccountPubkey
);
assert.equal(
reserveAdminAmountAfter,
reserveAdminAmountBefore - BigInt(amount.toString())
);
assert.equal(supplyAfterBurn, supplyBeforeBurn - BigInt(amount.toString()));
});
});
117 changes: 70 additions & 47 deletions tests/access_control/force-transfer-between.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import * as anchor from "@coral-xyz/anchor";
import { assert } from "chai";
import {
Keypair,
PublicKey
} from "@solana/web3.js";
import { Keypair, PublicKey } from "@solana/web3.js";

import {
TestEnvironment,
Expand Down Expand Up @@ -35,17 +32,28 @@ describe("Access Control force transfer between", () => {
testEnvironment = new TestEnvironment(testEnvironmentParams);
await testEnvironment.setup();

[reserveAdminWalletRole] = testEnvironment.accessControlHelper.walletRolePDA(testEnvironment.reserveAdmin.publicKey);
[walletsAdminWalletRole] = testEnvironment.accessControlHelper.walletRolePDA(testEnvironment.walletsAdmin.publicKey);
[transferAdminWalletRole] = testEnvironment.accessControlHelper.walletRolePDA(testEnvironment.transferAdmin.publicKey);
recipientTokenAccount = await testEnvironment.mintHelper.createAssociatedTokenAccount(
recipient.publicKey,
testEnvironment.contractAdmin,
);
targetTokenAccount = await testEnvironment.mintHelper.createAssociatedTokenAccount(
target.publicKey,
testEnvironment.contractAdmin,
);
[reserveAdminWalletRole] =
testEnvironment.accessControlHelper.walletRolePDA(
testEnvironment.reserveAdmin.publicKey
);
[walletsAdminWalletRole] =
testEnvironment.accessControlHelper.walletRolePDA(
testEnvironment.walletsAdmin.publicKey
);
[transferAdminWalletRole] =
testEnvironment.accessControlHelper.walletRolePDA(
testEnvironment.transferAdmin.publicKey
);
recipientTokenAccount =
await testEnvironment.mintHelper.createAssociatedTokenAccount(
recipient.publicKey,
testEnvironment.contractAdmin
);
targetTokenAccount =
await testEnvironment.mintHelper.createAssociatedTokenAccount(
target.publicKey,
testEnvironment.contractAdmin
);
});

// it("fails to mint more than maxTotalSupply", async () => {
Expand Down Expand Up @@ -76,7 +84,7 @@ describe("Access Control force transfer between", () => {
recipient.publicKey,
recipientTokenAccount,
testEnvironment.contractAdmin,
testEnvironment.connection,
testEnvironment.connection
);
assert.fail("Expected an error");
} catch (error) {
Expand All @@ -96,36 +104,51 @@ describe("Access Control force transfer between", () => {
await testEnvironment.transferRestrictionsHelper.initializeTransferRestrictionHolder(
new anchor.BN(targetHolderId),
walletsAdminWalletRole,
testEnvironment.walletsAdmin,
testEnvironment.walletsAdmin
);
await testEnvironment.transferRestrictionsHelper.initializeTransferRestrictionHolder(
new anchor.BN(recipientHolderId),
walletsAdminWalletRole,
testEnvironment.walletsAdmin,
testEnvironment.walletsAdmin
);
await testEnvironment.transferRestrictionsHelper.initializeTransferRestrictionGroup(
new anchor.BN(groupId),
transferAdminWalletRole,
testEnvironment.transferAdmin,
testEnvironment.transferAdmin
);
[targetHolderPubkey] = testEnvironment.transferRestrictionsHelper.holderPDA(new anchor.BN(targetHolderId));
[recipientHolderPubkey] = testEnvironment.transferRestrictionsHelper.holderPDA(new anchor.BN(recipientHolderId));
[groupPubkey] = testEnvironment.transferRestrictionsHelper.groupPDA(new anchor.BN(groupId));
[targetHolderGroupPubkey] = testEnvironment.transferRestrictionsHelper.holderGroupPDA(targetHolderPubkey, new anchor.BN(groupId));
[targetHolderPubkey] = testEnvironment.transferRestrictionsHelper.holderPDA(
new anchor.BN(targetHolderId)
);
[recipientHolderPubkey] =
testEnvironment.transferRestrictionsHelper.holderPDA(
new anchor.BN(recipientHolderId)
);
[groupPubkey] = testEnvironment.transferRestrictionsHelper.groupPDA(
new anchor.BN(groupId)
);
[targetHolderGroupPubkey] =
testEnvironment.transferRestrictionsHelper.holderGroupPDA(
targetHolderPubkey,
new anchor.BN(groupId)
);
await testEnvironment.transferRestrictionsHelper.initializeHolderGroup(
targetHolderGroupPubkey,
targetHolderPubkey,
groupPubkey,
walletsAdminWalletRole,
testEnvironment.walletsAdmin,
testEnvironment.walletsAdmin
);
[recipientHolderGroupPubkey] = testEnvironment.transferRestrictionsHelper.holderGroupPDA(recipientHolderPubkey, new anchor.BN(groupId));
[recipientHolderGroupPubkey] =
testEnvironment.transferRestrictionsHelper.holderGroupPDA(
recipientHolderPubkey,
new anchor.BN(groupId)
);
await testEnvironment.transferRestrictionsHelper.initializeHolderGroup(
recipientHolderGroupPubkey,
recipientHolderPubkey,
groupPubkey,
walletsAdminWalletRole,
testEnvironment.walletsAdmin,
testEnvironment.walletsAdmin
);
await testEnvironment.transferRestrictionsHelper.initializeSecurityAssociatedAccount(
groupPubkey,
Expand All @@ -134,7 +157,7 @@ describe("Access Control force transfer between", () => {
target.publicKey,
targetTokenAccount,
walletsAdminWalletRole,
testEnvironment.walletsAdmin,
testEnvironment.walletsAdmin
);
await testEnvironment.transferRestrictionsHelper.initializeSecurityAssociatedAccount(
groupPubkey,
Expand All @@ -143,11 +166,12 @@ describe("Access Control force transfer between", () => {
recipient.publicKey,
recipientTokenAccount,
walletsAdminWalletRole,
testEnvironment.walletsAdmin,
testEnvironment.walletsAdmin
);
});

const unauthorizedErrorMsg = "Program log: AnchorError occurred. Error Code: Unauthorized. Error Number: 6000. Error Message: Unauthorized.";
const unauthorizedErrorMsg =
"Program log: AnchorError occurred. Error Code: Unauthorized. Error Number: 6000. Error Message: Unauthorized.";
const amount = 1_000_000;
it("does not allow forceTransferBetween for contract admin", async () => {
try {
Expand All @@ -158,7 +182,7 @@ describe("Access Control force transfer between", () => {
recipient.publicKey,
recipientTokenAccount,
testEnvironment.contractAdmin,
testEnvironment.connection,
testEnvironment.connection
);
assert.fail("Expected an error");
} catch (error) {
Expand All @@ -175,7 +199,7 @@ describe("Access Control force transfer between", () => {
recipient.publicKey,
recipientTokenAccount,
testEnvironment.walletsAdmin,
testEnvironment.connection,
testEnvironment.connection
);
assert.fail("Expected an error");
} catch (error) {
Expand All @@ -192,7 +216,7 @@ describe("Access Control force transfer between", () => {
recipient.publicKey,
recipientTokenAccount,
testEnvironment.transferAdmin,
testEnvironment.connection,
testEnvironment.connection
);
assert.fail("Expected an error");
} catch (error) {
Expand All @@ -209,11 +233,14 @@ describe("Access Control force transfer between", () => {
recipient.publicKey,
recipientTokenAccount,
testEnvironment.reserveAdmin,
testEnvironment.connection,
testEnvironment.connection
);
assert.fail("Expected an error");
} catch (error) {
assert.equal(error, "Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 1: custom program error: 0x1");
assert.equal(
error,
"Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 1: custom program error: 0x1"
);
}
});

Expand All @@ -224,27 +251,23 @@ describe("Access Control force transfer between", () => {
targetTokenAccount,
testEnvironment.reserveAdmin
);
let {
amount: targetAmountBefore
} = await testEnvironment.mintHelper.getAccount(targetTokenAccount);
let {
amount: recipientAmountBefore
} = await testEnvironment.mintHelper.getAccount(recipientTokenAccount);
let { amount: targetAmountBefore } =
await testEnvironment.mintHelper.getAccount(targetTokenAccount);
let { amount: recipientAmountBefore } =
await testEnvironment.mintHelper.getAccount(recipientTokenAccount);
await testEnvironment.accessControlHelper.forceTransferBetween(
1_000_000,
target.publicKey,
targetTokenAccount,
recipient.publicKey,
recipientTokenAccount,
testEnvironment.reserveAdmin,
testEnvironment.connection,
testEnvironment.connection
);
let {
amount: targetAmountAfter
} = await testEnvironment.mintHelper.getAccount(targetTokenAccount);
let {
amount: recipientAmountAfter
} = await testEnvironment.mintHelper.getAccount(recipientTokenAccount);
let { amount: targetAmountAfter } =
await testEnvironment.mintHelper.getAccount(targetTokenAccount);
let { amount: recipientAmountAfter } =
await testEnvironment.mintHelper.getAccount(recipientTokenAccount);
assert.equal(targetAmountBefore - targetAmountAfter, BigInt(amount));
assert.equal(recipientAmountAfter - recipientAmountBefore, BigInt(amount));
});
Expand Down
Loading

0 comments on commit 46da4cd

Please sign in to comment.