UNPKG

@prism-hq/prism-ag

Version:

Prism Aggregator

373 lines (372 loc) 22.6 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.Prism = void 0; const web3_js_1 = require("@solana/web3.js"); const types_1 = require("./types/types"); const utils_1 = require("./utils/utils"); const axios_1 = __importDefault(require("axios")); const liquidityProviders_1 = require("./liquidity/providers/liquidityProviders"); const liquidityInfos_1 = require("./liquidity/infos/liquidityInfos"); const aggregator_1 = require("./aggregator/aggregator"); const swap_1 = require("./swap/swap"); const anchor_1 = require("@project-serum/anchor"); class Prism { constructor(connection, user, publicKey, settings, userAccounts, tokenList, liquidityProviders) { this.connection = connection; this.user = user; this.publicKey = publicKey; this.settings = settings; this.userAccounts = userAccounts; this.tokenList = tokenList; this.liquidityProviders = liquidityProviders; this.userOpenOrdersSerum = null; this.userOpenOrdersOpenBook = null; this.lastFromCoin = null; this.lastToCoin = null; // fetchUserOpenOrders( // this.tokenList, // this.connection, // publicKey, // new PublicKey(SERUM_PROGRAM_ID_V3) // ).then(result => { // this.userOpenOrdersSerum = result; // }); (0, utils_1.fetchUserOpenOrders)(this.tokenList, this.connection, publicKey, new web3_js_1.PublicKey(types_1.OPENBOOK_PROGRAM_ID)).then(result => { this.userOpenOrdersOpenBook = result; }); } static init(params) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; return __awaiter(this, void 0, void 0, function* () { let settings = { owner: { publicKey: new web3_js_1.PublicKey(types_1.PRISM_OWNER), fee: 0 }, host: params.host ? { publicKey: new web3_js_1.PublicKey(params.host.publicKey), fee: params.host.fee, } : { publicKey: new web3_js_1.PublicKey(types_1.PRISM_OWNER), fee: 0, }, slippage: params.slippage ? params.slippage : 0.5, priorityFee: params.priorityFee ? params.priorityFee : 10000, liqProviders: { // serum: params.liqProviders?.serum != undefined ? params.liqProviders?.serum : true, saber: ((_a = params.liqProviders) === null || _a === void 0 ? void 0 : _a.saber) != undefined ? (_b = params.liqProviders) === null || _b === void 0 ? void 0 : _b.saber : true, raydium: ((_c = params.liqProviders) === null || _c === void 0 ? void 0 : _c.raydium) != undefined ? (_d = params.liqProviders) === null || _d === void 0 ? void 0 : _d.raydium : true, // aldrin: params.liqProviders?.aldrin != undefined ? params.liqProviders?.aldrin : true, orca: ((_e = params.liqProviders) === null || _e === void 0 ? void 0 : _e.orca) != undefined ? (_f = params.liqProviders) === null || _f === void 0 ? void 0 : _f.orca : true, // lifinity: params.liqProviders?.lifinity != undefined ? params.liqProviders?.lifinity : true, // lifinityV2: params.liqProviders?.lifinityV2 != undefined ? params.liqProviders?.lifinityV2 : true, // symmetry: params.liqProviders?.symmetry != undefined ? params.liqProviders?.symmetry : true, // cropper: params.liqProviders?.cropper != undefined ? params.liqProviders?.cropper : true, // sencha: params.liqProviders?.sencha != undefined ? params.liqProviders?.sencha : true, // saros: params.liqProviders?.saros != undefined ? params.liqProviders?.saros : true, // step: params.liqProviders?.step != undefined ? params.liqProviders?.step : true, // penguin: params.liqProviders?.penguin != undefined ? params.liqProviders?.penguin : true, // mercurial: params.liqProviders?.mercurial != undefined ? params.liqProviders?.mercurial : true, // stepn: params.liqProviders?.stepn != undefined ? params.liqProviders?.stepn : true, // marinade: params.liqProviders?.marinade != undefined ? params.liqProviders?.marinade : true, // cykura: params.liqProviders?.cykura != undefined ? params.liqProviders?.cykura : true, // gooseFX: params.liqProviders?.gooseFX != undefined ? params.liqProviders?.gooseFX : true, // openbook: params.liqProviders?.openbook != undefined ? params.liqProviders?.openbook : true, // balansol: params.liqProviders?.balansol != undefined ? params.liqProviders?.balansol : true, whirlpools: ((_g = params.liqProviders) === null || _g === void 0 ? void 0 : _g.whirlpools) != undefined ? (_h = params.liqProviders) === null || _h === void 0 ? void 0 : _h.whirlpools : true, raydiumClmm: ((_j = params.liqProviders) === null || _j === void 0 ? void 0 : _j.raydiumClmm) != undefined ? (_k = params.liqProviders) === null || _k === void 0 ? void 0 : _k.raydiumClmm : true, // marcopolo: params.liqProviders?.marcopolo != undefined ? params.liqProviders?.marcopolo : true, // phoenix: params.liqProviders?.phoenix != undefined ? params.liqProviders?.phoenix : true, } }; let connection = params.connection ? params.connection : new web3_js_1.Connection(types_1.ENDPOINT); let user = params.user; //@ts-ignore let publicKey = params.user; //@ts-ignore if (user && user.publicKey) { //@ts-ignore publicKey = user.publicKey; //@ts-ignore try { user = new anchor_1.Wallet(user); } catch (_l) { } } let userAccountsPromise = params.tokenList ? (0, utils_1.fetchUserAccountsAndTokenList)(params.tokenList, connection, publicKey) : axios_1.default.get(types_1.TOKEN_LIST_URI).then(result => (0, utils_1.fetchUserAccountsAndTokenList)(result.data, connection, publicKey)); let liquidityProvidersPromise = yield (0, liquidityProviders_1.loadLiquidityProviders)(connection, settings.liqProviders); return yield Promise.all([ liquidityProvidersPromise, userAccountsPromise, ]).then(results => { return new Prism(connection, user, publicKey, settings, results[1].accounts, results[1].tokenList, results[0]); }); }); } static getCacheData(connection) { return __awaiter(this, void 0, void 0, function* () { return yield (0, liquidityProviders_1.getCacheData)(connection); }); } setSigner(user) { return __awaiter(this, void 0, void 0, function* () { //@ts-ignore try { user = new anchor_1.Wallet(user); } catch (_a) { } let walletChange = true; //@ts-ignore try { if (user.publicKey.equals(this.user)) walletChange = false; } catch (_b) { } //@ts-ignore try { if (user.publicKey.equals(this.user.publicKey)) walletChange = false; } catch (_c) { } if (walletChange) { this.publicKey = user.publicKey; this.userAccounts = yield (0, utils_1.fetchUserAccounts)(this.tokenList, this.connection, this.publicKey); this.userOpenOrdersSerum = null; this.userOpenOrdersOpenBook = null; // fetchUserOpenOrders( // this.tokenList, // this.connection, // //@ts-ignore // this.publicKey, // new PublicKey(SERUM_PROGRAM_ID_V3) // ).then(result => { // this.userOpenOrdersSerum = result; // }); (0, utils_1.fetchUserOpenOrders)(this.tokenList, this.connection, //@ts-ignore this.publicKey, new web3_js_1.PublicKey(types_1.OPENBOOK_PROGRAM_ID)).then(result => { this.userOpenOrdersOpenBook = result; }); } this.user = user; }); } setSlippage(slippage) { this.settings = Object.assign(Object.assign({}, this.settings), { slippage: slippage }); } setLiqProviders(liqProviders) { this.settings.liqProviders = { serum: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.serum) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.serum : true, saber: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.saber) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.saber : true, raydium: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.raydium) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.raydium : true, aldrin: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.aldrin) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.aldrin : true, orca: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.orca) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.orca : true, lifinity: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.lifinity) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.lifinity : true, lifinityV2: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.lifinityV2) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.lifinityV2 : true, symmetry: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.symmetry) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.symmetry : true, cropper: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.cropper) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.cropper : true, sencha: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.sencha) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.sencha : true, saros: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.saros) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.saros : true, step: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.step) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.step : true, penguin: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.penguin) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.penguin : true, mercurial: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.mercurial) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.mercurial : true, stepn: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.stepn) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.stepn : true, marinade: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.marinade) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.marinade : true, cykura: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.cykura) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.cykura : true, gooseFX: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.gooseFX) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.gooseFX : true, openbook: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.openbook) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.openbook : true, balansol: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.balansol) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.balansol : true, whirlpools: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.whirlpools) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.whirlpools : true, raydiumClmm: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.raydiumClmm) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.raydiumClmm : true, marcopolo: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.marcopolo) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.marcopolo : true, phoenix: (liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.phoenix) != undefined ? liqProviders === null || liqProviders === void 0 ? void 0 : liqProviders.phoenix : true, }; } getUserAccounts() { return this.userAccounts; } getUserOpenOrdersSerum() { return this.userOpenOrdersSerum || []; } getUserOpenOrdersOpenbook() { return this.userOpenOrdersOpenBook || []; } closeOpenOrders(openOrders) { return __awaiter(this, void 0, void 0, function* () { let txIds = null; yield (0, utils_1.closeOpenOrdersForUser)(this, openOrders) .catch(() => { }) .then(result => txIds = result); yield Promise.all([ (0, utils_1.fetchUserAccounts)(this.tokenList, this.connection, //@ts-ignore this.user.publicKey), // fetchUserOpenOrders( // this.tokenList, // this.connection, // //@ts-ignore // this.user.publicKey, // new PublicKey(SERUM_PROGRAM_ID_V3) // ), (0, utils_1.fetchUserOpenOrders)(this.tokenList, this.connection, //@ts-ignore this.user.publicKey, new web3_js_1.PublicKey(types_1.OPENBOOK_PROGRAM_ID)) ]).then(results => { this.userAccounts = results[0]; // this.userOpenOrdersSerum = results[1]; this.userOpenOrdersOpenBook = results[1]; }); return txIds; }); } unwrapWSolAccounts() { return __awaiter(this, void 0, void 0, function* () { let txIds = null; yield (0, utils_1.unwrapWSolAccounts)(this) .catch(() => { }) .then(result => txIds = result); yield (0, utils_1.fetchUserAccounts)(this.tokenList, this.connection, //@ts-ignore this.user.publicKey).then(result => { this.userAccounts = result; }); return txIds; }); } static loadPrismStats() { return __awaiter(this, void 0, void 0, function* () { return yield (0, utils_1.getGlobalStats)().catch(() => null); }); } loadPrismStats() { return __awaiter(this, void 0, void 0, function* () { return yield (0, utils_1.getGlobalStats)().catch(() => null); }); } static loadUserTradeHistory(publicKey) { return __awaiter(this, void 0, void 0, function* () { return yield (0, utils_1.getUserHistoty)(publicKey).catch(() => null); }); } loadUserTradeHistory() { return __awaiter(this, void 0, void 0, function* () { return yield (0, utils_1.getUserHistoty)( //@ts-ignore this.user.publicKey).catch(() => null); }); } loadRoutes(from, to, direct = false) { return __awaiter(this, void 0, void 0, function* () { this.fromCoin = (0, utils_1.findCoinFrom)(this.tokenList.tokens, from); this.toCoin = (0, utils_1.findCoinFrom)(this.tokenList.tokens, to); let tokenMap = {}; for (let i = 0; i < this.tokenList.tokens.length; i++) { tokenMap[this.tokenList.tokens[i].address] = Object.assign(Object.assign({}, (0, utils_1.coinInfo)(this.tokenList.tokens[i])), this.tokenList.tokens[i]); } if (this.lastFromCoin && this.lastToCoin && this.fromCoin.address == this.lastToCoin.address && this.toCoin.address == this.lastFromCoin.address) { this.lastFromCoin = this.fromCoin; this.lastToCoin = this.toCoin; this.liquidityInfos.routes = yield (0, liquidityInfos_1.loadLiquidityInfos)(this.fromCoin, this.toCoin, this.liquidityProviders, this.connection, tokenMap, direct, true); return; } this.lastFromCoin = this.fromCoin; this.lastToCoin = this.toCoin; this.liquidityInfos = yield (0, liquidityInfos_1.loadLiquidityInfos)(this.fromCoin, this.toCoin, this.liquidityProviders, this.connection, tokenMap, direct, false); }); } getRoutes(amount) { return (0, aggregator_1.findRoutes)(this.fromCoin, this.toCoin, amount, this.liquidityInfos, this.liquidityProviders, this.settings); } loadRoutesStatic(from, to) { return __awaiter(this, void 0, void 0, function* () { let fromCoin = (0, utils_1.findCoinFrom)(this.tokenList.tokens, from); let toCoin = (0, utils_1.findCoinFrom)(this.tokenList.tokens, to); let tokenMap = {}; for (let i = 0; i < this.tokenList.tokens.length; i++) tokenMap[this.tokenList.tokens[i].address] = Object.assign(Object.assign({}, (0, utils_1.coinInfo)(this.tokenList.tokens[i])), this.tokenList.tokens[i]); let info = { fromCoin: fromCoin, toCoin: toCoin, liquidityInfos: yield (0, liquidityInfos_1.loadLiquidityInfos)(fromCoin, toCoin, this.liquidityProviders, this.connection, tokenMap, true, false) }; return info; }); } getRoutesStatic(amount, info) { let { fromCoin, toCoin, liquidityInfos } = info; return (0, aggregator_1.findRoutes)(fromCoin, toCoin, amount, liquidityInfos, this.liquidityProviders, this.settings); } generateSwapTransactions(route, unWrapSol) { return __awaiter(this, void 0, void 0, function* () { if (unWrapSol === undefined) unWrapSol = true; let result = yield (0, swap_1.generateTransactions)(this, route, unWrapSol).catch(() => null); return { preTransaction: result.preTransaction, preSigners: result.preSigners, mainTransaction: result.mainTransaction, postTransaction: result.postTransaction, }; }); } generateSymmetryTransaction(route, fromTokenAccount, toTokenAccount) { return __awaiter(this, void 0, void 0, function* () { return yield (0, swap_1.generateSymmetryTransaction)(this, route, fromTokenAccount, toTokenAccount); }); } swap(route, unWrapSol) { return __awaiter(this, void 0, void 0, function* () { if (unWrapSol == undefined) unWrapSol = true; //@ts-ignore if (!this.user || !this.user.publicKey) throw new Error("Keypair or Wallet not provided. See setSigner function."); let result = yield (0, swap_1.executeSwap)(this, route, unWrapSol).catch((e) => { console.log(e); return undefined; }); (0, utils_1.fetchUserAccounts)(this.tokenList, this.connection, //@ts-ignore this.user.publicKey).then(result => { this.userAccounts = result; }); // fetchUserOpenOrders( // this.tokenList, // this.connection, // //@ts-ignore // this.user.publicKey, // new PublicKey(SERUM_PROGRAM_ID_V3) // ).then(result => { // this.userOpenOrdersSerum = result; // }); (0, utils_1.fetchUserOpenOrders)(this.tokenList, this.connection, //@ts-ignore this.user.publicKey, new web3_js_1.PublicKey(types_1.OPENBOOK_PROGRAM_ID)).then(result => { this.userOpenOrdersOpenBook = result; }); return result; }); } /** * @deprecated Swap function already confirms transaction */ confirmSwap(result) { return __awaiter(this, void 0, void 0, function* () { return result.response; }); } } exports.Prism = Prism;