UNPKG

@bluefin-exchange/bluefin7k-aggregator-sdk

Version:
158 lines (157 loc) 6.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SteammContract = void 0; const base_1 = require("../base"); const utils_1 = require("@mysten/sui/utils"); const sui_1 = require("../../../utils/sui"); class SteammContract extends base_1.BaseContract { async swap(tx) { if (this.extra.poolStructTag.includes("omm::OracleQuoter")) { return this.ommSwap(tx); } else if (this.extra.poolStructTag.includes("cpmm::CpQuoter")) { return this.cpmmSwap(tx); } throw new Error(`Unsupported pool type: ${this.extra.poolStructTag}`); } cpmmSwap(tx) { const extra = this.extra; if (!extra || !extra.bankAStructTag || !extra.bankBStructTag || !extra.poolStructTag || !extra.bankA || !extra.bankB || !extra.lendingMarketA || !extra.lendingMarketB) { throw new Error(`Invalid extra info for cpmmSwap`); } // the pool script v1 only support same lending market if (extra.lendingMarketA !== extra.lendingMarketB) { throw new Error(`Invalid lending market for cpmmSwap`); } const [btokenA, bTokenB, _quoter, lp] = (0, utils_1.parseStructTag)(extra.poolStructTag).typeParams; const [lendingMarket, coinTypeA, _bTokenA] = (0, utils_1.parseStructTag)(extra.bankAStructTag).typeParams; const [_lendingMarket, coinTypeB, _bTokenB] = (0, utils_1.parseStructTag)(extra.bankBStructTag).typeParams; const xToY = this.swapInfo.swapXtoY; const coinA = xToY ? this.inputCoinObject : sui_1.SuiUtils.zeroCoin(tx, (0, utils_1.normalizeStructTag)(coinTypeA)); const coinB = !xToY ? this.inputCoinObject : sui_1.SuiUtils.zeroCoin(tx, (0, utils_1.normalizeStructTag)(coinTypeB)); tx.moveCall({ target: `${this.config.steamm.script}::pool_script::cpmm_swap`, typeArguments: [ lendingMarket, coinTypeA, coinTypeB, btokenA, bTokenB, lp, ].map(utils_1.normalizeStructTag), arguments: [ tx.object(this.swapInfo.poolId), tx.object(extra.bankA), tx.object(extra.bankB), tx.object(extra.lendingMarketA), coinA, coinB, tx.pure.bool(xToY), this.getInputCoinValue(tx), tx.pure.u64(0), tx.object(utils_1.SUI_CLOCK_OBJECT_ID), ], }); const coinIn = xToY ? coinA : coinB; const coinOut = xToY ? coinB : coinA; sui_1.SuiUtils.collectDust(tx, this.swapInfo.assetIn, coinIn); return coinOut; } ommSwap(tx) { const extra = this.swapInfo.extra; if (!extra || !extra.bankAStructTag || !extra.bankBStructTag || !extra.poolStructTag || !extra.bankA || !extra.bankB || !extra.lendingMarketA || !extra.lendingMarketB) { throw new Error(`Invalid extra info for cpmmSwap`); } // the pool script v1 only support same lending market if (extra.lendingMarketA !== extra.lendingMarketB) { throw new Error(`Invalid lending market for ommSwap`); } const [btokenA, bTokenB, _quoter, lp] = (0, utils_1.parseStructTag)(extra.poolStructTag).typeParams; const [lendingMarket, coinTypeA, _bTokenA] = (0, utils_1.parseStructTag)(extra.bankAStructTag).typeParams; const [_lendingMarket, coinTypeB, _bTokenB] = (0, utils_1.parseStructTag)(extra.bankBStructTag).typeParams; const xToY = this.swapInfo.swapXtoY; const coinA = xToY ? this.inputCoinObject : sui_1.SuiUtils.zeroCoin(tx, (0, utils_1.normalizeStructTag)(coinTypeA)); const coinB = !xToY ? this.inputCoinObject : sui_1.SuiUtils.zeroCoin(tx, (0, utils_1.normalizeStructTag)(coinTypeB)); const [priceA, priceB] = this.getOraclePriceUpdate(tx); tx.moveCall({ target: `${this.config.steamm.script}::pool_script_v2::omm_swap`, typeArguments: [ lendingMarket, coinTypeA, coinTypeB, btokenA, bTokenB, lp, ].map(utils_1.normalizeStructTag), arguments: [ tx.object(this.swapInfo.poolId), tx.object(extra.bankA), tx.object(extra.bankB), tx.object(extra.lendingMarketA), priceA, priceB, coinA, coinB, tx.pure.bool(xToY), this.getInputCoinValue(tx), tx.pure.u64(0), tx.object(utils_1.SUI_CLOCK_OBJECT_ID), ], }); const coinIn = xToY ? coinA : coinB; const coinOut = xToY ? coinB : coinA; sui_1.SuiUtils.collectDust(tx, this.swapInfo.assetIn, coinIn); return coinOut; } getOraclePriceUpdate(tx) { const oracleA = this.extra.oracles?.[0]?.Pyth?.price_identifier?.bytes; const oracleB = this.extra.oracles?.[1]?.Pyth?.price_identifier?.bytes; const registry = this.extra.oracleRegistry; const indexes = this.extra.oracleIndexes; if (!oracleA || !oracleB || !registry || indexes?.length !== 2) { throw new Error(`Invalid oracle info for getOraclePriceUpdate`); } const [a] = tx.moveCall({ target: `${this.config.steamm.oracle}::oracles::get_pyth_price`, arguments: [ tx.object(registry), tx.object(this.pythMap["0x" + (0, utils_1.toHex)(Uint8Array.from(oracleA))]), tx.pure.u64(indexes[0]), tx.object(utils_1.SUI_CLOCK_OBJECT_ID), ], }); const [b] = tx.moveCall({ target: `${this.config.steamm.oracle}::oracles::get_pyth_price`, arguments: [ tx.object(registry), tx.object(this.pythMap["0x" + (0, utils_1.toHex)(Uint8Array.from(oracleB))]), tx.pure.u64(indexes[1]), tx.object(utils_1.SUI_CLOCK_OBJECT_ID), ], }); return [a, b]; } } exports.SteammContract = SteammContract;