@mercurial-finance/dynamic-amm-sdk
Version:
Mercurial Vaults SDK is a typescript library that allows you to interact with Mercurial v2's AMM.
67 lines โข 4.11 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { Connection, PublicKey, Keypair } from '@solana/web3.js';
import BN from 'bn.js';
import { Wallet, AnchorProvider } from '@coral-xyz/anchor';
import AmmImpl from '../amm';
import fs from 'fs';
import os from 'os';
function loadKeypairFromFile(filename) {
const secret = JSON.parse(fs.readFileSync(filename.replace('~', os.homedir)).toString());
const secretKey = Uint8Array.from(secret);
return Keypair.fromSecretKey(secretKey);
}
const payerKP = loadKeypairFromFile('~/.config/solana/id.json');
const payerWallet = new Wallet(payerKP);
console.log('Wallet Address: %s \n', payerKP.publicKey);
const devnetConnection = new Connection('https://api.devnet.solana.com');
const provider = new AnchorProvider(devnetConnection, payerWallet, {
commitment: 'confirmed',
});
function swap(poolAddress, swapAmount, swapAtoB) {
return __awaiter(this, void 0, void 0, function* () {
const pool = yield AmmImpl.create(provider.connection, poolAddress);
const poolInfo = pool.poolInfo;
const poolTokenAddress = yield pool.getPoolTokenMint();
console.log('Pool LP Token Mint Address: %s', poolTokenAddress.toString());
const lpSupply = yield pool.getLpSupply();
console.log('Pool LP Supply: %s', lpSupply.toNumber() / Math.pow(10, pool.decimals));
const LockedLpAmount = yield pool.getLockedLpAmount();
console.log('Locked Lp Amount: %s \n', LockedLpAmount.toNumber());
console.log('tokenA %s Amount: %s ', pool.tokenAMint.address, poolInfo.tokenAAmount.toNumber() / Math.pow(10, pool.tokenAMint.decimals));
console.log('tokenB %s Amount: %s ', pool.tokenBMint.address, poolInfo.tokenBAmount.toNumber() / Math.pow(10, pool.tokenBMint.decimals));
console.log('virtualPrice: %s \n', poolInfo.virtualPrice);
let swapInToken = swapAtoB ? pool.tokenAMint : pool.tokenBMint;
let swapOutToken = swapAtoB ? pool.tokenBMint : pool.tokenAMint;
let inTokenMint = new PublicKey(swapInToken.address);
let swapQuote = pool.getSwapQuote(inTokenMint, swapAmount, 100);
console.log('๐ ~ swapQuote:');
console.log('Swap In %s, Amount %s ', swapInToken.address, swapQuote.swapInAmount.toNumber() / Math.pow(10, swapInToken.decimals));
console.log('Swap Out %s, Amount %s \n', swapOutToken.address, swapQuote.swapOutAmount.toNumber() / Math.pow(10, swapOutToken.decimals));
console.log('Fee of the Swap %s %s', swapQuote.fee.toNumber() / Math.pow(10, swapInToken.decimals), swapInToken.address);
console.log('Price Impact of the Swap %s \n', swapQuote.priceImpact);
console.log('Swapping...โ๏ธ Please wait for a while๐โ๏ธ');
const swapTx = yield pool.swap(payerWallet.publicKey, new PublicKey(swapInToken.address), swapAmount, swapQuote.minSwapOutAmount);
const swapResult = yield provider.sendAndConfirm(swapTx);
console.log('Swap Transaction Hash: %s ', swapResult);
});
}
function main() {
return __awaiter(this, void 0, void 0, function* () {
// devnet, 9NG-SOL
const poolAddress = 'Bgf1Sy5kfeDgib4go4NgzHuZwek8wE8NZus56z6uizzi';
// swap 5 9NG token to SOL
// await swap(new PublicKey(poolAddress), new BN(5000_000_000), true);
// swap 0.01 SOL to 9NG token
yield swap(new PublicKey(poolAddress), new BN(10000000), false);
});
}
main();
//# sourceMappingURL=swap.js.map