Skip to content

Commit

Permalink
TS Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
dsctt committed Nov 23, 2024
1 parent 385e3d5 commit 80b60e7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 55 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
"anchor-bankrun": "^0.4.0",
"chai": "^5.1.1",
"solana-bankrun": "^0.3.0"
}
},
"packageManager": "[email protected]+sha512.6e2baf77d06b9362294152c851c4f278ede37ab1eba3a55fda317a4a17b209f4dbb973fb250a77abc463a341fcb1f17f17cfa24091c4eb319cda0d9b84278387"
}
65 changes: 29 additions & 36 deletions tokens/token-swap/anchor/tests/deposit-liquidity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as anchor from '@coral-xyz/anchor';
import type { Program } from '@coral-xyz/anchor';
import { BN } from 'bn.js';
import { expect } from 'chai';
import type { SwapExample } from '../target/types/swap_example';
import { type TestValues, createValues, mintingTokens } from './utils';
import { BN } from 'bn.js';

describe('Deposit liquidity', () => {
const provider = anchor.AnchorProvider.env();
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('Deposit liquidity', () => {
expect(depositTokenAccountA.value.amount).to.equal(values.defaultHolderAccountSupply.sub(values.depositAmountA).toString());
const depositTokenAccountB = await connection.getTokenAccountBalance(values.holderAccountB);
expect(depositTokenAccountB.value.amount).to.equal(values.defaultHolderAccountSupply.sub(values.depositAmountA).toString());

await program.methods
.depositLiquidity(values.depositAmountB, values.depositAmountB)
.accounts({
Expand All @@ -87,20 +87,19 @@ describe('Deposit liquidity', () => {

const depositTokenAccountLiquidity2 = await connection.getTokenAccountBalance(values.liquidityAccount);
// No minimumLiquidity subtraction since it's not the first deposit
expect(depositTokenAccountLiquidity2.value.amount).to.equal(new BN(depositTokenAccountLiquidity.value.amount)
.add(values.depositAmountB).toString());
expect(depositTokenAccountLiquidity2.value.amount).to.equal(
new BN(depositTokenAccountLiquidity.value.amount).add(values.depositAmountB).toString(),
);
const depositTokenAccountA2 = await connection.getTokenAccountBalance(values.holderAccountA);
expect(depositTokenAccountA2.value.amount).to.equal(new BN(depositTokenAccountA.value.amount)
.sub(values.depositAmountB).toString());
expect(depositTokenAccountA2.value.amount).to.equal(new BN(depositTokenAccountA.value.amount).sub(values.depositAmountB).toString());
const depositTokenAccountB2 = await connection.getTokenAccountBalance(values.holderAccountB);
expect(depositTokenAccountB2.value.amount).to.equal(new BN(depositTokenAccountB.value.amount)
.sub(values.depositAmountB).toString());
expect(depositTokenAccountB2.value.amount).to.equal(new BN(depositTokenAccountB.value.amount).sub(values.depositAmountB).toString());
});


it('Deposit amounts a > b, then a < b', async () => {
var depositAmountA = new BN(9 * 10 ** 6)
var depositAmountB = new BN(4 * 10 ** 6)
const depositAmountA = new BN(9 * 10 ** 6);
const depositAmountB = new BN(4 * 10 ** 6);
await program.methods
.depositLiquidity(depositAmountA, depositAmountB)
.accounts({
Expand All @@ -120,8 +119,7 @@ describe('Deposit liquidity', () => {
.rpc({ skipPreflight: true });

const depositTokenAccountLiquidity = await connection.getTokenAccountBalance(values.liquidityAccount);
expect(depositTokenAccountLiquidity.value.amount).to.equal(new BN(6 * 10 ** 6)
.sub(values.minimumLiquidity).toString());
expect(depositTokenAccountLiquidity.value.amount).to.equal(new BN(6 * 10 ** 6).sub(values.minimumLiquidity).toString());
const depositTokenAccountA = await connection.getTokenAccountBalance(values.holderAccountA);
expect(depositTokenAccountA.value.amount).to.equal(values.defaultHolderAccountSupply.sub(depositAmountA).toString());
const depositTokenAccountB = await connection.getTokenAccountBalance(values.holderAccountB);
Expand All @@ -130,10 +128,10 @@ describe('Deposit liquidity', () => {
// Expected behavior is that depositAmountA gets increased to
// (27 * 10 ** 6) * (9/4) = 60.75 * 10 ** 6
// to maintain the ratio established in the above deposit
depositAmountA = new BN(18 * 10 ** 6)
depositAmountB = new BN(27 * 10 ** 6)
const depositAmountA2 = new BN(18 * 10 ** 6);
const depositAmountB2 = new BN(27 * 10 ** 6);
await program.methods
.depositLiquidity(depositAmountA, depositAmountB)
.depositLiquidity(depositAmountA2, depositAmountB2)
.accounts({
pool: values.poolKey,
poolAuthority: values.poolAuthority,
Expand All @@ -151,20 +149,18 @@ describe('Deposit liquidity', () => {
.rpc({ skipPreflight: true });

const depositTokenAccountLiquidity2 = await connection.getTokenAccountBalance(values.liquidityAccount);
expect(depositTokenAccountLiquidity2.value.amount).to.equal(new BN(40.5 * 10 ** 6)
.add(new BN(depositTokenAccountLiquidity.value.amount))
.toString());
expect(depositTokenAccountLiquidity2.value.amount).to.equal(
new BN(40.5 * 10 ** 6).add(new BN(depositTokenAccountLiquidity.value.amount)).toString(),
);
const depositTokenAccountA2 = await connection.getTokenAccountBalance(values.holderAccountA);
expect(depositTokenAccountA2.value.amount).to.equal(new BN (depositTokenAccountA.value.amount)
.sub(new BN(60.75 * 10 ** 6)).toString());
expect(depositTokenAccountA2.value.amount).to.equal(new BN(depositTokenAccountA.value.amount).sub(new BN(60.75 * 10 ** 6)).toString());
const depositTokenAccountB2 = await connection.getTokenAccountBalance(values.holderAccountB);
expect(depositTokenAccountB2.value.amount).to.equal(new BN (depositTokenAccountB.value.amount)
.sub(depositAmountB).toString());
expect(depositTokenAccountB2.value.amount).to.equal(new BN(depositTokenAccountB.value.amount).sub(depositAmountB2).toString());
});

it('Deposit amounts a < b, then a > b', async () => {
var depositAmountA = new BN(4 * 10 ** 6)
var depositAmountB = new BN(9 * 10 ** 6)
const depositAmountA = new BN(4 * 10 ** 6);
const depositAmountB = new BN(9 * 10 ** 6);
await program.methods
.depositLiquidity(depositAmountA, depositAmountB)
.accounts({
Expand All @@ -184,8 +180,7 @@ describe('Deposit liquidity', () => {
.rpc({ skipPreflight: true });

const depositTokenAccountLiquidity = await connection.getTokenAccountBalance(values.liquidityAccount);
expect(depositTokenAccountLiquidity.value.amount).to.equal(new BN(6 * 10 ** 6)
.sub(values.minimumLiquidity).toString());
expect(depositTokenAccountLiquidity.value.amount).to.equal(new BN(6 * 10 ** 6).sub(values.minimumLiquidity).toString());
const depositTokenAccountA = await connection.getTokenAccountBalance(values.holderAccountA);
expect(depositTokenAccountA.value.amount).to.equal(values.defaultHolderAccountSupply.sub(depositAmountA).toString());
const depositTokenAccountB = await connection.getTokenAccountBalance(values.holderAccountB);
Expand All @@ -194,10 +189,10 @@ describe('Deposit liquidity', () => {
// Expected behavior is that depositAmountB gets increased to
// (27 * 10 ** 6) * (9/4) = 60.75 * 10 ** 6
// to maintain the ratio established in the above deposit
depositAmountA = new BN(27 * 10 ** 6)
depositAmountB = new BN(18 * 10 ** 6)
const depositAmountA2 = new BN(27 * 10 ** 6);
const depositAmountB2 = new BN(18 * 10 ** 6);
await program.methods
.depositLiquidity(depositAmountA, depositAmountB)
.depositLiquidity(depositAmountA2, depositAmountB2)
.accounts({
pool: values.poolKey,
poolAuthority: values.poolAuthority,
Expand All @@ -215,14 +210,12 @@ describe('Deposit liquidity', () => {
.rpc({ skipPreflight: true });

const depositTokenAccountLiquidity2 = await connection.getTokenAccountBalance(values.liquidityAccount);
expect(depositTokenAccountLiquidity2.value.amount).to.equal(new BN(40.5 * 10 ** 6)
.add(new BN(depositTokenAccountLiquidity.value.amount))
.toString());
expect(depositTokenAccountLiquidity2.value.amount).to.equal(
new BN(40.5 * 10 ** 6).add(new BN(depositTokenAccountLiquidity.value.amount)).toString(),
);
const depositTokenAccountA2 = await connection.getTokenAccountBalance(values.holderAccountA);
expect(depositTokenAccountA2.value.amount).to.equal(new BN (depositTokenAccountA.value.amount)
.sub(depositAmountA).toString());
expect(depositTokenAccountA2.value.amount).to.equal(new BN(depositTokenAccountA.value.amount).sub(depositAmountA2).toString());
const depositTokenAccountB2 = await connection.getTokenAccountBalance(values.holderAccountB);
expect(depositTokenAccountB2.value.amount).to.equal(new BN (depositTokenAccountB.value.amount)
.sub(new BN(60.75 * 10 ** 6)).toString());
expect(depositTokenAccountB2.value.amount).to.equal(new BN(depositTokenAccountB.value.amount).sub(new BN(60.75 * 10 ** 6)).toString());
})
});
25 changes: 9 additions & 16 deletions tokens/token-swap/anchor/tests/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,16 @@ describe('Swap', () => {

const traderTokenAccountA = await connection.getTokenAccountBalance(values.holderAccountA);
const traderTokenAccountB = await connection.getTokenAccountBalance(values.holderAccountB);
expect(traderTokenAccountA.value.amount).to.equal(values.defaultHolderAccountSupply
.sub(values.depositAmountA)
.sub(input).toString());
expect(Number(traderTokenAccountB.value.amount)).to.be.greaterThan(values.defaultHolderAccountSupply
.sub(values.depositAmountB).toNumber());
expect(Number(traderTokenAccountB.value.amount)).to.be.lessThan(values.defaultHolderAccountSupply
.sub(values.depositAmountB)
.add(input).toNumber());
expect(traderTokenAccountA.value.amount).to.equal(values.defaultHolderAccountSupply.sub(values.depositAmountA).sub(input).toString());
expect(Number(traderTokenAccountB.value.amount)).to.be.greaterThan(values.defaultHolderAccountSupply.sub(values.depositAmountB).toNumber());
expect(Number(traderTokenAccountB.value.amount)).to.be.lessThan(
values.defaultHolderAccountSupply.sub(values.depositAmountB).add(input).toNumber(),
);
});

it('Swap from a to b, initial deposit a < b', async () => {
const depositAmountA = new BN(10 * 10 ** 6)
const depositAmountB = new BN(30 * 10 ** 6)
const depositAmountA = new BN(10 * 10 ** 6);
const depositAmountB = new BN(30 * 10 ** 6);
await program.methods
.depositLiquidity(depositAmountA, depositAmountB)
.accounts({
Expand Down Expand Up @@ -130,11 +127,7 @@ describe('Swap', () => {

const traderTokenAccountA = await connection.getTokenAccountBalance(values.holderAccountA);
const traderTokenAccountB = await connection.getTokenAccountBalance(values.holderAccountB);
expect(traderTokenAccountA.value.amount).to.equal(values.defaultHolderAccountSupply
.sub(depositAmountA)
.sub(input).toString());
expect(Number(traderTokenAccountB.value.amount)).to.be.greaterThan(values.defaultHolderAccountSupply
.sub(depositAmountB)
.add(input).toNumber());
expect(traderTokenAccountA.value.amount).to.equal(values.defaultHolderAccountSupply.sub(depositAmountA).sub(input).toString());
expect(Number(traderTokenAccountB.value.amount)).to.be.greaterThan(values.defaultHolderAccountSupply.sub(depositAmountB).add(input).toNumber());
});
});
4 changes: 2 additions & 2 deletions tokens/token-swap/anchor/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export const expectRevert = async (promise: Promise<any>) => {
}
};

const defaultMintedAmount = 100
const defaultDecimals = 6
const defaultMintedAmount = 100;
const defaultDecimals = 6;
export const mintingTokens = async ({
connection,
creator,
Expand Down

0 comments on commit 80b60e7

Please sign in to comment.