Skip to content

Commit

Permalink
Merge pull request metaDAOproject#168 from metaproph3t/test/improve-i…
Browse files Browse the repository at this point in the history
…ntegration-tests

Improve integration tests
  • Loading branch information
metaproph3t committed Apr 23, 2024
2 parents 6af64b9 + 98eebaa commit 1184a36
Show file tree
Hide file tree
Showing 20 changed files with 2,213 additions and 3,372 deletions.
83 changes: 63 additions & 20 deletions app/src/AmmClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AnchorProvider, Program } from "@coral-xyz/anchor";
import { AnchorProvider, IdlTypes, Program } from "@coral-xyz/anchor";
import { AddressLookupTableAccount, Keypair, PublicKey } from "@solana/web3.js";

import { Amm as AmmIDLType, IDL as AmmIDL } from "./types/amm";
Expand All @@ -7,7 +7,10 @@ import * as ixs from "./instructions/amm";
import BN from "bn.js";
import { AMM_PROGRAM_ID } from "./constants";
import { Amm, AmmWrapper } from "./types";
import { getATA, getAmmLpMintAddr } from "./utils/pda";
import { getATA, getAmmLpMintAddr, getAmmAddr } from "./utils/pda";
import { MethodsBuilder } from "@coral-xyz/anchor/dist/cjs/program/namespace/methods";

export type SwapType = IdlTypes<AmmIDLType>["SwapType"];

export type CreateAmmClientParams = {
provider: AnchorProvider;
Expand Down Expand Up @@ -43,22 +46,55 @@ export class AmmClient {
return this.program.programId;
}

// both twap values need to be scaled beforehand
createAmm(
async createAmm(
baseMint: PublicKey,
quoteMint: PublicKey,
twapInitialObservation: BN,
twapMaxObservationChangePerUpdate: BN,
proposal: PublicKey
) {
return ixs.createAmmHandler(
this,
): Promise<PublicKey> {
let [amm] = getAmmAddr(this.getProgramId(), baseMint, quoteMint, proposal);

await this.createAmmIx(
baseMint,
quoteMint,
twapInitialObservation,
twapMaxObservationChangePerUpdate,
proposal
);
).rpc();

return amm;
}

// both twap values need to be scaled beforehand
createAmmIx(
baseMint: PublicKey,
quoteMint: PublicKey,
twapInitialObservation: BN,
twapMaxObservationChangePerUpdate: BN,
proposal: PublicKey
): MethodsBuilder<AmmIDLType, any> {
let [amm] = getAmmAddr(this.getProgramId(), baseMint, quoteMint, proposal);
let [lpMint] = getAmmLpMintAddr(this.getProgramId(), amm);

let [vaultAtaBase] = getATA(baseMint, amm);
let [vaultAtaQuote] = getATA(quoteMint, amm);

return this.program.methods
.createAmm({
twapInitialObservation,
twapMaxObservationChangePerUpdate,
proposal,
})
.accounts({
user: this.provider.publicKey,
amm,
lpMint,
baseMint,
quoteMint,
vaultAtaBase,
vaultAtaQuote,
});
}

async addLiquidity(
Expand Down Expand Up @@ -106,7 +142,7 @@ export class AmmClient {
maxBaseAmount,
maxQuoteAmount,
minBaseAmount,
minQuoteAmount
minQuoteAmount,
})
.accounts({
user,
Expand Down Expand Up @@ -156,19 +192,26 @@ export class AmmClient {
ammAddr: PublicKey,
baseMint: PublicKey,
quoteMint: PublicKey,
isQuoteToBase: boolean,
swapType: SwapType,
inputAmount: BN,
minOutputAmount: BN
outputAmountMin: BN
) {
return ixs.swapHandler(
this,
ammAddr,
baseMint,
quoteMint,
isQuoteToBase,
inputAmount,
minOutputAmount
);
return this.program.methods
.swap({
swapType,
inputAmount,
outputAmountMin,
})
.accounts({
user: this.provider.publicKey,
amm: ammAddr,
baseMint,
quoteMint,
userAtaBase: getATA(baseMint, this.provider.publicKey)[0],
userAtaQuote: getATA(quoteMint, this.provider.publicKey)[0],
vaultAtaBase: getATA(baseMint, ammAddr)[0],
vaultAtaQuote: getATA(quoteMint, ammAddr)[0],
});
}

async crankThatTwap(amm: PublicKey) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/AutocratClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export class AutocratClient {
);

await this.ammClient
.createAmm(
.createAmmIx(
passBase,
passQuote,
storedDao.twapInitialObservation,
Expand All @@ -319,7 +319,7 @@ export class AutocratClient {
.rpc();

await this.ammClient
.createAmm(
.createAmmIx(
failBase,
failQuote,
storedDao.twapInitialObservation,
Expand Down
5 changes: 1 addition & 4 deletions app/src/idl/amm.json
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,5 @@
"name": "ZeroLiquidityToAdd",
"msg": "Cannot add liquidity with 0 tokens on either side"
}
],
"metadata": {
"address": "Ens7Gx99whnA8zZm6ZiFnWgGq3x76nXbSmh5gaaJqpAz"
}
]
}
5 changes: 1 addition & 4 deletions app/src/idl/autocrat.json
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,5 @@
"name": "InsufficientLpTokenLock",
"msg": "The LP tokens passed in have less liquidity than the DAO's `min_quote_futarchic_liquidity` or `min_base_futachic_liquidity`"
}
],
"metadata": {
"address": "FuTPR6ScKMPHtZFwacq9qrtf9VjscawNEFTb2wSYr1gY"
}
]
}
5 changes: 1 addition & 4 deletions app/src/idl/autocrat_migrator.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,5 @@
],
"args": []
}
],
"metadata": {
"address": "MigRDW6uxyNMDBD8fX2njCRyJC4YZk2Rx9pDUZiAESt"
}
]
}
5 changes: 1 addition & 4 deletions app/src/idl/conditional_vault.json
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,5 @@
"name": "VaultAlreadySettled",
"msg": "Once a vault has been settled, its status as either finalized or reverted cannot be changed"
}
],
"metadata": {
"address": "vAuLTQjV5AZx5f3UgE75wcnkxnQowWxThn1hGjfCVwP"
}
]
}
7 changes: 6 additions & 1 deletion app/src/instructions/amm/addLiquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export const addLiquidityHandler = (
const [lpMint] = getAmmLpMintAddr(client.program.programId, ammAddr);

return client.program.methods
.addLiquidity({maxBaseAmount, maxQuoteAmount, minBaseAmount, minQuoteAmount})
.addLiquidity({
maxBaseAmount,
maxQuoteAmount,
minBaseAmount,
minQuoteAmount,
})
.accounts({
user: client.provider.publicKey,
amm: ammAddr,
Expand Down
43 changes: 0 additions & 43 deletions app/src/instructions/amm/createAmm.ts

This file was deleted.

2 changes: 0 additions & 2 deletions app/src/instructions/amm/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from "./addLiquidity";
export * from "./createAmm";
export * from "./swap";
34 changes: 0 additions & 34 deletions app/src/instructions/amm/swap.ts

This file was deleted.

Loading

0 comments on commit 1184a36

Please sign in to comment.