UNPKG

@devasher/kuru-sdk

Version:

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

88 lines 3.81 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OrderCanceler = 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 OrderCanceler { /** * @dev Constructs a transaction to cancel multiple orders. * @param signer - The signer instance to interact with the blockchain. * @param orderbookAddress - The address of the order book contract. * @param orderIds - An array of order IDs to be cancelled. * @param txOptions - Transaction options to be used for the transaction. * @returns A promise that resolves to the transaction request object. */ static async constructCancelOrdersTransaction(signer, orderbookAddress, orderIds, txOptions) { const address = await signer.getAddress(); const orderbookInterface = new ethers_1.ethers.Interface(OrderBook_json_1.default.abi); const data = orderbookInterface.encodeFunctionData('batchCancelOrders', [orderIds]); return (0, txConfig_1.default)({ to: orderbookAddress, from: address, data, txOptions, signer, }); } /** * @dev Cancels multiple orders by their IDs. * @param providerOrSigner - The ethers.js provider or signer to interact with the blockchain. * @param orderbookAddress - The address of the order book contract. * @param orderIds - An array of order IDs to be cancelled. * @param txOptions - Transaction options to be used for the transaction. * @returns A promise that resolves when the transaction is confirmed. */ static async cancelOrders(providerOrSigner, orderbookAddress, orderIds, txOptions) { try { // const orderbook = new ethers.Contract(orderbookAddress, orderbookAbi.abi, providerOrSigner); // Get signer from provider if needed let signer; try { signer = (await providerOrSigner.getAddress()) ? providerOrSigner : await providerOrSigner.getSigner(); } catch (_a) { signer = await providerOrSigner.getSigner(); } const tx = await OrderCanceler.constructCancelOrdersTransaction(signer, orderbookAddress, orderIds, txOptions); const transaction = await signer.sendTransaction(tx); const receipt = await transaction.wait(1); if (!receipt) { throw new Error('Transaction failed'); } return receipt; } catch (e) { console.log({ e }); if (!e.error) { throw e; } throw (0, utils_1.extractErrorMessage)(e); } } static async estimateGas(providerOrSigner, orderbookAddress, orderIds) { try { const orderbook = new ethers_1.ethers.Contract(orderbookAddress, OrderBook_json_1.default.abi, providerOrSigner); const gasEstimate = await orderbook.batchCancelOrders.estimateGas(orderIds); return gasEstimate; } catch (e) { console.log({ e }); if (!e.error) { throw e; } throw (0, utils_1.extractErrorMessage)(e); } } } exports.OrderCanceler = OrderCanceler; //# sourceMappingURL=cancel.js.map