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
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Connection, Keypair, PublicKey, TokenAmount } from "@solana/web3.js";
|
|
2
|
+
import {
|
|
3
|
+
LOOKUP_TABLE_CACHE,
|
|
4
|
+
Liquidity,
|
|
5
|
+
MAINNET_PROGRAM_ID,
|
|
6
|
+
ProgramId,
|
|
7
|
+
Token,
|
|
8
|
+
TxVersion
|
|
9
|
+
} from "@raydium-io/raydium-sdk";
|
|
10
|
+
import { getWalletTokenAccount, xWeiAmount } from "../utils/get-balance";
|
|
11
|
+
import { BN } from "bn.js";
|
|
12
|
+
import { getComputeBudgetConfig } from "../utils/budget";
|
|
13
|
+
import { GenerateTransactionError } from "../errors";
|
|
14
|
+
|
|
15
|
+
export const generateCreatePoolInst = async (
|
|
16
|
+
connection: Connection,
|
|
17
|
+
owner: Keypair,
|
|
18
|
+
marketId: PublicKey,
|
|
19
|
+
baseMint: PublicKey,
|
|
20
|
+
quoteMint: PublicKey,
|
|
21
|
+
baseDecimals: number,
|
|
22
|
+
quoteDecimals: number,
|
|
23
|
+
baseAmount: number,
|
|
24
|
+
quoteAmount: number,
|
|
25
|
+
programId: ProgramId = MAINNET_PROGRAM_ID,
|
|
26
|
+
delayTime: number = 0,
|
|
27
|
+
feeDestinationId: PublicKey = new PublicKey(
|
|
28
|
+
"7YttLkHDoNj9wyDur5pM1ejNaAvT9X4eqaYcHQqtj2G5"
|
|
29
|
+
)
|
|
30
|
+
): Promise<any> => {
|
|
31
|
+
try {
|
|
32
|
+
const tokenAccountInfos = await getWalletTokenAccount(
|
|
33
|
+
connection,
|
|
34
|
+
owner.publicKey
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const { innerTransactions } =
|
|
38
|
+
await Liquidity.makeCreatePoolV4InstructionV2Simple({
|
|
39
|
+
connection,
|
|
40
|
+
programId: programId.AmmV4,
|
|
41
|
+
marketInfo: {
|
|
42
|
+
programId: programId.OPENBOOK_MARKET,
|
|
43
|
+
marketId: marketId
|
|
44
|
+
},
|
|
45
|
+
associatedOnly: false,
|
|
46
|
+
ownerInfo: {
|
|
47
|
+
feePayer: owner.publicKey,
|
|
48
|
+
wallet: owner.publicKey,
|
|
49
|
+
tokenAccounts: tokenAccountInfos,
|
|
50
|
+
useSOLBalance: true
|
|
51
|
+
},
|
|
52
|
+
baseMintInfo: {
|
|
53
|
+
mint: baseMint,
|
|
54
|
+
decimals: baseDecimals
|
|
55
|
+
},
|
|
56
|
+
quoteMintInfo: {
|
|
57
|
+
mint: quoteMint,
|
|
58
|
+
decimals: quoteDecimals
|
|
59
|
+
},
|
|
60
|
+
startTime: new BN(Math.floor(Date.now() / 1000) + delayTime),
|
|
61
|
+
baseAmount: xWeiAmount(baseAmount, baseDecimals),
|
|
62
|
+
quoteAmount: xWeiAmount(quoteAmount, quoteDecimals),
|
|
63
|
+
checkCreateATAOwner: true,
|
|
64
|
+
makeTxVersion: TxVersion.V0,
|
|
65
|
+
feeDestinationId: feeDestinationId
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return innerTransactions;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
throw new GenerateTransactionError(`GenerateCreatePoolInst: ${error}`);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { LAMPORTS_PER_SOL, PublicKey, SystemProgram } from "@solana/web3.js";
|
|
2
|
+
|
|
3
|
+
export const generateSolTransferInst = (
|
|
4
|
+
from: PublicKey,
|
|
5
|
+
to: PublicKey,
|
|
6
|
+
amount: number
|
|
7
|
+
) => {
|
|
8
|
+
return SystemProgram.transfer({
|
|
9
|
+
fromPubkey: from,
|
|
10
|
+
toPubkey: to,
|
|
11
|
+
lamports: Math.floor(amount * LAMPORTS_PER_SOL)
|
|
12
|
+
});
|
|
13
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createTransferCheckedInstruction } from "@solana/spl-token";
|
|
2
|
+
import { PublicKey } from "@solana/web3.js";
|
|
3
|
+
|
|
4
|
+
export const generateTransferInst = (
|
|
5
|
+
sender: PublicKey,
|
|
6
|
+
fromAccount: PublicKey,
|
|
7
|
+
toAccount: PublicKey,
|
|
8
|
+
ca: PublicKey,
|
|
9
|
+
amount: number,
|
|
10
|
+
decimal: number
|
|
11
|
+
) => {
|
|
12
|
+
const txn = createTransferCheckedInstruction(
|
|
13
|
+
fromAccount,
|
|
14
|
+
ca,
|
|
15
|
+
toAccount,
|
|
16
|
+
sender,
|
|
17
|
+
amount,
|
|
18
|
+
decimal
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
return txn;
|
|
22
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Liquidity,
|
|
3
|
+
LiquidityPoolInfo,
|
|
4
|
+
LiquidityPoolKeys,
|
|
5
|
+
Percent,
|
|
6
|
+
Token,
|
|
7
|
+
TokenAmount
|
|
8
|
+
} from "@raydium-io/raydium-sdk";
|
|
9
|
+
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
|
|
10
|
+
import { Connection, PublicKey } from "@solana/web3.js";
|
|
11
|
+
import BN from "bn.js";
|
|
12
|
+
|
|
13
|
+
export class PoolManager {
|
|
14
|
+
private poolKey: LiquidityPoolKeys;
|
|
15
|
+
private poolInfo: any;
|
|
16
|
+
private baseToken: Token;
|
|
17
|
+
private quoteToken: Token;
|
|
18
|
+
|
|
19
|
+
constructor(poolKey: LiquidityPoolKeys) {
|
|
20
|
+
this.poolKey = poolKey;
|
|
21
|
+
|
|
22
|
+
this.baseToken = new Token(
|
|
23
|
+
TOKEN_PROGRAM_ID,
|
|
24
|
+
poolKey.baseMint,
|
|
25
|
+
poolKey.baseDecimals
|
|
26
|
+
);
|
|
27
|
+
this.quoteToken = new Token(
|
|
28
|
+
TOKEN_PROGRAM_ID,
|
|
29
|
+
poolKey.quoteMint,
|
|
30
|
+
poolKey.quoteDecimals
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
this.poolInfo = {
|
|
34
|
+
baseDecimals: poolKey.baseDecimals,
|
|
35
|
+
quoteDecimals: poolKey.baseDecimals,
|
|
36
|
+
baseReserve: new BN(0),
|
|
37
|
+
quoteReserve: new BN(0)
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public computeSolOut(baseIn: BN) {
|
|
42
|
+
return Liquidity.computeAmountOut({
|
|
43
|
+
poolKeys: this.poolKey,
|
|
44
|
+
poolInfo: this.poolInfo,
|
|
45
|
+
amountIn: new TokenAmount(this.baseToken, baseIn, true),
|
|
46
|
+
currencyOut: this.quoteToken,
|
|
47
|
+
slippage: new Percent(1, 100)
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public computeSolIn(baseAmount: BN) {
|
|
52
|
+
return Liquidity.computeAmountIn({
|
|
53
|
+
poolKeys: this.poolKey,
|
|
54
|
+
poolInfo: this.poolInfo,
|
|
55
|
+
amountOut: new TokenAmount(this.baseToken, baseAmount, true),
|
|
56
|
+
currencyIn: this.quoteToken,
|
|
57
|
+
slippage: new Percent(1, 100)
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public updateWithReverse(baseReserve: BN, quoteReserve: BN) {
|
|
62
|
+
this.poolInfo = {
|
|
63
|
+
...this.poolInfo,
|
|
64
|
+
baseReserve: baseReserve,
|
|
65
|
+
quoteReserve: quoteReserve
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public async updateWithReal(connection: Connection) {
|
|
70
|
+
try {
|
|
71
|
+
this.poolInfo = await Liquidity.fetchInfo({
|
|
72
|
+
connection,
|
|
73
|
+
poolKeys: this.poolKey
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return true;
|
|
77
|
+
} catch (error) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public getPoolInfo() {
|
|
83
|
+
return this.poolInfo;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CurrencyAmount,
|
|
3
|
+
Liquidity,
|
|
4
|
+
LiquidityPoolInfo,
|
|
5
|
+
Percent,
|
|
6
|
+
TOKEN_PROGRAM_ID,
|
|
7
|
+
Token,
|
|
8
|
+
TokenAmount
|
|
9
|
+
} from "@raydium-io/raydium-sdk";
|
|
10
|
+
import { EnvironmentManager, TOKEN_INFO } from "./global";
|
|
11
|
+
import { Connection, PublicKey } from "@solana/web3.js";
|
|
12
|
+
import { getMint } from "@solana/spl-token";
|
|
13
|
+
import { BN } from "bn.js";
|
|
14
|
+
import { Market } from "@project-serum/serum";
|
|
15
|
+
import { xWeiAmount } from "./utility";
|
|
16
|
+
|
|
17
|
+
interface PairToken {
|
|
18
|
+
base_token: Token;
|
|
19
|
+
quote_token: Token;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class PoolManager {
|
|
23
|
+
private base_token_info: TOKEN_INFO;
|
|
24
|
+
private quote_token_info: TOKEN_INFO;
|
|
25
|
+
private base_amount: number;
|
|
26
|
+
private quote_amount: number;
|
|
27
|
+
private market_id: PublicKey;
|
|
28
|
+
private pool_info: LiquidityPoolInfo;
|
|
29
|
+
private pool_keys: any;
|
|
30
|
+
|
|
31
|
+
constructor(
|
|
32
|
+
base_token_info: TOKEN_INFO,
|
|
33
|
+
quote_token_info: TOKEN_INFO,
|
|
34
|
+
base_amount: number,
|
|
35
|
+
quote_amount: number,
|
|
36
|
+
market_id: PublicKey
|
|
37
|
+
) {
|
|
38
|
+
this.base_token_info = base_token_info;
|
|
39
|
+
this.quote_token_info = quote_token_info;
|
|
40
|
+
this.base_amount = base_amount;
|
|
41
|
+
this.quote_amount = quote_amount;
|
|
42
|
+
this.market_id = market_id;
|
|
43
|
+
|
|
44
|
+
const { base_token, quote_token } = this.getPairToken();
|
|
45
|
+
|
|
46
|
+
this.pool_keys = Liquidity.getAssociatedPoolKeys({
|
|
47
|
+
version: 4,
|
|
48
|
+
marketVersion: 3,
|
|
49
|
+
baseMint: base_token.mint,
|
|
50
|
+
quoteMint: quote_token.mint,
|
|
51
|
+
baseDecimals: base_token.decimals,
|
|
52
|
+
quoteDecimals: quote_token.decimals,
|
|
53
|
+
marketId: this.market_id,
|
|
54
|
+
programId: EnvironmentManager.getProgramID().AmmV4,
|
|
55
|
+
marketProgramId: EnvironmentManager.getProgramID().OPENBOOK_MARKET
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
this.pool_info = {
|
|
59
|
+
status: new BN(0),
|
|
60
|
+
baseDecimals: this.base_token_info.decimal,
|
|
61
|
+
lpDecimals: this.quote_token_info.decimal,
|
|
62
|
+
quoteDecimals: this.quote_token_info.decimal,
|
|
63
|
+
baseReserve: xWeiAmount(this.base_amount, this.base_token_info.decimal),
|
|
64
|
+
quoteReserve: xWeiAmount(
|
|
65
|
+
this.quote_amount,
|
|
66
|
+
this.quote_token_info.decimal
|
|
67
|
+
),
|
|
68
|
+
lpSupply: new BN(base_amount),
|
|
69
|
+
startTime: new BN(0)
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
initializePoolInfo(market_id: PublicKey) {
|
|
74
|
+
this.market_id = market_id;
|
|
75
|
+
const { base_token, quote_token } = this.getPairToken();
|
|
76
|
+
this.pool_keys = Liquidity.getAssociatedPoolKeys({
|
|
77
|
+
version: 4,
|
|
78
|
+
marketVersion: 3,
|
|
79
|
+
baseMint: base_token.mint,
|
|
80
|
+
quoteMint: quote_token.mint,
|
|
81
|
+
baseDecimals: base_token.decimals,
|
|
82
|
+
quoteDecimals: quote_token.decimals,
|
|
83
|
+
marketId: this.market_id,
|
|
84
|
+
programId: EnvironmentManager.getProgramID().AmmV4,
|
|
85
|
+
marketProgramId: EnvironmentManager.getProgramID().OPENBOOK_MARKET
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
this.pool_info = {
|
|
89
|
+
status: new BN(0),
|
|
90
|
+
baseDecimals: this.base_token_info.decimal,
|
|
91
|
+
lpDecimals: this.quote_token_info.decimal,
|
|
92
|
+
quoteDecimals: this.quote_token_info.decimal,
|
|
93
|
+
baseReserve: xWeiAmount(this.base_amount, this.base_token_info.decimal),
|
|
94
|
+
quoteReserve: xWeiAmount(
|
|
95
|
+
this.quote_amount,
|
|
96
|
+
this.quote_token_info.decimal
|
|
97
|
+
),
|
|
98
|
+
lpSupply: new BN(this.base_amount),
|
|
99
|
+
startTime: new BN(0)
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
console.log(
|
|
103
|
+
"Simulated Pool baseReserve: ",
|
|
104
|
+
this.pool_info.baseReserve.toString()
|
|
105
|
+
);
|
|
106
|
+
console.log(
|
|
107
|
+
"Simulated Pool quoteReserve: ",
|
|
108
|
+
this.pool_info.quoteReserve.toString()
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
computeSolAmount(base_amount: number, in_out: boolean): CurrencyAmount {
|
|
113
|
+
const { base_token, quote_token } = this.getPairToken();
|
|
114
|
+
// console.log("Simulated PoolInfo: ", this.pool_info);
|
|
115
|
+
if (in_out) {
|
|
116
|
+
const { maxAmountIn } = Liquidity.computeAmountIn({
|
|
117
|
+
poolKeys: this.pool_keys,
|
|
118
|
+
poolInfo: this.pool_info,
|
|
119
|
+
amountOut: new TokenAmount(base_token, base_amount, false),
|
|
120
|
+
currencyIn: quote_token,
|
|
121
|
+
slippage: new Percent(1, 100)
|
|
122
|
+
});
|
|
123
|
+
return maxAmountIn;
|
|
124
|
+
} else {
|
|
125
|
+
const { minAmountOut } = Liquidity.computeAmountOut({
|
|
126
|
+
poolKeys: this.pool_keys,
|
|
127
|
+
poolInfo: this.pool_info,
|
|
128
|
+
amountIn: new TokenAmount(base_token, base_amount, false),
|
|
129
|
+
currencyOut: quote_token,
|
|
130
|
+
slippage: new Percent(1, 100)
|
|
131
|
+
});
|
|
132
|
+
return minAmountOut;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
computeCurrentPrice(): number {
|
|
136
|
+
return this.quote_amount / this.base_amount;
|
|
137
|
+
}
|
|
138
|
+
buyToken(base_amount: number) {
|
|
139
|
+
const sol_input = this.computeSolAmount(base_amount, true);
|
|
140
|
+
const { base_token, quote_token } = this.getPairToken();
|
|
141
|
+
const { amountOut } = Liquidity.computeAmountOut({
|
|
142
|
+
poolKeys: this.pool_keys,
|
|
143
|
+
poolInfo: this.pool_info,
|
|
144
|
+
amountIn: sol_input,
|
|
145
|
+
currencyOut: base_token,
|
|
146
|
+
slippage: new Percent(1, 100)
|
|
147
|
+
});
|
|
148
|
+
this.quote_amount += sol_input.raw
|
|
149
|
+
.div(new BN(10 ** this.quote_token_info.decimal))
|
|
150
|
+
.toNumber();
|
|
151
|
+
this.base_amount -= base_amount;
|
|
152
|
+
this.pool_info = {
|
|
153
|
+
...this.pool_info,
|
|
154
|
+
baseReserve: this.pool_info.baseReserve.sub(amountOut.raw),
|
|
155
|
+
quoteReserve: this.pool_info.quoteReserve.add(sol_input.raw)
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
console.log(
|
|
159
|
+
"Simulated Pool baseReserve: ",
|
|
160
|
+
this.pool_info.baseReserve.toString()
|
|
161
|
+
);
|
|
162
|
+
console.log(
|
|
163
|
+
"Simulated Pool quoteReserve: ",
|
|
164
|
+
this.pool_info.quoteReserve.toString()
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
// this.initializePoolInfo(this.market_id);
|
|
168
|
+
}
|
|
169
|
+
sellToken(base_amount: number) {
|
|
170
|
+
const sol_input = this.computeSolAmount(base_amount, false);
|
|
171
|
+
this.quote_amount -= sol_input.raw
|
|
172
|
+
.div(new BN(10 ** this.quote_token_info.decimal))
|
|
173
|
+
.toNumber();
|
|
174
|
+
this.base_amount += base_amount;
|
|
175
|
+
this.initializePoolInfo(this.market_id);
|
|
176
|
+
}
|
|
177
|
+
getPairToken(): PairToken {
|
|
178
|
+
const base_mint = new PublicKey(this.base_token_info.address);
|
|
179
|
+
const base = new Token(
|
|
180
|
+
TOKEN_PROGRAM_ID,
|
|
181
|
+
base_mint,
|
|
182
|
+
this.base_token_info.decimal,
|
|
183
|
+
this.base_token_info.symbol,
|
|
184
|
+
this.base_token_info.name
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
const quote = new Token(
|
|
188
|
+
TOKEN_PROGRAM_ID,
|
|
189
|
+
new PublicKey(this.quote_token_info.address),
|
|
190
|
+
this.quote_token_info.decimal,
|
|
191
|
+
this.quote_token_info.symbol,
|
|
192
|
+
this.quote_token_info.name
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
return { base_token: base, quote_token: quote };
|
|
196
|
+
}
|
|
197
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
|
|
2
|
+
import { EnvironmentManager, SPL_ERROR, TX_RET } from "./global";
|
|
3
|
+
import { TOKEN_PROGRAM_ID, getMint } from "@solana/spl-token";
|
|
4
|
+
import {
|
|
5
|
+
Liquidity,
|
|
6
|
+
LiquidityPoolKeys,
|
|
7
|
+
Token,
|
|
8
|
+
TokenAmount,
|
|
9
|
+
TxVersion,
|
|
10
|
+
buildSimpleTransaction
|
|
11
|
+
} from "@raydium-io/raydium-sdk";
|
|
12
|
+
import { getWalletAccounts } from "./utility";
|
|
13
|
+
|
|
14
|
+
export const sellToken = async (
|
|
15
|
+
connection: Connection,
|
|
16
|
+
buyer: Keypair,
|
|
17
|
+
token_address: string,
|
|
18
|
+
base_amount: number,
|
|
19
|
+
quote_amount: number,
|
|
20
|
+
pool_key: LiquidityPoolKeys
|
|
21
|
+
): Promise<TX_RET> => {
|
|
22
|
+
if (token_address.length <= 0 || base_amount <= 0) {
|
|
23
|
+
console.error("Error: [Buy Token] invalid argument iput!!!");
|
|
24
|
+
return { result: SPL_ERROR.E_INVALID_ARGUE, value: undefined };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const token_mint = new PublicKey(token_address);
|
|
29
|
+
const token_info = await getMint(connection, token_mint);
|
|
30
|
+
const base_token = new Token(
|
|
31
|
+
TOKEN_PROGRAM_ID,
|
|
32
|
+
token_address,
|
|
33
|
+
token_info.decimals
|
|
34
|
+
);
|
|
35
|
+
const quote_info = EnvironmentManager.getQuoteTokenInfo();
|
|
36
|
+
const quote_token = new Token(
|
|
37
|
+
TOKEN_PROGRAM_ID,
|
|
38
|
+
quote_info.address,
|
|
39
|
+
quote_info.decimal,
|
|
40
|
+
quote_info.symbol,
|
|
41
|
+
quote_info.name
|
|
42
|
+
);
|
|
43
|
+
const base_token_amount = new TokenAmount(base_token, base_amount, false);
|
|
44
|
+
const quote_token_amount = new TokenAmount(
|
|
45
|
+
quote_token,
|
|
46
|
+
quote_amount === 0 ? 1 : quote_amount,
|
|
47
|
+
quote_amount === 0 ? true : false
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const wallet_token_accounts = await getWalletAccounts(
|
|
51
|
+
connection,
|
|
52
|
+
buyer.publicKey
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const { innerTransactions } = await Liquidity.makeSwapInstructionSimple({
|
|
56
|
+
connection: connection,
|
|
57
|
+
poolKeys: pool_key,
|
|
58
|
+
userKeys: {
|
|
59
|
+
tokenAccounts: wallet_token_accounts,
|
|
60
|
+
owner: buyer.publicKey
|
|
61
|
+
},
|
|
62
|
+
amountIn: base_token_amount,
|
|
63
|
+
amountOut: quote_token_amount,
|
|
64
|
+
fixedSide: "in",
|
|
65
|
+
makeTxVersion: TxVersion.V0
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const transactions = await buildSimpleTransaction({
|
|
69
|
+
connection: connection,
|
|
70
|
+
makeTxVersion: TxVersion.V0,
|
|
71
|
+
payer: buyer.publicKey,
|
|
72
|
+
innerTransactions: innerTransactions,
|
|
73
|
+
addLookupTableInfo: EnvironmentManager.getCacheLTA(),
|
|
74
|
+
recentBlockhash: (await connection.getLatestBlockhash()).blockhash
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return { result: SPL_ERROR.E_OK, value: transactions };
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.error("Error: [buy Tokens] error code: ", error);
|
|
80
|
+
return { result: SPL_ERROR.E_FAIL, value: undefined };
|
|
81
|
+
}
|
|
82
|
+
};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Connection,
|
|
3
|
+
Keypair,
|
|
4
|
+
LAMPORTS_PER_SOL,
|
|
5
|
+
PublicKey
|
|
6
|
+
} from "@solana/web3.js";
|
|
7
|
+
import { TransactionInfo } from "../instructions/build-instruction";
|
|
8
|
+
import { searcherClient } from "jito-ts/dist/sdk/block-engine/searcher";
|
|
9
|
+
import { Bundle } from "jito-ts/dist/sdk/block-engine/types";
|
|
10
|
+
import base58 from "bs58";
|
|
11
|
+
import { TransactionExecuteError } from "../errors";
|
|
12
|
+
import { sleep } from "../utils/util";
|
|
13
|
+
import { signTransaction } from "./sign-tx";
|
|
14
|
+
|
|
15
|
+
export async function createAndSendBundle(
|
|
16
|
+
connection: Connection,
|
|
17
|
+
bundleTranasction: TransactionInfo[],
|
|
18
|
+
payer: Keypair,
|
|
19
|
+
jitoBlockEngine: string,
|
|
20
|
+
jitoAuther: Keypair,
|
|
21
|
+
fee: number
|
|
22
|
+
) {
|
|
23
|
+
const searcher = searcherClient(jitoBlockEngine, jitoAuther);
|
|
24
|
+
|
|
25
|
+
let txnConfirmResult: boolean = false;
|
|
26
|
+
let breakCheckTransactionStatus: boolean = false;
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
const blockhash = (await connection.getLatestBlockhash("finalized"))
|
|
30
|
+
.blockhash;
|
|
31
|
+
const realForBundle: any[] = [];
|
|
32
|
+
for (let i = 0; i < bundleTranasction.length; i++) {
|
|
33
|
+
bundleTranasction[i].txn.message.recentBlockhash = (
|
|
34
|
+
await connection.getLatestBlockhash("finalized")
|
|
35
|
+
).blockhash;
|
|
36
|
+
signTransaction(bundleTranasction[i]);
|
|
37
|
+
realForBundle.push(bundleTranasction[i].txn);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let bundleTx = new Bundle(realForBundle, 5);
|
|
41
|
+
if (payer) {
|
|
42
|
+
const tipAccount = new PublicKey((await searcher.getTipAccounts())[0]);
|
|
43
|
+
bundleTx.addTipTx(payer, fee * LAMPORTS_PER_SOL, tipAccount, blockhash);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
searcher.onBundleResult(
|
|
47
|
+
async (bundleResult: any) => {
|
|
48
|
+
console.log(bundleResult);
|
|
49
|
+
|
|
50
|
+
if (bundleResult.rejected) {
|
|
51
|
+
try {
|
|
52
|
+
if (
|
|
53
|
+
bundleResult.rejected.simulationFailure.msg.includes(
|
|
54
|
+
"custom program error"
|
|
55
|
+
) ||
|
|
56
|
+
bundleResult.rejected.simulationFailure.msg.includes(
|
|
57
|
+
"Error processing Instruction"
|
|
58
|
+
)
|
|
59
|
+
) {
|
|
60
|
+
breakCheckTransactionStatus = true;
|
|
61
|
+
} else if (
|
|
62
|
+
bundleResult.rejected.simulationFailure.msg.includes(
|
|
63
|
+
"This transaction has already been processed"
|
|
64
|
+
) ||
|
|
65
|
+
bundleResult.rejected.droppedBundle.msg.includes(
|
|
66
|
+
"Bundle partially processed"
|
|
67
|
+
)
|
|
68
|
+
) {
|
|
69
|
+
txnConfirmResult = true;
|
|
70
|
+
breakCheckTransactionStatus = true;
|
|
71
|
+
}
|
|
72
|
+
} catch (error) {}
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
(error) => {
|
|
76
|
+
breakCheckTransactionStatus = false;
|
|
77
|
+
}
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
console.log("Send");
|
|
81
|
+
await searcher.sendBundle(bundleTx);
|
|
82
|
+
console.log("Finish send");
|
|
83
|
+
setTimeout(() => {
|
|
84
|
+
breakCheckTransactionStatus = true;
|
|
85
|
+
}, 20000);
|
|
86
|
+
|
|
87
|
+
const txnHash = base58.encode(
|
|
88
|
+
realForBundle[realForBundle.length - 1].signatures[0]
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
console.log(txnHash);
|
|
92
|
+
while (!breakCheckTransactionStatus) {
|
|
93
|
+
await sleep(1000);
|
|
94
|
+
try {
|
|
95
|
+
const result = await connection.getSignatureStatus(txnHash, {
|
|
96
|
+
searchTransactionHistory: true
|
|
97
|
+
});
|
|
98
|
+
if (result && result.value && result.value.confirmationStatus) {
|
|
99
|
+
txnConfirmResult = true;
|
|
100
|
+
breakCheckTransactionStatus = true;
|
|
101
|
+
}
|
|
102
|
+
} catch (error) {
|
|
103
|
+
txnConfirmResult = false;
|
|
104
|
+
breakCheckTransactionStatus = false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (txnConfirmResult) {
|
|
109
|
+
return true;
|
|
110
|
+
} else {
|
|
111
|
+
throw new TransactionExecuteError(`Bundle Error: failed`);
|
|
112
|
+
}
|
|
113
|
+
} catch (error) {
|
|
114
|
+
throw new TransactionExecuteError(`Bundle Error: ${error}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Connection,
|
|
3
|
+
SendOptions,
|
|
4
|
+
TransactionConfirmationStrategy
|
|
5
|
+
} from "@solana/web3.js";
|
|
6
|
+
import { TransactionInfo } from "../instructions/build-instruction";
|
|
7
|
+
import { signTransaction } from "./sign-tx";
|
|
8
|
+
import { TransactionExecuteError } from "../errors";
|
|
9
|
+
|
|
10
|
+
export async function sendTx(
|
|
11
|
+
connection: Connection,
|
|
12
|
+
tx: TransactionInfo,
|
|
13
|
+
option?: SendOptions
|
|
14
|
+
): Promise<string> {
|
|
15
|
+
try {
|
|
16
|
+
tx.txn.message.recentBlockhash = (
|
|
17
|
+
await connection.getLatestBlockhash("finalized")
|
|
18
|
+
).blockhash;
|
|
19
|
+
signTransaction(tx);
|
|
20
|
+
return await connection.sendTransaction(tx.txn, option);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
throw new TransactionExecuteError(`sendTx: ${error}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function confirmTx(connection: Connection, txId: string) {
|
|
27
|
+
try {
|
|
28
|
+
await connection.confirmTransaction({
|
|
29
|
+
signature: txId,
|
|
30
|
+
abortSignal: AbortSignal.timeout(60000)
|
|
31
|
+
} as TransactionConfirmationStrategy);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
throw new TransactionExecuteError(`confirmTx: ${error}`);
|
|
34
|
+
}
|
|
35
|
+
}
|