UNPKG

@bayswap/sdk

Version:

SDK for BaySwap smart contract

142 lines 5.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LimitOrderModule = void 0; const sui_js_1 = require("@mysten/sui.js"); const OrderStatusMap = { 101: 'pending', 102: 'executed', 103: 'cancel', }; class LimitOrderModule { constructor(provider, registry, txBuilderConfig) { this._provider = provider; this._registry = registry; this._txBuilderConfig = txBuilderConfig; } async getSwapOrders(owner, filter) { const allOrderIds = await this.getAllOrderIds(owner); const allOrders = await this.getBatchOrderDetails(allOrderIds); return allOrders.filter((order) => { if (filter && (order.coinX != filter.coinX || order.coinY != filter.coinY || order.curve != filter.curve)) { return false; } return order.owner == owner; }); } async getAllOrderIds(owner) { const res = []; const events = await this._provider.queryEvents({ query: { MoveModule: { package: this._registry.packageID, module: 'limit_order_entry', }, }, limit: 20, order: 'descending', }); try { for (const eventData of events.data) { if (!eventData.type.includes('EventPlacedSwapOrder')) { continue; } if (owner && !eventData.parsedJson?.owner.includes(owner.slice(3))) { continue; } res.push(eventData.parsedJson?.detail_id); } } catch (e) { console.log(e); } return res; } async getBatchOrderDetails(ids) { const res = []; const resp = await this._provider.multiGetObjects({ ids, options: { showContent: true, }, }); try { for (const r of resp) { const obj = (0, sui_js_1.getMoveObject)(r); const fields = obj?.fields; const [coinX, coinY, curve] = this.parseEventRegisterSwapOrderManager(obj?.type ? obj.type : ''); res.push({ id: fields?.id.id, owner: fields?.owner, expired: +fields?.expired, status: OrderStatusMap[+fields?.status], coinX, coinY, curve, minOutX: BigInt(fields?.min_out_x), minOutY: BigInt(fields?.min_out_y), amountX: BigInt(fields?.coin_x_val), amountY: BigInt(fields?.coin_y_val), isReverse: fields?.is_reverse, timestamp: BigInt(fields?.timestamp), }); } } catch (e) { console.log(e); } return res; } buildPlaceSwapOrder(t, p) { const tx = new sui_js_1.TransactionBlock(); let funcName = 'place_swap_order_from_x_to_y'; if (t.coinY == p.inputType) { funcName = 'place_swap_order_from_y_to_x'; } if (p.inputIds.length > 1 && p.inputType != sui_js_1.SUI_TYPE_ARG) { tx.mergeCoins(tx.object(p.inputIds[0]), p.inputIds.slice(1).map((id) => tx.object(id))); } const targetSplit = p.inputType == sui_js_1.SUI_TYPE_ARG ? tx.gas : tx.object(p.inputIds[0]); const [usedCoin] = tx.splitCoins(targetSplit, [ tx.pure(p.swapAmt.toString()), ]); tx.moveCall({ target: `${this._registry.packageID}::limit_order_entry::${funcName}`, typeArguments: [t.coinX, t.coinY, t.curve], arguments: [ tx.object(this._registry.storageID), tx.object(sui_js_1.SUI_CLOCK_OBJECT_ID), usedCoin, tx.pure(p.minOut.toString()), tx.pure(p.expired.toString()), ], }); return tx; } buildCancelSwapOrder(t, p) { const tx = new sui_js_1.TransactionBlock(); tx.moveCall({ target: `${this._registry.packageID}::limit_order_entry::cancel_swap_order`, typeArguments: [t.coinX, t.coinY, t.curve], arguments: [ tx.object(this._registry.storageID), tx.object(sui_js_1.SUI_CLOCK_OBJECT_ID), tx.object(p.orderId), ], }); return tx; } parseEventRegisterSwapOrderManager(eventType) { const s = eventType.replaceAll(' ', '').match(/<(.*?)>/); if (!s) return []; const tokens = s[0].slice(1, -1).split(','); if (tokens.length < 3) return []; return tokens; } } exports.LimitOrderModule = LimitOrderModule; //# sourceMappingURL=limit-order.js.map