@mercurial-finance/dynamic-amm-sdk
Version:
Mercurial Vaults SDK is a typescript library that allows you to interact with Mercurial v2's AMM.
59 lines โข 3.58 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const web3_js_1 = require("@solana/web3.js");
const bn_js_1 = __importDefault(require("bn.js"));
const anchor_1 = require("@coral-xyz/anchor");
const amm_1 = __importDefault(require("../amm"));
const fs_1 = __importDefault(require("fs"));
const os_1 = __importDefault(require("os"));
function loadKeypairFromFile(filename) {
const secret = JSON.parse(fs_1.default.readFileSync(filename.replace('~', os_1.default.homedir)).toString());
const secretKey = Uint8Array.from(secret);
return web3_js_1.Keypair.fromSecretKey(secretKey);
}
const payerKP = loadKeypairFromFile('~/.config/solana/id.json');
const payerWallet = new anchor_1.Wallet(payerKP);
console.log('Wallet Address: %s \n', payerKP.publicKey);
const devnetConnection = new web3_js_1.Connection('https://api.devnet.solana.com');
const provider = new anchor_1.AnchorProvider(devnetConnection, payerWallet, {
commitment: 'confirmed',
});
async function swap(poolAddress, swapAmount, swapAtoB) {
const pool = await amm_1.default.create(provider.connection, poolAddress);
const poolInfo = pool.poolInfo;
const poolTokenAddress = await pool.getPoolTokenMint();
console.log('Pool LP Token Mint Address: %s', poolTokenAddress.toString());
const lpSupply = await pool.getLpSupply();
console.log('Pool LP Supply: %s', lpSupply.toNumber() / Math.pow(10, pool.decimals));
const LockedLpAmount = await 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 web3_js_1.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 = await pool.swap(payerWallet.publicKey, new web3_js_1.PublicKey(swapInToken.address), swapAmount, swapQuote.minSwapOutAmount);
const swapResult = await provider.sendAndConfirm(swapTx);
console.log('Swap Transaction Hash: %s ', swapResult);
}
async function main() {
// 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
await swap(new web3_js_1.PublicKey(poolAddress), new bn_js_1.default(10_000_000), false);
}
main();
//# sourceMappingURL=swap.js.map