Skip to content

Commit

Permalink
Merge branch 'feat/v1' of github.com:Aurory-Game/ocil into feat/v1
Browse files Browse the repository at this point in the history
  • Loading branch information
tevdoradze committed Oct 21, 2022
2 parents eb2998e + d6f3962 commit 8c31940
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Anchor.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[features]
seeds = false
[programs.localnet]
casier = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
casier = "FLoc9nBwGb2ayzVzb5GC9NttuPY3CxMhd4KDnApr79Ab"

[programs.mainnet]
casier = "CAsieqooSrgVxhgWRwh21gyjq7Rmuhmo4qTW9XzXtAvW"
Expand All @@ -11,7 +11,7 @@ url = "https://anchor.projectserum.com"

[provider]
cluster = "localnet"
wallet = "/home/levani/.config/solana/id.json"
wallet = "/home/<username>/.config/solana/id.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
24 changes: 12 additions & 12 deletions programs/casier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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));
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion programs/casier/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub struct Config {
pub struct Locker {
pub owner: Pubkey,
pub mints: Vec<Pubkey>,
pub amounts: Vec<u32>,
pub amounts: Vec<u64>,
pub version: u8,
pub space: u64,
}
55 changes: 27 additions & 28 deletions tests/casier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -389,8 +389,8 @@ 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 missingTokens = 3;
const withdrawAmount = new anchor.BN(0);
const missingTokens = new anchor.BN(3);
const withTransfer = true;

const { beforeAmount, finalAmount: tempFinalAmount } =
Expand All @@ -401,8 +401,7 @@ describe("casier", () => {
withdrawAmount,
withTransfer
);
const finalAmount = tempFinalAmount - missingTokens;

const finalAmount = tempFinalAmount.sub(missingTokens);
const user = users[userIndex];
const mint = mints[mintIndex];
const userTa = tokenAccounts[userIndex][mintIndex];
Expand Down Expand Up @@ -449,12 +448,12 @@ describe("casier", () => {
);

assert.strictEqual(
missingTokens,
burnTokenAccount.value.data.parsed.info.tokenAmount.uiAmount
missingTokens.toString(),
burnTokenAccount.value.data.parsed.info.tokenAmount.uiAmount.toString()
);
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);
});
Expand All @@ -468,7 +467,7 @@ describe("casier", () => {
"withdraw",
userIndex,
mintIndex,
0,
new anchor.BN(0),
withTransfer
);

Expand Down Expand Up @@ -519,11 +518,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;
}> {
Expand All @@ -534,36 +533,36 @@ async function getCheckAmounts(
(v) => v.toString() === mints[mintIndex].toString()
);
let beforeAmount =
lockerMintIndex !== -1 ? lockerAccount.amounts[lockerMintIndex] : 0;
const sign = txType == "deposit" ? 1 : -1;
lockerMintIndex !== -1 ? lockerAccount.amounts[lockerMintIndex] : new anchor.BN(0);
const sign = txType == "deposit" ? new anchor.BN(1) : new anchor.BN(-1);
let finalAmount = withTransfer
? beforeAmount + sign * withdrawAmount
? beforeAmount.add(sign.mul(withdrawAmount))
: 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<void> {
const vaultAccount = await provider.connection.getParsedAccountInfo(vaultTa);
const lockerAccount = await program.account.locker.fetch(locker);
const lockerMintIndex = lockerAccount.mints.findIndex(
(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);
Expand Down

0 comments on commit 8c31940

Please sign in to comment.