Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing tests #30

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/anchor-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Anchor Tests

on:
push:
pull_request:

jobs:
run-anchor-test:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- uses: metadaoproject/[email protected]
with:
anchor-version: "0.29.0"
solana-cli-version: "1.17.0"
9 changes: 5 additions & 4 deletions Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ url = "https://github.com/raydium-io/raydium-cp-swap"

[provider]
cluster = "Localnet"
wallet = "~/.config/solana/id.json"
wallet = "./admin.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
Expand All @@ -28,10 +28,11 @@ test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
startup_wait = 10000

[test.validator]
url = "https://api.mainnet-beta.solana.com"

[[test.validator.clone]]
[[test.validator.account]]
address = "DNXgeM9EiiaAbaWvwjHj9fQQLAX5ZsfHyvmYUNRAdNC8"
filename = "DNXgeM9EiiaAbaWvwjHj9fQQLAX5ZsfHyvmYUNRAdNC8.json"

[[test.validator.clone]]
[[test.validator.account]]
address = "D4FPEruKEHrG5TenZ2mpDGEfu1iUvTiqBxvpU8HLBvC2"
filename = "D4FPEruKEHrG5TenZ2mpDGEfu1iUvTiqBxvpU8HLBvC2.json"
14 changes: 14 additions & 0 deletions D4FPEruKEHrG5TenZ2mpDGEfu1iUvTiqBxvpU8HLBvC2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pubkey": "D4FPEruKEHrG5TenZ2mpDGEfu1iUvTiqBxvpU8HLBvC2",
"account": {
"lamports": 2533440,
"data": [
"2vQhaMvLK2//AAAAxAkAAAAAAADA1AEAAAAAAECcAAAAAAAAgNHwCAAAAAAF2xU6D/SH9pzeiQAf7ExGlbbUIV6iRLh71Otyh/hXqNcGSjj20XsRPl9m3PtjuHN//OJkSr8KALmLNDVtY4WDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
"base64"
],
"owner": "CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C",
"executable": false,
"rentEpoch": 18446744073709551615,
"space": 236
}
}
14 changes: 14 additions & 0 deletions DNXgeM9EiiaAbaWvwjHj9fQQLAX5ZsfHyvmYUNRAdNC8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pubkey": "DNXgeM9EiiaAbaWvwjHj9fQQLAX5ZsfHyvmYUNRAdNC8",
"account": {
"lamports": 1478452039280,
"data": [
"BpuIV/6rgYT7aH9jRhjANdrEOdwa6ztVmKDwAAAAAAHltitlyzu9pvVoiOZv7o5k3FVgGZwPiLEf4nO9BZ6KoYCwfDpYAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQEAAADwHR8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"base64"
],
"owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
"executable": false,
"rentEpoch": 18446744073709551615,
"space": 165
}
}
1 change: 1 addition & 0 deletions admin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[86,75,7,11,40,38,40,221,224,89,41,224,93,255,97,46,159,244,189,187,75,107,137,15,29,195,154,246,224,164,98,126,164,65,160,179,116,121,185,20,178,223,241,97,225,205,147,101,108,236,205,101,142,248,207,174,168,71,73,191,106,80,147,135]
2 changes: 1 addition & 1 deletion programs/cp-swap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub mod admin {
#[cfg(feature = "devnet")]
declare_id!("adMCyoCgfkg7bQiJ9aBJ59H3BXLY3r5LNLfPpQfMzBe");
#[cfg(not(feature = "devnet"))]
declare_id!("GThUX1Atko4tqhN2NaiTazWSeFWMuiUvfFnyJyUghFMJ");
declare_id!("C4BuRr1NhEvncwi67MfruMGeRJDgGp3yh4vueyN8LvJr");
}

pub mod create_pool_fee_reveiver {
Expand Down
23 changes: 22 additions & 1 deletion tests/swap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ import { RaydiumCpSwap } from "../target/types/raydium_cp_swap";
import { setupSwapTest, swap_base_input, swap_base_output } from "./utils";
import { assert } from "chai";
import { getAccount, getAssociatedTokenAddressSync } from "@solana/spl-token";
import { Connection } from "@solana/web3.js";

export async function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export async function waitForNextBlock(connection: Connection) {
const slot = await connection.getSlot();
console.log("Current slot:", slot);

// Wait for next block
while ((await connection.getSlot()) <= slot) {
await sleep(100);
}
console.log("New slot:", await connection.getSlot());
}

describe("swap test", () => {
anchor.setProvider(anchor.AnchorProvider.env());
Expand All @@ -29,6 +45,8 @@ describe("swap test", () => {
},
{ transferFeeBasisPoints: 0, MaxFee: 0 }
);
await waitForNextBlock(program.provider.connection);

const inputToken = poolState.token0Mint;
const inputTokenProgram = poolState.token0Program;
const inputTokenAccountAddr = getAssociatedTokenAddressSync(
Expand All @@ -53,7 +71,8 @@ describe("swap test", () => {
poolState.token1Mint,
poolState.token1Program,
amount_in,
new BN(0)
new BN(0),
confirmOptions
);
const inputTokenAccountAfter = await getAccount(
anchor.getProvider().connection,
Expand Down Expand Up @@ -81,6 +100,7 @@ describe("swap test", () => {
},
{ transferFeeBasisPoints: 0, MaxFee: 0 }
);
await waitForNextBlock(program.provider.connection);
const inputToken = poolState.token0Mint;
const inputTokenProgram = poolState.token0Program;
const inputTokenAccountAddr = getAssociatedTokenAddressSync(
Expand Down Expand Up @@ -143,6 +163,7 @@ describe("swap test", () => {
},
transferFeeConfig
);
await waitForNextBlock(program.provider.connection);

const inputToken = poolState.token0Mint;
const inputTokenProgram = poolState.token0Program;
Expand Down
28 changes: 19 additions & 9 deletions tests/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ import {
} from "@solana/spl-token";
import { sendTransaction } from "./index";

function compareByteArrays(a: Uint8Array, b: Uint8Array): number {
if (a.length !== b.length) {
throw new Error(
"Pubkeys should always have the same length of byte arrays"
);
}

// Compare elements
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return a[i] - b[i];
}

// Arrays are equal
return 0;
}

// create a token mint and a token2022 mint with transferFeeConfig
export async function createTokenMintAndAssociatedTokenAccount(
connection: Connection,
Expand Down Expand Up @@ -67,15 +83,9 @@ export async function createTokenMintAndAssociatedTokenAccount(

tokenArray.push({ address: token1, program: TOKEN_2022_PROGRAM_ID });

tokenArray.sort(function (x, y) {
if (x.address < y.address) {
return -1;
}
if (x.address > y.address) {
return 1;
}
return 0;
});
tokenArray.sort((x, y) =>
compareByteArrays(x.address.toBytes(), y.address.toBytes())
);

token0 = tokenArray[0].address;
token1 = tokenArray[1].address;
Expand Down
Loading