Skip to content

Commit

Permalink
Fix a bug ordering tokens (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjthieme authored Nov 5, 2024
1 parent 56c6c84 commit f3e9894
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
3 changes: 2 additions & 1 deletion ts-sdk/whirlpool/src/createPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from "@orca-so/whirlpools-core";
import { fetchAllMint, getTokenSize } from "@solana-program/token";
import assert from "assert";
import { orderMints } from "./token";

/**
* Represents the instructions and metadata for creating a pool.
Expand Down Expand Up @@ -142,7 +143,7 @@ export async function createConcentratedLiquidityPoolInstructions(
"Either supply a funder or set the default funder",
);
assert(
Buffer.from(tokenMintA) < Buffer.from(tokenMintB),
orderMints(tokenMintA, tokenMintB)[0] === tokenMintA,
"Token order needs to be flipped to match the canonical ordering (i.e. sorted on the byte repr. of the mint pubkeys)",
);
const instructions: IInstruction[] = [];
Expand Down
12 changes: 3 additions & 9 deletions ts-sdk/whirlpool/src/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
GetProgramAccountsApi,
} from "@solana/web3.js";
import { SPLASH_POOL_TICK_SPACING, WHIRLPOOLS_CONFIG_ADDRESS } from "./config";
import { orderMints } from "./token";

/**
* Type representing a pool that is not yet initialized.
Expand Down Expand Up @@ -123,10 +124,7 @@ export async function fetchConcentratedLiquidityPool(
tokenMintTwo: Address,
tickSpacing: number,
): Promise<PoolInfo> {
const [tokenMintA, tokenMintB] =
Buffer.from(tokenMintOne) < Buffer.from(tokenMintTwo)
? [tokenMintOne, tokenMintTwo]
: [tokenMintTwo, tokenMintOne];
const [tokenMintA, tokenMintB] = orderMints(tokenMintOne, tokenMintTwo);
const feeTierAddress = await getFeeTierAddress(
WHIRLPOOLS_CONFIG_ADDRESS,
tickSpacing,
Expand Down Expand Up @@ -196,11 +194,7 @@ export async function fetchWhirlpoolsByTokenPair(
tokenMintOne: Address,
tokenMintTwo: Address,
): Promise<PoolInfo[]> {
const [tokenMintA, tokenMintB] =
Buffer.from(tokenMintOne) < Buffer.from(tokenMintTwo)
? [tokenMintOne, tokenMintTwo]
: [tokenMintTwo, tokenMintOne];

const [tokenMintA, tokenMintB] = orderMints(tokenMintOne, tokenMintTwo);
const feeTierAccounts = await fetchAllFeeTierWithFilter(
rpc,
feeTierWhirlpoolsConfigFilter(WHIRLPOOLS_CONFIG_ADDRESS),
Expand Down
15 changes: 15 additions & 0 deletions ts-sdk/whirlpool/src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,18 @@ export function getCurrentTransferFee(
maxFee: transferFee.maximumFee,
};
}

/**
* Orders two mints by canonical byte order.
* @param {Address} mint1
* @param {Address} mint2
* @returns {[Address, Address]} [mint1, mint2] if mint1 should come first, [mint2, mint1] otherwise
*/
export function orderMints(mint1: Address, mint2: Address): [Address, Address] {
const encoder = getAddressEncoder();
const mint1Bytes = new Uint8Array(encoder.encode(mint1));
const mint2Bytes = new Uint8Array(encoder.encode(mint2));
return Buffer.compare(mint1Bytes, mint2Bytes) < 0
? [mint1, mint2]
: [mint2, mint1];
}
16 changes: 14 additions & 2 deletions ts-sdk/whirlpool/tests/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import {
setSolWrappingStrategy,
} from "../src/config";
import type { Address, TransactionSigner } from "@solana/web3.js";
import { createNoopSigner, generateKeyPairSigner } from "@solana/web3.js";
import { NATIVE_MINT, prepareTokenAccountsInstructions } from "../src/token";
import { address, createNoopSigner, generateKeyPairSigner } from "@solana/web3.js";
import { NATIVE_MINT, orderMints, prepareTokenAccountsInstructions } from "../src/token";
import assert from "assert";
import {
assertCloseAccountInstruction,
Expand Down Expand Up @@ -600,4 +600,16 @@ describe("Token Account Creation", () => {
owner: signer.address,
});
});

it("Should order mints by canonical byte order", () => {
const mint1 = address("Jd4M8bfJG3sAkd82RsGWyEXoaBXQP7njFzBwEaCTuDa");
const mint2 = address("BRjpCHtyQLNCo8gqRUr8jtdAj5AjPYQaoqbvcZiHok1k");
const [mintA, mintB] = orderMints(mint1, mint2);
assert.strictEqual(mintA, mint1);
assert.strictEqual(mintB, mint2);

const [mintC, mintD] = orderMints(mint2, mint1);
assert.strictEqual(mintC, mint1);
assert.strictEqual(mintD, mint2);
});
});

0 comments on commit f3e9894

Please sign in to comment.