-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathindex.ts
72 lines (63 loc) · 2.25 KB
/
index.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
import RaydiumSwap from './RaydiumSwap';
import { Transaction, VersionedTransaction } from '@solana/web3.js';
import 'dotenv/config';
import { swapConfig } from './swapConfig'; // Import the configuration
/**
* Performs a token swap on the Raydium protocol.
* Depending on the configuration, it can execute the swap or simulate it.
*/
const swap = async () => {
/**
* The RaydiumSwap instance for handling swaps.
*/
const raydiumSwap = new RaydiumSwap(process.env.RPC_URL, process.env.WALLET_PRIVATE_KEY);
console.log(`Raydium swap initialized`);
console.log(`Swapping ${swapConfig.tokenAAmount} of ${swapConfig.tokenAAddress} for ${swapConfig.tokenBAddress}...`)
/**
* Load pool keys from the Raydium API to enable finding pool information.
*/
await raydiumSwap.loadPoolKeys(swapConfig.liquidityFile);
console.log(`Loaded pool keys`);
/**
* Find pool information for the given token pair.
*/
const poolInfo = raydiumSwap.findPoolInfoForTokens(swapConfig.tokenAAddress, swapConfig.tokenBAddress);
if (!poolInfo) {
console.error('Pool info not found');
return 'Pool info not found';
} else {
console.log('Found pool info');
}
/**
* Prepare the swap transaction with the given parameters.
*/
const tx = await raydiumSwap.getSwapTransaction(
swapConfig.tokenBAddress,
swapConfig.tokenAAmount,
poolInfo,
swapConfig.maxLamports,
swapConfig.useVersionedTransaction,
swapConfig.direction
);
/**
* Depending on the configuration, execute or simulate the swap.
*/
if (swapConfig.executeSwap) {
/**
* Send the transaction to the network and log the transaction ID.
*/
const txid = swapConfig.useVersionedTransaction
? await raydiumSwap.sendVersionedTransaction(tx as VersionedTransaction, swapConfig.maxRetries)
: await raydiumSwap.sendLegacyTransaction(tx as Transaction, swapConfig.maxRetries);
console.log(`https://solscan.io/tx/${txid}`);
} else {
/**
* Simulate the transaction and log the result.
*/
const simRes = swapConfig.useVersionedTransaction
? await raydiumSwap.simulateVersionedTransaction(tx as VersionedTransaction)
: await raydiumSwap.simulateLegacyTransaction(tx as Transaction);
console.log(simRes);
}
};
swap();