@meteora-ag/cp-amm-sdk 1.0.1-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,201 @@
1
+ # CP-AMM SDK
2
+
3
+ The main example for SDK
4
+
5
+ ## 1. Create a CpAmm instance
6
+ ```ts
7
+ const cpAmm = new CpAmm(connection, programId)
8
+ ```
9
+ Note: Flexible setup for mainnet/devnet by connection and programId.
10
+ ## 2. Create customize pool
11
+ ```ts
12
+ // Setup base fee
13
+ const baseFee: BaseFee = {
14
+ cliffFeeNumerator: new BN(1_000_000), // 1%
15
+ numberOfPeriod: 10,
16
+ periodFrequency: new BN(10),
17
+ reductionFactor: new BN(2),
18
+ feeSchedulerMode: 0, // 0: Linear, 1: Exponential
19
+ };
20
+
21
+ // Setup dynamic fee (Optional)
22
+ type DynamicFee = {
23
+ binStep: number;
24
+ binStepU128: BN;
25
+ filterPeriod: number;
26
+ decayPeriod: number;
27
+ reductionFactor: number;
28
+ maxVolatilityAccumulator: number;
29
+ variableFeeControl: number;
30
+ };
31
+
32
+ // Setup pool fees
33
+ const poolFees: PoolFeesParams = {
34
+ baseFee,
35
+ protocolFeePercent: 20,
36
+ partnerFeePercent: 0,
37
+ referralFeePercent: 20,
38
+ dynamicFee: null, // Optional dynamic fee
39
+ };
40
+
41
+ // Setup params
42
+ const params: InitializeCustomizeablePoolParams = {
43
+ payer: wallet.publicKey,
44
+ creator: wallet.publicKey,
45
+ positionNft: positionNft.publicKey, // position nft pubkey, User can choose specific keypair.
46
+ tokenX,
47
+ tokenY,
48
+ tokenXAmount: new BN(1000 * 10 ** 6), // amount token X
49
+ tokenYAmount: new BN(1000 * 10 ** 9), // amount token Y
50
+ tokenXDecimal: 6,
51
+ tokenYDecimal: 9,
52
+ poolFees, // poolFees setup above
53
+ hasAlphaVault: false,
54
+ activationType: 1, // 0: slot, 1: timestamp
55
+ collectFeeMode: 0, // 0: BothToken 1: OnlyB
56
+ activationPoint: null,
57
+ };
58
+
59
+ const {tx: transaction, pool} = await cpAmm.createCustomPool(params);
60
+ ```
61
+ Note: We need two signers for transaction: positionNft & payer.
62
+
63
+ ## 3. Create position
64
+ ```ts
65
+ // Prepare create position params
66
+ const createPositionParams: CreatePositionParams = {
67
+ owner: wallet.publicKey,
68
+ payer: wallet.publicKey,
69
+ pool: pool,
70
+ positionNft: positionNft.publicKey,
71
+ };
72
+ // Build transaction
73
+ const transaction = await cpAmm.createPosition(createPositionParams);
74
+ ```
75
+ Note: We need two signers for transaction: positionNft & payer.
76
+
77
+ ## 5. Add liquidity
78
+ ```ts
79
+ // Calculate liquidity delta will add by supply max amount for tokenA & tokenB
80
+ const positionState = await cpAmm.fetchPositionState(position);
81
+ const poolState = await cpAmm.fetchPoolState(pool);
82
+ const {
83
+ sqrtPrice,
84
+ sqrtMaxPrice,
85
+ sqrtMinPrice,
86
+ tokenAMint,
87
+ tokenBMint,
88
+ tokenAVault,
89
+ tokenBVault,
90
+ tokenAFlag,
91
+ tokenBFlag,
92
+ } = poolState;
93
+
94
+ const liquidityDelta = await cpAmm.getLiquidityDelta({
95
+ maxAmountTokenA: new BN(100_000 * 10 ** 6),
96
+ maxAmountTokenB: new BN(100_000 * 10 ** 9),
97
+ tokenAMint,
98
+ tokenBMint,
99
+ sqrtMaxPrice,
100
+ sqrtMinPrice,
101
+ sqrtPrice,
102
+ });
103
+
104
+ const transaction = await cpAmm.addLiquidity({
105
+ owner: wallet.publicKey,
106
+ position,
107
+ pool,
108
+ positionNftMint: positionState.nftMint,
109
+ liquidityDeltaQ64: liquidityDelta,
110
+ tokenAAmountThreshold: new BN(100000000735553),
111
+ tokenBAmountThreshold: new BN(100000000735553),
112
+ tokenAMint,
113
+ tokenBMint,
114
+ tokenAVault,
115
+ tokenBVault,
116
+ tokenAProgram: getTokenProgram(tokenAFlag),
117
+ tokenBProgram: getTokenProgram(tokenBFlag),
118
+ });
119
+ ```
120
+
121
+ ## 6. Swap
122
+ ```ts
123
+ const poolState = await cpAmm.fetchPoolState(pool);
124
+ const {
125
+ tokenAMint,
126
+ tokenBMint,
127
+ tokenAVault,
128
+ tokenBVault,
129
+ tokenAFlag,
130
+ tokenBFlag,
131
+ } = poolState;
132
+
133
+ const slippage = 5; // 5%
134
+ // Get quotes
135
+ const quotes = await cpAmm.getQuote({
136
+ inAmount: new BN(1000 * 10 ** 6),
137
+ inputTokenMint: tokenAMint,
138
+ slippage,
139
+ poolState,
140
+ });
141
+
142
+ const transaction = await cpAmm.swap({
143
+ payer: wallet.publicKey,
144
+ pool,
145
+ inputTokenMint: tokenAMint,
146
+ outputTokenMint: tokenBMint,
147
+ amountIn: new BN(1000 * 10 ** 6),
148
+ minimumAmountOut: new BN(10),
149
+ tokenAMint,
150
+ tokenBMint,
151
+ tokenAVault,
152
+ tokenBVault,
153
+ tokenAProgram: getTokenProgram(tokenAFlag),
154
+ tokenBProgram: getTokenProgram(tokenBFlag),
155
+ referralTokenAccount: null,
156
+ });
157
+ ```
158
+
159
+ ## 7. Remove liquidity
160
+ ```ts
161
+ const positionState = await cpAmm.fetchPositionState(position);
162
+ const poolState = await cpAmm.fetchPoolState(pool);
163
+ const {
164
+ sqrtPrice,
165
+ sqrtMaxPrice,
166
+ sqrtMinPrice,
167
+ tokenAMint,
168
+ tokenBMint,
169
+ tokenAVault,
170
+ tokenBVault,
171
+ tokenAFlag,
172
+ tokenBFlag,
173
+ } = poolState;
174
+
175
+ const liquidityDelta = await cpAmm.getLiquidityDelta({
176
+ maxAmountTokenA: new BN(100_000 * 10 ** 6),
177
+ maxAmountTokenB: new BN(100_000 * 10 ** 9),
178
+ tokenAMint,
179
+ tokenBMint,
180
+ sqrtMaxPrice,
181
+ sqrtMinPrice,
182
+ sqrtPrice,
183
+ });
184
+
185
+ const transaction = await cpAmm.removeLiquidity({
186
+ owner: wallet.publicKey,
187
+ position,
188
+ pool,
189
+ positionNftMint: positionState.nftMint,
190
+ liquidityDeltaQ64: liquidityDelta,
191
+ tokenAAmountThreshold: new BN(0),
192
+ tokenBAmountThreshold: new BN(0),
193
+ tokenAMint,
194
+ tokenBMint,
195
+ tokenAVault,
196
+ tokenBVault,
197
+ tokenAProgram: getTokenProgram(tokenAFlag),
198
+ tokenBProgram: getTokenProgram(tokenBFlag),
199
+ });
200
+ ```
201
+