-
Notifications
You must be signed in to change notification settings - Fork 74
/
pool.ts
253 lines (200 loc) · 8.46 KB
/
pool.ts
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { PublicKey } from "@solana/web3.js";
import {exitProcess} from "./utils/logger"
import {
jsonInfo2PoolKeys,
Liquidity,
MAINNET_PROGRAM_ID,
MARKET_STATE_LAYOUT_V3,LiquidityPoolKeys,
Token, TokenAmount, ZERO,ONE,TEN,
TOKEN_PROGRAM_ID,parseBigNumberish, bool
} from "@raydium-io/raydium-sdk";
import { unpackMint } from "@solana/spl-token";
import {
getTokenAccountBalance,
assert,
getWalletTokenAccount,
} from "./utils/get_balance";
import { bull_dozer } from "./jito_bundle/send-bundle";
import { buildAndSendTx, build_swap_instructions, build_create_pool_instructions} from "./utils/build_a_sendtxn";
import {
connection,
LP_wallet_keypair,swap_wallet_keypair,
market_id,
quote_Mint_amount,
input_baseMint_tokens_percentage,
lookupTableCache,
delay_pool_open_time,DEFAULT_TOKEN,swap_sol_amount
} from "./config";
import { monitor_both } from "./sell_and_remove_lp/monitor";
type WalletTokenAccounts = Awaited<ReturnType<typeof getWalletTokenAccount>>
async function txCreateAndInitNewPool() {
console.log("LP Wallet Address: ", LP_wallet_keypair.publicKey.toString());
// ------- get pool keys
console.log("------------- get pool keys for pool creation---------")
const tokenAccountRawInfos_LP = await getWalletTokenAccount(
connection,
LP_wallet_keypair.publicKey
)
const tokenAccountRawInfos_Swap = await getWalletTokenAccount(
connection,
swap_wallet_keypair.publicKey
)
const marketBufferInfo = await connection.getAccountInfo(market_id);
if (!marketBufferInfo) return;
const {
baseMint,
quoteMint,
baseLotSize,
quoteLotSize,
baseVault: marketBaseVault,
quoteVault: marketQuoteVault,
bids: marketBids ,
asks: marketAsks,
eventQueue: marketEventQueue
} =
MARKET_STATE_LAYOUT_V3.decode(marketBufferInfo.data);
console.log("Base mint: ", baseMint.toString());
console.log("Quote mint: ", quoteMint.toString());
const accountInfo_base = await connection.getAccountInfo(baseMint);
if (!accountInfo_base) return;
const baseTokenProgramId = accountInfo_base.owner;
const baseDecimals = unpackMint(
baseMint,
accountInfo_base,
baseTokenProgramId
).decimals;
console.log("Base Decimals: ", baseDecimals);
const accountInfo_quote = await connection.getAccountInfo(quoteMint);
if (!accountInfo_quote) return;
const quoteTokenProgramId = accountInfo_quote.owner;
const quoteDecimals = unpackMint(
quoteMint,
accountInfo_quote,
quoteTokenProgramId
).decimals;
console.log("Quote Decimals: ", quoteDecimals);
const associatedPoolKeys = await Liquidity.getAssociatedPoolKeys({
version: 4,
marketVersion: 3,
baseMint,
quoteMint,
baseDecimals,
quoteDecimals,
marketId: new PublicKey(market_id),
programId: MAINNET_PROGRAM_ID.AmmV4,
marketProgramId: MAINNET_PROGRAM_ID.OPENBOOK_MARKET,
});
const { id: ammId, lpMint } = associatedPoolKeys;
console.log("AMM ID: ", ammId.toString());
console.log("lpMint: ", lpMint.toString());
// --------------------------------------------
let quote_amount = quote_Mint_amount * 10 ** quoteDecimals;
// -------------------------------------- Get balance
let base_balance: number;
let quote_balance: number;
if (baseMint.toString() == "So11111111111111111111111111111111111111112") {
base_balance = await connection.getBalance(LP_wallet_keypair.publicKey);
if (!base_balance) return;
console.log("SOL Balance:", base_balance);
} else {
const temp = await getTokenAccountBalance(
connection,
LP_wallet_keypair.publicKey.toString(),
baseMint.toString()
);
base_balance = temp || 0;
}
if (quoteMint.toString() == "So11111111111111111111111111111111111111112") {
quote_balance = await connection.getBalance(LP_wallet_keypair.publicKey);
if (!quote_balance) return;
console.log("SOL Balance:", quote_balance);
assert(
quote_amount <= quote_balance,
"Sol LP input is greater than current balance"
);
} else {
const temp = await getTokenAccountBalance(
connection,
LP_wallet_keypair.publicKey.toString(),
quoteMint.toString()
);
quote_balance = temp || 0;
}
let base_amount_input = Math.ceil(base_balance * input_baseMint_tokens_percentage);
console.log("Input Base: ", base_amount_input);
// step2: init new pool (inject money into the created pool)
const lp_ix = await build_create_pool_instructions(Liquidity,MAINNET_PROGRAM_ID,market_id, LP_wallet_keypair, tokenAccountRawInfos_LP,baseMint,baseDecimals, quoteMint,quoteDecimals,delay_pool_open_time, base_amount_input, quote_amount, lookupTableCache);
console.log("-------- pool creation instructions [DONE] ---------\n")
// -------------------------------------------------
// ---- Swap info
const targetPoolInfo = {
id: associatedPoolKeys.id.toString(),
baseMint: associatedPoolKeys.baseMint.toString(),
quoteMint: associatedPoolKeys.quoteMint.toString(),
lpMint: associatedPoolKeys.lpMint.toString(),
baseDecimals: associatedPoolKeys.baseDecimals,
quoteDecimals: associatedPoolKeys.quoteDecimals,
lpDecimals: associatedPoolKeys.lpDecimals,
version: 4,
programId: associatedPoolKeys.programId.toString(),
authority: associatedPoolKeys.authority.toString(),
openOrders: associatedPoolKeys.openOrders.toString(),
targetOrders: associatedPoolKeys.targetOrders.toString(),
baseVault: associatedPoolKeys.baseVault.toString(),
quoteVault: associatedPoolKeys.quoteVault.toString(),
withdrawQueue: associatedPoolKeys.withdrawQueue.toString(),
lpVault: associatedPoolKeys.lpVault.toString(),
marketVersion: 3,
marketProgramId: associatedPoolKeys.marketProgramId.toString(),
marketId: associatedPoolKeys.marketId.toString(),
marketAuthority: associatedPoolKeys.marketAuthority.toString(),
marketBaseVault: marketBaseVault.toString(),
marketQuoteVault: marketQuoteVault.toString(),
marketBids: marketBids.toString(),
marketAsks: marketAsks.toString(),
marketEventQueue: marketEventQueue.toString(),
lookupTableAccount: PublicKey.default.toString(),
};
console.log(targetPoolInfo)
const poolKeys = jsonInfo2PoolKeys(targetPoolInfo) as LiquidityPoolKeys;
console.log("\n -------- Now getting swap instructions --------");
const TOKEN_TYPE = new Token(TOKEN_PROGRAM_ID, baseMint, baseDecimals, 'ABC', 'ABC')
const inputTokenAmount = new TokenAmount(DEFAULT_TOKEN.WSOL , (swap_sol_amount * (10 ** quoteDecimals)))
const minAmountOut = new TokenAmount(TOKEN_TYPE, parseBigNumberish(ONE))
console.log("Swap wsol [Lamports]: ",inputTokenAmount.raw.words[0])
console.log("Min Amount Out[Lamports]: ",minAmountOut.raw.words[0])
console.log()
const swap_ix = await build_swap_instructions(Liquidity,connection, poolKeys, tokenAccountRawInfos_Swap, swap_wallet_keypair, inputTokenAmount, minAmountOut)
console.log("-------- swap coin instructions [DONE] ---------\n")
// swap ix end ------------------------------------------------------------
console.log("------------- Bundle & Send ---------")
console.log("Please wait for 30 seconds for bundle to be completely executed by all nearests available leaders!");
let success = await bull_dozer(lp_ix,swap_ix);
while (success < 1){
success = await bull_dozer(lp_ix,swap_ix);
}
if (success > 0){
console.log("------------- Bundle Successful ---------");
}
while (true){
try{
await monitor_both(poolKeys);
}
catch {
}
}
// // ----- test simple
// const txids = await buildAndSendTx(LP_wallet_keypair,swap_ix, { skipPreflight: false,maxRetries: 30 });
// console.log("Signature",txids[0]);
// // ------------ test both with errors
// let txids = await buildAndSendTx(LP_wallet_keypair,lp_ix, { skipPreflight: true});
// console.log("Signature",txids[0]);
// let temp = await connection.confirmTransaction(txids[0],'confirmed')
// console.log(temp)
// console.log("AMM_ID: ", ammId.toString());
// txids = await buildAndSendTx(swap_wallet_keypair,swap_ix, { skipPreflight: true});
// console.log("Signature",txids[0]);
// temp = await connection.confirmTransaction(txids[0],'confirmed')
// console.log(temp)
}
txCreateAndInitNewPool();