-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_pool_and_buy.ts
More file actions
168 lines (145 loc) · 5.53 KB
/
Copy pathcreate_pool_and_buy.ts
File metadata and controls
168 lines (145 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
创建Raydium池子 + 捆绑买入
*/
import * as Fs from "fs";
import { Keypair, Connection, PublicKey } from "@solana/web3.js";
import { onBundleResult, onBundleResultEX, sendBundles } from "./utils";
import { searcherClient } from "jito-ts/dist/sdk/block-engine/searcher";
import { bs58 } from "@project-serum/anchor/dist/cjs/utils/bytes";
import { buildSwapTransaction } from "./build_swap_tx";
import { getSlippage, sendAndConfirmTransactionEx } from "../base/utils";
import { buildCreatePoolTransaction } from "./build_create_pool_tx";
import { BaseRay } from "../base/baseRay";
import { BN } from "@project-serum/anchor";
import { Liquidity, MAINNET_PROGRAM_ID } from "@raydium-io/raydium-sdk";
import { getOpenBookMarketKeypair } from "../base/getOpenBookMarketKeypair";
require("dotenv").config();
// 只能用主网来测试
const RPC_URL =
"https://mainnet.helius-rpc.com/?api-key=a72af9a3-d315-4df0-8e00-883ed4cebb61";
// const BLOCK_ENGINE_URL = "mainnet.block-engine.jito.wtf";
const BLOCK_ENGINE_URL = "ny.mainnet.block-engine.jito.wtf";
// const BLOCK_ENGINE_URL = "slc.mainnet.block-engine.jito.wtf";
const BUNDLE_TRANSACTION_LIMIT = 4;
const main = async () => {
const blockEngineUrl = BLOCK_ENGINE_URL;
console.log("BLOCK_ENGINE_URL:", blockEngineUrl);
let createPoolKey = process.env.CREATE_POOL_KEY ?? ""; // 创建池子的私钥
let firstBuyerKey = process.env.FIRST_BUYER_KEY ?? ""; // 买入地址私钥
console.log("createPoolKey: ", createPoolKey);
console.log("firstBuyerKey: ", firstBuyerKey);
const createPoolKeypair = Keypair.fromSecretKey(bs58.decode(createPoolKey));
const buyerKeypair1 = Keypair.fromSecretKey(bs58.decode(firstBuyerKey));
// if (createPoolKeypair) {
// return;
// }
const bundleTransactionLimit = BUNDLE_TRANSACTION_LIMIT;
const c = searcherClient(blockEngineUrl);
// let tipAccounts = await c.getTipAccounts();
// console.log("tipAccounts: ", tipAccounts);
let isLeaderSlot = false;
while (!isLeaderSlot) {
const next_leader = await c.getNextScheduledLeader();
if (!next_leader.ok) {
return next_leader;
}
const num_slots =
next_leader.value.nextLeaderSlot - next_leader.value.currentSlot;
isLeaderSlot = num_slots <= 2;
console.log(`next jito leader slot in ${num_slots} slots`);
await new Promise((r) => setTimeout(r, 2000));
}
console.log("RPC_URL:", RPC_URL);
const conn = new Connection(RPC_URL, "confirmed");
// 共用一个 BaseRay对象,以便来获得 PoolKeys
const baseRay = new BaseRay({ rpcEndpointUrl: conn.rpcEndpoint });
let baseMintAmount = 2_0690_0000;
let baseDecimals = 6;
let quoteMintAmount = 20;
let quoteDecimals = 9;
let mint = "84FhSgZexvSf2pjGGRSiAWtvJJHZcS6VVrXhJmqYmidx"; // TODO
let marketId = (await getOpenBookMarketKeypair(mint)).publicKey.toBase58();
console.log("marketId: ", marketId);
// if (createPoolKeypair) {
// return;
// }
let poolId1 = Liquidity.getAssociatedId({
marketId: new PublicKey(marketId),
programId: new PublicKey(
MAINNET_PROGRAM_ID.AmmV4 // "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8"
),
});
console.log("poolId(预计算): ", poolId1.toBase58());
console.log("=====buildCreatePoolTransaction 开始 =====");
let { tx: createPoolTx, txInfo: tx1Info } =
await buildCreatePoolTransaction(baseRay, conn, createPoolKeypair, {
marketId: new PublicKey(marketId),
baseMintAmount: baseMintAmount,
quoteMintAmount: quoteMintAmount,
});
console.log("=====buildCreatePoolTransaction 结束=====");
let poolId = tx1Info.poolId;
console.log("poolId(生成): ", poolId.toBase58());
if (poolId.toBase58() != poolId1.toBase58()) {
console.log(
"计算的 poolId 和生成的 poolId 不匹配, 请检查marketId是否正确"
);
return;
}
// if (createPoolKeypair) {
// return;
// }
// https://github.com/raydium-io/raydium-amm/blob/master/program/src/processor.rs#L1144-L1153
let lpSupply =
Math.round(
Math.sqrt(
baseMintAmount *
Math.pow(10, baseDecimals) *
quoteMintAmount *
Math.pow(10, quoteDecimals)
)
) - Math.pow(10, 6);
let baseReserve = Math.round(baseMintAmount * Math.pow(10, baseDecimals));
let quoteReserve = Math.round(
quoteMintAmount * Math.pow(10, quoteDecimals)
);
// 就用一个地址买入即可, 不必藏着掖着,光明磊落
let buyTx1 = await buildSwapTransaction(
baseRay,
conn,
buyerKeypair1,
{
poolId: poolId,
buyToken: "base", // 买入 Token
sellToken: "quote",
amountSide: "send",
amount: 115, // 110 SOL
slippage: getSlippage(3),
},
new BN(lpSupply),
new BN(baseReserve),
new BN(quoteReserve)
);
const result = await sendBundles(
c,
bundleTransactionLimit,
createPoolKeypair,
conn,
[createPoolTx, buyTx1]
// []
);
if (!result.ok) {
console.error("Failed to send bundles:", result.error);
return;
}
console.log("Successfully sent bundles:", result.value);
// onBundleResult(c);
await onBundleResultEX(c);
};
main()
.then(() => {
console.log("Sending bundle");
})
.catch((e) => {
throw e;
});