Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wjthieme committed Oct 19, 2024
1 parent 0073f8d commit ff9dbf5
Show file tree
Hide file tree
Showing 10 changed files with 586 additions and 63 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"lint-staged": "^15.2.10",
"nx": "^20.0.2",
"rimraf": "^6.0.1",
"tsup": "^8.3.0",
"vitest": "^2.1.3"
},
"workspaces": [
Expand Down
15 changes: 13 additions & 2 deletions ts-sdk/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@
"version": "0.0.1",
"description": "Typescript client to interact with Orca's on-chain Whirlpool program.",
"type": "module",
"main": "./dist/index.js",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
"import": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"require": "./dist/index.cjs"
}
},
"sideEffects": false,
"files": [
"dist",
"README.md"
],
"scripts": {
"build": "node ./kinobi.js && tsc",
"build": "node ./kinobi.js && tsup src/index.ts --format cjs,esm --dts --sourcemap",
"test": "vitest run tests",
"clean": "rimraf dist src/generated"
},
Expand Down
2 changes: 1 addition & 1 deletion ts-sdk/lint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"type": "module",
"scripts": {
"format": "cd ../.. && eslint -c ts-sdk/lint/eslint.config.js --fix && prettier './**/*.{js,jsx,ts,tsx,json}' --write",
"format": "cd ../.. && prettier './**/*.{js,jsx,ts,tsx,json}' --write && eslint -c ts-sdk/lint/eslint.config.js --fix",
"lint": "cd ../.. && eslint -c ts-sdk/lint/eslint.config.js"
},
"devDependencies": {
Expand Down
15 changes: 13 additions & 2 deletions ts-sdk/whirlpool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@
"version": "0.0.1",
"description": "Orca's high-level typescript sdk to interact with Orca's on-chain Whirlpool program.",
"type": "module",
"main": "./dist/index.js",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
"import": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"require": "./dist/index.cjs"
}
},
"sideEffects": false,
"files": [
"dist",
"README.md"
],
"scripts": {
"build": "tsc",
"build": "tsup src/index.ts --format cjs,esm --dts --sourcemap",
"test": "vitest run tests",
"clean": "rimraf dist"
},
Expand Down
40 changes: 19 additions & 21 deletions ts-sdk/whirlpool/src/token.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
fetchAllMaybeToken,
fetchAllMint,
fetchMaybeToken,
findAssociatedTokenPda,
getCloseAccountInstruction,
getCreateAssociatedTokenInstruction,
Expand Down Expand Up @@ -54,7 +53,7 @@ type TokenAccountInstructions = {
};

