forked from CryptoTrendingBots/pumpfun-smart-contract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pump.ts
390 lines (351 loc) · 15 KB
/
pump.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Pump } from "../target/types/pump"
import { Connection, PublicKey, Keypair, SystemProgram, Transaction, sendAndConfirmTransaction, ComputeBudgetProgram, SYSVAR_RENT_PUBKEY } from "@solana/web3.js"
import { createMint, getOrCreateAssociatedTokenAccount, mintTo, getAssociatedTokenAddress } from "@solana/spl-token"
import { expect } from "chai";
import { BN } from "bn.js";
import keys from '../keys/users.json'
import key2 from '../keys/user2.json'
import { ASSOCIATED_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@coral-xyz/anchor/dist/cjs/utils/token";
// const connection = new Connection("https://api.devnet.solana.com")
const connection = new Connection("http://localhost:8899")
const curveSeed = "CurveConfiguration"
const POOL_SEED_PREFIX = "liquidity_pool"
const LP_SEED_PREFIX = "LiqudityProvider"
function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
describe("pump", () => {
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace.Pump as Program<Pump>;
// custom setting
const user = Keypair.fromSecretKey(new Uint8Array(keys))
const user2 = Keypair.fromSecretKey(new Uint8Array(key2))
const tokenDecimal = 6
const amount = new BN(1000000000).mul(new BN(10 ** tokenDecimal))
console.log(BigInt(amount.toString()))
console.log(BigInt(amount.toString()).toString())
console.log("🚀 ~ describe ~ amount:", amount.toString())
let mint1: PublicKey
let tokenAta1: PublicKey
// let mint2: PublicKey
// let tokenAta2: PublicKey
console.log("Admin's wallet address is : ", user.publicKey.toBase58())
// it("Airdrop to admin wallet", async () => {
// console.log(`Requesting airdrop to admin for 1SOL : ${user.publicKey.toBase58()}`)
// // 1 - Request Airdrop
// const signature = await connection.requestAirdrop(
// user.publicKey,
// 10 ** 9
// );
// // 2 - Fetch the latest blockhash
// const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
// // 3 - Confirm transaction success
// await connection.confirmTransaction({
// blockhash,
// lastValidBlockHeight,
// signature
// }, 'finalized');
// console.log("admin wallet balance : ", (await connection.getBalance(user.publicKey)) / 10 ** 9, "SOL")
// })
// it("Airdrop to user wallet", async () => {
// console.log("Created a user, address is ", user2.publicKey.toBase58())
// console.log(`Requesting airdrop for another user ${user.publicKey.toBase58()}`)
// // 1 - Request Airdrop
// const signature = await connection.requestAirdrop(
// user2.publicKey,
// 10 ** 9
// );
// // 2 - Fetch the latest blockhash
// const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
// // 3 - Confirm transaction success
// await connection.confirmTransaction({
// blockhash,
// lastValidBlockHeight,
// signature
// }, 'finalized');
// console.log("user balance : ", (await connection.getBalance(user.publicKey)) / 10 ** 9, "SOL")
// })
// it("Mint token1 to user wallet", async () => {
// console.log("Trying to create and mint token1 to user's wallet")
// try {
// mint1 = await createMint(connection, user, user.publicKey, user.publicKey, tokenDecimal)
// console.log('mint1 address: ', mint1.toBase58());
// tokenAta1 = (await getOrCreateAssociatedTokenAccount(connection, user, mint1, user.publicKey)).address
// console.log('token1 account address: ', tokenAta1.toBase58());
// try {
// //minting 100 new tokens to the token address we just created
// await mintTo(connection, user, mint1, tokenAta1, user.publicKey, BigInt(amount.toString()))
// } catch (error) {
// console.log("🚀 ~ here:", error)
// }
// const tokenBalance = await connection.getTokenAccountBalance(tokenAta1)
// console.log("tokenBalance1 in user:", tokenBalance.value.uiAmount)
// console.log('token 1 successfully minted');
// } catch (error) {
// console.log("Token 1 creation error \n", error)
// }
// })
// it("Mint token 2 to user wallet", async () => {
// console.log("Trying to create and mint token 2 to user's wallet")
// try {
// mint2 = await createMint(connection, user, user.publicKey, user.publicKey, tokenDecimal)
// console.log('mint 2 address: ', mint2.toBase58());
// tokenAta2 = (await getOrCreateAssociatedTokenAccount(connection, user, mint2, user.publicKey)).address
// console.log('token 2 account address: ', tokenAta2.toBase58());
// await mintTo(connection, user, mint2, tokenAta2, user.publicKey, BigInt(amount.toString()))
// const tokenBalance = await connection.getTokenAccountBalance(tokenAta2)
// console.log("token 2 Balance in user:", tokenBalance.value.uiAmount)
// console.log('token 2 successfully minted');
// } catch (error) {
// console.log("Token 2 creation error \n", error)
// }
// })
// it("Initialize the contract", async () => {
// try {
// const [curveConfig] = PublicKey.findProgramAddressSync(
// [Buffer.from(curveSeed)],
// program.programId
// )
// const tx = new Transaction()
// .add(
// ComputeBudgetProgram.setComputeUnitLimit({ units: 10_000 }),
// ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 1200_000 }),
// await program.methods
// .initialize(1)
// .accounts({
// dexConfigurationAccount: curveConfig,
// admin: user.publicKey,
// rent: SYSVAR_RENT_PUBKEY,
// systemProgram: SystemProgram.programId
// })
// .instruction()
// )
// tx.feePayer = user.publicKey
// tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash
// // console.log(await connection.simulateTransaction(tx))
// const sig = await sendAndConfirmTransaction(connection, tx, [user], { skipPreflight: true })
// console.log("Successfully initialized : ", sig)
// let pool = await program.account.curveConfiguration.fetch(curveConfig)
// console.log("Pool State : ", pool)
// } catch (error) {
// console.log("Error in initialization :", error)
// }
// });
// it("create pool", async () => {
// try {
// const [poolPda] = PublicKey.findProgramAddressSync(
// [Buffer.from(POOL_SEED_PREFIX), mint1.toBuffer(), mint2.toBuffer()],
// program.programId
// )
// const [liquidityProviderAccount] = PublicKey.findProgramAddressSync(
// [Buffer.from(LP_SEED_PREFIX), poolPda.toBuffer(), user.publicKey.toBuffer()],
// program.programId
// )
// const poolTokenOne = await getAssociatedTokenAddress(
// mint1, poolPda, true
// )
// const poolTokenTwo = await getAssociatedTokenAddress(
// mint2, poolPda, true
// )
// const userAta1 = await getAssociatedTokenAddress(
// mint1, user.publicKey
// )
// const userAta2 = await getAssociatedTokenAddress(
// mint2, user.publicKey
// )
// const tx = new Transaction()
// .add(
// ComputeBudgetProgram.setComputeUnitLimit({ units: 200_000 }),
// ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 200_000 }),
// await program.methods
// .createPool()
// .accounts({
// pool: poolPda,
// mintTokenOne: mint1,
// mintTokenTwo: mint2,
// poolTokenAccountOne: poolTokenOne,
// poolTokenAccountTwo: poolTokenTwo,
// payer: user.publicKey,
// tokenProgram: TOKEN_PROGRAM_ID,
// rent: SYSVAR_RENT_PUBKEY,
// associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
// systemProgram: SystemProgram.programId
// })
// .instruction()
// )
// tx.feePayer = user.publicKey
// tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash
// // console.log(await connection.simulateTransaction(tx))
// const sig = await sendAndConfirmTransaction(connection, tx, [user], { skipPreflight: true })
// console.log("Successfully created pool : ", sig)
// } catch (error) {
// console.log("Error in creating pool", error)
// }
// })
// it("add liquidity", async () => {
// try {
// const [poolPda] = PublicKey.findProgramAddressSync(
// [Buffer.from(POOL_SEED_PREFIX), mint1.toBuffer()],
// program.programId
// )
// const [liquidityProviderAccount] = PublicKey.findProgramAddressSync(
// [Buffer.from(LP_SEED_PREFIX), poolPda.toBuffer(), user.publicKey.toBuffer()],
// program.programId
// )
// const poolTokenOne = await getAssociatedTokenAddress(
// mint1, poolPda, true
// )
// const userAta1 = await getAssociatedTokenAddress(
// mint1, user.publicKey
// )
// const tx = new Transaction()
// .add(
// ComputeBudgetProgram.setComputeUnitLimit({ units: 200_000 }),
// ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 200_000 }),
// await program.methods
// .addLiquidity(new BN(1000000000000000), new BN(30000000000))
// .accounts({
// pool: poolPda,
// mintTokenOne: mint1,
// poolTokenAccountOne: poolTokenOne,
// userTokenAccountOne: userAta1,
// liquidityProviderAccount: liquidityProviderAccount,
// user: user.publicKey,
// tokenProgram: TOKEN_PROGRAM_ID,
// associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
// rent: SYSVAR_RENT_PUBKEY,
// systemProgram: SystemProgram.programId
// })
// .instruction()
// )
// tx.feePayer = user.publicKey
// tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash
// // console.log(await connection.simulateTransaction(tx))
// const sig = await sendAndConfirmTransaction(connection, tx, [user], { skipPreflight: true })
// console.log("Successfully added liquidity : ", sig)
// const signature = await connection.requestAirdrop(
// poolPda,
// 10 ** 9
// );
// // 2 - Fetch the latest blockhash
// const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
// // 3 - Confirm transaction success
// await connection.confirmTransaction({
// blockhash,
// lastValidBlockHeight,
// signature
// }, 'finalized');
// } catch (error) {
// console.log("Error in adding liquidity", error)
// }
// })
it("Swap token", async () => {
try {
const [curveConfig] = PublicKey.findProgramAddressSync(
[Buffer.from(curveSeed)],
program.programId
)
const [poolPda] = PublicKey.findProgramAddressSync(
[Buffer.from(POOL_SEED_PREFIX), mint1.toBuffer()],
program.programId
)
const poolTokenOne = await getAssociatedTokenAddress(
mint1, poolPda, true
)
const userAta1 = await getAssociatedTokenAddress(
mint1, user.publicKey
)
const tx = new Transaction()
.add(
ComputeBudgetProgram.setComputeUnitLimit({ units: 200_000 }),
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 200_000 }),
await program.methods
.swap(new BN(200000000), new BN(2))
.accounts({
pool: poolPda,
mintTokenOne: mint1,
poolTokenAccountOne: poolTokenOne,
userTokenAccountOne: userAta1,
dexConfigurationAccount: curveConfig,
user: user.publicKey,
tokenProgram: TOKEN_PROGRAM_ID,
associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
rent: SYSVAR_RENT_PUBKEY,
systemProgram: SystemProgram.programId
})
.instruction()
)
tx.feePayer = user.publicKey
tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash
// console.log(await connection.simulateTransaction(tx))
const sig = await sendAndConfirmTransaction(connection, tx, [user], { skipPreflight: true })
console.log("Successfully swapped : ", sig)
} catch (error) {
console.log("Error in swap transaction", error)
}
})
// it("Remove liquidity", async () => {
// try {
// const [poolPda] = PublicKey.findProgramAddressSync(
// [Buffer.from(POOL_SEED_PREFIX), mint1.toBuffer()],
// program.programId
// )
// const [liquidityProviderAccount] = PublicKey.findProgramAddressSync(
// [Buffer.from(LP_SEED_PREFIX), poolPda.toBuffer(), user.publicKey.toBuffer()],
// program.programId
// )
// const poolTokenOne = await getAssociatedTokenAddress(
// mint1, poolPda, true
// )
// const userAta1 = await getAssociatedTokenAddress(
// mint1, user.publicKey
// )
// const tx = new Transaction()
// .add(
// ComputeBudgetProgram.setComputeUnitLimit({ units: 200_000 }),
// ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 200_000 }),
// await program.methods
// .removeLiquidity(new BN(10))
// .accounts({
// pool: poolPda,
// mintTokenOne: mint1,
// poolTokenAccountOne: poolTokenOne,
// userTokenAccountOne: userAta1,
// liquidityProviderAccount: liquidityProviderAccount,
// user: user.publicKey,
// tokenProgram: TOKEN_PROGRAM_ID,
// associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
// rent: SYSVAR_RENT_PUBKEY,
// systemProgram: SystemProgram.programId
// })
// .instruction()
// )
// tx.feePayer = user.publicKey
// tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash
// console.log(await connection.simulateTransaction(tx))
// const sig = await sendAndConfirmTransaction(connection, tx, [user], { skipPreflight: true })
// console.log("Successfully Removed liquidity : ", sig)
// } catch (error) {
// console.log("Error in removing liquidity", error)
// }
// })
});
function comparePublicKeys(pubkey1: PublicKey, pubkey2: PublicKey): number {
const key1Bytes = pubkey1.toBuffer();
const key2Bytes = pubkey2.toBuffer();
for (let i = 0; i < key1Bytes.length; i++) {
if (key1Bytes[i] > key2Bytes[i]) {
return 1;
} else if (key1Bytes[i] < key2Bytes[i]) {
return -1;
}
}
return 0;
}
function generateSeed(tokenOne: PublicKey, tokenTwo: PublicKey): string {
return comparePublicKeys(tokenOne, tokenTwo) > 0
? `${tokenOne.toString()}${tokenTwo.toString()}`
: `${tokenTwo.toString()}${tokenOne.toString()}`;
}