UNPKG

@devasher/kuru-sdk

Version:

Ethers v6 SDK to interact with Kuru (forked from @kuru-labs/kuru-sdk)

82 lines 3.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OrderBatcher = void 0; // ============ External Imports ============ const ethers_1 = require("ethers"); // ============ Internal Imports ============ const utils_1 = require("../utils"); // ============ Config Imports ============ const OrderBook_json_1 = __importDefault(require("../../abi/OrderBook.json")); const txConfig_1 = __importDefault(require("../utils/txConfig")); class OrderBatcher { /** * @dev Batch updates the order book by placing multiple buy and sell limit orders and canceling existing orders. * @param providerOrSigner - The ethers.js provider or signer to interact with the blockchain. * @param orderbookAddress - The address of the order book contract. * @param marketParams - The market parameters including price and size precision. * @param batchUpdate - The batch update object containing limit orders and order IDs to cancel. * @returns A promise that resolves when the transaction is confirmed. */ static async batchUpdate(providerOrSigner, orderbookAddress, marketParams, batchUpdate) { const orderbook = new ethers_1.ethers.Contract(orderbookAddress, OrderBook_json_1.default.abi, providerOrSigner); // Initialize arrays for buy and sell prices and sizes const buyPrices = []; const buySizes = []; const sellPrices = []; const sellSizes = []; // Separate the limit orders into buy and sell arrays for (const order of batchUpdate.limitOrders) { const pricePrecision = (0, utils_1.log10BigNumber)(marketParams.pricePrecision); const sizePrecision = (0, utils_1.log10BigNumber)(marketParams.sizePrecision); // Round the numbers to their respective precisions before parsing const priceStr = Number(order.price).toFixed(pricePrecision); const sizeStr = Number(order.size).toFixed(sizePrecision); const priceBn = (0, ethers_1.parseUnits)(priceStr, pricePrecision); const sizeBn = (0, ethers_1.parseUnits)(sizeStr, sizePrecision); if (order.isBuy) { buyPrices.push(priceBn); buySizes.push(sizeBn); } else { sellPrices.push(priceBn); sellSizes.push(sizeBn); } } try { const signer = providerOrSigner; const address = await signer.getAddress(); const data = orderbook.interface.encodeFunctionData('batchUpdate', [ buyPrices, buySizes, sellPrices, sellSizes, batchUpdate.cancelOrders, batchUpdate.postOnly, ]); const tx = await (0, txConfig_1.default)({ to: orderbookAddress, from: address, data, txOptions: batchUpdate.txOptions, signer, }); const transaction = await signer.sendTransaction(tx); const receipt = await transaction.wait(); if (!receipt) { throw new Error('Transaction failed'); } return receipt; } catch (e) { if (!e.error) { throw e; } throw (0, utils_1.extractErrorMessage)(e); } } } exports.OrderBatcher = OrderBatcher; //# sourceMappingURL=batch.js.map