-
Notifications
You must be signed in to change notification settings - Fork 133
/
open-position-ix.ts
109 lines (100 loc) · 3.92 KB
/
open-position-ix.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Program } from "@project-serum/anchor";
import { Whirlpool } from "../artifacts/whirlpool";
import { PublicKey } from "@solana/web3.js";
import { PDA, Instruction } from "@orca-so/common-sdk";
import { METADATA_PROGRAM_ADDRESS } from "..";
import {
OpenPositionBumpsData,
OpenPositionWithMetadataBumpsData,
} from "../types/public/anchor-types";
import { openPositionAccounts } from "../utils/instructions-util";
/**
* Parameters to open a position 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 positionMintAddress - PublicKey for the mint token for the Position token.
* @param positionTokenAccount - The associated token address for the position token in the owners wallet.
* @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 funder - The account that would fund the creation of this account
*/
export type OpenPositionParams = {
whirlpool: PublicKey;
owner: PublicKey;
positionPda: PDA;
positionMintAddress: PublicKey;
positionTokenAccount: PublicKey;
tickLowerIndex: number;
tickUpperIndex: number;
funder: PublicKey;
};
/**
* Open a position in a Whirlpool. A unique token will be minted to represent the position in the users wallet.
* 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 - OpenPositionParams object
* @returns - Instruction to perform the action.
*/
export function openPositionIx(
program: Program<Whirlpool>,
params: OpenPositionParams
): Instruction {
const { positionPda, tickLowerIndex, tickUpperIndex } = params;
const bumps: OpenPositionBumpsData = {
positionBump: positionPda.bump,
};
const ix = program.instruction.openPosition(bumps, tickLowerIndex, tickUpperIndex, {
accounts: openPositionAccounts(params),
});
// TODO: Require Keypair and auto sign this ix
return {
instructions: [ix],
cleanupInstructions: [],
signers: [],
};
}
/**
* Open a position in a Whirlpool. A unique token will be minted to represent the position
* in the users wallet. Additional Metaplex metadata is appended to identify the token.
* 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 - OpenPositionParams object and a derived PDA that hosts the position's metadata.
* @returns - Instruction to perform the action.
*/
export function openPositionWithMetadataIx(
program: Program<Whirlpool>,
params: OpenPositionParams & { metadataPda: PDA }
): Instruction {
const { positionPda, metadataPda, tickLowerIndex, tickUpperIndex } = params;
const bumps: OpenPositionWithMetadataBumpsData = {
positionBump: positionPda.bump,
metadataBump: metadataPda.bump,
};
const ix = program.instruction.openPositionWithMetadata(bumps, tickLowerIndex, tickUpperIndex, {
accounts: {
...openPositionAccounts(params),
positionMetadataAccount: metadataPda.publicKey,
metadataProgram: METADATA_PROGRAM_ADDRESS,
metadataUpdateAuth: new PublicKey("3axbTs2z5GBy6usVbNVoqEgZMng3vZvMnAoX29BFfwhr"),
},
});
// TODO: Require Keypair and auto sign this ix
return {
instructions: [ix],
cleanupInstructions: [],
signers: [],
};
}