-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPoolMarketCap.ts
74 lines (54 loc) · 2.39 KB
/
getPoolMarketCap.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 solanaWeb3 from '@solana/web3.js'
import { struct, blob } from '@solana/buffer-layout'
import BN from 'bn.js'
function u64(property) {
return blob(8, property);
}
function publicKey(property) {
return blob(32, property);
}
const RPC = "https://api.mainnet-beta.solana.com";
const connection = new solanaWeb3.Connection(RPC, 'confirmed');
const POOL_ID = "8sLbNZoA1cfnvMJLPfp98ZLAnFSYCFApfJKMbiXNLwxj";
const POOL_LAYOUT = struct([
u64('baseReserve'),
u64('quoteReserve'),
publicKey('baseMint'),
publicKey('quoteMint'),
]);
export async function getPoolMarketCap(poolId): Promise<BN> {
try {
// Get the account info
const poolPublicKey = new solanaWeb3.PublicKey(poolId);
const accountInfo = await connection.getAccountInfo(poolPublicKey);
if (!accountInfo) {
throw new Error("Pool ID is invalid or not found.");
}
// Parse the pool data
const poolData = POOL_LAYOUT.decode(accountInfo.data);
const baseReserve = new BN(poolData.baseReserve, 'le');
const quoteReserve = new BN(poolData.quoteReserve, 'le');
const baseMintAddress = new solanaWeb3.PublicKey(poolData.baseMint);
const quoteMintAddress = new solanaWeb3.PublicKey(poolData.quoteMint);
// console.log(`Base Token Mint: ${baseMintAddress.toBase58()}`);
// console.log(`Quote Token Mint: ${quoteMintAddress.toBase58()}`);
// console.log(`Base Token Reserve: ${baseReserve.toString()}`);
// console.log(`Quote Token Reserve: ${quoteReserve.toString()}`);
const baseTokenPrice = await getTokenPrice("SOL"); // Replace with correct base token symbol
const quoteTokenPrice = await getTokenPrice("USDC"); // Replace with correct quote token symbol
const baseTokenMarketCap = baseReserve.mul(new BN(baseTokenPrice * 1e6)).div(new BN(1e6));
const quoteTokenMarketCap = quoteReserve.mul(new BN(quoteTokenPrice * 1e6)).div(new BN(1e6));
const totalMarketCap = baseTokenMarketCap.add(quoteTokenMarketCap);
console.log(`Total Market Cap: $${totalMarketCap.toString()}`);
return totalMarketCap
} catch (error) {
console.error(`Error fetching market cap: ${error.message}`);
}
}
async function getTokenPrice(tokenSymbol) {
const prices = {
'SOL': 20,
'USDC': 1,
};
return prices[tokenSymbol];
}