Skip to content

Commit

Permalink
instruction name change
Browse files Browse the repository at this point in the history
issue on conversion between camel and snake (pos_2022 >> pos2022 >> pos2022) and generate wrong instruction disc code (instruction not found)
  • Loading branch information
yugure-orca committed Sep 26, 2024
1 parent 1b8d35c commit 1f803ec
Show file tree
Hide file tree
Showing 12 changed files with 939 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { Program } from "@coral-xyz/anchor";
import type { Instruction } from "@orca-so/common-sdk";
import { TOKEN_2022_PROGRAM_ID } from "@solana/spl-token";
import type { PublicKey } from "@solana/web3.js";
import type { Whirlpool } from "../artifacts/whirlpool";

/**
* Parameters to close a position (based on Token-2022) in a Whirlpool.
*
* @category Instruction Types
* @param receiver - PublicKey for the wallet that will receive the rented lamports.
* @param position - PublicKey for the position.
* @param positionMint - PublicKey for the mint token for the Position token.
* @param positionTokenAccount - The associated token address for the position token in the owners wallet.
* @param positionAuthority - Authority that owns the position token.
*/
export type ClosePositionWithTokenExtensionsParams = {
receiver: PublicKey;
position: PublicKey;
positionMint: PublicKey;
positionTokenAccount: PublicKey;
positionAuthority: PublicKey;
};

/**
* Close a position in a Whirlpool. Burns the position token in the owner's wallet.
* Mint and TokenAccount are based on Token-2022. And Mint accout will be also closed.
*
* @category Instructions
* @param context - Context object containing services required to generate the instruction
* @param params - ClosePositionWithTokenExtensionsParams object
* @returns - Instruction to perform the action.
*/
export function closePositionWithTokenExtensionsIx(
program: Program<Whirlpool>,
params: ClosePositionWithTokenExtensionsParams,
): Instruction {
const {
positionAuthority,
receiver: receiver,
position: position,
positionMint,
positionTokenAccount,
} = params;

const ix = program.instruction.closePositionWithTokenExtensions({
accounts: {
positionAuthority,
receiver,
position,
positionMint,
positionTokenAccount,
token2022Program: TOKEN_2022_PROGRAM_ID,
},
});

return {
instructions: [ix],
cleanupInstructions: [],
signers: [],
};
}
2 changes: 2 additions & 0 deletions legacy-sdk/whirlpool/src/instructions/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./close-bundled-position-ix";
export * from "./close-position-ix";
export * from "./close-position-with-token-extensions-ix";
export * from "./collect-fees-ix";
export * from "./collect-protocol-fees-ix";
export * from "./collect-reward-ix";
Expand All @@ -15,6 +16,7 @@ export * from "./initialize-reward-ix";
export * from "./initialize-tick-array-ix";
export * from "./open-bundled-position-ix";
export * from "./open-position-ix";
export * from "./open-position-with-token-extensions-ix";
export * from "./set-collect-protocol-fees-authority-ix";
export * from "./set-default-fee-rate-ix";
export * from "./set-default-protocol-fee-rate-ix";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { Program } from "@coral-xyz/anchor";
import type { Instruction, PDA } from "@orca-so/common-sdk";
import type { PublicKey } from "@solana/web3.js";
import { WHIRLPOOL_NFT_UPDATE_AUTH } from "..";
import type { Whirlpool } from "../artifacts/whirlpool";
import { openPositionWithTokenExtensionsAccounts } from "../utils/instructions-util";

/**
* Parameters to open a position (based on Token-2022) in a Whirlpool.
*
* @category Instruction Types
* @param whirlpool - PublicKey for the whirlpool that the position will be opened for.
* @param ownerKey - PublicKey for the wallet that will host the minted position token.
* @param positionPda - PDA for the derived position address.
* @param positionMint - PublicKey for the mint token for the Position token.
* @param positionTokenAccount - The associated token address for the position token in the owners wallet.
* @param funder - The account that would fund the creation of this account
* @param tickLowerIndex - The tick specifying the lower end of the position range.
* @param tickUpperIndex - The tick specifying the upper end of the position range.
* @param withTokenMetadataExtension - If true, the position token will have a TokenMetadata extension.
*/
export type OpenPositionWithTokenExtensionsParams = {
whirlpool: PublicKey;
owner: PublicKey;
positionPda: PDA;
positionMint: PublicKey;
positionTokenAccount: PublicKey;
funder: PublicKey;
tickLowerIndex: number;
tickUpperIndex: number;
withTokenMetadataExtension: boolean;
};

