Skip to content

Commit

Permalink
fix: ts sdk existing fn use v2 endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
codewithgun committed Sep 5, 2024
1 parent b65bd20 commit 9217626
Show file tree
Hide file tree
Showing 7 changed files with 3,978 additions and 2,226 deletions.
4 changes: 4 additions & 0 deletions ts-client/src/dlmm/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export const LBCLMM_PROGRAM_IDS = {
"mainnet-beta": "LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo",
};

export const MEMO_PROGRAM_ID = new PublicKey(
"MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"
);

export const ADMIN = {
devnet: "6WaLrrRfReGKBYUSkmx2K6AuT21ida4j8at2SUiZdXu8",
localhost: "bossj3JvwiNK7pvjr149DqdtJxf2gdygbcmEPTkb2F1",
Expand Down
40 changes: 39 additions & 1 deletion ts-client/src/dlmm/helpers/binArray.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BN } from "@coral-xyz/anchor";
import { PublicKey } from "@solana/web3.js";
import { AccountMeta, PublicKey } from "@solana/web3.js";
import { MAX_BIN_ARRAY_SIZE, MAX_BIN_PER_POSITION } from "../constants";
import {
Bin,
Expand Down Expand Up @@ -364,3 +364,41 @@ export function getBinArraysRequiredByPositionRange(
index,
}));
}

export function getBinArrayIndexesByBinRange(lowerBinId: BN, upperBinId: BN) {
let binArrayIndex = binIdToBinArrayIndex(lowerBinId);
const indexes: BN[] = [];
while (true) {
const [binArrayLowerBinId, binArrayUpperBinId] =
getBinArrayLowerUpperBinId(binArrayIndex);
indexes.push(binArrayIndex);

if (
upperBinId.gte(binArrayLowerBinId) &&
upperBinId.lte(binArrayUpperBinId)
) {
break;
} else {
binArrayIndex = binArrayIndex.add(new BN(1));
}
}

return indexes;
}

export function getBinArrayAccounstMetaByBinRange(
lbPair: PublicKey,
lowerBinId: BN,
upperBinId: BN,
programId: PublicKey
): AccountMeta[] {
const indexes = getBinArrayIndexesByBinRange(lowerBinId, upperBinId);
return indexes.map((index) => {
const [binArray] = deriveBinArray(lbPair, index, programId);
return {
pubkey: binArray,
isSigner: false,
isWritable: true,
};
});
}
56 changes: 56 additions & 0 deletions ts-client/src/dlmm/helpers/token2022.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
addExtraAccountMetasForExecute,
createTransferCheckedInstruction,
getTransferHook,
TOKEN_PROGRAM_ID,
unpackMint,
} from "@solana/spl-token";
import { AccountInfo, Connection, PublicKey } from "@solana/web3.js";

export async function getExtraAccountMetasForTransferHook(
connection: Connection,
mintAddress: PublicKey,
mintAccountInfo: AccountInfo<Buffer>
) {
const mintState = unpackMint(
mintAddress,
mintAccountInfo,
mintAccountInfo.owner
);

if (mintAccountInfo.owner.equals(TOKEN_PROGRAM_ID)) {
return [];
}

const transferHook = getTransferHook(mintState);

if (!transferHook) {
return [];
} else {
// We just need the instruction, therefore we do not need source and destination key
const instruction = createTransferCheckedInstruction(
PublicKey.default,
mintAddress,
PublicKey.default,
PublicKey.default,
BigInt(0),
mintState.decimals,
[],
mintAccountInfo.owner
);

await addExtraAccountMetasForExecute(
connection,
instruction,
transferHook.programId,
PublicKey.default,
mintAddress,
PublicKey.default,
PublicKey.default,
BigInt(0)
);

// Only 4 keys needed if it's single signer. https://github.com/solana-labs/solana-program-library/blob/d72289c79a04411c69a8bf1054f7156b6196f9b3/token/js/src/extensions/transferFee/instructions.ts#L251
return instruction.keys.slice(4);
}
}
Loading

0 comments on commit 9217626

Please sign in to comment.