sol-ihor-lab 1.0.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.
Files changed (173) hide show
  1. package/README.md +35 -0
  2. package/lib/cjs/bundle.d.ts +4 -0
  3. package/lib/cjs/bundle.js +102 -0
  4. package/lib/cjs/buy_token.d.ts +5 -0
  5. package/lib/cjs/buy_token.js +59 -0
  6. package/lib/cjs/create_open_market.d.ts +4 -0
  7. package/lib/cjs/create_open_market.js +340 -0
  8. package/lib/cjs/create_pool.d.ts +4 -0
  9. package/lib/cjs/create_pool.js +107 -0
  10. package/lib/cjs/create_token.d.ts +4 -0
  11. package/lib/cjs/create_token.js +164 -0
  12. package/lib/cjs/errors.d.ts +18 -0
  13. package/lib/cjs/errors.js +44 -0
  14. package/lib/cjs/global.d.ts +62 -0
  15. package/lib/cjs/global.js +107 -0
  16. package/lib/cjs/index.d.ts +17 -0
  17. package/lib/cjs/index.js +33 -0
  18. package/lib/cjs/instructions/build-instruction.d.ts +8 -0
  19. package/lib/cjs/instructions/build-instruction.js +66 -0
  20. package/lib/cjs/instructions/buy-instruction.d.ts +3 -0
  21. package/lib/cjs/instructions/buy-instruction.js +44 -0
  22. package/lib/cjs/instructions/cache-alt-instruction.d.ts +4 -0
  23. package/lib/cjs/instructions/cache-alt-instruction.js +41 -0
  24. package/lib/cjs/instructions/createATA-instruction.d.ts +3 -0
  25. package/lib/cjs/instructions/createATA-instruction.js +10 -0
  26. package/lib/cjs/instructions/createpool-instruction.d.ts +4 -0
  27. package/lib/cjs/instructions/createpool-instruction.js +58 -0
  28. package/lib/cjs/instructions/sol-transfer-instruction.d.ts +3 -0
  29. package/lib/cjs/instructions/sol-transfer-instruction.js +12 -0
  30. package/lib/cjs/instructions/transfer-instruction.d.ts +3 -0
  31. package/lib/cjs/instructions/transfer-instruction.js +9 -0
  32. package/lib/cjs/pool/pool-manager.d.ts +42 -0
  33. package/lib/cjs/pool/pool-manager.js +70 -0
  34. package/lib/cjs/pool_manager.d.ts +25 -0
  35. package/lib/cjs/pool_manager.js +127 -0
  36. package/lib/cjs/sell_token.d.ts +5 -0
  37. package/lib/cjs/sell_token.js +59 -0
  38. package/lib/cjs/transaction/bundle.d.ts +4 -0
  39. package/lib/cjs/transaction/bundle.js +97 -0
  40. package/lib/cjs/transaction/execute-rpc.d.ts +5 -0
  41. package/lib/cjs/transaction/execute-rpc.js +41 -0
  42. package/lib/cjs/transaction/sign-tx.d.ts +2 -0
  43. package/lib/cjs/transaction/sign-tx.js +7 -0
  44. package/lib/cjs/transaction/transaction-exectue.d.ts +30 -0
  45. package/lib/cjs/transaction/transaction-exectue.js +119 -0
  46. package/lib/cjs/transaction-helper/check_transaction.d.ts +3 -0
  47. package/lib/cjs/transaction-helper/check_transaction.js +43 -0
  48. package/lib/cjs/transaction-helper/transaction.d.ts +7 -0
  49. package/lib/cjs/transaction-helper/transaction.js +75 -0
  50. package/lib/cjs/upload-metadata/upload-metaplex.d.ts +3 -0
  51. package/lib/cjs/upload-metadata/upload-metaplex.js +48 -0
  52. package/lib/cjs/upload-metadata/upload-nftstorage.d.ts +0 -0
  53. package/lib/cjs/upload-metadata/upload-nftstorage.js +1 -0
  54. package/lib/cjs/upload-metadata/upload-pinata.d.ts +1 -0
  55. package/lib/cjs/upload-metadata/upload-pinata.js +17 -0
  56. package/lib/cjs/upload-metadata/utility.d.ts +1 -0
  57. package/lib/cjs/upload-metadata/utility.js +28 -0
  58. package/lib/cjs/utility.d.ts +33 -0
  59. package/lib/cjs/utility.js +129 -0
  60. package/lib/cjs/utils/budget.d.ts +9 -0
  61. package/lib/cjs/utils/budget.js +83 -0
  62. package/lib/cjs/utils/error-helper.d.ts +7 -0
  63. package/lib/cjs/utils/error-helper.js +15 -0
  64. package/lib/cjs/utils/get-balance.d.ts +11 -0
  65. package/lib/cjs/utils/get-balance.js +52 -0
  66. package/lib/cjs/utils/util.d.ts +27 -0
  67. package/lib/cjs/utils/util.js +346 -0
  68. package/lib/cjs/validation_check.d.ts +3 -0
  69. package/lib/cjs/validation_check.js +26 -0
  70. package/lib/esm/bundle.d.ts +4 -0
  71. package/lib/esm/bundle.js +75 -0
  72. package/lib/esm/buy_token.d.ts +5 -0
  73. package/lib/esm/buy_token.js +55 -0
  74. package/lib/esm/create_open_market.d.ts +4 -0
  75. package/lib/esm/create_open_market.js +310 -0
  76. package/lib/esm/create_pool.d.ts +4 -0
  77. package/lib/esm/create_pool.js +80 -0
  78. package/lib/esm/create_token.d.ts +4 -0
  79. package/lib/esm/create_token.js +137 -0
  80. package/lib/esm/errors.d.ts +18 -0
  81. package/lib/esm/errors.js +35 -0
  82. package/lib/esm/global.d.ts +62 -0
  83. package/lib/esm/global.js +103 -0
  84. package/lib/esm/index.d.ts +17 -0
  85. package/lib/esm/index.js +17 -0
  86. package/lib/esm/instructions/build-instruction.d.ts +8 -0
  87. package/lib/esm/instructions/build-instruction.js +61 -0
  88. package/lib/esm/instructions/buy-instruction.d.ts +3 -0
  89. package/lib/esm/instructions/buy-instruction.js +40 -0
  90. package/lib/esm/instructions/cache-alt-instruction.d.ts +4 -0
  91. package/lib/esm/instructions/cache-alt-instruction.js +36 -0
  92. package/lib/esm/instructions/createATA-instruction.d.ts +3 -0
  93. package/lib/esm/instructions/createATA-instruction.js +6 -0
  94. package/lib/esm/instructions/createpool-instruction.d.ts +4 -0
  95. package/lib/esm/instructions/createpool-instruction.js +52 -0
  96. package/lib/esm/instructions/sol-transfer-instruction.d.ts +3 -0
  97. package/lib/esm/instructions/sol-transfer-instruction.js +8 -0
  98. package/lib/esm/instructions/transfer-instruction.d.ts +3 -0
  99. package/lib/esm/instructions/transfer-instruction.js +5 -0
  100. package/lib/esm/pool/pool-manager.d.ts +42 -0
  101. package/lib/esm/pool/pool-manager.js +63 -0
  102. package/lib/esm/pool_manager.d.ts +25 -0
  103. package/lib/esm/pool_manager.js +123 -0
  104. package/lib/esm/sell_token.d.ts +5 -0
  105. package/lib/esm/sell_token.js +55 -0
  106. package/lib/esm/transaction/bundle.d.ts +4 -0
  107. package/lib/esm/transaction/bundle.js +90 -0
  108. package/lib/esm/transaction/execute-rpc.d.ts +5 -0
  109. package/lib/esm/transaction/execute-rpc.js +36 -0
  110. package/lib/esm/transaction/sign-tx.d.ts +2 -0
  111. package/lib/esm/transaction/sign-tx.js +3 -0
  112. package/lib/esm/transaction/transaction-exectue.d.ts +30 -0
  113. package/lib/esm/transaction/transaction-exectue.js +115 -0
  114. package/lib/esm/transaction-helper/check_transaction.d.ts +3 -0
  115. package/lib/esm/transaction-helper/check_transaction.js +16 -0
  116. package/lib/esm/transaction-helper/transaction.d.ts +7 -0
  117. package/lib/esm/transaction-helper/transaction.js +68 -0
  118. package/lib/esm/upload-metadata/upload-metaplex.d.ts +3 -0
  119. package/lib/esm/upload-metadata/upload-metaplex.js +43 -0
  120. package/lib/esm/upload-metadata/upload-nftstorage.d.ts +0 -0
  121. package/lib/esm/upload-metadata/upload-nftstorage.js +1 -0
  122. package/lib/esm/upload-metadata/upload-pinata.d.ts +1 -0
  123. package/lib/esm/upload-metadata/upload-pinata.js +13 -0
  124. package/lib/esm/upload-metadata/utility.d.ts +1 -0
  125. package/lib/esm/upload-metadata/utility.js +21 -0
  126. package/lib/esm/utility.d.ts +33 -0
  127. package/lib/esm/utility.js +93 -0
  128. package/lib/esm/utils/budget.d.ts +9 -0
  129. package/lib/esm/utils/budget.js +73 -0
  130. package/lib/esm/utils/error-helper.d.ts +7 -0
  131. package/lib/esm/utils/error-helper.js +11 -0
  132. package/lib/esm/utils/get-balance.d.ts +11 -0
  133. package/lib/esm/utils/get-balance.js +42 -0
  134. package/lib/esm/utils/util.d.ts +25 -0
  135. package/lib/esm/utils/util.js +107 -0
  136. package/lib/esm/validation_check.d.ts +3 -0
  137. package/lib/esm/validation_check.js +22 -0
  138. package/package.json +69 -0
  139. package/src/assert.ts +4 -0
  140. package/src/bundle.ts +99 -0
  141. package/src/buy_token.ts +82 -0
  142. package/src/create_open_market.ts +464 -0
  143. package/src/create_pool.ts +120 -0
  144. package/src/create_token.ts +267 -0
  145. package/src/errors.ts +25 -0
  146. package/src/global.ts +156 -0
  147. package/src/index.ts +17 -0
  148. package/src/instructions/build-instruction.ts +87 -0
  149. package/src/instructions/buy-instruction.ts +52 -0
  150. package/src/instructions/cache-alt-instruction.ts +40 -0
  151. package/src/instructions/createATA-instruction.ts +29 -0
  152. package/src/instructions/createpool-instruction.ts +72 -0
  153. package/src/instructions/sol-transfer-instruction.ts +13 -0
  154. package/src/instructions/transfer-instruction.ts +22 -0
  155. package/src/pool/pool-manager.ts +85 -0
  156. package/src/pool_manager.ts +197 -0
  157. package/src/sell_token.ts +82 -0
  158. package/src/transaction/bundle.ts +116 -0
  159. package/src/transaction/execute-rpc.ts +35 -0
  160. package/src/transaction/sign-tx.ts +5 -0
  161. package/src/transaction/transaction-exectue.ts +134 -0
  162. package/src/transaction-helper/check_transaction.ts +23 -0
  163. package/src/transaction-helper/transaction.ts +90 -0
  164. package/src/upload-metadata/upload-metaplex.ts +51 -0
  165. package/src/upload-metadata/upload-nftstorage.ts +0 -0
  166. package/src/upload-metadata/upload-pinata.ts +8 -0
  167. package/src/upload-metadata/utility.ts +10 -0
  168. package/src/utility.ts +127 -0
  169. package/src/utils/budget.ts +104 -0
  170. package/src/utils/error-helper.ts +20 -0
  171. package/src/utils/get-balance.ts +55 -0
  172. package/src/utils/util.ts +111 -0
  173. package/src/validation_check.ts +26 -0