function mintFilter(x: Address) {
if (SOL_WRAPPING_STRATEGY === "none") {
if (SOL_WRAPPING_STRATEGY === "none" || SOL_WRAPPING_STRATEGY === "ata") {
return true;
}
return x != NATIVE_MINT;
Expand Down Expand Up @@ -85,7 +84,8 @@ export async function prepareTokenAccountsInstructions(
const mintAddresses = Array.isArray(spec)
? spec
: (Object.keys(spec) as Address[]);
const hasSolMint = mintAddresses.includes(NATIVE_MINT);
const solMintIndex = mintAddresses.indexOf(NATIVE_MINT);
const hasSolMint = solMintIndex !== -1;
const mints = await fetchAllMint(rpc, mintAddresses.filter(mintFilter));
const tokenAddresses = await Promise.all(
mints.map((mint) =>
Expand Down Expand Up @@ -160,20 +160,22 @@ export async function prepareTokenAccountsInstructions(
// With date, it should only create collisions if the same owner
// creates multiple accounts at exactly the same time (in ms)
const seed = Date.now().toString();
const buffer = await new SubtleCrypto().digest(
"sha256",
const buffer = await crypto.subtle.digest(
"SHA-256",
Buffer.concat([
Buffer.from(getAddressEncoder().encode(owner.address)),
Buffer.from(seed),
Buffer.from(getAddressEncoder().encode(TOKEN_PROGRAM_ADDRESS)),
]),
);
const address = getAddressDecoder().decode(new Uint8Array(buffer));
tokenAccountAddresses[NATIVE_MINT] = getAddressDecoder().decode(
new Uint8Array(buffer),
);

createInstructions.push(
getCreateAccountWithSeedInstruction({
payer: owner,
newAccount: address,
newAccount: tokenAccountAddresses[NATIVE_MINT],
base: owner.address,
baseAccount: owner,
seed: seed,
Expand All @@ -182,28 +184,24 @@ export async function prepareTokenAccountsInstructions(
programAddress: TOKEN_PROGRAM_ADDRESS,
}),
getInitializeAccount3Instruction({
account: address,
account: tokenAccountAddresses[NATIVE_MINT],
mint: NATIVE_MINT,
owner: owner.address,
}),
);

cleanupInstructions.push(
getCloseAccountInstruction({
account: tokenAccountAddresses[NATIVE_MINT],
owner,
destination: owner.address,
}),
);
}

if (hasSolMint && SOL_WRAPPING_STRATEGY === "ata") {
const account = await fetchMaybeToken(
rpc,
tokenAccountAddresses[NATIVE_MINT],
);
const account = tokenAccounts[solMintIndex];
if (!account.exists) {
createInstructions.push(
getCreateAssociatedTokenInstruction({
payer: owner,
owner: owner.address,
ata: account.address,
mint: NATIVE_MINT,
tokenProgram: TOKEN_PROGRAM_ADDRESS,
}),
);
cleanupInstructions.push(
getCloseAccountInstruction({
account: account.address,
Expand Down
36 changes: 18 additions & 18 deletions ts-sdk/whirlpool/tests/assertInstruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export function assertCreateAtaInstruction(
assert.fail("Could not decode instruction data");
}

assert.strictEqual(instruction.accounts?.[0], checks.owner);
assert.strictEqual(instruction.accounts?.[1], checks.ata);
assert.strictEqual(instruction.accounts?.[2], checks.owner);
assert.strictEqual(instruction.accounts?.[3], checks.mint);
assert.strictEqual(instruction.accounts?.[0].address, checks.owner);
assert.strictEqual(instruction.accounts?.[1].address, checks.ata);
assert.strictEqual(instruction.accounts?.[2].address, checks.owner);
assert.strictEqual(instruction.accounts?.[3].address, checks.mint);
assert.strictEqual(
instruction.accounts?.[5],
instruction.accounts?.[5].address,
checks.tokenProgram ?? TOKEN_PROGRAM_ADDRESS,
);
}
Expand All @@ -65,9 +65,9 @@ export function assertCloseAccountInstruction(
assert.fail("Could not decode instruction data");
}

assert.strictEqual(instruction.accounts?.[0], checks.account);
assert.strictEqual(instruction.accounts?.[1], checks.owner);
assert.strictEqual(instruction.accounts?.[2], checks.owner);
assert.strictEqual(instruction.accounts?.[0].address, checks.account);
assert.strictEqual(instruction.accounts?.[1].address, checks.owner);
assert.strictEqual(instruction.accounts?.[2].address, checks.owner);
}

export function assertSolTransferInstruction(
Expand All @@ -86,8 +86,8 @@ export function assertSolTransferInstruction(
assert.fail("Could not decode instruction data");
}

assert.strictEqual(instruction.accounts?.[0], checks.from);
assert.strictEqual(instruction.accounts?.[1], checks.to);
assert.strictEqual(instruction.accounts?.[0].address, checks.from);
assert.strictEqual(instruction.accounts?.[1].address, checks.to);
}

export function assertSyncNativeInstruction(
Expand All @@ -105,7 +105,7 @@ export function assertSyncNativeInstruction(
assert.fail("Could not decode instruction data");
}

assert.strictEqual(instruction.accounts?.[0], checks.account);
assert.strictEqual(instruction.accounts?.[0].address, checks.account);
}

export function assertCreateAccountInstruction(
Expand All @@ -124,8 +124,8 @@ export function assertCreateAccountInstruction(
assert.fail("Could not decode instruction data");
}

assert.strictEqual(instruction.accounts?.[0], checks.payer);
assert.strictEqual(instruction.accounts?.[1], checks.account);
assert.strictEqual(instruction.accounts?.[0].address, checks.payer);
assert.strictEqual(instruction.accounts?.[1].address, checks.account);
}

export function assertCreateAccountWithSeedInstruction(
Expand All @@ -145,9 +145,9 @@ export function assertCreateAccountWithSeedInstruction(
assert.fail("Could not decode instruction data");
}

assert.strictEqual(instruction.accounts?.[0], checks.payer);
assert.strictEqual(instruction.accounts?.[1], checks.account);
assert.strictEqual(instruction.accounts?.[2], checks.payer);
assert.strictEqual(instruction.accounts?.[0].address, checks.payer);
assert.strictEqual(instruction.accounts?.[1].address, checks.account);
assert.strictEqual(instruction.accounts?.[2].address, checks.payer);
}

export function assertInitializeAccountInstruction(
Expand All @@ -166,6 +166,6 @@ export function assertInitializeAccountInstruction(
assert.fail("Could not decode instruction data");
}

assert.strictEqual(instruction.accounts?.[0], checks.account);
assert.strictEqual(instruction.accounts?.[1], checks.mint);
assert.strictEqual(instruction.accounts?.[0].address, checks.account);
assert.strictEqual(instruction.accounts?.[1].address, checks.mint);
}
33 changes: 26 additions & 7 deletions ts-sdk/whirlpool/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ import {
WHIRLPOOLS_CONFIG_EXTENSION_ADDRESS,
DEFAULT_SLIPPAGE_TOLERANCE_BPS,
DEFAULT_SOL_WRAPPING_STRATEGY,
DEFAULT_WHIRLPOOLS_CONFIG_ADDRESS,
DEFAULT_WHIRLPOOLS_CONFIG_EXTENSION_ADDRESS,
} from "../src/config";
import assert from "assert";
import { createKeyPairSignerFromPrivateKeyBytes } from "@solana/web3.js";
import {
address,
createKeyPairSignerFromPrivateKeyBytes,
} from "@solana/web3.js";

// Tests in order, which is important here

Expand All @@ -25,9 +30,17 @@ describe("Configuration", () => {
});

it("Should be able to set whirlpool config", async () => {
await setWhirlpoolsConfig(DEFAULT_ADDRESS);
assert.strictEqual(WHIRLPOOLS_CONFIG_ADDRESS, DEFAULT_ADDRESS);
assert.strictEqual(WHIRLPOOLS_CONFIG_EXTENSION_ADDRESS, "");
await setWhirlpoolsConfig(
address("GdDMspJi2oQaKDtABKE24wAQgXhGBoxq8sC21st7GJ3E"),
);
assert.strictEqual(
WHIRLPOOLS_CONFIG_ADDRESS,
"GdDMspJi2oQaKDtABKE24wAQgXhGBoxq8sC21st7GJ3E",
);
assert.strictEqual(
WHIRLPOOLS_CONFIG_EXTENSION_ADDRESS,
"Ez4MMUVb7VrKFcTSbi9Yz2ivXwdwCqJicnDaRHbe96Yk",
);
});

it("Should be able to set default funder to an address", () => {
Expand All @@ -36,7 +49,7 @@ describe("Configuration", () => {
});

it("Should be able to set default funder to a signer", async () => {
const bytes = new Uint8Array(64);
const bytes = new Uint8Array(32);
const signer = await createKeyPairSignerFromPrivateKeyBytes(bytes);
setDefaultFunder(signer);
assert.strictEqual(FUNDER.address, signer.address);
Expand All @@ -54,8 +67,14 @@ describe("Configuration", () => {

it("Should be able to reset the configuration", () => {
resetConfiguration();
assert.strictEqual(WHIRLPOOLS_CONFIG_ADDRESS, DEFAULT_ADDRESS);
assert.strictEqual(WHIRLPOOLS_CONFIG_EXTENSION_ADDRESS, "");
assert.strictEqual(
WHIRLPOOLS_CONFIG_ADDRESS,
DEFAULT_WHIRLPOOLS_CONFIG_ADDRESS,
);
assert.strictEqual(
WHIRLPOOLS_CONFIG_EXTENSION_ADDRESS,
DEFAULT_WHIRLPOOLS_CONFIG_EXTENSION_ADDRESS,
);
assert.strictEqual(FUNDER.address, DEFAULT_ADDRESS);
assert.strictEqual(SLIPPAGE_TOLERANCE_BPS, DEFAULT_SLIPPAGE_TOLERANCE_BPS);
assert.strictEqual(SOL_WRAPPING_STRATEGY, DEFAULT_SOL_WRAPPING_STRATEGY);
Expand Down
18 changes: 13 additions & 5 deletions ts-sdk/whirlpool/tests/mockRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { DEFAULT_ADDRESS } from "../src/config";
import { getMintEncoder, TOKEN_PROGRAM_ADDRESS } from "@solana-program/token";
import { TOKEN_2022_PROGRAM_ADDRESS } from "@solana-program/token-2022";
import { NATIVE_MINT } from "../src/token";
import { SYSTEM_PROGRAM_ADDRESS } from "@solana-program/system";

export const [
TOKEN_MINT_1,
Expand Down Expand Up @@ -117,19 +118,19 @@ function getAccountData<T>(address: unknown, opts: unknown): unknown {
assertIsAddress(address);
const data = mockAccounts[address];
if (data == null) {
throw new Error(`No mock account found for ${address}`);
return null as T;
}
return {
data: [decoder.decode(data.bytes), opts.encoding],
executable: false,
lamports: data.bytes.length * 10,
owner: data.owner ?? DEFAULT_ADDRESS,
owner: data.owner ?? SYSTEM_PROGRAM_ADDRESS,
rentEpoch: 0,
space: data.bytes.length,
} as T;
}

function getResponse<T>(value: unknown): T {
function getResponseWithContext<T>(value: unknown): T {
return {
jsonrpc: "2.0",
result: {
Expand All @@ -141,6 +142,13 @@ function getResponse<T>(value: unknown): T {
} as T;
}

function getResponse<T>(value: unknown): T {
return {
jsonrpc: "2.0",
result: value,
} as T;
}

function mockTransport<T>(
config: Readonly<{
payload: unknown;
Expand All @@ -159,13 +167,13 @@ function mockTransport<T>(
const address = config.payload.params[0];
assert(typeof address === "string");
const accountData = getAccountData(address, config.payload.params[1]);
return Promise.resolve(getResponse<T>(accountData));
return Promise.resolve(getResponseWithContext<T>(accountData));
case "getMultipleAccounts":
const addresses = config.payload.params[0];
const opts = config.payload.params[1];
assert(Array.isArray(addresses));
const accountsData = addresses.map((x) => getAccountData(x, opts));
return Promise.resolve(getResponse<T>(accountsData));
return Promise.resolve(getResponseWithContext<T>(accountsData));
case "getMinimumBalanceForRentExemption":
const space = config.payload.params[0];
assert(typeof space === "number");
Expand Down
4 changes: 2 additions & 2 deletions ts-sdk/whirlpool/tests/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe("Token Account Creation", () => {
});

afterEach(async () => {
await resetConfiguration();
resetConfiguration();
});

it("No native mint", async () => {
Expand Down Expand Up @@ -160,7 +160,7 @@ describe("Token Account Creation", () => {
assertCreateAtaInstruction(result.createInstructions[0], {
ata: tokenAddress,
owner: signer.address,
mint: TOKEN_MINT_2,
mint: TOKEN_2022_MINT,
tokenProgram: TOKEN_2022_PROGRAM_ADDRESS,
});
assert.strictEqual(result.cleanupInstructions.length, 0);
Expand Down
Loading

0 comments on commit ff9dbf5

Please sign in to comment.