-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
119 lines (105 loc) · 3.45 KB
/
Copy pathindex.ts
File metadata and controls
119 lines (105 loc) · 3.45 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
import {
Connection,
Keypair,
PublicKey,
LAMPORTS_PER_SOL,
VersionedTransaction,
} from "@solana/web3.js";
import { NATIVE_MINT } from "@solana/spl-token";
import bs58 from "bs58";
import dotenv from "dotenv";
import { Wallet } from "@project-serum/anchor";
import { getSwapInfo, getSwapTransaction } from "./jupiterService";
import { sendBundle } from "./jitoService";
dotenv.config();
const WALLET_PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY as string;
const RPC_URL = process.env.RPC_URL as string;
const wallet = Keypair.fromSecretKey(bs58.decode(WALLET_PRIVATE_KEY));
const anchorWallet = new Wallet(wallet);
async function SplTokenSwap(
tokenAddress: string,
amount: number,
isBuySell: string
) {
const connection = new Connection(RPC_URL, "confirmed");
const tokenDecimals = await getTokenDecimals(connection, tokenAddress);
const slippageBps = parseInt(process.env.SLIPPAGEBPS || "") || 50;
let swapInfo: any;
if (isBuySell == "buy") {
swapInfo = await getSwapInfo(
NATIVE_MINT.toBase58(),
tokenAddress,
amount * LAMPORTS_PER_SOL,
slippageBps
);
} else {
// sell
swapInfo = await getSwapInfo(
tokenAddress,
NATIVE_MINT.toBase58(),
amount * Math.pow(10, tokenDecimals),
slippageBps
);
}
const swapTransaction = await getSwapTransaction(swapInfo, anchorWallet);
const swapTransactionBuf = Buffer.from(swapTransaction, "base64");
const latestBlockHash = await connection.getLatestBlockhash();
const versionedTransaction =
VersionedTransaction.deserialize(swapTransactionBuf);
versionedTransaction.message.recentBlockhash = latestBlockHash.blockhash;
versionedTransaction.sign([anchorWallet.payer]);
const bundleResult = await sendBundle(
connection,
wallet,
versionedTransaction
);
// console.log(
// "Jito Bundle: ",
// `https://explorer.jito.wtf/bundle/${bundleResult}`
// );
console.log(
"Transaction Signature: ",
`https://solscan.io/tx/${bundleResult}`
);
// ############# Without Bundle #############
// try {
// const transactionSignature = await connection.sendTransaction(
// versionedTransaction,
// { maxRetries: 20 }
// );
// const confirmation = await connection.confirmTransaction(
// {
// signature: transactionSignature,
// blockhash: latestBlockHash.blockhash,
// lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
// },
// "confirmed"
// );
// if (confirmation.value.err) {
// throw new Error("Transaction not confirmed");
// }
// console.log(
// "Transaction Signature: ",
// `https://solscan.io/tx/${transactionSignature}`
// );
// } catch (error) {
// console.error("Error occurred during swap:", error);
// }
}
async function getTokenDecimals(connection: Connection, tokenAddress: string) {
const mintInfo = await connection.getParsedAccountInfo(
new PublicKey(tokenAddress)
);
if (!mintInfo) {
throw new Error("Token account not found");
}
const decimals = (mintInfo.value?.data as any).parsed.info.decimals;
return decimals;
}
const TOKEN_ADDRESS = "4aR3jtFKWuYzkNE27WG4V7Jt6DDhwKcc2qjzN5Tkpump";
// const TOKEN_ADDRESS = "FXnZdiH6zrW33tZ9CLsm81xrSeS7m53V7ZtgFsyhpump";
let amount: number;
amount = 0.0001;
SplTokenSwap(TOKEN_ADDRESS, amount, "buy"); // Buy token
amount = 1000;
SplTokenSwap(TOKEN_ADDRESS, amount, "sell"); // Sell token