Skip to content

Commit 697e3ed

Browse files
committed
Replace with a trimmed mainnet.json
Plus a couple of other fixes
1 parent 132b08b commit 697e3ed

File tree

4 files changed

+103
-6
lines changed

4 files changed

+103
-6
lines changed

src/RaydiumSwap.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
} from '@raydium-io/raydium-sdk'
1414
import { Wallet } from '@coral-xyz/anchor'
1515
import bs58 from 'bs58'
16+
import fs from 'fs';
17+
import path from 'path';
1618

1719
/**
1820
* Class representing a Raydium Swap operation.
@@ -39,9 +41,14 @@ class RaydiumSwap {
3941
* @returns {Promise<void>}
4042
*/
4143
async loadPoolKeys(liquidityFile: string) {
42-
const liquidityJsonResp = await fetch(liquidityFile);
43-
if (!liquidityJsonResp.ok) return
44-
const liquidityJson = (await liquidityJsonResp.json()) as { official: any; unOfficial: any }
44+
let liquidityJson;
45+
if (liquidityFile.startsWith('http')) {
46+
const liquidityJsonResp = await fetch(liquidityFile);
47+
if (!liquidityJsonResp.ok) return;
48+
liquidityJson = await liquidityJsonResp.json();
49+
} else {
50+
liquidityJson = JSON.parse(fs.readFileSync(path.join(__dirname, liquidityFile), 'utf-8'));
51+
}
4552
const allPoolKeysJson = [...(liquidityJson?.official ?? []), ...(liquidityJson?.unOfficial ?? [])]
4653

4754
this.allPoolKeysJson = allPoolKeysJson
@@ -271,4 +278,4 @@ class RaydiumSwap {
271278
}
272279
}
273280

274-
export default RaydiumSwap
281+
export default RaydiumSwap

src/swapConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
export const swapConfig = {
22
executeSwap: false, // Send tx when true, simulate tx when false
33
useVersionedTransaction: true,
4-
tokenAAmount: 0.01, // Swap 0.01 SOL for USDT in this example
4+
tokenAAmount: 0.001, // Swap 0.01 SOL for USDC in this example
55
tokenAAddress: "So11111111111111111111111111111111111111112", // Token to swap for the other, SOL in this case
66
tokenBAddress: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC address
77
maxLamports: 1500000, // Micro lamports for priority fee
88
direction: "in" as "in" | "out", // Swap direction: 'in' or 'out'
9-
liquidityFile: "https://api.raydium.io/v2/sdk/liquidity/mainnet.json",
9+
liquidityFile: "trimmed_mainnet.json",
1010
maxRetries: 20,
1111
};

src/trimMainnet.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import fs from 'fs';
2+
import { swapConfig } from './swapConfig';
3+
4+
interface PoolInfo {
5+
id: string;
6+
baseMint: string;
7+
quoteMint: string;
8+
lpMint: string;
9+
version: number;
10+
programId: string;
11+
authority: string;
12+
openOrders: string;
13+
targetOrders: string;
14+
baseVault: string;
15+
quoteVault: string;
16+
withdrawQueue: string;
17+
lpVault: string;
18+
marketVersion: number;
19+
marketProgramId: string;
20+
marketId: string;
21+
marketAuthority: string;
22+
marketBaseVault: string;
23+
marketQuoteVault: string;
24+
marketBids: string;
25+
marketAsks: string;
26+
marketEventQueue: string;
27+
}
28+
29+
function trimMainnetJson() {
30+
// Read the local mainnet.json file
31+
const mainnetData = JSON.parse(fs.readFileSync('../mainnet.json', 'utf-8'));
32+
33+
// Get the token addresses from swapConfig
34+
const { tokenAAddress, tokenBAddress } = swapConfig;
35+
36+
// Find the pool that matches the token pair
37+
const relevantPool = mainnetData.official.find((pool: PoolInfo) =>
38+
(pool.baseMint === tokenAAddress && pool.quoteMint === tokenBAddress) ||
39+
(pool.baseMint === tokenBAddress && pool.quoteMint === tokenAAddress)
40+
);
41+
42+
if (!relevantPool) {
43+
console.error('No matching pool found for the given token pair');
44+
return;
45+
}
46+
47+
// Create a new object with only the necessary information
48+
const trimmedData = {
49+
official: [relevantPool]
50+
};
51+
52+
// Write the trimmed data to a new file
53+
fs.writeFileSync('trimmed_mainnet.json', JSON.stringify(trimmedData, null, 2));
54+
55+
console.log('Trimmed mainnet.json file has been created as trimmed_mainnet.json');
56+
}
57+
58+
trimMainnetJson();

src/trimmed_mainnet.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"official": [
3+
{
4+
"id": "58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2",
5+
"baseMint": "So11111111111111111111111111111111111111112",
6+
"quoteMint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
7+
"lpMint": "8HoQnePLqPj4M7PUDzfw8e3Ymdwgc7NLGnaTUapubyvu",
8+
"baseDecimals": 9,
9+
"quoteDecimals": 6,
10+
"lpDecimals": 9,
11+
"version": 4,
12+
"programId": "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8",
13+
"authority": "5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1",
14+
"openOrders": "HmiHHzq4Fym9e1D4qzLS6LDDM3tNsCTBPDWHTLZ763jY",
15+
"targetOrders": "CZza3Ej4Mc58MnxWA385itCC9jCo3L1D7zc3LKy1bZMR",
16+
"baseVault": "DQyrAcCrDXQ7NeoqGgDCZwBvWDcYmFCjSb9JtteuvPpz",
17+
"quoteVault": "HLmqeL62xR1QoZ1HKKbXRrdN1p3phKpxRMb2VVopvBBz",
18+
"withdrawQueue": "11111111111111111111111111111111",
19+
"lpVault": "11111111111111111111111111111111",
20+
"marketVersion": 4,
21+
"marketProgramId": "srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX",
22+
"marketId": "8BnEgHoWFysVcuFFX7QztDmzuH8r5ZFvyP3sYwn1XTh6",
23+
"marketAuthority": "CTz5UMLQm2SRWHzQnU62Pi4yJqbNGjgRBHqqp6oDHfF7",
24+
"marketBaseVault": "CKxTHwM9fPMRRvZmFnFoqKNd9pQR21c5Aq9bh5h9oghX",
25+
"marketQuoteVault": "6A5NHCj1yF6urc9wZNe6Bcjj4LVszQNj5DwAWG97yzMu",
26+
"marketBids": "5jWUncPNBMZJ3sTHKmMLszypVkoRK6bfEQMQUHweeQnh",
27+
"marketAsks": "EaXdHx7x3mdGA38j5RSmKYSXMzAFzzUXCLNBEDXDn1d5",
28+
"marketEventQueue": "8CvwxZ9Db6XbLD46NZwwmVDZZRDy7eydFcAGkXKh9axa",
29+
"lookupTableAccount": "3q8sZGGpPESLxurJjNmr7s7wcKS5RPCCHMagbuHP9U2W"
30+
}
31+
]
32+
}

0 commit comments

Comments
 (0)