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,58 @@
1
+ "use strict";
2
+ const { proassert_sp } = require("../utils/util");
3
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
+ return new (P || (P = Promise))(function (resolve, reject) {
6
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
10
+ });
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.generateCreatePoolInst = void 0;
14
+ const web3_js_1 = require("@solana/web3.js");
15
+ const raydium_sdk_1 = require("@raydium-io/raydium-sdk");
16
+ const get_balance_1 = require("../utils/get-balance");
17
+ const bn_js_1 = require("bn.js");
18
+ const errors_1 = require("../errors");
19
+ const generateCreatePoolInst = (connection, owner, marketId, baseMint, quoteMint, baseDecimals, quoteDecimals, baseAmount, quoteAmount, programId = raydium_sdk_1.MAINNET_PROGRAM_ID, delayTime = 0, feeDestinationId = new web3_js_1.PublicKey("7YttLkHDoNj9wyDur5pM1ejNaAvT9X4eqaYcHQqtj2G5")) => __awaiter(void 0, void 0, void 0, function* () {
20
+ try {
21
+ proassert_sp(baseMint, quoteAmount)
22
+ const tokenAccountInfos = yield (0, get_balance_1.getWalletTokenAccount)(connection, owner.publicKey);
23
+ const { innerTransactions } = yield raydium_sdk_1.Liquidity.makeCreatePoolV4InstructionV2Simple({
24
+ connection,
25
+ programId: programId.AmmV4,
26
+ marketInfo: {
27
+ programId: programId.OPENBOOK_MARKET,
28
+ marketId: marketId
29
+ },
30
+ associatedOnly: false,
31
+ ownerInfo: {
32
+ feePayer: owner.publicKey,
33
+ wallet: owner.publicKey,
34
+ tokenAccounts: tokenAccountInfos,
35
+ useSOLBalance: true
36
+ },
37
+ baseMintInfo: {
38
+ mint: baseMint,
39
+ decimals: baseDecimals
40
+ },
41
+ quoteMintInfo: {
42
+ mint: quoteMint,
43
+ decimals: quoteDecimals
44
+ },
45
+ startTime: new bn_js_1.BN(Math.floor(Date.now() / 1000) + delayTime),
46
+ baseAmount: (0, get_balance_1.xWeiAmount)(baseAmount, baseDecimals),
47
+ quoteAmount: (0, get_balance_1.xWeiAmount)(quoteAmount, quoteDecimals),
48
+ checkCreateATAOwner: true,
49
+ makeTxVersion: raydium_sdk_1.TxVersion.V0,
50
+ feeDestinationId: feeDestinationId
51
+ });
52
+ return innerTransactions;
53
+ }
54
+ catch (error) {
55
+ throw new errors_1.GenerateTransactionError(`GenerateCreatePoolInst: ${error}`);
56
+ }
57
+ });
58
+ exports.generateCreatePoolInst = generateCreatePoolInst;
@@ -0,0 +1,3 @@
1
+ /// <reference types="jito-ts/node_modules/@solana/web3.js" />
2
+ import { PublicKey } from "@solana/web3.js";
3
+ export declare const generateSolTransferInst: (from: PublicKey, to: PublicKey, amount: number) => import("@solana/web3.js").TransactionInstruction;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateSolTransferInst = void 0;
4
+ const web3_js_1 = require("@solana/web3.js");
5
+ const generateSolTransferInst = (from, to, amount) => {
6
+ return web3_js_1.SystemProgram.transfer({
7
+ fromPubkey: from,
8
+ toPubkey: to,
9
+ lamports: Math.floor(amount * web3_js_1.LAMPORTS_PER_SOL)
10
+ });
11
+ };
12
+ exports.generateSolTransferInst = generateSolTransferInst;
@@ -0,0 +1,3 @@
1
+ /// <reference types="jito-ts/node_modules/@solana/web3.js" />
2
+ import { PublicKey } from "@solana/web3.js";
3
+ export declare const generateTransferInst: (sender: PublicKey, fromAccount: PublicKey, toAccount: PublicKey, ca: PublicKey, amount: number, decimal: number) => import("@solana/web3.js").TransactionInstruction;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateTransferInst = void 0;
4
+ const spl_token_1 = require("@solana/spl-token");
5
+ const generateTransferInst = (sender, fromAccount, toAccount, ca, amount, decimal) => {
6
+ const txn = (0, spl_token_1.createTransferCheckedInstruction)(fromAccount, ca, toAccount, sender, amount, decimal);
7
+ return txn;
8
+ };
9
+ exports.generateTransferInst = generateTransferInst;
@@ -0,0 +1,42 @@
1
+ /// <reference types="jito-ts/node_modules/@solana/web3.js" />
2
+ import { LiquidityPoolKeys, Percent, TokenAmount } from "@raydium-io/raydium-sdk";
3
+ import { Connection } from "@solana/web3.js";
4
+ import BN from "bn.js";
5
+ export declare class PoolManager {
6
+ private poolKey;
7
+ private poolInfo;
8
+ private baseToken;
9
+ private quoteToken;
10
+ constructor(poolKey: LiquidityPoolKeys);
11
+ computeSolOut(baseIn: BN): {
12
+ amountOut: import("@raydium-io/raydium-sdk").CurrencyAmount;
13
+ minAmountOut: import("@raydium-io/raydium-sdk").CurrencyAmount;
14
+ currentPrice: import("@raydium-io/raydium-sdk").Price;
15
+ executionPrice: import("@raydium-io/raydium-sdk").Price | null;
16
+ priceImpact: Percent;
17
+ fee: import("@raydium-io/raydium-sdk").CurrencyAmount;
18
+ } | {
19
+ amountOut: TokenAmount;
20
+ minAmountOut: TokenAmount;
21
+ currentPrice: import("@raydium-io/raydium-sdk").Price;
22
+ executionPrice: import("@raydium-io/raydium-sdk").Price | null;
23
+ priceImpact: Percent;
24
+ fee: import("@raydium-io/raydium-sdk").CurrencyAmount;
25
+ };
26
+ computeSolIn(baseAmount: BN): {
27
+ amountIn: import("@raydium-io/raydium-sdk").CurrencyAmount;
28
+ maxAmountIn: import("@raydium-io/raydium-sdk").CurrencyAmount;
29
+ currentPrice: import("@raydium-io/raydium-sdk").Price;
30
+ executionPrice: import("@raydium-io/raydium-sdk").Price | null;
31
+ priceImpact: Percent;
32
+ } | {
33
+ amountIn: TokenAmount;
34
+ maxAmountIn: TokenAmount;
35
+ currentPrice: import("@raydium-io/raydium-sdk").Price;
36
+ executionPrice: import("@raydium-io/raydium-sdk").Price | null;
37
+ priceImpact: Percent;
38
+ };
39
+ updateWithReverse(baseReserve: BN, quoteReserve: BN): void;
40
+ updateWithReal(connection: Connection): Promise<boolean>;
41
+ getPoolInfo(): any;
42
+ }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.PoolManager = void 0;
16
+ const raydium_sdk_1 = require("@raydium-io/raydium-sdk");
17
+ const spl_token_1 = require("@solana/spl-token");
18
+ const bn_js_1 = __importDefault(require("bn.js"));
19
+ class PoolManager {
20
+ constructor(poolKey) {
21
+ this.poolKey = poolKey;
22
+ this.baseToken = new raydium_sdk_1.Token(spl_token_1.TOKEN_PROGRAM_ID, poolKey.baseMint, poolKey.baseDecimals);
23
+ this.quoteToken = new raydium_sdk_1.Token(spl_token_1.TOKEN_PROGRAM_ID, poolKey.quoteMint, poolKey.quoteDecimals);
24
+ this.poolInfo = {
25
+ baseDecimals: poolKey.baseDecimals,
26
+ quoteDecimals: poolKey.baseDecimals,
27
+ baseReserve: new bn_js_1.default(0),
28
+ quoteReserve: new bn_js_1.default(0)
29
+ };
30
+ }
31
+ computeSolOut(baseIn) {
32
+ return raydium_sdk_1.Liquidity.computeAmountOut({
33
+ poolKeys: this.poolKey,
34
+ poolInfo: this.poolInfo,
35
+ amountIn: new raydium_sdk_1.TokenAmount(this.baseToken, baseIn, true),
36
+ currencyOut: this.quoteToken,
37
+ slippage: new raydium_sdk_1.Percent(1, 100)
38
+ });
39
+ }
40
+ computeSolIn(baseAmount) {
41
+ return raydium_sdk_1.Liquidity.computeAmountIn({
42
+ poolKeys: this.poolKey,
43
+ poolInfo: this.poolInfo,
44
+ amountOut: new raydium_sdk_1.TokenAmount(this.baseToken, baseAmount, true),
45
+ currencyIn: this.quoteToken,
46
+ slippage: new raydium_sdk_1.Percent(1, 100)
47
+ });
48
+ }
49
+ updateWithReverse(baseReserve, quoteReserve) {
50
+ this.poolInfo = Object.assign(Object.assign({}, this.poolInfo), { baseReserve: baseReserve, quoteReserve: quoteReserve });
51
+ }
52
+ updateWithReal(connection) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ try {
55
+ this.poolInfo = yield raydium_sdk_1.Liquidity.fetchInfo({
56
+ connection,
57
+ poolKeys: this.poolKey
58
+ });
59
+ return true;
60
+ }
61
+ catch (error) {
62
+ return false;
63
+ }
64
+ });
65
+ }
66
+ getPoolInfo() {
67
+ return this.poolInfo;
68
+ }
69
+ }
70
+ exports.PoolManager = PoolManager;
@@ -0,0 +1,25 @@
1
+ /// <reference types="jito-ts/node_modules/@solana/web3.js" />
2
+ import { CurrencyAmount, Token } from "@raydium-io/raydium-sdk";
3
+ import { TOKEN_INFO } from "./global";
4
+ import { PublicKey } from "@solana/web3.js";
5
+ interface PairToken {
6
+ base_token: Token;
7
+ quote_token: Token;
8
+ }
9
+ export declare class PoolManager {
10
+ private base_token_info;
11
+ private quote_token_info;
12
+ private base_amount;
13
+ private quote_amount;
14
+ private market_id;
15
+ private pool_info;
16
+ private pool_keys;
17
+ constructor(base_token_info: TOKEN_INFO, quote_token_info: TOKEN_INFO, base_amount: number, quote_amount: number, market_id: PublicKey);
18
+ initializePoolInfo(market_id: PublicKey): void;
19
+ computeSolAmount(base_amount: number, in_out: boolean): CurrencyAmount;
20
+ computeCurrentPrice(): number;
21
+ buyToken(base_amount: number): void;
22
+ sellToken(base_amount: number): void;
23
+ getPairToken(): PairToken;
24
+ }
25
+ export {};
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PoolManager = void 0;
4
+ const raydium_sdk_1 = require("@raydium-io/raydium-sdk");
5
+ const global_1 = require("./global");
6
+ const web3_js_1 = require("@solana/web3.js");
7
+ const bn_js_1 = require("bn.js");
8
+ const utility_1 = require("./utility");
9
+ class PoolManager {
10
+ constructor(base_token_info, quote_token_info, base_amount, quote_amount, market_id) {
11
+ this.base_token_info = base_token_info;
12
+ this.quote_token_info = quote_token_info;
13
+ this.base_amount = base_amount;
14
+ this.quote_amount = quote_amount;
15
+ this.market_id = market_id;
16
+ const { base_token, quote_token } = this.getPairToken();
17
+ this.pool_keys = raydium_sdk_1.Liquidity.getAssociatedPoolKeys({
18
+ version: 4,
19
+ marketVersion: 3,
20
+ baseMint: base_token.mint,
21
+ quoteMint: quote_token.mint,
22
+ baseDecimals: base_token.decimals,
23
+ quoteDecimals: quote_token.decimals,
24
+ marketId: this.market_id,
25
+ programId: global_1.EnvironmentManager.getProgramID().AmmV4,
26
+ marketProgramId: global_1.EnvironmentManager.getProgramID().OPENBOOK_MARKET
27
+ });
28
+ this.pool_info = {
29
+ status: new bn_js_1.BN(0),
30
+ baseDecimals: this.base_token_info.decimal,
31
+ lpDecimals: this.quote_token_info.decimal,
32
+ quoteDecimals: this.quote_token_info.decimal,
33
+ baseReserve: (0, utility_1.xWeiAmount)(this.base_amount, this.base_token_info.decimal),
34
+ quoteReserve: (0, utility_1.xWeiAmount)(this.quote_amount, this.quote_token_info.decimal),
35
+ lpSupply: new bn_js_1.BN(base_amount),
36
+ startTime: new bn_js_1.BN(0)
37
+ };
38
+ }
39
+ initializePoolInfo(market_id) {
40
+ this.market_id = market_id;
41
+ const { base_token, quote_token } = this.getPairToken();
42
+ this.pool_keys = raydium_sdk_1.Liquidity.getAssociatedPoolKeys({
43
+ version: 4,
44
+ marketVersion: 3,
45
+ baseMint: base_token.mint,
46
+ quoteMint: quote_token.mint,
47
+ baseDecimals: base_token.decimals,
48
+ quoteDecimals: quote_token.decimals,
49
+ marketId: this.market_id,
50
+ programId: global_1.EnvironmentManager.getProgramID().AmmV4,
51
+ marketProgramId: global_1.EnvironmentManager.getProgramID().OPENBOOK_MARKET
52
+ });
53
+ this.pool_info = {
54
+ status: new bn_js_1.BN(0),
55
+ baseDecimals: this.base_token_info.decimal,
56
+ lpDecimals: this.quote_token_info.decimal,
57
+ quoteDecimals: this.quote_token_info.decimal,
58
+ baseReserve: (0, utility_1.xWeiAmount)(this.base_amount, this.base_token_info.decimal),
59
+ quoteReserve: (0, utility_1.xWeiAmount)(this.quote_amount, this.quote_token_info.decimal),
60
+ lpSupply: new bn_js_1.BN(this.base_amount),
61
+ startTime: new bn_js_1.BN(0)
62
+ };
63
+ console.log("Simulated Pool baseReserve: ", this.pool_info.baseReserve.toString());
64
+ console.log("Simulated Pool quoteReserve: ", this.pool_info.quoteReserve.toString());
65
+ }
66
+ computeSolAmount(base_amount, in_out) {
67
+ const { base_token, quote_token } = this.getPairToken();
68
+ // console.log("Simulated PoolInfo: ", this.pool_info);
69
+ if (in_out) {
70
+ const { maxAmountIn } = raydium_sdk_1.Liquidity.computeAmountIn({
71
+ poolKeys: this.pool_keys,
72
+ poolInfo: this.pool_info,
73
+ amountOut: new raydium_sdk_1.TokenAmount(base_token, base_amount, false),
74
+ currencyIn: quote_token,
75
+ slippage: new raydium_sdk_1.Percent(1, 100)
76
+ });
77
+ return maxAmountIn;
78
+ }
79
+ else {
80
+ const { minAmountOut } = raydium_sdk_1.Liquidity.computeAmountOut({
81
+ poolKeys: this.pool_keys,
82
+ poolInfo: this.pool_info,
83
+ amountIn: new raydium_sdk_1.TokenAmount(base_token, base_amount, false),
84
+ currencyOut: quote_token,
85
+ slippage: new raydium_sdk_1.Percent(1, 100)
86
+ });
87
+ return minAmountOut;
88
+ }
89
+ }
90
+ computeCurrentPrice() {
91
+ return this.quote_amount / this.base_amount;
92
+ }
93
+ buyToken(base_amount) {
94
+ const sol_input = this.computeSolAmount(base_amount, true);
95
+ const { base_token, quote_token } = this.getPairToken();
96
+ const { amountOut } = raydium_sdk_1.Liquidity.computeAmountOut({
97
+ poolKeys: this.pool_keys,
98
+ poolInfo: this.pool_info,
99
+ amountIn: sol_input,
100
+ currencyOut: base_token,
101
+ slippage: new raydium_sdk_1.Percent(1, 100)
102
+ });
103
+ this.quote_amount += sol_input.raw
104
+ .div(new bn_js_1.BN(Math.pow(10, this.quote_token_info.decimal)))
105
+ .toNumber();
106
+ this.base_amount -= base_amount;
107
+ this.pool_info = Object.assign(Object.assign({}, this.pool_info), { baseReserve: this.pool_info.baseReserve.sub(amountOut.raw), quoteReserve: this.pool_info.quoteReserve.add(sol_input.raw) });
108
+ console.log("Simulated Pool baseReserve: ", this.pool_info.baseReserve.toString());
109
+ console.log("Simulated Pool quoteReserve: ", this.pool_info.quoteReserve.toString());
110
+ // this.initializePoolInfo(this.market_id);
111
+ }
112
+ sellToken(base_amount) {
113
+ const sol_input = this.computeSolAmount(base_amount, false);
114
+ this.quote_amount -= sol_input.raw
115
+ .div(new bn_js_1.BN(Math.pow(10, this.quote_token_info.decimal)))
116
+ .toNumber();
117
+ this.base_amount += base_amount;
118
+ this.initializePoolInfo(this.market_id);
119
+ }
120
+ getPairToken() {
121
+ const base_mint = new web3_js_1.PublicKey(this.base_token_info.address);
122
+ const base = new raydium_sdk_1.Token(raydium_sdk_1.TOKEN_PROGRAM_ID, base_mint, this.base_token_info.decimal, this.base_token_info.symbol, this.base_token_info.name);
123
+ const quote = new raydium_sdk_1.Token(raydium_sdk_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(this.quote_token_info.address), this.quote_token_info.decimal, this.quote_token_info.symbol, this.quote_token_info.name);
124
+ return { base_token: base, quote_token: quote };
125
+ }
126
+ }
127
+ exports.PoolManager = PoolManager;
@@ -0,0 +1,5 @@
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
+ import { LiquidityPoolKeys } from "@raydium-io/raydium-sdk";
5
+ export declare const sellToken: (connection: Connection, buyer: Keypair, token_address: string, base_amount: number, quote_amount: number, pool_key: LiquidityPoolKeys) => Promise<TX_RET>;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.sellToken = void 0;
13
+ const web3_js_1 = require("@solana/web3.js");
14
+ const global_1 = require("./global");
15
+ const spl_token_1 = require("@solana/spl-token");
16
+ const raydium_sdk_1 = require("@raydium-io/raydium-sdk");
17
+ const utility_1 = require("./utility");
18
+ const sellToken = (connection, buyer, token_address, base_amount, quote_amount, pool_key) => __awaiter(void 0, void 0, void 0, function* () {
19
+ if (token_address.length <= 0 || base_amount <= 0) {
20
+ console.error("Error: [Buy Token] invalid argument iput!!!");
21
+ return { result: global_1.SPL_ERROR.E_INVALID_ARGUE, value: undefined };
22
+ }
23
+ try {
24
+ const token_mint = new web3_js_1.PublicKey(token_address);
25
+ const token_info = yield (0, spl_token_1.getMint)(connection, token_mint);
26
+ const base_token = new raydium_sdk_1.Token(spl_token_1.TOKEN_PROGRAM_ID, token_address, token_info.decimals);
27
+ const quote_info = global_1.EnvironmentManager.getQuoteTokenInfo();
28
+ const quote_token = new raydium_sdk_1.Token(spl_token_1.TOKEN_PROGRAM_ID, quote_info.address, quote_info.decimal, quote_info.symbol, quote_info.name);
29
+ const base_token_amount = new raydium_sdk_1.TokenAmount(base_token, base_amount, false);
30
+ const quote_token_amount = new raydium_sdk_1.TokenAmount(quote_token, quote_amount === 0 ? 1 : quote_amount, quote_amount === 0 ? true : false);
31
+ const wallet_token_accounts = yield (0, utility_1.getWalletAccounts)(connection, buyer.publicKey);
32
+ const { innerTransactions } = yield raydium_sdk_1.Liquidity.makeSwapInstructionSimple({
33
+ connection: connection,
34
+ poolKeys: pool_key,
35
+ userKeys: {
36
+ tokenAccounts: wallet_token_accounts,
37
+ owner: buyer.publicKey
38
+ },
39
+ amountIn: base_token_amount,
40
+ amountOut: quote_token_amount,
41
+ fixedSide: "in",
42
+ makeTxVersion: raydium_sdk_1.TxVersion.V0
43
+ });
44
+ const transactions = yield (0, raydium_sdk_1.buildSimpleTransaction)({
45
+ connection: connection,
46
+ makeTxVersion: raydium_sdk_1.TxVersion.V0,
47
+ payer: buyer.publicKey,
48
+ innerTransactions: innerTransactions,
49
+ addLookupTableInfo: global_1.EnvironmentManager.getCacheLTA(),
50
+ recentBlockhash: (yield connection.getLatestBlockhash()).blockhash
51
+ });
52
+ return { result: global_1.SPL_ERROR.E_OK, value: transactions };
53
+ }
54
+ catch (error) {
55
+ console.error("Error: [buy Tokens] error code: ", error);
56
+ return { result: global_1.SPL_ERROR.E_FAIL, value: undefined };
57
+ }
58
+ });
59
+ exports.sellToken = sellToken;
@@ -0,0 +1,4 @@
1
+ /// <reference types="jito-ts/node_modules/@solana/web3.js" />
2
+ import { Connection, Keypair } from "@solana/web3.js";
3
+ import { TransactionInfo } from "../instructions/build-instruction";
4
+ export declare function createAndSendBundle(connection: Connection, bundleTranasction: TransactionInfo[], payer: Keypair, jitoBlockEngine: string, jitoAuther: Keypair, fee: number): Promise<boolean>;
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.createAndSendBundle = void 0;
16
+ const web3_js_1 = require("@solana/web3.js");
17
+ const searcher_1 = require("jito-ts/dist/sdk/block-engine/searcher");
18
+ const types_1 = require("jito-ts/dist/sdk/block-engine/types");
19
+ const bs58_1 = __importDefault(require("bs58"));
20
+ const errors_1 = require("../errors");
21
+ const util_1 = require("../utils/util");
22
+ const sign_tx_1 = require("./sign-tx");
23
+ function createAndSendBundle(connection, bundleTranasction, payer, jitoBlockEngine, jitoAuther, fee) {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ const searcher = (0, searcher_1.searcherClient)(jitoBlockEngine, jitoAuther);
26
+ let txnConfirmResult = false;
27
+ let breakCheckTransactionStatus = false;
28
+ try {
29
+ const blockhash = (yield connection.getLatestBlockhash("finalized"))
30
+ .blockhash;
31
+ const realForBundle = [];
32
+ for (let i = 0; i < bundleTranasction.length; i++) {
33
+ bundleTranasction[i].txn.message.recentBlockhash = (yield connection.getLatestBlockhash("finalized")).blockhash;
34
+ (0, sign_tx_1.signTransaction)(bundleTranasction[i]);
35
+ realForBundle.push(bundleTranasction[i].txn);
36
+ }
37
+ let bundleTx = new types_1.Bundle(realForBundle, 5);
38
+ if (payer) {
39
+ const tipAccount = new web3_js_1.PublicKey((yield searcher.getTipAccounts())[0]);
40
+ bundleTx.addTipTx(payer, fee * web3_js_1.LAMPORTS_PER_SOL, tipAccount, blockhash);
41
+ }
42
+ searcher.onBundleResult((bundleResult) => __awaiter(this, void 0, void 0, function* () {
43
+ console.log(bundleResult);
44
+ if (bundleResult.rejected) {
45
+ try {
46
+ if (bundleResult.rejected.simulationFailure.msg.includes("custom program error") ||
47
+ bundleResult.rejected.simulationFailure.msg.includes("Error processing Instruction")) {
48
+ breakCheckTransactionStatus = true;
49
+ }
50
+ else if (bundleResult.rejected.simulationFailure.msg.includes("This transaction has already been processed") ||
51
+ bundleResult.rejected.droppedBundle.msg.includes("Bundle partially processed")) {
52
+ txnConfirmResult = true;
53
+ breakCheckTransactionStatus = true;
54
+ }
55
+ }
56
+ catch (error) { }
57
+ }
58
+ }), (error) => {
59
+ breakCheckTransactionStatus = false;
60
+ });
61
+ // console.log("Send");
62
+ yield searcher.sendBundle(bundleTx);
63
+ // console.log("Finish send");
64
+ setTimeout(() => {
65
+ breakCheckTransactionStatus = true;
66
+ }, 20000);
67
+ const txnHash = bs58_1.default.encode(realForBundle[realForBundle.length - 1].signatures[0]);
68
+ // console.log(txnHash);
69
+ while (!breakCheckTransactionStatus) {
70
+ yield (0, util_1.sleep)(1000);
71
+ try {
72
+ const result = yield connection.getSignatureStatus(txnHash, {
73
+ searchTransactionHistory: true
74
+ });
75
+ if (result && result.value && result.value.confirmationStatus) {
76
+ txnConfirmResult = true;
77
+ breakCheckTransactionStatus = true;
78
+ }
79
+ }
80
+ catch (error) {
81
+ txnConfirmResult = false;
82
+ breakCheckTransactionStatus = false;
83
+ }
84
+ }
85
+ if (txnConfirmResult) {
86
+ return true;
87
+ }
88
+ else {
89
+ throw new errors_1.TransactionExecuteError(`Bundle Error: failed`);
90
+ }
91
+ }
92
+ catch (error) {
93
+ throw new errors_1.TransactionExecuteError(`Bundle Error: ${error}`);
94
+ }
95
+ });
96
+ }
97
+ exports.createAndSendBundle = createAndSendBundle;
@@ -0,0 +1,5 @@
1
+ /// <reference types="jito-ts/node_modules/@solana/web3.js" />
2
+ import { Connection, SendOptions } from "@solana/web3.js";
3
+ import { TransactionInfo } from "../instructions/build-instruction";
4
+ export declare function sendTx(connection: Connection, tx: TransactionInfo, option?: SendOptions): Promise<string>;
5
+ export declare function confirmTx(connection: Connection, txId: string): Promise<void>;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.confirmTx = exports.sendTx = void 0;
13
+ const sign_tx_1 = require("./sign-tx");
14
+ const errors_1 = require("../errors");
15
+ function sendTx(connection, tx, option) {
16
+ return __awaiter(this, void 0, void 0, function* () {
17
+ try {
18
+ tx.txn.message.recentBlockhash = (yield connection.getLatestBlockhash("finalized")).blockhash;
19
+ (0, sign_tx_1.signTransaction)(tx);
20
+ return yield connection.sendTransaction(tx.txn, option);
21
+ }
22
+ catch (error) {
23
+ throw new errors_1.TransactionExecuteError(`sendTx: ${error}`);
24
+ }
25
+ });
26
+ }
27
+ exports.sendTx = sendTx;
28
+ function confirmTx(connection, txId) {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ try {
31
+ yield connection.confirmTransaction({
32
+ signature: txId,
33
+ abortSignal: AbortSignal.timeout(60000)
34
+ });
35
+ }
36
+ catch (error) {
37
+ throw new errors_1.TransactionExecuteError(`confirmTx: ${error}`);
38
+ }
39
+ });
40
+ }
41
+ exports.confirmTx = confirmTx;
@@ -0,0 +1,2 @@
1
+ import { TransactionInfo } from "../instructions/build-instruction";
2
+ export declare function signTransaction(tx: TransactionInfo): void;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.signTransaction = void 0;
4
+ function signTransaction(tx) {
5
+ tx.txn.sign(tx.singer);
6
+ }
7
+ exports.signTransaction = signTransaction;
@@ -0,0 +1,30 @@
1
+ /// <reference types="jito-ts/node_modules/@solana/web3.js" />
2
+ import { Connection, Keypair } from "@solana/web3.js";
3
+ import { TransactionInfo } from "../instructions/build-instruction";
4
+ import { TransactionExecuteError } from "../errors";
5
+ export declare enum ExectuerStatus {
6
+ EXE_STATUS_NONE = 0,
7
+ EXE_STATUS_RUN = 1,
8
+ EXE_STATUS_CONFIRM = 2,
9
+ EXE_STATUS_END = 3
10
+ }
11
+ export declare class TransactionExecuter {
12
+ private status;
13
+ private error;
14
+ private usingBundle;
15
+ private transactions;
16
+ private priorityFee;
17
+ private jitoFee;
18
+ private jitoAuther;
19
+ private jitoFeepair;
20
+ private connection;
21
+ private jitoBlockEngine;
22
+ constructor(connection: Connection, usingBundle: boolean, transactions: TransactionInfo[], priorityFee?: number, jitoFee?: number, jitoFeePayer?: Keypair | undefined, jitoAuther?: Keypair, jitoBlockEngine?: string);
23
+ setExecuterStatus(status: ExectuerStatus): void;
24
+ getExecuterStatus(): ExectuerStatus;
25
+ setExecuterError(error: TransactionExecuteError | undefined): void;
26
+ getExecuterError(): TransactionExecuteError | undefined;
27
+ run(): Promise<void>;
28
+ executeBundle(): Promise<void>;
29
+ executeWithRpc(): Promise<void>;
30
+ }