/**
* Open a position in a Whirlpool. A unique token will be minted to represent the position
* in the users wallet. Additional TokenMetadata extension is initialized to identify the token if requested.
* Mint and Token account are based on Token-2022.
* The position will start off with 0 liquidity.
*
* #### Special Errors
* `InvalidTickIndex` - If a provided tick is out of bounds, out of order or not a multiple of the tick-spacing in this pool.
*
* @category Instructions
* @param context - Context object containing services required to generate the instruction
* @param params - OpenPositionWithTokenExtensionsParams object and a derived PDA that hosts the position's metadata.
* @returns - Instruction to perform the action.
*/
export function openPositionWithTokenExtensionsIx(
program: Program<Whirlpool>,
params: OpenPositionWithTokenExtensionsParams,
): Instruction {
const { tickLowerIndex, tickUpperIndex, withTokenMetadataExtension } = params;

const ix = program.instruction.openPositionWithTokenExtensions(
tickLowerIndex,
tickUpperIndex,
withTokenMetadataExtension,
{
accounts: {
...openPositionWithTokenExtensionsAccounts(params),
metadataUpdateAuth: WHIRLPOOL_NFT_UPDATE_AUTH,
},
},
);

// TODO: Require Keypair and auto sign this ix
return {
instructions: [ix],
cleanupInstructions: [],
signers: [],
};
}
36 changes: 36 additions & 0 deletions legacy-sdk/whirlpool/src/ix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,42 @@ export class WhirlpoolIx {
return ix.closeBundledPositionIx(program, params);
}

/**
* Open a position in a Whirlpool. A unique token will be minted to represent the position
* in the users wallet. Additional TokenMetadata extension is initialized to identify the token if requested.
* Mint and Token account are based on Token-2022.
* The position will start off with 0 liquidity.
*
* #### Special Errors
* `InvalidTickIndex` - If a provided tick is out of bounds, out of order or not a multiple of the tick-spacing in this pool.
*
* @param context - Context object containing services required to generate the instruction
* @param params - OpenPositionWithTokenExtensionsParams object and a derived PDA that hosts the position's metadata.
* @returns - Instruction to perform the action.
*/
public static openPositionWithTokenExtensionsIx(
program: Program<Whirlpool>,
params: ix.OpenPositionWithTokenExtensionsParams,
) {
return ix.openPositionWithTokenExtensionsIx(program, params);
}

/**
* Close a position in a Whirlpool. Burns the position token in the owner's wallet.
* Mint and TokenAccount are based on Token-2022. And Mint accout will be also closed.
*
* @category Instructions
* @param context - Context object containing services required to generate the instruction
* @param params - ClosePositionWithTokenExtensionsParams object
* @returns - Instruction to perform the action.
*/
public static closePositionWithTokenExtensionsIx(
program: Program<Whirlpool>,
params: ix.ClosePositionWithTokenExtensionsParams,
) {
return ix.closePositionWithTokenExtensionsIx(program, params);
}

// V2 instructions
// TODO: comments
public static collectFeesV2Ix(
Expand Down
25 changes: 24 additions & 1 deletion legacy-sdk/whirlpool/src/utils/instructions-util.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as anchor from "@coral-xyz/anchor";
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_2022_PROGRAM_ID,
TOKEN_PROGRAM_ID,
} from "@solana/spl-token";
import { SystemProgram } from "@solana/web3.js";
import type { OpenPositionParams } from "../instructions";
import type { OpenPositionParams, OpenPositionWithTokenExtensionsParams } from "../instructions";

export function openPositionAccounts(params: OpenPositionParams) {
const {
Expand All @@ -28,3 +29,25 @@ export function openPositionAccounts(params: OpenPositionParams) {
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
};
}

export function openPositionWithTokenExtensionsAccounts(params: OpenPositionWithTokenExtensionsParams) {
const {
funder,
owner,
positionPda,
positionMint,
positionTokenAccount,
whirlpool: whirlpoolKey,
} = params;
return {
funder: funder,
owner,
position: positionPda.publicKey,
positionMint,
positionTokenAccount,
whirlpool: whirlpoolKey,
token2022Program: TOKEN_2022_PROGRAM_ID,
systemProgram: SystemProgram.programId,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
};
}
Loading

0 comments on commit 1f803ec

Please sign in to comment.