@@ -0,0 +1,464 @@
1
+ import {
2
+ Connection,
3
+ Keypair,
4
+ PublicKey,
5
+ SYSVAR_RENT_PUBKEY,
6
+ SystemProgram,
7
+ TransactionInstruction
8
+ } from "@solana/web3.js";
9
+ import { EnvironmentManager, SPL_ERROR, TOKEN_INFO } from "./global";
10
+ import {
11
+ CacheLTA,
12
+ InnerSimpleTransaction,
13
+ InstructionType,
14
+ MARKET_STATE_LAYOUT_V2,
15
+ ProgramId,
16
+ Token,
17
+ TxVersion,
18
+ buildSimpleTransaction,
19
+ generatePubKey,
20
+ splitTxAndSigners,
21
+ struct,
22
+ u16,
23
+ u32,
24
+ u64,
25
+ u8
26
+ } from "@raydium-io/raydium-sdk";
27
+ import {
28
+ TOKEN_PROGRAM_ID,
29
+ createInitializeAccountInstruction,
30
+ getMint
31
+ } from "@solana/spl-token";
32
+ import BN from "bn.js";
33
+ import * as transactions from "./transaction-helper/transaction";
34
+
35
+ interface CreateMarketInfo {
36
+ connection: Connection;
37
+ wallet: PublicKey;
38
+ baseInfo: Token;
39
+ quoteInfo: Token;
40
+ lotSize: number;
41
+ tickSize: number;
42
+ dexProgramId: PublicKey;
43
+ makeTxVersion: TxVersion;
44
+ lookupTableCache: CacheLTA | undefined;
45
+ }
46
+
47
+ async function makeCreateMarketInstruction({
48
+ connection,
49
+ wallet,
50
+ baseInfo,
51
+ quoteInfo,
52
+ lotSize, // 1
53
+ tickSize, // 0.01
54
+ dexProgramId,
55
+ makeTxVersion,
56
+ lookupTableCache
57
+ }: CreateMarketInfo) {
58
+ const market = generatePubKey({
59
+ fromPublicKey: wallet,
60
+ programId: dexProgramId
61
+ });
62
+ const requestQueue = generatePubKey({
63
+ fromPublicKey: wallet,
64
+ programId: dexProgramId
65
+ });
66
+ const eventQueue = generatePubKey({
67
+ fromPublicKey: wallet,
68
+ programId: dexProgramId
69
+ });
70
+ const bids = generatePubKey({
71
+ fromPublicKey: wallet,
72
+ programId: dexProgramId
73
+ });
74
+ const asks = generatePubKey({
75
+ fromPublicKey: wallet,
76
+ programId: dexProgramId
77
+ });
78
+ const baseVault = generatePubKey({
79
+ fromPublicKey: wallet,
80
+ programId: TOKEN_PROGRAM_ID
81
+ });
82
+ const quoteVault = generatePubKey({
83
+ fromPublicKey: wallet,
84
+ programId: TOKEN_PROGRAM_ID
85
+ });
86
+ const feeRateBps = 0;
87
+ const quoteDustThreshold = new BN(100);
88
+
89
+ function getVaultOwnerAndNonce() {
90
+ const vaultSignerNonce = new BN(0);
91
+ while (true) {
92
+ try {
93
+ const vaultOwner = PublicKey.createProgramAddressSync(
94
+ [
95
+ market.publicKey.toBuffer(),
96
+ vaultSignerNonce.toArrayLike(Buffer, "le", 8)
97
+ ],
98
+ dexProgramId
99
+ );
100
+ return { vaultOwner, vaultSignerNonce };
101
+ } catch (e) {
102
+ vaultSignerNonce.iaddn(1);
103
+ if (vaultSignerNonce.gt(new BN(25555)))
104
+ throw Error("find vault owner error");
105
+ }
106
+ }
107
+ }
108
+
109
+ function initializeMarketInstruction({
110
+ programId,
111
+ marketInfo
112
+ }: {
113
+ programId: PublicKey;
114
+ marketInfo: {
115
+ id: PublicKey;
116
+ requestQueue: PublicKey;
117
+ eventQueue: PublicKey;
118
+ bids: PublicKey;
119
+ asks: PublicKey;
120
+ baseVault: PublicKey;
121
+ quoteVault: PublicKey;
122
+ baseMint: PublicKey;
123
+ quoteMint: PublicKey;
124
+ baseLotSize: BN;
125
+ quoteLotSize: BN;
126
+ feeRateBps: number;
127
+ vaultSignerNonce: BN;
128
+ quoteDustThreshold: BN;
129
+ authority?: PublicKey;
130
+ pruneAuthority?: PublicKey;
131
+ };
132
+ }) {
133
+ const dataLayout = struct([
134
+ u8("version"),
135
+ u32("instruction"),
136
+ u64("baseLotSize"),
137
+ u64("quoteLotSize"),
138
+ u16("feeRateBps"),
139
+ u64("vaultSignerNonce"),
140
+ u64("quoteDustThreshold")
141
+ ]);
142
+
143
+ const keys = [
144
+ { pubkey: marketInfo.id, isSigner: false, isWritable: true },
145
+ { pubkey: marketInfo.requestQueue, isSigner: false, isWritable: true },
146
+ { pubkey: marketInfo.eventQueue, isSigner: false, isWritable: true },
147
+ { pubkey: marketInfo.bids, isSigner: false, isWritable: true },
148
+ { pubkey: marketInfo.asks, isSigner: false, isWritable: true },
149
+ { pubkey: marketInfo.baseVault, isSigner: false, isWritable: true },
150
+ { pubkey: marketInfo.quoteVault, isSigner: false, isWritable: true },
151
+ { pubkey: marketInfo.baseMint, isSigner: false, isWritable: false },
152
+ { pubkey: marketInfo.quoteMint, isSigner: false, isWritable: false },
153
+ // Use a dummy address if using the new dex upgrade to save tx space.
154
+ {
155
+ pubkey: marketInfo.authority
156
+ ? marketInfo.quoteMint
157
+ : SYSVAR_RENT_PUBKEY,
158
+ isSigner: false,
159
+ isWritable: false
160
+ }
161
+ ]
162
+ .concat(
163
+ marketInfo.authority
164
+ ? { pubkey: marketInfo.authority, isSigner: false, isWritable: false }
165
+ : []
166
+ )
167
+ .concat(
168
+ marketInfo.authority && marketInfo.pruneAuthority
169
+ ? {
170
+ pubkey: marketInfo.pruneAuthority,
171
+ isSigner: false,
172
+ isWritable: false
173
+ }
174
+ : []
175
+ );
176
+
177
+ const data = Buffer.alloc(dataLayout.span);
178
+ dataLayout.encode(
179
+ {
180
+ version: 0,
181
+ instruction: 0,
182
+ baseLotSize: marketInfo.baseLotSize,
183
+ quoteLotSize: marketInfo.quoteLotSize,
184
+ feeRateBps: marketInfo.feeRateBps,
185
+ vaultSignerNonce: marketInfo.vaultSignerNonce,
186
+ quoteDustThreshold: marketInfo.quoteDustThreshold
187
+ },
188
+ data
189
+ );
190
+
191
+ return new TransactionInstruction({
192
+ keys,
193
+ programId,
194
+ data
195
+ });
196
+ }
197
+
198
+ const { vaultOwner, vaultSignerNonce } = getVaultOwnerAndNonce();
199
+
200
+ const ZERO = new BN(0);
201
+ const baseLotSize = new BN(Math.round(10 ** baseInfo.decimals * lotSize));
202
+ const quoteLotSize = new BN(
203
+ Math.round(lotSize * 10 ** quoteInfo.decimals * tickSize)
204
+ );
205
+ if (baseLotSize.eq(ZERO)) throw Error("lot size is too small");
206
+ if (quoteLotSize.eq(ZERO)) throw Error("tick size or lot size is too small");
207
+
208
+ const ins1: TransactionInstruction[] = [];
209
+ const accountLamports =
210
+ await connection.getMinimumBalanceForRentExemption(165);
211
+ ins1.push(
212
+ SystemProgram.createAccountWithSeed({
213
+ fromPubkey: wallet,
214
+ basePubkey: wallet,
215
+ seed: baseVault.seed,
216
+ newAccountPubkey: baseVault.publicKey,
217
+ lamports: accountLamports,
218
+ space: 165,
219
+ programId: TOKEN_PROGRAM_ID
220
+ }),
221
+ SystemProgram.createAccountWithSeed({
222
+ fromPubkey: wallet,
223
+ basePubkey: wallet,
224
+ seed: quoteVault.seed,
225
+ newAccountPubkey: quoteVault.publicKey,
226
+ lamports: accountLamports,
227
+ space: 165,
228
+ programId: TOKEN_PROGRAM_ID
229
+ }),
230
+ createInitializeAccountInstruction(
231
+ baseVault.publicKey,
232
+ baseInfo.mint,
233
+ vaultOwner
234
+ ),
235
+ createInitializeAccountInstruction(
236
+ quoteVault.publicKey,
237
+ quoteInfo.mint,
238
+ vaultOwner
239
+ )
240
+ );
241
+
242
+ const EVENT_QUEUE_ITEMS = 128; // Default: 2978
243
+ const REQUEST_QUEUE_ITEMS = 63; // Default: 63
244
+ const ORDERBOOK_ITEMS = 201; // Default: 909
245
+
246
+ const eventQueueSpace = EVENT_QUEUE_ITEMS * 88 + 44 + 48;
247
+ const requestQueueSpace = REQUEST_QUEUE_ITEMS * 80 + 44 + 48;
248
+ const orderBookSpace = ORDERBOOK_ITEMS * 80 + 44 + 48;
249
+
250
+ const ins2: TransactionInstruction[] = [];
251
+ ins2.push(
252
+ SystemProgram.createAccountWithSeed({
253
+ fromPubkey: wallet,
254
+ basePubkey: wallet,
255
+ seed: market.seed,
256
+ newAccountPubkey: market.publicKey,
257
+ lamports: await connection.getMinimumBalanceForRentExemption(
258
+ MARKET_STATE_LAYOUT_V2.span
259
+ ),
260
+ space: MARKET_STATE_LAYOUT_V2.span,
261
+ programId: dexProgramId
262
+ }),
263
+ SystemProgram.createAccountWithSeed({
264
+ fromPubkey: wallet,
265
+ basePubkey: wallet,
266
+ seed: requestQueue.seed,
267
+ newAccountPubkey: requestQueue.publicKey,
268
+ lamports:
269
+ await connection.getMinimumBalanceForRentExemption(requestQueueSpace),
270
+ space: requestQueueSpace,
271
+ programId: dexProgramId
272
+ }),
273
+ SystemProgram.createAccountWithSeed({
274
+ fromPubkey: wallet,
275
+ basePubkey: wallet,
276
+ seed: eventQueue.seed,
277
+ newAccountPubkey: eventQueue.publicKey,
278
+ lamports:
279
+ await connection.getMinimumBalanceForRentExemption(eventQueueSpace),
280
+ space: eventQueueSpace,
281
+ programId: dexProgramId
282
+ }),
283
+ SystemProgram.createAccountWithSeed({
284
+ fromPubkey: wallet,
285
+ basePubkey: wallet,
286
+ seed: bids.seed,
287
+ newAccountPubkey: bids.publicKey,
288
+ lamports:
289
+ await connection.getMinimumBalanceForRentExemption(orderBookSpace),
290
+ space: orderBookSpace,
291
+ programId: dexProgramId
292
+ }),
293
+ SystemProgram.createAccountWithSeed({
294
+ fromPubkey: wallet,
295
+ basePubkey: wallet,
296
+ seed: asks.seed,
297
+ newAccountPubkey: asks.publicKey,
298
+ lamports:
299
+ await connection.getMinimumBalanceForRentExemption(orderBookSpace),
300
+ space: orderBookSpace,
301
+ programId: dexProgramId
302
+ }),
303
+ initializeMarketInstruction({
304
+ programId: dexProgramId,
305
+ marketInfo: {
306
+ id: market.publicKey,
307
+ requestQueue: requestQueue.publicKey,
308
+ eventQueue: eventQueue.publicKey,
309
+ bids: bids.publicKey,
310
+ asks: asks.publicKey,
311
+ baseVault: baseVault.publicKey,
312
+ quoteVault: quoteVault.publicKey,
313
+ baseMint: baseInfo.mint,
314
+ quoteMint: quoteInfo.mint,
315
+ baseLotSize: baseLotSize,
316
+ quoteLotSize: quoteLotSize,
317
+ feeRateBps: feeRateBps,
318
+ vaultSignerNonce: vaultSignerNonce,
319
+ quoteDustThreshold: quoteDustThreshold
320
+ }
321
+ })
322
+ );
323
+
324
+ const ins = {
325
+ address: {
326
+ marketId: market.publicKey,
327
+ requestQueue: requestQueue.publicKey,
328
+ eventQueue: eventQueue.publicKey,
329
+ bids: bids.publicKey,
330
+ asks: asks.publicKey,
331
+ baseVault: baseVault.publicKey,
332
+ quoteVault: quoteVault.publicKey,
333
+ baseMint: baseInfo.mint,
334
+ quoteMint: quoteInfo.mint
335
+ },
336
+ innerTransactions: [
337
+ {
338
+ instructions: ins1,
339
+ signers: [],
340
+ instructionTypes: [
341
+ InstructionType.createAccount,
342
+ InstructionType.createAccount,
343
+ InstructionType.initAccount,
344
+ InstructionType.initAccount
345
+ ]
346
+ },
347
+ {
348
+ instructions: ins2,
349
+ signers: [],
350
+ instructionTypes: [
351
+ InstructionType.createAccount,
352
+ InstructionType.createAccount,
353
+ InstructionType.createAccount,
354
+ InstructionType.createAccount,
355
+ InstructionType.createAccount,
356
+ InstructionType.initMarket
357
+ ]
358
+ }
359
+ ]
360
+ };
361
+
362
+ return {
363
+ address: ins.address,
364
+ innerTransactions: await splitTxAndSigners({
365
+ connection,
366
+ makeTxVersion,
367
+ computeBudgetConfig: undefined,
368
+ payer: wallet,
369
+ innerTransaction: ins.innerTransactions,
370
+ lookupTableCache
371
+ })
372
+ };
373
+ }
374
+
375
+ export const createOpenBookMarket = async (
376
+ connection: Connection,
377
+ token_owner: Keypair,
378
+ token_address: string,
379
+ min_order_size: number = 1,
380
+ tick_size = 0.01
381
+ ): Promise<SPL_ERROR> => {
382
+ if (
383
+ token_owner.publicKey.toBase58().length <= 0 ||
384
+ token_address.length <= 0
385
+ ) {
386
+ console.log(
387
+ "Error: [Create Open Book Market] invalid argument for create open book market"
388
+ );
389
+ return SPL_ERROR.E_INVALID_ARGUE;
390
+ }
391
+
392
+ try {
393
+ const token_mint = new PublicKey(token_address);
394
+ const mint_info = await getMint(connection, token_mint);
395
+
396
+ const base_token = new Token(
397
+ TOKEN_PROGRAM_ID,
398
+ token_address,
399
+ mint_info.decimals
400
+ );
401
+
402
+ const quote_token_info: TOKEN_INFO = EnvironmentManager.getQuoteTokenInfo();
403
+ const quote_token = new Token(
404
+ TOKEN_PROGRAM_ID,
405
+ quote_token_info.address,
406
+ quote_token_info.decimal,
407
+ quote_token_info.symbol,
408
+ quote_token_info.name
409
+ );
410
+
411
+ console.log(
412
+ "[Create Open Book Market]<--------------------make marekt instruction"
413
+ );
414
+
415
+ const { innerTransactions, address } = await makeCreateMarketInstruction({
416
+ connection: connection,
417
+ wallet: token_owner.publicKey,
418
+ baseInfo: base_token,
419
+ quoteInfo: quote_token,
420
+ lotSize: min_order_size,
421
+ tickSize: tick_size,
422
+ dexProgramId: EnvironmentManager.getProgramID().OPENBOOK_MARKET,
423
+ makeTxVersion: TxVersion.V0,
424
+ lookupTableCache: EnvironmentManager.getCacheLTA()
425
+ });
426
+
427
+ console.log(
428
+ "[Create Open Book Market]<--------------------create simple transaction"
429
+ );
430
+
431
+ const txns = await buildSimpleTransaction({
432
+ connection: connection,
433
+ makeTxVersion: TxVersion.V0,
434
+ payer: token_owner.publicKey,
435
+ innerTransactions: innerTransactions,
436
+ addLookupTableInfo: EnvironmentManager.getCacheLTA()
437
+ });
438
+
439
+ console.log(
440
+ "[Create Open Book Market]<--------------------send and confirm transaction"
441
+ );
442
+
443
+ const txn_result = await transactions.sendAndConfirmTransactionsWithCheck(
444
+ connection,
445
+ token_owner,
446
+ txns
447
+ );
448
+
449
+ if (txn_result !== SPL_ERROR.E_OK) {
450
+ console.error(
451
+ "Error: [Create Open Book Market] failed to send and confirm transaction"
452
+ );
453
+ return SPL_ERROR.E_FAIL;
454
+ }
455
+ } catch (error) {
456
+ console.error("Error: [Create Open Book Market] error occured: ", error);
457
+ return SPL_ERROR.E_FAIL;
458
+ }
459
+
460
+ console.log(
461
+ "Success: [Create Open Book Market] Success to create open book market id"
462
+ );
463
+ return SPL_ERROR.E_OK;
464
+ };
@@ -0,0 +1,120 @@
1
+ import { Connection, Keypair, PublicKey } from "@solana/web3.js";
2
+ import { EnvironmentManager, SPL_ERROR, TX_RET } from "./global";
3
+ import { TOKEN_PROGRAM_ID, getMint } from "@solana/spl-token";
4
+ import {
5
+ Liquidity,
6
+ Token,
7
+ TxVersion,
8
+ buildSimpleTransaction
9
+ } from "@raydium-io/raydium-sdk";
10
+ import { Market } from "@project-serum/serum";
11
+
12
+ import * as utiles from "./utility";
13
+ import { BN } from "bn.js";
14
+
15
+ export const createPool = async (
16
+ connection: Connection,
17
+ token_owner: Keypair,
18
+ token_address: string,
19
+ input_token_amount: number,
20
+ input_quote_amount: number
21
+ ): Promise<TX_RET> => {
22
+ try {
23
+ if (token_address.length <= 0) {
24
+ console.log("Error: [Create Pool] invalid argument for create pool");
25
+ return { result: SPL_ERROR.E_INVALID_ARGUE, value: undefined };
26
+ }
27
+
28
+ console.log("<---------------------[Create Pool]-----------------------");
29
+
30
+ const token_mint = new PublicKey(token_address);
31
+ const mint_info = await getMint(connection, token_mint);
32
+
33
+ const base_token = new Token(
34
+ TOKEN_PROGRAM_ID,
35
+ token_address,
36
+ mint_info.decimals
37
+ );
38
+ const quote_token_info = EnvironmentManager.getQuoteTokenInfo();
39
+ const quote_token = new Token(
40
+ TOKEN_PROGRAM_ID,
41
+ quote_token_info.address,
42
+ quote_token_info.decimal,
43
+ quote_token_info.symbol,
44
+ quote_token_info.name
45
+ );
46
+
47
+ const accounts = await Market.findAccountsByMints(
48
+ connection,
49
+ base_token.mint,
50
+ quote_token.mint,
51
+ EnvironmentManager.getProgramID().OPENBOOK_MARKET
52
+ );
53
+
54
+ if (accounts.length === 0) {
55
+ throw "Get market account failed";
56
+ }
57
+
58
+ console.log("Market Found");
59
+
60
+ const market_id = accounts[0].publicKey;
61
+ const start_time = Math.floor(Date.now() / 1000);
62
+ const base_amount = utiles.xWeiAmount(
63
+ input_token_amount,
64
+ base_token.decimals
65
+ );
66
+ const quote_amount = utiles.xWeiAmount(
67
+ input_quote_amount,
68
+ quote_token.decimals
69
+ );
70
+
71
+ const wallet_token_accounts = await utiles.getWalletAccounts(
72
+ connection,
73
+ token_owner.publicKey
74
+ );
75
+
76
+ if (!wallet_token_accounts || wallet_token_accounts.length <= 0) {
77
+ throw "Get wallet account failed";
78
+ }
79
+
80
+ const { innerTransactions, address } =
81
+ await Liquidity.makeCreatePoolV4InstructionV2Simple({
82
+ connection: connection,
83
+ programId: EnvironmentManager.getProgramID().AmmV4,
84
+ marketInfo: {
85
+ marketId: market_id,
86
+ programId: EnvironmentManager.getProgramID().OPENBOOK_MARKET
87
+ },
88
+ baseMintInfo: base_token,
89
+ quoteMintInfo: quote_token,
90
+ baseAmount: base_amount,
91
+ quoteAmount: quote_amount,
92
+ startTime: new BN(start_time),
93
+ ownerInfo: {
94
+ feePayer: token_owner.publicKey,
95
+ wallet: token_owner.publicKey,
96
+ tokenAccounts: wallet_token_accounts,
97
+ useSOLBalance: true
98
+ },
99
+ makeTxVersion: TxVersion.V0,
100
+ associatedOnly: false,
101
+ checkCreateATAOwner: true,
102
+ feeDestinationId: EnvironmentManager.getFeeDestinationId()
103
+ });
104
+
105
+ const txns = await buildSimpleTransaction({
106
+ connection: connection,
107
+ makeTxVersion: TxVersion.V0,
108
+ payer: token_owner.publicKey,
109
+ innerTransactions: innerTransactions,
110
+ addLookupTableInfo: EnvironmentManager.getCacheLTA(),
111
+ recentBlockhash: (await connection.getLatestBlockhash()).blockhash
112
+ });
113
+
114
+ console.log("Success: [Create Pool] made transaction successfully");
115
+ return { result: SPL_ERROR.E_OK, value: txns };
116
+ } catch (error) {
117
+ console.error("Error: [Create Pool] err: ", error);
118
+ return { result: SPL_ERROR.E_FAIL, value: undefined };
119
+ }
120
+ };