-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap_quote.ts
36 lines (30 loc) · 1.35 KB
/
swap_quote.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
import {
Connection,
PublicKey,
Keypair,
} from "@solana/web3.js";
import BN from "bn.js";
import { Wallet, AnchorProvider, Program } from '@coral-xyz/anchor';
import AmmImpl from '@mercurial-finance/dynamic-amm-sdk';
import { Amm as AmmIdl, IDL as AmmIDL } from './idl';
export const PROGRAM_ID = 'Eo7WjKq67rjJQSZxS6z3YkapzY3eMj6Xy8X5EQVn5UaB';
const mainnetConnection = new Connection('https://api.mainnet-beta.solana.com');
const mockWallet = new Wallet(new Keypair());
const provider = new AnchorProvider(mainnetConnection, mockWallet, {
commitment: 'confirmed',
});
async function swapQuote(poolAddress: PublicKey, swapAmount: BN, swapAtoB: boolean) {
const ammProgram = new Program<AmmIdl>(AmmIDL, PROGRAM_ID, provider);
let poolState = await ammProgram.account.pool.fetch(poolAddress);
const pool = await AmmImpl.create(provider.connection, poolAddress);
let inTokenMint = swapAtoB ? poolState.tokenAMint : poolState.tokenBMint;
let swapQuote = pool.getSwapQuote(inTokenMint, swapAmount, 100);
console.log("🚀 ~ swapQuote:", swapQuote);
console.log("SwapInAmount %s swapOutAmount %s fee %s", swapQuote.swapInAmount.toString(), swapQuote.swapOutAmount.toString(), swapQuote.fee.toString());
}
async function main() {
await swapQuote(new PublicKey(
"Htnih5T64YYvwbkNDmeac2jbiAe1Gec7s5MCiUjTwUPw"
), new BN(10_000_000), false);
}
main()