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,310 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { PublicKey, SYSVAR_RENT_PUBKEY, SystemProgram, TransactionInstruction } from "@solana/web3.js";
11
+ import { EnvironmentManager, SPL_ERROR } from "./global";
12
+ import { InstructionType, MARKET_STATE_LAYOUT_V2, Token, TxVersion, buildSimpleTransaction, generatePubKey, splitTxAndSigners, struct, u16, u32, u64, u8 } from "@raydium-io/raydium-sdk";
13
+ import { TOKEN_PROGRAM_ID, createInitializeAccountInstruction, getMint } from "@solana/spl-token";
14
+ import BN from "bn.js";
15
+ import * as transactions from "./transaction-helper/transaction";
16
+ function makeCreateMarketInstruction(_a) {
17
+ return __awaiter(this, arguments, void 0, function* ({ connection, wallet, baseInfo, quoteInfo, lotSize, // 1
18
+ tickSize, // 0.01
19
+ dexProgramId, makeTxVersion, lookupTableCache }) {
20
+ const market = generatePubKey({
21
+ fromPublicKey: wallet,
22
+ programId: dexProgramId
23
+ });
24
+ const requestQueue = generatePubKey({
25
+ fromPublicKey: wallet,
26
+ programId: dexProgramId
27
+ });
28
+ const eventQueue = generatePubKey({
29
+ fromPublicKey: wallet,
30
+ programId: dexProgramId
31
+ });
32
+ const bids = generatePubKey({
33
+ fromPublicKey: wallet,
34
+ programId: dexProgramId
35
+ });
36
+ const asks = generatePubKey({
37
+ fromPublicKey: wallet,
38
+ programId: dexProgramId
39
+ });
40
+ const baseVault = generatePubKey({
41
+ fromPublicKey: wallet,
42
+ programId: TOKEN_PROGRAM_ID
43
+ });
44
+ const quoteVault = generatePubKey({
45
+ fromPublicKey: wallet,
46
+ programId: TOKEN_PROGRAM_ID
47
+ });
48
+ const feeRateBps = 0;
49
+ const quoteDustThreshold = new BN(100);
50
+ function getVaultOwnerAndNonce() {
51
+ const vaultSignerNonce = new BN(0);
52
+ while (true) {
53
+ try {
54
+ const vaultOwner = PublicKey.createProgramAddressSync([
55
+ market.publicKey.toBuffer(),
56
+ vaultSignerNonce.toArrayLike(Buffer, "le", 8)
57
+ ], dexProgramId);
58
+ return { vaultOwner, vaultSignerNonce };
59
+ }
60
+ catch (e) {
61
+ vaultSignerNonce.iaddn(1);
62
+ if (vaultSignerNonce.gt(new BN(25555)))
63
+ throw Error("find vault owner error");
64
+ }
65
+ }
66
+ }
67
+ function initializeMarketInstruction({ programId, marketInfo }) {
68
+ const dataLayout = struct([
69
+ u8("version"),
70
+ u32("instruction"),
71
+ u64("baseLotSize"),
72
+ u64("quoteLotSize"),
73
+ u16("feeRateBps"),
74
+ u64("vaultSignerNonce"),
75
+ u64("quoteDustThreshold")
76
+ ]);
77
+ const keys = [
78
+ { pubkey: marketInfo.id, isSigner: false, isWritable: true },
79
+ { pubkey: marketInfo.requestQueue, isSigner: false, isWritable: true },
80
+ { pubkey: marketInfo.eventQueue, isSigner: false, isWritable: true },
81
+ { pubkey: marketInfo.bids, isSigner: false, isWritable: true },
82
+ { pubkey: marketInfo.asks, isSigner: false, isWritable: true },
83
+ { pubkey: marketInfo.baseVault, isSigner: false, isWritable: true },
84
+ { pubkey: marketInfo.quoteVault, isSigner: false, isWritable: true },
85
+ { pubkey: marketInfo.baseMint, isSigner: false, isWritable: false },
86
+ { pubkey: marketInfo.quoteMint, isSigner: false, isWritable: false },
87
+ // Use a dummy address if using the new dex upgrade to save tx space.
88
+ {
89
+ pubkey: marketInfo.authority
90
+ ? marketInfo.quoteMint
91
+ : SYSVAR_RENT_PUBKEY,
92
+ isSigner: false,
93
+ isWritable: false
94
+ }
95
+ ]
96
+ .concat(marketInfo.authority
97
+ ? { pubkey: marketInfo.authority, isSigner: false, isWritable: false }
98
+ : [])
99
+ .concat(marketInfo.authority && marketInfo.pruneAuthority
100
+ ? {
101
+ pubkey: marketInfo.pruneAuthority,
102
+ isSigner: false,
103
+ isWritable: false
104
+ }
105
+ : []);
106
+ const data = Buffer.alloc(dataLayout.span);
107
+ dataLayout.encode({
108
+ version: 0,
109
+ instruction: 0,
110
+ baseLotSize: marketInfo.baseLotSize,
111
+ quoteLotSize: marketInfo.quoteLotSize,
112
+ feeRateBps: marketInfo.feeRateBps,
113
+ vaultSignerNonce: marketInfo.vaultSignerNonce,
114
+ quoteDustThreshold: marketInfo.quoteDustThreshold
115
+ }, data);
116
+ return new TransactionInstruction({
117
+ keys,
118
+ programId,
119
+ data
120
+ });
121
+ }
122
+ const { vaultOwner, vaultSignerNonce } = getVaultOwnerAndNonce();
123
+ const ZERO = new BN(0);
124
+ const baseLotSize = new BN(Math.round(Math.pow(10, baseInfo.decimals) * lotSize));
125
+ const quoteLotSize = new BN(Math.round(lotSize * Math.pow(10, quoteInfo.decimals) * tickSize));
126
+ if (baseLotSize.eq(ZERO))
127
+ throw Error("lot size is too small");
128
+ if (quoteLotSize.eq(ZERO))
129
+ throw Error("tick size or lot size is too small");
130
+ const ins1 = [];
131
+ const accountLamports = yield connection.getMinimumBalanceForRentExemption(165);
132
+ ins1.push(SystemProgram.createAccountWithSeed({
133
+ fromPubkey: wallet,
134
+ basePubkey: wallet,
135
+ seed: baseVault.seed,
136
+ newAccountPubkey: baseVault.publicKey,
137
+ lamports: accountLamports,
138
+ space: 165,
139
+ programId: TOKEN_PROGRAM_ID
140
+ }), SystemProgram.createAccountWithSeed({
141
+ fromPubkey: wallet,
142
+ basePubkey: wallet,
143
+ seed: quoteVault.seed,
144
+ newAccountPubkey: quoteVault.publicKey,
145
+ lamports: accountLamports,
146
+ space: 165,
147
+ programId: TOKEN_PROGRAM_ID
148
+ }), createInitializeAccountInstruction(baseVault.publicKey, baseInfo.mint, vaultOwner), createInitializeAccountInstruction(quoteVault.publicKey, quoteInfo.mint, vaultOwner));
149
+ const EVENT_QUEUE_ITEMS = 128; // Default: 2978
150
+ const REQUEST_QUEUE_ITEMS = 63; // Default: 63
151
+ const ORDERBOOK_ITEMS = 201; // Default: 909
152
+ const eventQueueSpace = EVENT_QUEUE_ITEMS * 88 + 44 + 48;
153
+ const requestQueueSpace = REQUEST_QUEUE_ITEMS * 80 + 44 + 48;
154
+ const orderBookSpace = ORDERBOOK_ITEMS * 80 + 44 + 48;
155
+ const ins2 = [];
156
+ ins2.push(SystemProgram.createAccountWithSeed({
157
+ fromPubkey: wallet,
158
+ basePubkey: wallet,
159
+ seed: market.seed,
160
+ newAccountPubkey: market.publicKey,
161
+ lamports: yield connection.getMinimumBalanceForRentExemption(MARKET_STATE_LAYOUT_V2.span),
162
+ space: MARKET_STATE_LAYOUT_V2.span,
163
+ programId: dexProgramId
164
+ }), SystemProgram.createAccountWithSeed({
165
+ fromPubkey: wallet,
166
+ basePubkey: wallet,
167
+ seed: requestQueue.seed,
168
+ newAccountPubkey: requestQueue.publicKey,
169
+ lamports: yield connection.getMinimumBalanceForRentExemption(requestQueueSpace),
170
+ space: requestQueueSpace,
171
+ programId: dexProgramId
172
+ }), SystemProgram.createAccountWithSeed({
173
+ fromPubkey: wallet,
174
+ basePubkey: wallet,
175
+ seed: eventQueue.seed,
176
+ newAccountPubkey: eventQueue.publicKey,
177
+ lamports: yield connection.getMinimumBalanceForRentExemption(eventQueueSpace),
178
+ space: eventQueueSpace,
179
+ programId: dexProgramId
180
+ }), SystemProgram.createAccountWithSeed({
181
+ fromPubkey: wallet,
182
+ basePubkey: wallet,
183
+ seed: bids.seed,
184
+ newAccountPubkey: bids.publicKey,
185
+ lamports: yield connection.getMinimumBalanceForRentExemption(orderBookSpace),
186
+ space: orderBookSpace,
187
+ programId: dexProgramId
188
+ }), SystemProgram.createAccountWithSeed({
189
+ fromPubkey: wallet,
190
+ basePubkey: wallet,
191
+ seed: asks.seed,
192
+ newAccountPubkey: asks.publicKey,
193
+ lamports: yield connection.getMinimumBalanceForRentExemption(orderBookSpace),
194
+ space: orderBookSpace,
195
+ programId: dexProgramId
196
+ }), initializeMarketInstruction({
197
+ programId: dexProgramId,
198
+ marketInfo: {
199
+ id: market.publicKey,
200
+ requestQueue: requestQueue.publicKey,
201
+ eventQueue: eventQueue.publicKey,
202
+ bids: bids.publicKey,
203
+ asks: asks.publicKey,
204
+ baseVault: baseVault.publicKey,
205
+ quoteVault: quoteVault.publicKey,
206
+ baseMint: baseInfo.mint,
207
+ quoteMint: quoteInfo.mint,
208
+ baseLotSize: baseLotSize,
209
+ quoteLotSize: quoteLotSize,
210
+ feeRateBps: feeRateBps,
211
+ vaultSignerNonce: vaultSignerNonce,
212
+ quoteDustThreshold: quoteDustThreshold
213
+ }
214
+ }));
215
+ const ins = {
216
+ address: {
217
+ marketId: market.publicKey,
218
+ requestQueue: requestQueue.publicKey,
219
+ eventQueue: eventQueue.publicKey,
220
+ bids: bids.publicKey,
221
+ asks: asks.publicKey,
222
+ baseVault: baseVault.publicKey,
223
+ quoteVault: quoteVault.publicKey,
224
+ baseMint: baseInfo.mint,
225
+ quoteMint: quoteInfo.mint
226
+ },
227
+ innerTransactions: [
228
+ {
229
+ instructions: ins1,
230
+ signers: [],
231
+ instructionTypes: [
232
+ InstructionType.createAccount,
233
+ InstructionType.createAccount,
234
+ InstructionType.initAccount,
235
+ InstructionType.initAccount
236
+ ]
237
+ },
238
+ {
239
+ instructions: ins2,
240
+ signers: [],
241
+ instructionTypes: [
242
+ InstructionType.createAccount,
243
+ InstructionType.createAccount,
244
+ InstructionType.createAccount,
245
+ InstructionType.createAccount,
246
+ InstructionType.createAccount,
247
+ InstructionType.initMarket
248
+ ]
249
+ }
250
+ ]
251
+ };
252
+ return {
253
+ address: ins.address,
254
+ innerTransactions: yield splitTxAndSigners({
255
+ connection,
256
+ makeTxVersion,
257
+ computeBudgetConfig: undefined,
258
+ payer: wallet,
259
+ innerTransaction: ins.innerTransactions,
260
+ lookupTableCache
261
+ })
262
+ };
263
+ });
264
+ }
265
+ export const createOpenBookMarket = (connection_1, token_owner_1, token_address_1, ...args_1) => __awaiter(void 0, [connection_1, token_owner_1, token_address_1, ...args_1], void 0, function* (connection, token_owner, token_address, min_order_size = 1, tick_size = 0.01) {
266
+ if (token_owner.publicKey.toBase58().length <= 0 ||
267
+ token_address.length <= 0) {
268
+ console.log("Error: [Create Open Book Market] invalid argument for create open book market");
269
+ return SPL_ERROR.E_INVALID_ARGUE;
270
+ }
271
+ try {
272
+ const token_mint = new PublicKey(token_address);
273
+ const mint_info = yield getMint(connection, token_mint);
274
+ const base_token = new Token(TOKEN_PROGRAM_ID, token_address, mint_info.decimals);
275
+ const quote_token_info = EnvironmentManager.getQuoteTokenInfo();
276
+ const quote_token = new Token(TOKEN_PROGRAM_ID, quote_token_info.address, quote_token_info.decimal, quote_token_info.symbol, quote_token_info.name);
277
+ console.log("[Create Open Book Market]<--------------------make marekt instruction");
278
+ const { innerTransactions, address } = yield makeCreateMarketInstruction({
279
+ connection: connection,
280
+ wallet: token_owner.publicKey,
281
+ baseInfo: base_token,
282
+ quoteInfo: quote_token,
283
+ lotSize: min_order_size,
284
+ tickSize: tick_size,
285
+ dexProgramId: EnvironmentManager.getProgramID().OPENBOOK_MARKET,
286
+ makeTxVersion: TxVersion.V0,
287
+ lookupTableCache: EnvironmentManager.getCacheLTA()
288
+ });
289
+ console.log("[Create Open Book Market]<--------------------create simple transaction");
290
+ const txns = yield buildSimpleTransaction({
291
+ connection: connection,
292
+ makeTxVersion: TxVersion.V0,
293
+ payer: token_owner.publicKey,
294
+ innerTransactions: innerTransactions,
295
+ addLookupTableInfo: EnvironmentManager.getCacheLTA()
296
+ });
297
+ console.log("[Create Open Book Market]<--------------------send and confirm transaction");
298
+ const txn_result = yield transactions.sendAndConfirmTransactionsWithCheck(connection, token_owner, txns);
299
+ if (txn_result !== SPL_ERROR.E_OK) {
300
+ console.error("Error: [Create Open Book Market] failed to send and confirm transaction");
301
+ return SPL_ERROR.E_FAIL;
302
+ }
303
+ }
304
+ catch (error) {
305
+ console.error("Error: [Create Open Book Market] error occured: ", error);
306
+ return SPL_ERROR.E_FAIL;
307
+ }
308
+ console.log("Success: [Create Open Book Market] Success to create open book market id");
309
+ return SPL_ERROR.E_OK;
310
+ });
@@ -0,0 +1,4 @@
1
+ /// <reference types="jito-ts/node_modules/@solana/web3.js" />
2
+ import { Connection, Keypair } from "@solana/web3.js";
3
+ import { TX_RET } from "./global";
4
+ export declare const createPool: (connection: Connection, token_owner: Keypair, token_address: string, input_token_amount: number, input_quote_amount: number) => Promise<TX_RET>;
@@ -0,0 +1,80 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { PublicKey } from "@solana/web3.js";
11
+ import { EnvironmentManager, SPL_ERROR } from "./global";
12
+ import { TOKEN_PROGRAM_ID, getMint } from "@solana/spl-token";
13
+ import { Liquidity, Token, TxVersion, buildSimpleTransaction } from "@raydium-io/raydium-sdk";
14
+ import { Market } from "@project-serum/serum";
15
+ import * as utiles from "./utility";
16
+ import { BN } from "bn.js";
17
+ export const createPool = (connection, token_owner, token_address, input_token_amount, input_quote_amount) => __awaiter(void 0, void 0, void 0, function* () {
18
+ try {
19
+ if (token_address.length <= 0) {
20
+ console.log("Error: [Create Pool] invalid argument for create pool");
21
+ return { result: SPL_ERROR.E_INVALID_ARGUE, value: undefined };
22
+ }
23
+ console.log("<---------------------[Create Pool]-----------------------");
24
+ const token_mint = new PublicKey(token_address);
25
+ const mint_info = yield getMint(connection, token_mint);
26
+ const base_token = new Token(TOKEN_PROGRAM_ID, token_address, mint_info.decimals);
27
+ const quote_token_info = EnvironmentManager.getQuoteTokenInfo();
28
+ const quote_token = new Token(TOKEN_PROGRAM_ID, quote_token_info.address, quote_token_info.decimal, quote_token_info.symbol, quote_token_info.name);
29
+ const accounts = yield Market.findAccountsByMints(connection, base_token.mint, quote_token.mint, EnvironmentManager.getProgramID().OPENBOOK_MARKET);
30
+ if (accounts.length === 0) {
31
+ throw "Get market account failed";
32
+ }
33
+ console.log("Market Found");
34
+ const market_id = accounts[0].publicKey;
35
+ const start_time = Math.floor(Date.now() / 1000);
36
+ const base_amount = utiles.xWeiAmount(input_token_amount, base_token.decimals);
37
+ const quote_amount = utiles.xWeiAmount(input_quote_amount, quote_token.decimals);
38
+ const wallet_token_accounts = yield utiles.getWalletAccounts(connection, token_owner.publicKey);
39
+ if (!wallet_token_accounts || wallet_token_accounts.length <= 0) {
40
+ throw "Get wallet account failed";
41
+ }
42
+ const { innerTransactions, address } = yield Liquidity.makeCreatePoolV4InstructionV2Simple({
43
+ connection: connection,
44
+ programId: EnvironmentManager.getProgramID().AmmV4,
45
+ marketInfo: {
46
+ marketId: market_id,
47
+ programId: EnvironmentManager.getProgramID().OPENBOOK_MARKET
48
+ },
49
+ baseMintInfo: base_token,
50
+ quoteMintInfo: quote_token,
51
+ baseAmount: base_amount,
52
+ quoteAmount: quote_amount,
53
+ startTime: new BN(start_time),
54
+ ownerInfo: {
55
+ feePayer: token_owner.publicKey,
56
+ wallet: token_owner.publicKey,
57
+ tokenAccounts: wallet_token_accounts,
58
+ useSOLBalance: true
59
+ },
60
+ makeTxVersion: TxVersion.V0,
61
+ associatedOnly: false,
62
+ checkCreateATAOwner: true,
63
+ feeDestinationId: EnvironmentManager.getFeeDestinationId()
64
+ });
65
+ const txns = yield buildSimpleTransaction({
66
+ connection: connection,
67
+ makeTxVersion: TxVersion.V0,
68
+ payer: token_owner.publicKey,
69
+ innerTransactions: innerTransactions,
70
+ addLookupTableInfo: EnvironmentManager.getCacheLTA(),
71
+ recentBlockhash: (yield connection.getLatestBlockhash()).blockhash
72
+ });
73
+ console.log("Success: [Create Pool] made transaction successfully");
74
+ return { result: SPL_ERROR.E_OK, value: txns };
75
+ }
76
+ catch (error) {
77
+ console.error("Error: [Create Pool] err: ", error);
78
+ return { result: SPL_ERROR.E_FAIL, value: undefined };
79
+ }
80
+ });
@@ -0,0 +1,4 @@
1
+ /// <reference types="jito-ts/node_modules/@solana/web3.js" />
2
+ import { Connection, Keypair } from "@solana/web3.js";
3
+ import { TX_RET } from "./global";
4
+ export declare const createToken: (connection: Connection, token_owner: Keypair, name: string, symbol: string, decimal: number, total_supply: number, token_logo: string, description?: string) => Promise<TX_RET>;
@@ -0,0 +1,137 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { PublicKey, Transaction } from "@solana/web3.js";
11
+ import { EnvironmentManager, SPL_ERROR } from "./global";
12
+ import { createMint, getMint, getOrCreateAssociatedTokenAccount, mintTo } from "@solana/spl-token";
13
+ import { Metaplex, bundlrStorage, keypairIdentity, toMetaplexFile } from "@metaplex-foundation/js";
14
+ import { readFileSync } from "fs";
15
+ import { checkFileExists, xWeiAmount } from "./utility";
16
+ import { PROGRAM_ID, createCreateMetadataAccountV3Instruction } from "@metaplex-foundation/mpl-token-metadata";
17
+ import * as transaction from "./transaction-helper/transaction";
18
+ const totalSupplyMint = (connection, token_owner, token_addr, total_supply) => __awaiter(void 0, void 0, void 0, function* () {
19
+ const token_mint = new PublicKey(token_addr);
20
+ const mint_info = yield getMint(connection, token_mint);
21
+ try {
22
+ const owner_token_account = yield getOrCreateAssociatedTokenAccount(connection, token_owner, token_mint, token_owner.publicKey);
23
+ if (owner_token_account.address.toBase58().length <= 0) {
24
+ console.log("Error: [Total Supply Mint] failed to create associated token account");
25
+ return SPL_ERROR.E_TOTAL_MINT_FAIL;
26
+ }
27
+ const token_amount = xWeiAmount(total_supply, mint_info.decimals);
28
+ const mint_result = yield mintTo(connection, token_owner, token_mint, owner_token_account.address, token_owner, BigInt(token_amount.toString()));
29
+ if (mint_result.length <= 0) {
30
+ console.log("Error: [Total Supply Mint] failed to mint to owner wallet");
31
+ return SPL_ERROR.E_TOTAL_MINT_FAIL;
32
+ }
33
+ }
34
+ catch (error) {
35
+ console.log("Error: [Total Supply Mint] failed to mint to owner wallet");
36
+ return SPL_ERROR.E_TOTAL_MINT_FAIL;
37
+ }
38
+ return SPL_ERROR.E_OK;
39
+ });
40
+ const createTokenMetaData = (connection, token_owner, token_addr, name, symbol, token_logo, rpc_url, description) => __awaiter(void 0, void 0, void 0, function* () {
41
+ try {
42
+ const metaplex = Metaplex.make(connection)
43
+ .use(keypairIdentity(token_owner))
44
+ .use(bundlrStorage({
45
+ address: EnvironmentManager.getBundlrUrl(),
46
+ providerUrl: rpc_url,
47
+ timeout: 60000
48
+ }));
49
+ const buffer = readFileSync(token_logo);
50
+ const file = toMetaplexFile(buffer, "token-logo.png");
51
+ const logo_url = yield metaplex.storage().upload(file);
52
+ if (logo_url.length <= 0) {
53
+ console.log("Error: [Create Token Meta Data] failed to load metapelx data!!!");
54
+ return SPL_ERROR.E_FAIL;
55
+ }
56
+ const metaplex_data = {
57
+ name: name,
58
+ symbol: symbol,
59
+ image: logo_url,
60
+ description
61
+ };
62
+ const { uri } = yield metaplex.nfts().uploadMetadata(metaplex_data);
63
+ if (uri.length <= 0) {
64
+ console.log("Error: [Create Token Meta Data] failed to upload metaplex data!!!");
65
+ return SPL_ERROR.E_FAIL;
66
+ }
67
+ const token_mint = new PublicKey(token_addr);
68
+ const [metadata_PDA] = PublicKey.findProgramAddressSync([Buffer.from("metadata"), PROGRAM_ID.toBuffer(), token_mint.toBuffer()], PROGRAM_ID);
69
+ const token_meta_data = {
70
+ name: name,
71
+ symbol: symbol,
72
+ uri: uri,
73
+ sellerFeeBasisPoints: 0,
74
+ creators: null,
75
+ collection: null,
76
+ uses: null
77
+ };
78
+ const txn = new Transaction().add(createCreateMetadataAccountV3Instruction({
79
+ metadata: metadata_PDA,
80
+ mint: token_mint,
81
+ mintAuthority: token_owner.publicKey,
82
+ payer: token_owner.publicKey,
83
+ updateAuthority: token_owner.publicKey
84
+ }, {
85
+ createMetadataAccountArgsV3: {
86
+ data: token_meta_data,
87
+ isMutable: true,
88
+ collectionDetails: null
89
+ }
90
+ }));
91
+ if ((yield transaction.sendAndConfirmTransactionWithCheck(connection, token_owner, txn)) !== SPL_ERROR.E_OK) {
92
+ return SPL_ERROR.E_FAIL;
93
+ }
94
+ }
95
+ catch (error) {
96
+ console.log("Error: [Create Token Meta Data] failed to create meta data -", error);
97
+ return SPL_ERROR.E_FAIL;
98
+ }
99
+ return SPL_ERROR.E_OK;
100
+ });
101
+ export const createToken = (connection, token_owner, name, symbol, decimal, total_supply, token_logo, description) => __awaiter(void 0, void 0, void 0, function* () {
102
+ if (name.length <= 0 ||
103
+ symbol.length <= 0 ||
104
+ token_logo.length <= 0 ||
105
+ token_owner.publicKey.toBase58().length <= 0 ||
106
+ EnvironmentManager.getRpcNetUrl().length <= 0 ||
107
+ decimal <= 0 ||
108
+ total_supply <= 0) {
109
+ console.log("Error: [Create Token] invalid argument to create token!!!");
110
+ return { result: SPL_ERROR.E_INVALID_ARGUE, value: undefined };
111
+ }
112
+ if ((yield checkFileExists(token_logo)) === false) {
113
+ console.log("Error: [Create Token] invalid argument to create token - token logo path invalid!!!");
114
+ return { result: SPL_ERROR.E_INVALID_ARGUE, value: undefined };
115
+ }
116
+ console.log("<-----------------[Create Token]---------------------");
117
+ console.log("Name: ", name, "Symbol: ", symbol, "Decimal: ", decimal, "Total Supply: ", total_supply, "Token Logo: ", token_logo, "Token Description: ", description);
118
+ console.log("<-----------------[Create Token]---------------------");
119
+ const token_mint = yield createMint(connection, token_owner, token_owner.publicKey, token_owner.publicKey, decimal);
120
+ if (token_mint.toBase58().length <= 0) {
121
+ console.log("Error: [Create Token] failed to create mint!!!");
122
+ return { result: SPL_ERROR.E_FAIL, value: undefined };
123
+ }
124
+ console.log("<-----------------[Create Token Meta Data]---------------------");
125
+ const meta_result = yield createTokenMetaData(connection, token_owner, token_mint.toBase58(), name, symbol, token_logo, EnvironmentManager.getRpcNetUrl(), description);
126
+ if (meta_result !== SPL_ERROR.E_OK) {
127
+ console.log("Error: [Create Token] failed to create meta data!!!");
128
+ return { result: SPL_ERROR.E_CREATE_META_FAILED, value: undefined };
129
+ }
130
+ console.log("<-----------------[Token mint]---------------------");
131
+ if ((yield totalSupplyMint(connection, token_owner, token_mint.toBase58(), total_supply)) !== SPL_ERROR.E_OK) {
132
+ console.log("Error: [Create Token] failed to mint total supply!!!");
133
+ return { result: SPL_ERROR.E_TOTAL_MINT_FAIL, value: undefined };
134
+ }
135
+ console.log("Success: [Create Token] Mint Address: ", token_mint.toBase58());
136
+ return { result: SPL_ERROR.E_OK, value: token_mint.toBase58() };
137
+ });
@@ -0,0 +1,18 @@
1
+ export declare abstract class BaseError extends Error {
2
+ constructor(message?: string);
3
+ }
4
+ export declare class UploadMetadataError extends BaseError {
5
+ name: string;
6
+ }
7
+ export declare class GenerateTransactionError extends BaseError {
8
+ name: string;
9
+ }
10
+ export declare class BuildInstructionError extends BaseError {
11
+ name: string;
12
+ }
13
+ export declare class SetTxPriorityError extends BaseError {
14
+ name: string;
15
+ }
16
+ export declare class TransactionExecuteError extends BaseError {
17
+ name: string;
18
+ }
@@ -0,0 +1,35 @@
1
+ export class BaseError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ }
5
+ }
6
+ export class UploadMetadataError extends BaseError {
7
+ constructor() {
8
+ super(...arguments);
9
+ this.name = "UploadMetadataError";
10
+ }
11
+ }
12
+ export class GenerateTransactionError extends BaseError {
13
+ constructor() {
14
+ super(...arguments);
15
+ this.name = "GenerateTransactionError";
16
+ }
17
+ }
18
+ export class BuildInstructionError extends BaseError {
19
+ constructor() {
20
+ super(...arguments);
21
+ this.name = "BuildInstructionError";
22
+ }
23
+ }
24
+ export class SetTxPriorityError extends BaseError {
25
+ constructor() {
26
+ super(...arguments);
27
+ this.name = "SetTxPriorityError";
28
+ }
29
+ }
30
+ export class TransactionExecuteError extends BaseError {
31
+ constructor() {
32
+ super(...arguments);
33
+ this.name = "TransactionExecuteError";
34
+ }
35
+ }
@@ -0,0 +1,62 @@
1
+ /// <reference types="jito-ts/node_modules/@solana/web3.js" />
2
+ import { CacheLTA, ProgramId } from "@raydium-io/raydium-sdk";
3
+ import { Keypair, PublicKey, Transaction, VersionedTransaction } from "@solana/web3.js";
4
+ export declare enum SPL_ERROR {
5
+ E_INVALID_ARGUE = -1,
6
+ E_OK = 0,
7
+ E_FAIL = 1,
8
+ E_CHECK_FAIL = 2,
9
+ E_SEND_TX_FAIL = 3,
10
+ E_CONFIRM_TX_FAIL = 4,
11
+ E_CREATE_META_FAILED = 5,
12
+ E_TOTAL_MINT_FAIL = 6
13
+ }
14
+ export declare enum NETWORK_MODE {
15
+ NETWORK_MAIN = 0,
16
+ NETWORK_DEV = 1,
17
+ NETWORK_TEST = 2
18
+ }
19
+ export interface TX_RET {
20
+ result: SPL_ERROR;
21
+ value: string | (VersionedTransaction | Transaction)[] | undefined;
22
+ }
23
+ export interface TOKEN_INFO {
24
+ address: string;
25
+ name: string;
26
+ symbol: string;
27
+ decimal: number;
28
+ }
29
+ export interface BUNDLE_TRANSACTION {
30
+ txn: VersionedTransaction;
31
+ signer: Keypair;
32
+ }
33
+ export declare class EnvironmentManager {
34
+ private static NET_MODE;
35
+ private static JITO_BLOCKENGINE_URL;
36
+ private static RPC_CHECK_URL;
37
+ private static RPC_MAIN_URL;
38
+ private static RPC_DEVNET_URL;
39
+ private static RPC_TESTNET_URL;
40
+ private static JITO_KEYPAIR;
41
+ private static QUOTE_TOKEN_INFO;
42
+ static setNetworkMode(mode: NETWORK_MODE): void;
43
+ static setMainNetURL(url: string): void;
44
+ static setDevNetURL(url: string): void;
45
+ static setTestNettURL(url: string): void;
46
+ static getMainNetURL(): string;
47
+ static getDevNetURL(): string;
48
+ static getTestNetURL(): string;
49
+ static getNetworkMode(): NETWORK_MODE;
50
+ static getRpcNetUrl(): string;
51
+ static setNetUrls(main_url: string, dev_url: string, test_url?: string): void;
52
+ static getBundlrUrl(): string;
53
+ static getCheckUrl(): string;
54
+ static getProgramID(): ProgramId;
55
+ static setQuoteTokenInfo(token_info: TOKEN_INFO): void;
56
+ static getQuoteTokenInfo(): TOKEN_INFO;
57
+ static getCacheLTA(): CacheLTA | undefined;
58
+ static getFeeDestinationId(): PublicKey;
59
+ static getJitoBlockEngine(): string;
60
+ static setJitoKeypair(auth_key: Keypair): void;
61
+ static getJitoKeypair(): Keypair;
62
+ }