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.
- package/README.md +35 -0
- package/lib/cjs/bundle.d.ts +4 -0
- package/lib/cjs/bundle.js +102 -0
- package/lib/cjs/buy_token.d.ts +5 -0
- package/lib/cjs/buy_token.js +59 -0
- package/lib/cjs/create_open_market.d.ts +4 -0
- package/lib/cjs/create_open_market.js +340 -0
- package/lib/cjs/create_pool.d.ts +4 -0
- package/lib/cjs/create_pool.js +107 -0
- package/lib/cjs/create_token.d.ts +4 -0
- package/lib/cjs/create_token.js +164 -0
- package/lib/cjs/errors.d.ts +18 -0
- package/lib/cjs/errors.js +44 -0
- package/lib/cjs/global.d.ts +62 -0
- package/lib/cjs/global.js +107 -0
- package/lib/cjs/index.d.ts +17 -0
- package/lib/cjs/index.js +33 -0
- package/lib/cjs/instructions/build-instruction.d.ts +8 -0
- package/lib/cjs/instructions/build-instruction.js +66 -0
- package/lib/cjs/instructions/buy-instruction.d.ts +3 -0
- package/lib/cjs/instructions/buy-instruction.js +44 -0
- package/lib/cjs/instructions/cache-alt-instruction.d.ts +4 -0
- package/lib/cjs/instructions/cache-alt-instruction.js +41 -0
- package/lib/cjs/instructions/createATA-instruction.d.ts +3 -0
- package/lib/cjs/instructions/createATA-instruction.js +10 -0
- package/lib/cjs/instructions/createpool-instruction.d.ts +4 -0
- package/lib/cjs/instructions/createpool-instruction.js +58 -0
- package/lib/cjs/instructions/sol-transfer-instruction.d.ts +3 -0
- package/lib/cjs/instructions/sol-transfer-instruction.js +12 -0
- package/lib/cjs/instructions/transfer-instruction.d.ts +3 -0
- package/lib/cjs/instructions/transfer-instruction.js +9 -0
- package/lib/cjs/pool/pool-manager.d.ts +42 -0
- package/lib/cjs/pool/pool-manager.js +70 -0
- package/lib/cjs/pool_manager.d.ts +25 -0
- package/lib/cjs/pool_manager.js +127 -0
- package/lib/cjs/sell_token.d.ts +5 -0
- package/lib/cjs/sell_token.js +59 -0
- package/lib/cjs/transaction/bundle.d.ts +4 -0
- package/lib/cjs/transaction/bundle.js +97 -0
- package/lib/cjs/transaction/execute-rpc.d.ts +5 -0
- package/lib/cjs/transaction/execute-rpc.js +41 -0
- package/lib/cjs/transaction/sign-tx.d.ts +2 -0
- package/lib/cjs/transaction/sign-tx.js +7 -0
- package/lib/cjs/transaction/transaction-exectue.d.ts +30 -0
- package/lib/cjs/transaction/transaction-exectue.js +119 -0
- package/lib/cjs/transaction-helper/check_transaction.d.ts +3 -0
- package/lib/cjs/transaction-helper/check_transaction.js +43 -0
- package/lib/cjs/transaction-helper/transaction.d.ts +7 -0
- package/lib/cjs/transaction-helper/transaction.js +75 -0
- package/lib/cjs/upload-metadata/upload-metaplex.d.ts +3 -0
- package/lib/cjs/upload-metadata/upload-metaplex.js +48 -0
- package/lib/cjs/upload-metadata/upload-nftstorage.d.ts +0 -0
- package/lib/cjs/upload-metadata/upload-nftstorage.js +1 -0
- package/lib/cjs/upload-metadata/upload-pinata.d.ts +1 -0
- package/lib/cjs/upload-metadata/upload-pinata.js +17 -0
- package/lib/cjs/upload-metadata/utility.d.ts +1 -0
- package/lib/cjs/upload-metadata/utility.js +28 -0
- package/lib/cjs/utility.d.ts +33 -0
- package/lib/cjs/utility.js +129 -0
- package/lib/cjs/utils/budget.d.ts +9 -0
- package/lib/cjs/utils/budget.js +83 -0
- package/lib/cjs/utils/error-helper.d.ts +7 -0
- package/lib/cjs/utils/error-helper.js +15 -0
- package/lib/cjs/utils/get-balance.d.ts +11 -0
- package/lib/cjs/utils/get-balance.js +52 -0
- package/lib/cjs/utils/util.d.ts +27 -0
- package/lib/cjs/utils/util.js +346 -0
- package/lib/cjs/validation_check.d.ts +3 -0
- package/lib/cjs/validation_check.js +26 -0
- package/lib/esm/bundle.d.ts +4 -0
- package/lib/esm/bundle.js +75 -0
- package/lib/esm/buy_token.d.ts +5 -0
- package/lib/esm/buy_token.js +55 -0
- package/lib/esm/create_open_market.d.ts +4 -0
- package/lib/esm/create_open_market.js +310 -0
- package/lib/esm/create_pool.d.ts +4 -0
- package/lib/esm/create_pool.js +80 -0
- package/lib/esm/create_token.d.ts +4 -0
- package/lib/esm/create_token.js +137 -0
- package/lib/esm/errors.d.ts +18 -0
- package/lib/esm/errors.js +35 -0
- package/lib/esm/global.d.ts +62 -0
- package/lib/esm/global.js +103 -0
- package/lib/esm/index.d.ts +17 -0
- package/lib/esm/index.js +17 -0
- package/lib/esm/instructions/build-instruction.d.ts +8 -0
- package/lib/esm/instructions/build-instruction.js +61 -0
- package/lib/esm/instructions/buy-instruction.d.ts +3 -0
- package/lib/esm/instructions/buy-instruction.js +40 -0
- package/lib/esm/instructions/cache-alt-instruction.d.ts +4 -0
- package/lib/esm/instructions/cache-alt-instruction.js +36 -0
- package/lib/esm/instructions/createATA-instruction.d.ts +3 -0
- package/lib/esm/instructions/createATA-instruction.js +6 -0
- package/lib/esm/instructions/createpool-instruction.d.ts +4 -0
- package/lib/esm/instructions/createpool-instruction.js +52 -0
- package/lib/esm/instructions/sol-transfer-instruction.d.ts +3 -0
- package/lib/esm/instructions/sol-transfer-instruction.js +8 -0
- package/lib/esm/instructions/transfer-instruction.d.ts +3 -0
- package/lib/esm/instructions/transfer-instruction.js +5 -0
- package/lib/esm/pool/pool-manager.d.ts +42 -0
- package/lib/esm/pool/pool-manager.js +63 -0
- package/lib/esm/pool_manager.d.ts +25 -0
- package/lib/esm/pool_manager.js +123 -0
- package/lib/esm/sell_token.d.ts +5 -0
- package/lib/esm/sell_token.js +55 -0
- package/lib/esm/transaction/bundle.d.ts +4 -0
- package/lib/esm/transaction/bundle.js +90 -0
- package/lib/esm/transaction/execute-rpc.d.ts +5 -0
- package/lib/esm/transaction/execute-rpc.js +36 -0
- package/lib/esm/transaction/sign-tx.d.ts +2 -0
- package/lib/esm/transaction/sign-tx.js +3 -0
- package/lib/esm/transaction/transaction-exectue.d.ts +30 -0
- package/lib/esm/transaction/transaction-exectue.js +115 -0
- package/lib/esm/transaction-helper/check_transaction.d.ts +3 -0
- package/lib/esm/transaction-helper/check_transaction.js +16 -0
- package/lib/esm/transaction-helper/transaction.d.ts +7 -0
- package/lib/esm/transaction-helper/transaction.js +68 -0
- package/lib/esm/upload-metadata/upload-metaplex.d.ts +3 -0
- package/lib/esm/upload-metadata/upload-metaplex.js +43 -0
- package/lib/esm/upload-metadata/upload-nftstorage.d.ts +0 -0
- package/lib/esm/upload-metadata/upload-nftstorage.js +1 -0
- package/lib/esm/upload-metadata/upload-pinata.d.ts +1 -0
- package/lib/esm/upload-metadata/upload-pinata.js +13 -0
- package/lib/esm/upload-metadata/utility.d.ts +1 -0
- package/lib/esm/upload-metadata/utility.js +21 -0
- package/lib/esm/utility.d.ts +33 -0
- package/lib/esm/utility.js +93 -0
- package/lib/esm/utils/budget.d.ts +9 -0
- package/lib/esm/utils/budget.js +73 -0
- package/lib/esm/utils/error-helper.d.ts +7 -0
- package/lib/esm/utils/error-helper.js +11 -0
- package/lib/esm/utils/get-balance.d.ts +11 -0
- package/lib/esm/utils/get-balance.js +42 -0
- package/lib/esm/utils/util.d.ts +25 -0
- package/lib/esm/utils/util.js +107 -0
- package/lib/esm/validation_check.d.ts +3 -0
- package/lib/esm/validation_check.js +22 -0
- package/package.json +69 -0
- package/src/assert.ts +4 -0
- package/src/bundle.ts +99 -0
- package/src/buy_token.ts +82 -0
- package/src/create_open_market.ts +464 -0
- package/src/create_pool.ts +120 -0
- package/src/create_token.ts +267 -0
- package/src/errors.ts +25 -0
- package/src/global.ts +156 -0
- package/src/index.ts +17 -0
- package/src/instructions/build-instruction.ts +87 -0
- package/src/instructions/buy-instruction.ts +52 -0
- package/src/instructions/cache-alt-instruction.ts +40 -0
- package/src/instructions/createATA-instruction.ts +29 -0
- package/src/instructions/createpool-instruction.ts +72 -0
- package/src/instructions/sol-transfer-instruction.ts +13 -0
- package/src/instructions/transfer-instruction.ts +22 -0
- package/src/pool/pool-manager.ts +85 -0
- package/src/pool_manager.ts +197 -0
- package/src/sell_token.ts +82 -0
- package/src/transaction/bundle.ts +116 -0
- package/src/transaction/execute-rpc.ts +35 -0
- package/src/transaction/sign-tx.ts +5 -0
- package/src/transaction/transaction-exectue.ts +134 -0
- package/src/transaction-helper/check_transaction.ts +23 -0
- package/src/transaction-helper/transaction.ts +90 -0
- package/src/upload-metadata/upload-metaplex.ts +51 -0
- package/src/upload-metadata/upload-nftstorage.ts +0 -0
- package/src/upload-metadata/upload-pinata.ts +8 -0
- package/src/upload-metadata/utility.ts +10 -0
- package/src/utility.ts +127 -0
- package/src/utils/budget.ts +104 -0
- package/src/utils/error-helper.ts +20 -0
- package/src/utils/get-balance.ts +55 -0
- package/src/utils/util.ts +111 -0
- package/src/validation_check.ts +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# sol-ihor-lab
|
|
2
|
+
|
|
3
|
+
Solana laboratory utility code
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install via NPM:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
#### javascript
|
|
17
|
+
|
|
18
|
+
```javascript
|
|
19
|
+
var myApp = require("sol-ihor-lab");
|
|
20
|
+
|
|
21
|
+
myApp.callbackTransaction(); // print and return " Hello World! "
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
#### TypeScript
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import * as myApp from "sol-ihor-lab";
|
|
28
|
+
|
|
29
|
+
myApp.callbackTransaction(); // print and return " Hello World! "
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
This project is licensed under the terms of the
|
|
35
|
+
[MIT license](/LICENSE).
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/// <reference types="jito-ts/node_modules/@solana/web3.js" />
|
|
2
|
+
import { Connection, Keypair } from "@solana/web3.js";
|
|
3
|
+
import { BUNDLE_TRANSACTION, SPL_ERROR } from "./global";
|
|
4
|
+
export declare const createAndSendBundle: (connection: Connection, bundleTip: number, transactions: BUNDLE_TRANSACTION[], payer: Keypair) => Promise<SPL_ERROR>;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.createAndSendBundle = void 0;
|
|
36
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
37
|
+
const searcher_1 = require("jito-ts/dist/sdk/block-engine/searcher");
|
|
38
|
+
const global_1 = require("./global");
|
|
39
|
+
const types_1 = require("jito-ts/dist/sdk/block-engine/types");
|
|
40
|
+
const utils = __importStar(require("./utility"));
|
|
41
|
+
const transaction_1 = require("./transaction-helper/transaction");
|
|
42
|
+
const createAndSendBundle = (connection, bundleTip, transactions, payer) => __awaiter(void 0, void 0, void 0, function* () {
|
|
43
|
+
const searcher = (0, searcher_1.searcherClient)(global_1.EnvironmentManager.getJitoBlockEngine(), global_1.EnvironmentManager.getJitoKeypair());
|
|
44
|
+
const _tipAccount = (yield searcher.getTipAccounts())[0];
|
|
45
|
+
const tipAccount = new web3_js_1.PublicKey(_tipAccount);
|
|
46
|
+
const recentBlockhash = (yield connection.getLatestBlockhash("finalized"))
|
|
47
|
+
.blockhash;
|
|
48
|
+
const bundleTransactions = [];
|
|
49
|
+
for (let i = 0; i < transactions.length; i++) {
|
|
50
|
+
transactions[i].txn.message.recentBlockhash = recentBlockhash;
|
|
51
|
+
(0, transaction_1.signTransaction)(transactions[i].signer, transactions[i].txn);
|
|
52
|
+
bundleTransactions.push(transactions[i].txn);
|
|
53
|
+
}
|
|
54
|
+
let bundleTx = new types_1.Bundle(bundleTransactions, 5);
|
|
55
|
+
bundleTx.addTipTx(payer, bundleTip, tipAccount, recentBlockhash);
|
|
56
|
+
try {
|
|
57
|
+
searcher.onBundleResult((bundleResult) => {
|
|
58
|
+
var _a, _b, _c;
|
|
59
|
+
if (bundleResult.bundleId === bundleUUID) {
|
|
60
|
+
if (bundleResult.accepted) {
|
|
61
|
+
console.log(bundleResult.accepted, `Bundle ${bundleResult.bundleId} accepted in slot ${bundleResult.accepted.slot}`);
|
|
62
|
+
}
|
|
63
|
+
if (bundleResult.rejected) {
|
|
64
|
+
console.log(bundleResult.rejected, `Bundle ${bundleResult.bundleId} rejected:`);
|
|
65
|
+
checked = true;
|
|
66
|
+
if (((_a = bundleResult.rejected.droppedBundle) === null || _a === void 0 ? void 0 : _a.msg.includes("processed")) ||
|
|
67
|
+
((_c = (_b = bundleResult.rejected.simulationFailure) === null || _b === void 0 ? void 0 : _b.msg) === null || _c === void 0 ? void 0 : _c.includes("processed"))) {
|
|
68
|
+
success = true;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
success = false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (bundleResult.processed) {
|
|
75
|
+
console.log(`Bundle ${bundleResult.bundleId} processed`);
|
|
76
|
+
checked = true;
|
|
77
|
+
success = true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}, (error) => {
|
|
81
|
+
console.error("Bundle Error: ", error);
|
|
82
|
+
success = false;
|
|
83
|
+
checked = true;
|
|
84
|
+
});
|
|
85
|
+
const bundleUUID = yield searcher.sendBundle(bundleTx);
|
|
86
|
+
console.log("bundle id: ", bundleUUID);
|
|
87
|
+
let checked = false;
|
|
88
|
+
let success = false;
|
|
89
|
+
while (!checked) {
|
|
90
|
+
yield utils.sleep(1000);
|
|
91
|
+
}
|
|
92
|
+
if (success)
|
|
93
|
+
return global_1.SPL_ERROR.E_OK;
|
|
94
|
+
else
|
|
95
|
+
return global_1.SPL_ERROR.E_FAIL;
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
console.error("Bundle Error: ", error);
|
|
99
|
+
return global_1.SPL_ERROR.E_FAIL;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
exports.createAndSendBundle = createAndSendBundle;
|
|
@@ -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 buyToken: (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.buyToken = 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 buyToken = (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, 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: quote_token_amount,
|
|
40
|
+
amountOut: base_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.buyToken = buyToken;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/// <reference types="jito-ts/node_modules/@solana/web3.js" />
|
|
2
|
+
import { Connection, Keypair } from "@solana/web3.js";
|
|
3
|
+
import { SPL_ERROR } from "./global";
|
|
4
|
+
export declare const createOpenBookMarket: (connection: Connection, token_owner: Keypair, token_address: string, min_order_size?: number, tick_size?: number) => Promise<SPL_ERROR>;
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.createOpenBookMarket = void 0;
|
|
39
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
40
|
+
const global_1 = require("./global");
|
|
41
|
+
const raydium_sdk_1 = require("@raydium-io/raydium-sdk");
|
|
42
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
43
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
44
|
+
const transactions = __importStar(require("./transaction-helper/transaction"));
|
|
45
|
+
function makeCreateMarketInstruction(_a) {
|
|
46
|
+
return __awaiter(this, arguments, void 0, function* ({ connection, wallet, baseInfo, quoteInfo, lotSize, // 1
|
|
47
|
+
tickSize, // 0.01
|
|
48
|
+
dexProgramId, makeTxVersion, lookupTableCache }) {
|
|
49
|
+
const market = (0, raydium_sdk_1.generatePubKey)({
|
|
50
|
+
fromPublicKey: wallet,
|
|
51
|
+
programId: dexProgramId
|
|
52
|
+
});
|
|
53
|
+
const requestQueue = (0, raydium_sdk_1.generatePubKey)({
|
|
54
|
+
fromPublicKey: wallet,
|
|
55
|
+
programId: dexProgramId
|
|
56
|
+
});
|
|
57
|
+
const eventQueue = (0, raydium_sdk_1.generatePubKey)({
|
|
58
|
+
fromPublicKey: wallet,
|
|
59
|
+
programId: dexProgramId
|
|
60
|
+
});
|
|
61
|
+
const bids = (0, raydium_sdk_1.generatePubKey)({
|
|
62
|
+
fromPublicKey: wallet,
|
|
63
|
+
programId: dexProgramId
|
|
64
|
+
});
|
|
65
|
+
const asks = (0, raydium_sdk_1.generatePubKey)({
|
|
66
|
+
fromPublicKey: wallet,
|
|
67
|
+
programId: dexProgramId
|
|
68
|
+
});
|
|
69
|
+
const baseVault = (0, raydium_sdk_1.generatePubKey)({
|
|
70
|
+
fromPublicKey: wallet,
|
|
71
|
+
programId: spl_token_1.TOKEN_PROGRAM_ID
|
|
72
|
+
});
|
|
73
|
+
const quoteVault = (0, raydium_sdk_1.generatePubKey)({
|
|
74
|
+
fromPublicKey: wallet,
|
|
75
|
+
programId: spl_token_1.TOKEN_PROGRAM_ID
|
|
76
|
+
});
|
|
77
|
+
const feeRateBps = 0;
|
|
78
|
+
const quoteDustThreshold = new bn_js_1.default(100);
|
|
79
|
+
function getVaultOwnerAndNonce() {
|
|
80
|
+
const vaultSignerNonce = new bn_js_1.default(0);
|
|
81
|
+
while (true) {
|
|
82
|
+
try {
|
|
83
|
+
const vaultOwner = web3_js_1.PublicKey.createProgramAddressSync([
|
|
84
|
+
market.publicKey.toBuffer(),
|
|
85
|
+
vaultSignerNonce.toArrayLike(Buffer, "le", 8)
|
|
86
|
+
], dexProgramId);
|
|
87
|
+
return { vaultOwner, vaultSignerNonce };
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
vaultSignerNonce.iaddn(1);
|
|
91
|
+
if (vaultSignerNonce.gt(new bn_js_1.default(25555)))
|
|
92
|
+
throw Error("find vault owner error");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function initializeMarketInstruction({ programId, marketInfo }) {
|
|
97
|
+
const dataLayout = (0, raydium_sdk_1.struct)([
|
|
98
|
+
(0, raydium_sdk_1.u8)("version"),
|
|
99
|
+
(0, raydium_sdk_1.u32)("instruction"),
|
|
100
|
+
(0, raydium_sdk_1.u64)("baseLotSize"),
|
|
101
|
+
(0, raydium_sdk_1.u64)("quoteLotSize"),
|
|
102
|
+
(0, raydium_sdk_1.u16)("feeRateBps"),
|
|
103
|
+
(0, raydium_sdk_1.u64)("vaultSignerNonce"),
|
|
104
|
+
(0, raydium_sdk_1.u64)("quoteDustThreshold")
|
|
105
|
+
]);
|
|
106
|
+
const keys = [
|
|
107
|
+
{ pubkey: marketInfo.id, isSigner: false, isWritable: true },
|
|
108
|
+
{ pubkey: marketInfo.requestQueue, isSigner: false, isWritable: true },
|
|
109
|
+
{ pubkey: marketInfo.eventQueue, isSigner: false, isWritable: true },
|
|
110
|
+
{ pubkey: marketInfo.bids, isSigner: false, isWritable: true },
|
|
111
|
+
{ pubkey: marketInfo.asks, isSigner: false, isWritable: true },
|
|
112
|
+
{ pubkey: marketInfo.baseVault, isSigner: false, isWritable: true },
|
|
113
|
+
{ pubkey: marketInfo.quoteVault, isSigner: false, isWritable: true },
|
|
114
|
+
{ pubkey: marketInfo.baseMint, isSigner: false, isWritable: false },
|
|
115
|
+
{ pubkey: marketInfo.quoteMint, isSigner: false, isWritable: false },
|
|
116
|
+
// Use a dummy address if using the new dex upgrade to save tx space.
|
|
117
|
+
{
|
|
118
|
+
pubkey: marketInfo.authority
|
|
119
|
+
? marketInfo.quoteMint
|
|
120
|
+
: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
121
|
+
isSigner: false,
|
|
122
|
+
isWritable: false
|
|
123
|
+
}
|
|
124
|
+
]
|
|
125
|
+
.concat(marketInfo.authority
|
|
126
|
+
? { pubkey: marketInfo.authority, isSigner: false, isWritable: false }
|
|
127
|
+
: [])
|
|
128
|
+
.concat(marketInfo.authority && marketInfo.pruneAuthority
|
|
129
|
+
? {
|
|
130
|
+
pubkey: marketInfo.pruneAuthority,
|
|
131
|
+
isSigner: false,
|
|
132
|
+
isWritable: false
|
|
133
|
+
}
|
|
134
|
+
: []);
|
|
135
|
+
const data = Buffer.alloc(dataLayout.span);
|
|
136
|
+
dataLayout.encode({
|
|
137
|
+
version: 0,
|
|
138
|
+
instruction: 0,
|
|
139
|
+
baseLotSize: marketInfo.baseLotSize,
|
|
140
|
+
quoteLotSize: marketInfo.quoteLotSize,
|
|
141
|
+
feeRateBps: marketInfo.feeRateBps,
|
|
142
|
+
vaultSignerNonce: marketInfo.vaultSignerNonce,
|
|
143
|
+
quoteDustThreshold: marketInfo.quoteDustThreshold
|
|
144
|
+
}, data);
|
|
145
|
+
return new web3_js_1.TransactionInstruction({
|
|
146
|
+
keys,
|
|
147
|
+
programId,
|
|
148
|
+
data
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
const { vaultOwner, vaultSignerNonce } = getVaultOwnerAndNonce();
|
|
152
|
+
const ZERO = new bn_js_1.default(0);
|
|
153
|
+
const baseLotSize = new bn_js_1.default(Math.round(Math.pow(10, baseInfo.decimals) * lotSize));
|
|
154
|
+
const quoteLotSize = new bn_js_1.default(Math.round(lotSize * Math.pow(10, quoteInfo.decimals) * tickSize));
|
|
155
|
+
if (baseLotSize.eq(ZERO))
|
|
156
|
+
throw Error("lot size is too small");
|
|
157
|
+
if (quoteLotSize.eq(ZERO))
|
|
158
|
+
throw Error("tick size or lot size is too small");
|
|
159
|
+
const ins1 = [];
|
|
160
|
+
const accountLamports = yield connection.getMinimumBalanceForRentExemption(165);
|
|
161
|
+
ins1.push(web3_js_1.SystemProgram.createAccountWithSeed({
|
|
162
|
+
fromPubkey: wallet,
|
|
163
|
+
basePubkey: wallet,
|
|
164
|
+
seed: baseVault.seed,
|
|
165
|
+
newAccountPubkey: baseVault.publicKey,
|
|
166
|
+
lamports: accountLamports,
|
|
167
|
+
space: 165,
|
|
168
|
+
programId: spl_token_1.TOKEN_PROGRAM_ID
|
|
169
|
+
}), web3_js_1.SystemProgram.createAccountWithSeed({
|
|
170
|
+
fromPubkey: wallet,
|
|
171
|
+
basePubkey: wallet,
|
|
172
|
+
seed: quoteVault.seed,
|
|
173
|
+
newAccountPubkey: quoteVault.publicKey,
|
|
174
|
+
lamports: accountLamports,
|
|
175
|
+
space: 165,
|
|
176
|
+
programId: spl_token_1.TOKEN_PROGRAM_ID
|
|
177
|
+
}), (0, spl_token_1.createInitializeAccountInstruction)(baseVault.publicKey, baseInfo.mint, vaultOwner), (0, spl_token_1.createInitializeAccountInstruction)(quoteVault.publicKey, quoteInfo.mint, vaultOwner));
|
|
178
|
+
const EVENT_QUEUE_ITEMS = 128; // Default: 2978
|
|
179
|
+
const REQUEST_QUEUE_ITEMS = 63; // Default: 63
|
|
180
|
+
const ORDERBOOK_ITEMS = 201; // Default: 909
|
|
181
|
+
const eventQueueSpace = EVENT_QUEUE_ITEMS * 88 + 44 + 48;
|
|
182
|
+
const requestQueueSpace = REQUEST_QUEUE_ITEMS * 80 + 44 + 48;
|
|
183
|
+
const orderBookSpace = ORDERBOOK_ITEMS * 80 + 44 + 48;
|
|
184
|
+
const ins2 = [];
|
|
185
|
+
ins2.push(web3_js_1.SystemProgram.createAccountWithSeed({
|
|
186
|
+
fromPubkey: wallet,
|
|
187
|
+
basePubkey: wallet,
|
|
188
|
+
seed: market.seed,
|
|
189
|
+
newAccountPubkey: market.publicKey,
|
|
190
|
+
lamports: yield connection.getMinimumBalanceForRentExemption(raydium_sdk_1.MARKET_STATE_LAYOUT_V2.span),
|
|
191
|
+
space: raydium_sdk_1.MARKET_STATE_LAYOUT_V2.span,
|
|
192
|
+
programId: dexProgramId
|
|
193
|
+
}), web3_js_1.SystemProgram.createAccountWithSeed({
|
|
194
|
+
fromPubkey: wallet,
|
|
195
|
+
basePubkey: wallet,
|
|
196
|
+
seed: requestQueue.seed,
|
|
197
|
+
newAccountPubkey: requestQueue.publicKey,
|
|
198
|
+
lamports: yield connection.getMinimumBalanceForRentExemption(requestQueueSpace),
|
|
199
|
+
space: requestQueueSpace,
|
|
200
|
+
programId: dexProgramId
|
|
201
|
+
}), web3_js_1.SystemProgram.createAccountWithSeed({
|
|
202
|
+
fromPubkey: wallet,
|
|
203
|
+
basePubkey: wallet,
|
|
204
|
+
seed: eventQueue.seed,
|
|
205
|
+
newAccountPubkey: eventQueue.publicKey,
|
|
206
|
+
lamports: yield connection.getMinimumBalanceForRentExemption(eventQueueSpace),
|
|
207
|
+
space: eventQueueSpace,
|
|
208
|
+
programId: dexProgramId
|
|
209
|
+
}), web3_js_1.SystemProgram.createAccountWithSeed({
|
|
210
|
+
fromPubkey: wallet,
|
|
211
|
+
basePubkey: wallet,
|
|
212
|
+
seed: bids.seed,
|
|
213
|
+
newAccountPubkey: bids.publicKey,
|
|
214
|
+
lamports: yield connection.getMinimumBalanceForRentExemption(orderBookSpace),
|
|
215
|
+
space: orderBookSpace,
|
|
216
|
+
programId: dexProgramId
|
|
217
|
+
}), web3_js_1.SystemProgram.createAccountWithSeed({
|
|
218
|
+
fromPubkey: wallet,
|
|
219
|
+
basePubkey: wallet,
|
|
220
|
+
seed: asks.seed,
|
|
221
|
+
newAccountPubkey: asks.publicKey,
|
|
222
|
+
lamports: yield connection.getMinimumBalanceForRentExemption(orderBookSpace),
|
|
223
|
+
space: orderBookSpace,
|
|
224
|
+
programId: dexProgramId
|
|
225
|
+
}), initializeMarketInstruction({
|
|
226
|
+
programId: dexProgramId,
|
|
227
|
+
marketInfo: {
|
|
228
|
+
id: market.publicKey,
|
|
229
|
+
requestQueue: requestQueue.publicKey,
|
|
230
|
+
eventQueue: eventQueue.publicKey,
|
|
231
|
+
bids: bids.publicKey,
|
|
232
|
+
asks: asks.publicKey,
|
|
233
|
+
baseVault: baseVault.publicKey,
|
|
234
|
+
quoteVault: quoteVault.publicKey,
|
|
235
|
+
baseMint: baseInfo.mint,
|
|
236
|
+
quoteMint: quoteInfo.mint,
|
|
237
|
+
baseLotSize: baseLotSize,
|
|
238
|
+
quoteLotSize: quoteLotSize,
|
|
239
|
+
feeRateBps: feeRateBps,
|
|
240
|
+
vaultSignerNonce: vaultSignerNonce,
|
|
241
|
+
quoteDustThreshold: quoteDustThreshold
|
|
242
|
+
}
|
|
243
|
+
}));
|
|
244
|
+
const ins = {
|
|
245
|
+
address: {
|
|
246
|
+
marketId: market.publicKey,
|
|
247
|
+
requestQueue: requestQueue.publicKey,
|
|
248
|
+
eventQueue: eventQueue.publicKey,
|
|
249
|
+
bids: bids.publicKey,
|
|
250
|
+
asks: asks.publicKey,
|
|
251
|
+
baseVault: baseVault.publicKey,
|
|
252
|
+
quoteVault: quoteVault.publicKey,
|
|
253
|
+
baseMint: baseInfo.mint,
|
|
254
|
+
quoteMint: quoteInfo.mint
|
|
255
|
+
},
|
|
256
|
+
innerTransactions: [
|
|
257
|
+
{
|
|
258
|
+
instructions: ins1,
|
|
259
|
+
signers: [],
|
|
260
|
+
instructionTypes: [
|
|
261
|
+
raydium_sdk_1.InstructionType.createAccount,
|
|
262
|
+
raydium_sdk_1.InstructionType.createAccount,
|
|
263
|
+
raydium_sdk_1.InstructionType.initAccount,
|
|
264
|
+
raydium_sdk_1.InstructionType.initAccount
|
|
265
|
+
]
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
instructions: ins2,
|
|
269
|
+
signers: [],
|
|
270
|
+
instructionTypes: [
|
|
271
|
+
raydium_sdk_1.InstructionType.createAccount,
|
|
272
|
+
raydium_sdk_1.InstructionType.createAccount,
|
|
273
|
+
raydium_sdk_1.InstructionType.createAccount,
|
|
274
|
+
raydium_sdk_1.InstructionType.createAccount,
|
|
275
|
+
raydium_sdk_1.InstructionType.createAccount,
|
|
276
|
+
raydium_sdk_1.InstructionType.initMarket
|
|
277
|
+
]
|
|
278
|
+
}
|
|
279
|
+
]
|
|
280
|
+
};
|
|
281
|
+
return {
|
|
282
|
+
address: ins.address,
|
|
283
|
+
innerTransactions: yield (0, raydium_sdk_1.splitTxAndSigners)({
|
|
284
|
+
connection,
|
|
285
|
+
makeTxVersion,
|
|
286
|
+
computeBudgetConfig: undefined,
|
|
287
|
+
payer: wallet,
|
|
288
|
+
innerTransaction: ins.innerTransactions,
|
|
289
|
+
lookupTableCache
|
|
290
|
+
})
|
|
291
|
+
};
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
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) {
|
|
295
|
+
if (token_owner.publicKey.toBase58().length <= 0 ||
|
|
296
|
+
token_address.length <= 0) {
|
|
297
|
+
console.log("Error: [Create Open Book Market] invalid argument for create open book market");
|
|
298
|
+
return global_1.SPL_ERROR.E_INVALID_ARGUE;
|
|
299
|
+
}
|
|
300
|
+
try {
|
|
301
|
+
const token_mint = new web3_js_1.PublicKey(token_address);
|
|
302
|
+
const mint_info = yield (0, spl_token_1.getMint)(connection, token_mint);
|
|
303
|
+
const base_token = new raydium_sdk_1.Token(spl_token_1.TOKEN_PROGRAM_ID, token_address, mint_info.decimals);
|
|
304
|
+
const quote_token_info = global_1.EnvironmentManager.getQuoteTokenInfo();
|
|
305
|
+
const quote_token = new raydium_sdk_1.Token(spl_token_1.TOKEN_PROGRAM_ID, quote_token_info.address, quote_token_info.decimal, quote_token_info.symbol, quote_token_info.name);
|
|
306
|
+
console.log("[Create Open Book Market]<--------------------make marekt instruction");
|
|
307
|
+
const { innerTransactions, address } = yield makeCreateMarketInstruction({
|
|
308
|
+
connection: connection,
|
|
309
|
+
wallet: token_owner.publicKey,
|
|
310
|
+
baseInfo: base_token,
|
|
311
|
+
quoteInfo: quote_token,
|
|
312
|
+
lotSize: min_order_size,
|
|
313
|
+
tickSize: tick_size,
|
|
314
|
+
dexProgramId: global_1.EnvironmentManager.getProgramID().OPENBOOK_MARKET,
|
|
315
|
+
makeTxVersion: raydium_sdk_1.TxVersion.V0,
|
|
316
|
+
lookupTableCache: global_1.EnvironmentManager.getCacheLTA()
|
|
317
|
+
});
|
|
318
|
+
console.log("[Create Open Book Market]<--------------------create simple transaction");
|
|
319
|
+
const txns = yield (0, raydium_sdk_1.buildSimpleTransaction)({
|
|
320
|
+
connection: connection,
|
|
321
|
+
makeTxVersion: raydium_sdk_1.TxVersion.V0,
|
|
322
|
+
payer: token_owner.publicKey,
|
|
323
|
+
innerTransactions: innerTransactions,
|
|
324
|
+
addLookupTableInfo: global_1.EnvironmentManager.getCacheLTA()
|
|
325
|
+
});
|
|
326
|
+
console.log("[Create Open Book Market]<--------------------send and confirm transaction");
|
|
327
|
+
const txn_result = yield transactions.sendAndConfirmTransactionsWithCheck(connection, token_owner, txns);
|
|
328
|
+
if (txn_result !== global_1.SPL_ERROR.E_OK) {
|
|
329
|
+
console.error("Error: [Create Open Book Market] failed to send and confirm transaction");
|
|
330
|
+
return global_1.SPL_ERROR.E_FAIL;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
catch (error) {
|
|
334
|
+
console.error("Error: [Create Open Book Market] error occured: ", error);
|
|
335
|
+
return global_1.SPL_ERROR.E_FAIL;
|
|
336
|
+
}
|
|
337
|
+
console.log("Success: [Create Open Book Market] Success to create open book market id");
|
|
338
|
+
return global_1.SPL_ERROR.E_OK;
|
|
339
|
+
});
|
|
340
|
+
exports.createOpenBookMarket = createOpenBookMarket;
|
|
@@ -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,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.createPool = void 0;
|
|
36
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
37
|
+
const global_1 = require("./global");
|
|
38
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
39
|
+
const raydium_sdk_1 = require("@raydium-io/raydium-sdk");
|
|
40
|
+
const serum_1 = require("@project-serum/serum");
|
|
41
|
+
const utiles = __importStar(require("./utility"));
|
|
42
|
+
const bn_js_1 = require("bn.js");
|
|
43
|
+
const createPool = (connection, token_owner, token_address, input_token_amount, input_quote_amount) => __awaiter(void 0, void 0, void 0, function* () {
|
|
44
|
+
try {
|
|
45
|
+
if (token_address.length <= 0) {
|
|
46
|
+
console.log("Error: [Create Pool] invalid argument for create pool");
|
|
47
|
+
return { result: global_1.SPL_ERROR.E_INVALID_ARGUE, value: undefined };
|
|
48
|
+
}
|
|
49
|
+
console.log("<---------------------[Create Pool]-----------------------");
|
|
50
|
+
const token_mint = new web3_js_1.PublicKey(token_address);
|
|
51
|
+
const mint_info = yield (0, spl_token_1.getMint)(connection, token_mint);
|
|
52
|
+
const base_token = new raydium_sdk_1.Token(spl_token_1.TOKEN_PROGRAM_ID, token_address, mint_info.decimals);
|
|
53
|
+
const quote_token_info = global_1.EnvironmentManager.getQuoteTokenInfo();
|
|
54
|
+
const quote_token = new raydium_sdk_1.Token(spl_token_1.TOKEN_PROGRAM_ID, quote_token_info.address, quote_token_info.decimal, quote_token_info.symbol, quote_token_info.name);
|
|
55
|
+
const accounts = yield serum_1.Market.findAccountsByMints(connection, base_token.mint, quote_token.mint, global_1.EnvironmentManager.getProgramID().OPENBOOK_MARKET);
|
|
56
|
+
if (accounts.length === 0) {
|
|
57
|
+
throw "Get market account failed";
|
|
58
|
+
}
|
|
59
|
+
console.log("Market Found");
|
|
60
|
+
const market_id = accounts[0].publicKey;
|
|
61
|
+
const start_time = Math.floor(Date.now() / 1000);
|
|
62
|
+
const base_amount = utiles.xWeiAmount(input_token_amount, base_token.decimals);
|
|
63
|
+
const quote_amount = utiles.xWeiAmount(input_quote_amount, quote_token.decimals);
|
|
64
|
+
const wallet_token_accounts = yield utiles.getWalletAccounts(connection, token_owner.publicKey);
|
|
65
|
+
if (!wallet_token_accounts || wallet_token_accounts.length <= 0) {
|
|
66
|
+
throw "Get wallet account failed";
|
|
67
|
+
}
|
|
68
|
+
const { innerTransactions, address } = yield raydium_sdk_1.Liquidity.makeCreatePoolV4InstructionV2Simple({
|
|
69
|
+
connection: connection,
|
|
70
|
+
programId: global_1.EnvironmentManager.getProgramID().AmmV4,
|
|
71
|
+
marketInfo: {
|
|
72
|
+
marketId: market_id,
|
|
73
|
+
programId: global_1.EnvironmentManager.getProgramID().OPENBOOK_MARKET
|
|
74
|
+
},
|
|
75
|
+
baseMintInfo: base_token,
|
|
76
|
+
quoteMintInfo: quote_token,
|
|
77
|
+
baseAmount: base_amount,
|
|
78
|
+
quoteAmount: quote_amount,
|
|
79
|
+
startTime: new bn_js_1.BN(start_time),
|
|
80
|
+
ownerInfo: {
|
|
81
|
+
feePayer: token_owner.publicKey,
|
|
82
|
+
wallet: token_owner.publicKey,
|
|
83
|
+
tokenAccounts: wallet_token_accounts,
|
|
84
|
+
useSOLBalance: true
|
|
85
|
+
},
|
|
86
|
+
makeTxVersion: raydium_sdk_1.TxVersion.V0,
|
|
87
|
+
associatedOnly: false,
|
|
88
|
+
checkCreateATAOwner: true,
|
|
89
|
+
feeDestinationId: global_1.EnvironmentManager.getFeeDestinationId()
|
|
90
|
+
});
|
|
91
|
+
const txns = yield (0, raydium_sdk_1.buildSimpleTransaction)({
|
|
92
|
+
connection: connection,
|
|
93
|
+
makeTxVersion: raydium_sdk_1.TxVersion.V0,
|
|
94
|
+
payer: token_owner.publicKey,
|
|
95
|
+
innerTransactions: innerTransactions,
|
|
96
|
+
addLookupTableInfo: global_1.EnvironmentManager.getCacheLTA(),
|
|
97
|
+
recentBlockhash: (yield connection.getLatestBlockhash()).blockhash
|
|
98
|
+
});
|
|
99
|
+
console.log("Success: [Create Pool] made transaction successfully");
|
|
100
|
+
return { result: global_1.SPL_ERROR.E_OK, value: txns };
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
console.error("Error: [Create Pool] err: ", error);
|
|
104
|
+
return { result: global_1.SPL_ERROR.E_FAIL, value: undefined };
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
exports.createPool = createPool;
|