diff --git a/tokens/token-swap/poseidon/token_swap/migrations/deploy.ts b/tokens/token-swap/poseidon/token_swap/migrations/deploy.ts index 82fb175f..64a1c359 100644 --- a/tokens/token-swap/poseidon/token_swap/migrations/deploy.ts +++ b/tokens/token-swap/poseidon/token_swap/migrations/deploy.ts @@ -2,9 +2,9 @@ // single deploy script that's invoked from the CLI, injecting a provider // configured from the workspace's Anchor.toml. -const anchor = require("@coral-xyz/anchor"); +const anchor = require('@coral-xyz/anchor'); -module.exports = async function (provider) { +module.exports = async (provider) => { // Configure client to use the provider. anchor.setProvider(provider); diff --git a/tokens/token-swap/poseidon/token_swap/tests/create-amm.ts b/tokens/token-swap/poseidon/token_swap/tests/create-amm.ts index c0210b28..e452fd5f 100644 --- a/tokens/token-swap/poseidon/token_swap/tests/create-amm.ts +++ b/tokens/token-swap/poseidon/token_swap/tests/create-amm.ts @@ -1,22 +1,18 @@ -import * as anchor from "@coral-xyz/anchor"; -import type { Program } from "@coral-xyz/anchor"; -import { expect } from "chai"; -import type { TokenSwap } from "../target/types/token_swap"; -import { type TestValues, createValues, expectRevert } from "./utils"; -import { PublicKey } from "@solana/web3.js"; -import { startAnchor } from "solana-bankrun"; -import { BankrunProvider } from "anchor-bankrun"; - -const IDL = require("../target/idl/token_swap.json"); +import * as anchor from '@coral-xyz/anchor'; +import type { Program } from '@coral-xyz/anchor'; +import { PublicKey } from '@solana/web3.js'; +import { BankrunProvider } from 'anchor-bankrun'; +import { expect } from 'chai'; +import { startAnchor } from 'solana-bankrun'; +import type { TokenSwap } from '../target/types/token_swap'; +import { type TestValues, createValues, expectRevert } from './utils'; + +const IDL = require('../target/idl/token_swap.json'); const PROGRAM_ID = new PublicKey(IDL.address); -describe("Create AMM", async () => { +describe('Create AMM', async () => { // Configure the client to use the anchor-bankrun - const context = await startAnchor( - "", - [{ name: "token_swap", programId: PROGRAM_ID }], - [] - ); + const context = await startAnchor('', [{ name: 'token_swap', programId: PROGRAM_ID }], []); const provider = new BankrunProvider(context); @@ -32,38 +28,33 @@ describe("Create AMM", async () => { values = createValues(); }); - it("Creation", async () => { + it('Creation', async () => { const id = new anchor.BN(values.id); const fee = values.fee; await program.methods .createAmm(id, fee) - .accounts({ - payer:payer.publicKey, + .accounts({ + payer: payer.publicKey, }) .rpc(); const ammAccount = await program.account.amm.fetch(values.ammKey); expect(ammAccount.id.toString()).to.equal(values.id.toString()); - expect(ammAccount.admin.toString()).to.equal( - values.admin.publicKey.toString() - ); + expect(ammAccount.admin.toString()).to.equal(values.admin.publicKey.toString()); expect(ammAccount.fee.toString()).to.equal(values.fee.toString()); }); - it("Invalid fee", async () => { + it('Invalid fee', async () => { const id = new anchor.BN(values.id); values.fee = 10000; await expectRevert( program.methods - .createAmm( - id, - values.fee - ) - .accounts({ - payer:payer.publicKey + .createAmm(id, values.fee) + .accounts({ + payer: payer.publicKey, }) - .rpc() + .rpc(), ); }); }); diff --git a/tokens/token-swap/poseidon/token_swap/tests/create-pool.ts b/tokens/token-swap/poseidon/token_swap/tests/create-pool.ts index 234af4a8..1731c33d 100644 --- a/tokens/token-swap/poseidon/token_swap/tests/create-pool.ts +++ b/tokens/token-swap/poseidon/token_swap/tests/create-pool.ts @@ -1,44 +1,35 @@ -import * as anchor from "@coral-xyz/anchor"; -import type { Program } from "@coral-xyz/anchor"; -import { PublicKey } from "@solana/web3.js"; -import type { TokenSwap } from "../target/types/token_swap"; -import { - type TestValues, - createValues, - expectRevert, - mintingTokens, -} from "./utils"; -import { startAnchor } from "solana-bankrun"; -import { BankrunProvider } from "anchor-bankrun"; +import * as anchor from '@coral-xyz/anchor'; +import type { Program } from '@coral-xyz/anchor'; +import { PublicKey } from '@solana/web3.js'; +import { BankrunProvider } from 'anchor-bankrun'; +import { startAnchor } from 'solana-bankrun'; +import type { TokenSwap } from '../target/types/token_swap'; +import { type TestValues, createValues, expectRevert, mintingTokens } from './utils'; -const IDL = require("../target/idl/token_swap.json"); +const IDL = require('../target/idl/token_swap.json'); const PROGRAM_ID = new PublicKey(IDL.address); -describe("Create pool", async () => { - const context = await startAnchor( - "", - [{ name: "token_swap", programId: PROGRAM_ID }], - [] - ); +describe('Create pool', async () => { + const context = await startAnchor('', [{ name: 'token_swap', programId: PROGRAM_ID }], []); - const provider = new BankrunProvider(context); + const provider = new BankrunProvider(context); -const connection = provider.connection; + const connection = provider.connection; - const payer = provider.wallet as anchor.Wallet; + const payer = provider.wallet as anchor.Wallet; - const program = new anchor.Program(IDL, provider); + const program = new anchor.Program(IDL, provider); let values: TestValues; beforeEach(async () => { values = createValues(); - const id = new anchor.BN(values.id) - const fee = values.fee + const id = new anchor.BN(values.id); + const fee = values.fee; await program.methods .createAmm(id, fee) .accounts({ - // admin: values.admin.publicKey + // admin: values.admin.publicKey }) .rpc(); @@ -50,7 +41,7 @@ const connection = provider.connection; }); }); - it("Creation", async () => { + it('Creation', async () => { const id = new anchor.BN(values.id); await program.methods .createPool(id) @@ -67,28 +58,19 @@ const connection = provider.connection; .rpc({ skipPreflight: true }); }); - it("Invalid mints", async () => { + it('Invalid mints', async () => { values = createValues({ mintBKeypair: values.mintAKeypair, poolKey: PublicKey.findProgramAddressSync( - [ - Buffer.alloc(values.id), - values.mintAKeypair.publicKey.toBuffer(), - values.mintBKeypair.publicKey.toBuffer(), - ], - program.programId + [Buffer.alloc(values.id), values.mintAKeypair.publicKey.toBuffer(), values.mintBKeypair.publicKey.toBuffer()], + program.programId, )[0], poolAuthority: PublicKey.findProgramAddressSync( - [ - Buffer.alloc(values.id), - values.mintAKeypair.publicKey.toBuffer(), - values.mintBKeypair.publicKey.toBuffer(), - Buffer.from("authority"), - ], - program.programId + [Buffer.alloc(values.id), values.mintAKeypair.publicKey.toBuffer(), values.mintBKeypair.publicKey.toBuffer(), Buffer.from('authority')], + program.programId, )[0], }); - const id = new anchor.BN(values.id); + const id = new anchor.BN(values.id); await expectRevert( program.methods .createPool(id) @@ -102,7 +84,7 @@ const connection = provider.connection; // poolAccountA: values.poolAccountA, // poolAccountB: values.poolAccountB, }) - .rpc() + .rpc(), ); }); }); diff --git a/tokens/token-swap/poseidon/token_swap/tests/utils.ts b/tokens/token-swap/poseidon/token_swap/tests/utils.ts index 1cf07046..eb513c7e 100644 --- a/tokens/token-swap/poseidon/token_swap/tests/utils.ts +++ b/tokens/token-swap/poseidon/token_swap/tests/utils.ts @@ -1,33 +1,20 @@ -import * as anchor from "@coral-xyz/anchor"; -import { - createMint, - getAssociatedTokenAddressSync, - getOrCreateAssociatedTokenAccount, - mintTo, -} from "@solana/spl-token"; -import { - type Connection, - Keypair, - PublicKey, - type Signer, -} from "@solana/web3.js"; -import { BN } from "bn.js"; -import { randomUUID } from "crypto"; +import * as anchor from '@coral-xyz/anchor'; +import { createMint, getAssociatedTokenAddressSync, getOrCreateAssociatedTokenAccount, mintTo } from '@solana/spl-token'; +import { type Connection, Keypair, PublicKey, type Signer } from '@solana/web3.js'; +import { BN } from 'bn.js'; export async function sleep(seconds: number) { new Promise((resolve) => setTimeout(resolve, seconds * 1000)); } export const generateSeededKeypair = (seed: string) => { - return Keypair.fromSeed( - anchor.utils.bytes.utf8.encode(anchor.utils.sha256.hash(seed)).slice(0, 32) - ); + return Keypair.fromSeed(anchor.utils.bytes.utf8.encode(anchor.utils.sha256.hash(seed)).slice(0, 32)); }; export const expectRevert = async (promise: Promise) => { try { await promise; - throw new Error("Expected a revert"); + throw new Error('Expected a revert'); } catch { return; } @@ -51,67 +38,31 @@ export const mintingTokens = async ({ decimals?: number; }) => { // Mint tokens - await connection.confirmTransaction( - await connection.requestAirdrop(creator.publicKey, 10 ** 10) - ); - await createMint( - connection, - creator, - creator.publicKey, - creator.publicKey, - decimals, - mintAKeypair - ); - await createMint( - connection, - creator, - creator.publicKey, - creator.publicKey, - decimals, - mintBKeypair - ); - await getOrCreateAssociatedTokenAccount( - connection, - holder, - mintAKeypair.publicKey, - holder.publicKey, - true - ); - await getOrCreateAssociatedTokenAccount( - connection, - holder, - mintBKeypair.publicKey, - holder.publicKey, - true - ); + await connection.confirmTransaction(await connection.requestAirdrop(creator.publicKey, 10 ** 10)); + await createMint(connection, creator, creator.publicKey, creator.publicKey, decimals, mintAKeypair); + await createMint(connection, creator, creator.publicKey, creator.publicKey, decimals, mintBKeypair); + await getOrCreateAssociatedTokenAccount(connection, holder, mintAKeypair.publicKey, holder.publicKey, true); + await getOrCreateAssociatedTokenAccount(connection, holder, mintBKeypair.publicKey, holder.publicKey, true); await mintTo( connection, creator, mintAKeypair.publicKey, - getAssociatedTokenAddressSync( - mintAKeypair.publicKey, - holder.publicKey, - true - ), + getAssociatedTokenAddressSync(mintAKeypair.publicKey, holder.publicKey, true), creator.publicKey, - mintedAmount * 10 ** decimals + mintedAmount * 10 ** decimals, ); await mintTo( connection, creator, mintBKeypair.publicKey, - getAssociatedTokenAddressSync( - mintBKeypair.publicKey, - holder.publicKey, - true - ), + getAssociatedTokenAddressSync(mintBKeypair.publicKey, holder.publicKey, true), creator.publicKey, - mintedAmount * 10 ** decimals + mintedAmount * 10 ** decimals, ); }; export interface TestValues { - id : number; + id: number; fee: number; admin: Keypair; mintAKeypair: Keypair; @@ -136,49 +87,31 @@ type TestValuesDefaults = { }; export function createValues(defaults?: TestValuesDefaults): TestValues { // const id = defaults?.id || Keypair.generate().publicKey; - const id = Math.random() + const id = Math.random(); const admin = Keypair.generate(); const ammKey = PublicKey.findProgramAddressSync( - [Buffer.alloc(id)], //create a buffer from the id - anchor.workspace.TokenSwap.programId + [Buffer.alloc(id)], //create a buffer from the id + anchor.workspace.TokenSwap.programId, )[0]; // Making sure tokens are in the right order const mintAKeypair = Keypair.generate(); let mintBKeypair = Keypair.generate(); - while ( - new BN(mintBKeypair.publicKey.toBytes()).lt( - new BN(mintAKeypair.publicKey.toBytes()) - ) - ) { + while (new BN(mintBKeypair.publicKey.toBytes()).lt(new BN(mintAKeypair.publicKey.toBytes()))) { mintBKeypair = Keypair.generate(); } const poolAuthority = PublicKey.findProgramAddressSync( - [ - ammKey.toBuffer(), - mintAKeypair.publicKey.toBuffer(), - mintBKeypair.publicKey.toBuffer(), - Buffer.from("authority"), - ], - anchor.workspace.TokenSwap.programId + [ammKey.toBuffer(), mintAKeypair.publicKey.toBuffer(), mintBKeypair.publicKey.toBuffer(), Buffer.from('authority')], + anchor.workspace.TokenSwap.programId, )[0]; const mintLiquidity = PublicKey.findProgramAddressSync( - [ - ammKey.toBuffer(), - mintAKeypair.publicKey.toBuffer(), - mintBKeypair.publicKey.toBuffer(), - Buffer.from("liquidity"), - ], - anchor.workspace.TokenSwap.programId + [ammKey.toBuffer(), mintAKeypair.publicKey.toBuffer(), mintBKeypair.publicKey.toBuffer(), Buffer.from('liquidity')], + anchor.workspace.TokenSwap.programId, )[0]; const poolKey = PublicKey.findProgramAddressSync( - [ - ammKey.toBuffer(), - mintAKeypair.publicKey.toBuffer(), - mintBKeypair.publicKey.toBuffer(), - ], - anchor.workspace.TokenSwap.programId + [ammKey.toBuffer(), mintAKeypair.publicKey.toBuffer(), mintBKeypair.publicKey.toBuffer()], + anchor.workspace.TokenSwap.programId, )[0]; return { id, @@ -190,31 +123,11 @@ export function createValues(defaults?: TestValuesDefaults): TestValues { mintLiquidity, poolKey, poolAuthority, - poolAccountA: getAssociatedTokenAddressSync( - mintAKeypair.publicKey, - poolAuthority, - true - ), - poolAccountB: getAssociatedTokenAddressSync( - mintBKeypair.publicKey, - poolAuthority, - true - ), - liquidityAccount: getAssociatedTokenAddressSync( - mintLiquidity, - admin.publicKey, - true - ), - holderAccountA: getAssociatedTokenAddressSync( - mintAKeypair.publicKey, - admin.publicKey, - true - ), - holderAccountB: getAssociatedTokenAddressSync( - mintBKeypair.publicKey, - admin.publicKey, - true - ), + poolAccountA: getAssociatedTokenAddressSync(mintAKeypair.publicKey, poolAuthority, true), + poolAccountB: getAssociatedTokenAddressSync(mintBKeypair.publicKey, poolAuthority, true), + liquidityAccount: getAssociatedTokenAddressSync(mintLiquidity, admin.publicKey, true), + holderAccountA: getAssociatedTokenAddressSync(mintAKeypair.publicKey, admin.publicKey, true), + holderAccountB: getAssociatedTokenAddressSync(mintBKeypair.publicKey, admin.publicKey, true), depositAmountA: new BN(4 * 10 ** 6), depositAmountB: new BN(1 * 10 ** 6), minimumLiquidity: new BN(100), diff --git a/tokens/token-swap/poseidon/token_swap/ts-programs/src/token_swap.ts b/tokens/token-swap/poseidon/token_swap/ts-programs/src/token_swap.ts index 3275a3d8..a6e77a10 100644 --- a/tokens/token-swap/poseidon/token_swap/ts-programs/src/token_swap.ts +++ b/tokens/token-swap/poseidon/token_swap/ts-programs/src/token_swap.ts @@ -1,35 +1,31 @@ import { Account, - Pubkey, - Signer, - u8, - u64, - u16, + AssociatedTokenAccount, Constraint, - PoseidonError, Mint, + PoseidonError, + Pubkey, + Seeds, + Signer, TokenAccount, - AssociatedTokenAccount, TokenProgram, - Seeds, i64, -} from "@solanaturbine/poseidon"; + u8, + u16, + u64, +} from '@solanaturbine/poseidon'; -//Poseidon cannot support custom instructions currently , so most of the amm logic has been commented out +//Poseidon cannot support custom instructions currently , so most of the amm logic has been commented out export default class tokenSwap { - static PROGRAM_ID = new Pubkey( - "3dDaJxmPcmQVfSx9rX4xHyP5rJvkwdKcNujcX2z9KB9h" - ); + static PROGRAM_ID = new Pubkey('3dDaJxmPcmQVfSx9rX4xHyP5rJvkwdKcNujcX2z9KB9h'); create_amm(payer: Signer, amm: AMM, admin: Admin, id: u64, fee: u16) { amm .derive([id.toBytes()]) //Custom constraints don't transpile to corresponding anchor constraints yet - .constraints([ - new Constraint(fee < new u16(10000), new PoseidonError("invalid fee")), - ]) + .constraints([new Constraint(fee < new u16(10000), new PoseidonError('invalid fee'))]) .init(); - admin.derive(["admin"]).init(); + admin.derive(['admin']).init(); amm.id = id; amm.admin = admin.key; amm.fee = fee; @@ -45,14 +41,12 @@ export default class tokenSwap { mint_liquidity: Mint, mint_a: Mint, mint_b: Mint, - id: u64 + id: u64, ) { amm.derive([id.toBytes()]).init(); pool.derive([amm.key, mint_a.key, mint_b.key]).init(); - pool_authority.derive([amm.key, mint_a.key, mint_b.key, "authority"]); - mint_liquidity - .derive([amm.key, mint_a.key, mint_b.key, "liquidity"]) - .init(); + pool_authority.derive([amm.key, mint_a.key, mint_b.key, 'authority']); + mint_liquidity.derive([amm.key, mint_a.key, mint_b.key, 'liquidity']).init(); pool_account_a.derive(mint_a, pool_authority.key).init(); pool_account_b.derive(mint_b, pool_authority.key).init(); @@ -77,15 +71,13 @@ export default class tokenSwap { mint_a: Mint, mint_b: Mint, amount_a: u64, - amount_b: u64 + amount_b: u64, ) { pool.derive([amm.key, mint_a.key, mint_b.key]).has([mint_a, mint_b]); - pool_authority.derive([amm.key, mint_a.key, mint_b.key, "authority"]); + pool_authority.derive([amm.key, mint_a.key, mint_b.key, 'authority']); - mint_liquidity - .derive([amm.key, mint_a.key, mint_b.key, "liquidity"]) - .init(); + mint_liquidity.derive([amm.key, mint_a.key, mint_b.key, 'liquidity']).init(); pool_account_a.derive(mint_a, pool_authority.key).init(); pool_account_b.derive(mint_b, pool_authority.key).init(); @@ -189,11 +181,11 @@ export default class tokenSwap { // swap_a:bool input_amount: u64, min_input_amount: u64, - id: u64 + id: u64, ) { amm.derive([id.toBytes()]); pool.derive([amm.key, mint_a.key, mint_b.key]).has([amm, mint_a, mint_b]); - pool_authority.derive([amm.key, mint_a.key, mint_b.key, "authority"]); + pool_authority.derive([amm.key, mint_a.key, mint_b.key, 'authority']); pool_account_a.derive(mint_a, pool_authority.key).init(); pool_account_b.derive(mint_b, pool_authority.key).init(); trader_account_a.derive(mint_a, trader.key).init(); @@ -293,11 +285,11 @@ export default class tokenSwap { mint_a: Mint, mint_b: Mint, amount: u64, - id: u64 + id: u64, ) { amm.derive([id.toBytes()]); pool.derive([amm.key, mint_a.key, mint_b.key]).has([mint_a, mint_b]); - pool_authority.derive([amm.key, mint_a.key, mint_b.key, "authority"]); + pool_authority.derive([amm.key, mint_a.key, mint_b.key, 'authority']); pool_account_a.derive(mint_a, pool_authority.key).init(); pool_account_b.derive(mint_b, pool_authority.key).init(); @@ -348,7 +340,7 @@ export default class tokenSwap { } export interface AMM extends Account { - /// The primary key of the AMM + /// The primary key of the AMM id: u64; /// Account that has admin authority over the AMM @@ -374,7 +366,4 @@ export interface Pool extends Account { export interface Admin extends Account {} //Read only authority -export interface PoolAuthority extends Account { - -} - +export interface PoolAuthority extends Account {}