UNPKG

@prism-hq/prism-ag

Version:

Prism Aggregator

68 lines (67 loc) 3.89 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.raydiumClmmSwap = void 0; const web3_js_1 = require("@solana/web3.js"); const bn_js_1 = __importDefault(require("bn.js")); const types_1 = require("../types/types"); const prism_raydium_clmm_sdk_1 = require("prism-raydium-clmm-sdk"); const jsbi_1 = __importDefault(require("jsbi")); var SwapMode; (function (SwapMode) { SwapMode["ExactIn"] = "ExactIn"; SwapMode["ExactOut"] = "ExactOut"; })(SwapMode || (SwapMode = {})); function raydiumClmmSwap(user, program, route, fromTokenAccount, toTokenAccount, fees, hostFees, useT = null, disableFees = false) { return __awaiter(this, void 0, void 0, function* () { let { fromCoin, toCoin, pool } = route.routeData; let amountIn = new bn_js_1.default(Math.floor(route.amountIn * 10 ** fromCoin.decimals)); let amountOut = new bn_js_1.default(Math.floor(route.minimumReceived * 10 ** toCoin.decimals)); let isBaseInput = fromCoin.mintAddress === pool.poolState.tokenMint0.toBase58(); let remAccounts = pool.getSwapLegAndAccounts({ sourceMint: new web3_js_1.PublicKey(fromCoin.mintAddress), destinationMint: new web3_js_1.PublicKey(toCoin.mintAddress), userSourceTokenAccount: fromTokenAccount, userDestinationTokenAccount: toTokenAccount, userTransferAuthority: user, amount: jsbi_1.default.BigInt(amountIn.toString()), swapMode: SwapMode.ExactIn, }); // take all accounts except the first one let remainingAccounts = remAccounts[1].slice(1); //@ts-ignore let tickArray = prism_raydium_clmm_sdk_1.PoolUtils.getFirstInitializedTickArray(pool.ammV3PoolInfo, isBaseInput).nextAccountMeta; return program.instruction.raydiumClmmSwap(amountIn, amountOut, new bn_js_1.default(0), new bn_js_1.default(0), true, useT ? true : false, new bn_js_1.default(hostFees), { accounts: { raydiumClmmProgram: new web3_js_1.PublicKey(types_1.RAYDIUM_CLMM_PROGRAM_ID), payer: user, ammConfig: pool.poolState.ammConfig, poolState: new web3_js_1.PublicKey(pool.id), inputTokenAccount: fromTokenAccount, outputTokenAccount: toTokenAccount, inputVault: isBaseInput ? pool.poolState.tokenVault0 : pool.poolState.tokenVault1, outputVault: isBaseInput ? pool.poolState.tokenVault1 : pool.poolState.tokenVault0, observationState: pool.poolState.observationKey, tickArray: tickArray, systemProgram: web3_js_1.SystemProgram.programId, tokenProgram: new web3_js_1.PublicKey(types_1.TOKEN_PROGRAM_ID), host: fees.host, feeSweeper: fees.owner, transitiveState: new web3_js_1.PublicKey(types_1.TRANSITIVE_STATE), }, remainingAccounts: remainingAccounts, }); }); } exports.raydiumClmmSwap = raydiumClmmSwap;