diff --git a/Anchor.toml b/Anchor.toml index 2cadc68..199f004 100644 --- a/Anchor.toml +++ b/Anchor.toml @@ -1,14 +1,14 @@ [features] seeds = false [programs.localnet] -casier = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS" +casier = "FLoc9nBwGb2ayzVzb5GC9NttuPY3CxMhd4KDnApr79Ab" [registry] url = "https://anchor.projectserum.com" [provider] cluster = "localnet" -wallet = "/home/levani/.config/solana/id.json" +wallet = "/home//.config/solana/id.json" [scripts] test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts" diff --git a/README.md b/README.md index 31d41db..74933e2 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,6 @@ TODO # Tests Tested with `anchor-cli 0.24.2` + +1. Update path to local wallet in `Anchor.toml` +2. `anchor test` diff --git a/programs/casier/src/lib.rs b/programs/casier/src/lib.rs index 766c6b5..0b55be5 100644 --- a/programs/casier/src/lib.rs +++ b/programs/casier/src/lib.rs @@ -8,7 +8,7 @@ use anchor_lang::solana_program::{pubkey::Pubkey, rent::Rent}; use anchor_spl; use std::collections::BTreeMap; -declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"); +declare_id!("FLoc9nBwGb2ayzVzb5GC9NttuPY3CxMhd4KDnApr79Ab"); #[program] pub mod casier { @@ -40,8 +40,8 @@ pub mod casier { pub fn deposit<'a, 'b, 'c, 'info>( ctx: Context<'a, 'b, 'c, 'info, Deposit>, vault_bump: u8, - deposit_amount: u32, - before_amount: u32, + deposit_amount: u64, + before_amount: u64, ) -> Result<()> { let locker = &mut ctx.accounts.locker; let mk = ctx.accounts.mint.key(); @@ -116,9 +116,9 @@ pub mod casier { pub fn withdraw<'a, 'b, 'c, 'info>( ctx: Context<'a, 'b, 'c, 'info, Withdraw>, vault_bump: u8, - withdraw_amount: u32, - before_amount: u32, - final_amount: u32, + withdraw_amount: u64, + before_amount: u64, + final_amount: u64, with_transfer: bool, ) -> Result<()> { let locker = &mut ctx.accounts.locker; @@ -131,8 +131,8 @@ pub mod casier { if locker.amounts[i] != before_amount { return Err(error!(ErrorCode::InvalidBeforeState)); // if final amount is lower than the amounts of tokens that will be left, we should call withdraw_and_burn - } else if (final_amount as u64) - < ctx.accounts.vault_ta.amount - (withdraw_amount as u64) + } else if (final_amount) + < ctx.accounts.vault_ta.amount - withdraw_amount { return Err(error!(ErrorCode::BurnRequired)); } @@ -203,9 +203,9 @@ pub mod casier { ctx: Context<'a, 'b, 'c, 'info, WithdrawAndBurn>, vault_bump: u8, burn_bump: u8, - withdraw_amount: u32, - before_amount: u32, - final_amount: u32, + withdraw_amount: u64, + before_amount: u64, + final_amount: u64, with_transfer: bool, ) -> Result<()> { if ctx.accounts.vault_ta.amount <= final_amount.into() { @@ -335,7 +335,7 @@ pub mod casier { &[vault_bump], ]], ), - ctx.accounts.vault_ta.amount - final_amount as u64, + ctx.accounts.vault_ta.amount - final_amount, )?; ctx.accounts.vault_ta.reload()?; if ctx.accounts.vault_ta.amount == 0 { diff --git a/programs/casier/src/state.rs b/programs/casier/src/state.rs index 083a06a..53bd59d 100644 --- a/programs/casier/src/state.rs +++ b/programs/casier/src/state.rs @@ -178,7 +178,7 @@ pub struct Config { pub struct Locker { pub owner: Pubkey, pub mints: Vec, - pub amounts: Vec, + pub amounts: Vec, pub version: u8, pub space: u64, } diff --git a/tests/casier.ts b/tests/casier.ts index 8ddbf6b..909cde2 100644 --- a/tests/casier.ts +++ b/tests/casier.ts @@ -202,7 +202,7 @@ describe("casier", () => { it("Deposit to close vault TA: u: 0, m: 0, a: 100", async () => { const userIndex = 0; const mintIndex = 0; - const deposit_amount = 100; + const deposit_amount = new anchor.BN(100); const { beforeAmount, finalAmount } = await getCheckAmounts( "deposit", @@ -242,7 +242,7 @@ describe("casier", () => { it("Withdraw: u: 0, m: 0, a: 1", async () => { const userIndex = 0; const mintIndex = 0; - const withdrawAmount = 1; + const withdrawAmount = new anchor.BN(1); const withTransfer = true; const { beforeAmount, finalAmount } = await getCheckAmounts( @@ -291,7 +291,7 @@ describe("casier", () => { it("Deposit to open vault TA: u: 0, m: 0, a: 1", async () => { const userIndex = 0; const mintIndex = 0; - const deposit_amount = 1; + const deposit_amount = new anchor.BN(1); const { beforeAmount, finalAmount } = await getCheckAmounts( "deposit", @@ -341,7 +341,7 @@ describe("casier", () => { it("Withdraw to closed user TA: u: 0, m: 0, a: 1", async () => { const userIndex = 0; const mintIndex = 0; - const withdrawAmount = 1; + const withdrawAmount = new anchor.BN(1); const withTransfer = true; const { beforeAmount, finalAmount } = await getCheckAmounts( @@ -389,7 +389,7 @@ describe("casier", () => { it("Withdraw & set a lower final amount to burn the tokens: u: 0, m: 0, a: 1", async () => { const userIndex = 0; const mintIndex = 0; - const withdrawAmount = 0; + const withdrawAmount = new anchor.BN(0); const missingTokens = 3; const withTransfer = true; @@ -401,7 +401,7 @@ describe("casier", () => { withdrawAmount, withTransfer ); - const finalAmount = tempFinalAmount - missingTokens; + const finalAmount = new anchor.BN(tempFinalAmount.toNumber() - missingTokens); const user = users[userIndex]; const mint = mints[mintIndex]; @@ -453,8 +453,8 @@ describe("casier", () => { burnTokenAccount.value.data.parsed.info.tokenAmount.uiAmount ); assert.strictEqual( - finalAmount, - vaultAccount.value.data.parsed.info.tokenAmount.uiAmount + finalAmount.toString(), + vaultAccount.value.data.parsed.info.tokenAmount.uiAmount.toString() ); await afterChecks(mintIndex, vaultTa, locker, finalAmount, mint); }); @@ -468,7 +468,7 @@ describe("casier", () => { "withdraw", userIndex, mintIndex, - 0, + new anchor.BN(0), withTransfer ); @@ -519,11 +519,11 @@ async function getCheckAmounts( txType: "deposit" | "withdraw", userIndex: number, mintIndex: number, - withdrawAmount: number, + withdrawAmount: anchor.BN, withTransfer: boolean = true ): Promise<{ - beforeAmount: number; - finalAmount: number; + beforeAmount: anchor.BN; + finalAmount: anchor.BN; lockerAccount: any; lockerMintIndex: number; }> { @@ -534,20 +534,20 @@ async function getCheckAmounts( (v) => v.toString() === mints[mintIndex].toString() ); let beforeAmount = - lockerMintIndex !== -1 ? lockerAccount.amounts[lockerMintIndex] : 0; + lockerMintIndex !== -1 ? lockerAccount.amounts[lockerMintIndex] : new anchor.BN(0); const sign = txType == "deposit" ? 1 : -1; let finalAmount = withTransfer - ? beforeAmount + sign * withdrawAmount + ? new anchor.BN(beforeAmount.toNumber() + sign * withdrawAmount.toNumber()) : beforeAmount; return { beforeAmount, finalAmount, lockerAccount, lockerMintIndex }; } async function afterChecks( - mintIndex, - vaultTa, - locker, - finalAmount, - mint + mintIndex: number, + vaultTa: anchor.web3.PublicKey, + locker: anchor.web3.PublicKey, + finalAmount: anchor.BN, + mint: string, ): Promise { const vaultAccount = await provider.connection.getParsedAccountInfo(vaultTa); const lockerAccount = await program.account.locker.fetch(locker); @@ -555,15 +555,15 @@ async function afterChecks( (v) => v.toString() === mints[mintIndex].toString() ); - if (finalAmount) { - assert.strictEqual(lockerAccount.amounts[lockerMintIndex], finalAmount); + if (finalAmount.toString() !== '0') { + assert.strictEqual(lockerAccount.amounts[lockerMintIndex].toString(), finalAmount.toString()); assert.strictEqual( lockerAccount.mints[lockerMintIndex].toString(), mint.toString() ); assert.strictEqual( - (vaultAccount.value.data as any).parsed.info.tokenAmount.uiAmount, - finalAmount + (vaultAccount.value.data as any).parsed.info.tokenAmount.uiAmount.toString(), + finalAmount.toString() ); } else { assert.isNull(vaultAccount.value);