Skip to content

Commit

Permalink
test: token 2022 util
Browse files Browse the repository at this point in the history
  • Loading branch information
codewithgun committed Jan 3, 2025
1 parent af5d176 commit 0be4b86
Show file tree
Hide file tree
Showing 12 changed files with 594 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ program = "./artifacts/token_2022.so"
address = "LbVRzDTvBDEcrthxfZ4RL6yiq3uZw8bS6MwtdY6UhFQ"
program = "./artifacts/lb_clmm.so"

[[test.genesis]]
address = "EBZDYx7599krFc4m2govwBdZcicr4GgepqC78m71nsHS"
program = "./artifacts/transfer_hook_counter.so"

[scripts]
test = "yarn run ts-mocha --sort --type-check --bail -p ./tsconfig.json -t 1000000 tests/*.ts"
build-local = "anchor build"
Expand Down
Binary file modified artifacts/token_2022.so
Binary file not shown.
Binary file added artifacts/transfer_hook_counter.so
Binary file not shown.
3 changes: 2 additions & 1 deletion ts-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"scripts": {
"build": "tsup",
"start": "npm run build -- --watch",
"test": "jest 'src/test/sdk.test.ts'",
"test": "jest 'src/test/(sdk|token_2022).test.ts'",
"debug": "jest src/test/sdk_token2022.test.ts",
"unit-test": "jest src/test/calculate_distribution.test.ts",
"example": "dotenv -e .env npx ts-node src/example.ts",
"start-server": "npx tsc && node dist/src/server/index.js"
Expand Down
8 changes: 5 additions & 3 deletions ts-client/src/dlmm/helpers/token_2022.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export async function getExtraAccountMetasForTransferHook(
mintAccountInfo: AccountInfo<Buffer>
) {
if (
![TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID].includes(mintAccountInfo.owner)
![TOKEN_PROGRAM_ID.toBase58(), TOKEN_2022_PROGRAM_ID.toBase58()].includes(
mintAccountInfo.owner.toBase58()
)
) {
return [];
}
Expand Down Expand Up @@ -143,7 +145,7 @@ function calculateInverseFee(transferFee: TransferFee, postFeeAmount: BN) {
);
}

interface transferFeeIncludedAmount {
interface TransferFeeIncludedAmount {
amount: BN;
transferFee: BN;
}
Expand All @@ -152,7 +154,7 @@ export function calculateTransferFeeIncludedAmount(
transferFeeExcludedAmount: BN,
mint: Mint,
currentEpoch: number
): TransferFeeExcludedAmount {
): TransferFeeIncludedAmount {
if (transferFeeExcludedAmount.isZero()) {
return {
amount: new BN(0),
Expand Down
2 changes: 0 additions & 2 deletions ts-client/src/test/calculate_distribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import {
Transaction,
} from "@solana/web3.js";
import babar from "babar";
import Decimal from "decimal.js";
import fs from "fs";
import {
calculateBidAskDistribution,
calculateNormalDistribution,
calculateSpotDistribution,
getPriceOfBinByBinId,
toAmountsBothSideByStrategy,
toAmountsOneSideByStrategy,
toWeightDistribution,
Expand Down
34 changes: 34 additions & 0 deletions ts-client/src/test/external/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Program, web3 } from "@coral-xyz/anchor";
import { TransferHookCounter } from "./transfer_hook_counter";
import { getExtraAccountMetaAddress } from "@solana/spl-token";

export async function createExtraAccountMetaListAndCounter(
program: Program<TransferHookCounter>,
mint: web3.PublicKey
) {
const extraAccountMetaList = getExtraAccountMetaAddress(
mint,
program.programId
);
const counterAccount = deriveCounter(mint, program.programId);

await program.methods
.initializeExtraAccountMetaList()
.accounts({
mint,
counterAccount,
extraAccountMetaList,
})
.rpc();

return [extraAccountMetaList, counterAccount];
}

export function deriveCounter(mint: web3.PublicKey, programId: web3.PublicKey) {
const [counter] = web3.PublicKey.findProgramAddressSync(
[Buffer.from("counter"), mint.toBuffer()],
programId
);

return counter;
}
28 changes: 28 additions & 0 deletions ts-client/src/test/external/program.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { AnchorProvider, Program, Wallet, web3 } from "@coral-xyz/anchor";
import {
TransferHookCounter,
IDL as TransferHookCounterIDL,
} from "./transfer_hook_counter";
import { Connection } from "@solana/web3.js";

export const TRANSFER_HOOK_COUNTER_PROGRAM_ID = new web3.PublicKey(
"EBZDYx7599krFc4m2govwBdZcicr4GgepqC78m71nsHS"
);

export function createTransferHookCounterProgram(
wallet: Wallet,
programId: web3.PublicKey,
connection: Connection
): Program<TransferHookCounter> {
const provider = new AnchorProvider(connection, wallet, {
maxRetries: 3,
});

const program = new Program<TransferHookCounter>(
TransferHookCounterIDL,
programId,
provider
);

return program;
}
219 changes: 219 additions & 0 deletions ts-client/src/test/external/transfer_hook_counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
export type TransferHookCounter = {
"version": "0.1.0",
"name": "transfer_hook_counter",
"instructions": [
{
"name": "initializeExtraAccountMetaList",
"accounts": [
{
"name": "payer",
"isMut": true,
"isSigner": true
},
{
"name": "extraAccountMetaList",
"isMut": true,
"isSigner": false
},
{
"name": "mint",
"isMut": false,
"isSigner": false
},
{
"name": "counterAccount",
"isMut": true,
"isSigner": false
},
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "associatedTokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
}
],
"args": []
},
{
"name": "transferHook",
"accounts": [
{
"name": "sourceToken",
"isMut": false,
"isSigner": false
},
{
"name": "mint",
"isMut": false,
"isSigner": false
},
{
"name": "destinationToken",
"isMut": false,
"isSigner": false
},
{
"name": "owner",
"isMut": false,
"isSigner": false
},
{
"name": "extraAccountMetaList",
"isMut": false,
"isSigner": false
},
{
"name": "counterAccount",
"isMut": true,
"isSigner": false
}
],
"args": [
{
"name": "amount",
"type": "u64"
}
]
}
],
"accounts": [
{
"name": "counterAccount",
"type": {
"kind": "struct",
"fields": [
{
"name": "counter",
"type": "u32"
}
]
}
}
],
"errors": [
{
"code": 6000,
"name": "AmountTooBig",
"msg": "The amount is too big"
}
]
};

export const IDL: TransferHookCounter = {
"version": "0.1.0",
"name": "transfer_hook_counter",
"instructions": [
{
"name": "initializeExtraAccountMetaList",
"accounts": [
{
"name": "payer",
"isMut": true,
"isSigner": true
},
{
"name": "extraAccountMetaList",
"isMut": true,
"isSigner": false
},
{
"name": "mint",
"isMut": false,
"isSigner": false
},
{
"name": "counterAccount",
"isMut": true,
"isSigner": false
},
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "associatedTokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
}
],
"args": []
},
{
"name": "transferHook",
"accounts": [
{
"name": "sourceToken",
"isMut": false,
"isSigner": false
},
{
"name": "mint",
"isMut": false,
"isSigner": false
},
{
"name": "destinationToken",
"isMut": false,
"isSigner": false
},
{
"name": "owner",
"isMut": false,
"isSigner": false
},
{
"name": "extraAccountMetaList",
"isMut": false,
"isSigner": false
},
{
"name": "counterAccount",
"isMut": true,
"isSigner": false
}
],
"args": [
{
"name": "amount",
"type": "u64"
}
]
}
],
"accounts": [
{
"name": "counterAccount",
"type": {
"kind": "struct",
"fields": [
{
"name": "counter",
"type": "u32"
}
]
}
}
],
"errors": [
{
"code": 6000,
"name": "AmountTooBig",
"msg": "The amount is too big"
}
]
};
4 changes: 1 addition & 3 deletions ts-client/src/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
transfer,
} from "@solana/spl-token";
import {
ComputeBudgetProgram,
Connection,
Keypair,
LAMPORTS_PER_SOL,
Expand All @@ -20,7 +19,6 @@ import {
import Decimal from "decimal.js";
import fs from "fs";
import {
BASIS_POINT_MAX,
DEFAULT_BIN_PER_POSITION,
LBCLMM_PROGRAM_IDS,
} from "../dlmm/constants";
Expand All @@ -35,6 +33,7 @@ import {
findSwappableMinMaxBinId,
getQPriceFromId,
} from "../dlmm/helpers/math";
import { DynamicPosition } from "../dlmm/helpers/positions";
import { IDL } from "../dlmm/idl";
import { DLMM } from "../dlmm/index";
import {
Expand All @@ -43,7 +42,6 @@ import {
ResizeSide,
StrategyType,
} from "../dlmm/types";
import { DynamicPosition, wrapPosition } from "../dlmm/helpers/positions";

const keypairBuffer = fs.readFileSync(
"../keys/localnet/admin-bossj3JvwiNK7pvjr149DqdtJxf2gdygbcmEPTkb2F1.json",
Expand Down
Empty file.
Loading

0 comments on commit 0be4b86

Please sign in to comment.