@devasher/kuru-sdk
Version:
Ethers v6 SDK to interact with Kuru (forked from @kuru-labs/kuru-sdk)
161 lines • 7.62 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GTC = 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 GTC {
/**
* @dev Places a limit order (buy or sell) on the order book.
* @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 order - The limit order object containing price, size, isBuy, and postOnly properties.
* @param txOptions - The transaction options for the order.
* @returns A promise that resolves to a boolean indicating success or failure.
*/
static async placeLimit(providerOrSigner, orderbookAddress, marketParams, order) {
const orderbook = new ethers_1.ethers.Contract(orderbookAddress, OrderBook_json_1.default.abi, providerOrSigner);
const clippedPrice = (0, utils_1.clipToDecimals)(order.price, (0, utils_1.log10BigNumber)(marketParams.pricePrecision));
const clippedSize = (0, utils_1.clipToDecimals)(order.size, (0, utils_1.log10BigNumber)(marketParams.sizePrecision));
const priceBn = (0, ethers_1.parseUnits)(clippedPrice, (0, utils_1.log10BigNumber)(marketParams.pricePrecision));
const sizeBn = (0, ethers_1.parseUnits)(clippedSize, (0, utils_1.log10BigNumber)(marketParams.sizePrecision));
return order.isBuy
? GTC.addBuyOrder(orderbook, priceBn, sizeBn, order.postOnly, order.txOptions, providerOrSigner)
: GTC.addSellOrder(orderbook, priceBn, sizeBn, order.postOnly, order.txOptions, providerOrSigner);
}
static async estimateGas(providerOrSigner, orderbookAddress, marketParams, order) {
const orderbook = new ethers_1.ethers.Contract(orderbookAddress, OrderBook_json_1.default.abi, providerOrSigner);
const clippedPrice = (0, utils_1.clipToDecimals)(order.price, (0, utils_1.log10BigNumber)(marketParams.pricePrecision));
const clippedSize = (0, utils_1.clipToDecimals)(order.size, (0, utils_1.log10BigNumber)(marketParams.sizePrecision));
const priceBn = (0, ethers_1.parseUnits)(clippedPrice, (0, utils_1.log10BigNumber)(marketParams.pricePrecision));
const sizeBn = (0, ethers_1.parseUnits)(clippedSize, (0, utils_1.log10BigNumber)(marketParams.sizePrecision));
return order.isBuy
? estimateGasBuy(orderbook, priceBn, sizeBn, order.postOnly)
: estimateGasSell(orderbook, priceBn, sizeBn, order.postOnly);
}
/**
* @dev Constructs a transaction for a buy limit order.
* @param signer - The signer instance.
* @param orderbookAddress - The address of the order book contract.
* @param price - The price of the order.
* @param size - The size of the order.
* @param postOnly - Whether the order is post-only.
* @param txOptions - Transaction options.
* @returns A promise that resolves to the transaction request object.
*/
static async constructBuyOrderTransaction(signer, orderbookAddress, price, size, postOnly, txOptions) {
const address = await signer.getAddress();
const orderbookInterface = new ethers_1.ethers.Interface(OrderBook_json_1.default.abi);
const data = orderbookInterface.encodeFunctionData('addBuyOrder', [price, size, postOnly]);
return (0, txConfig_1.default)({
to: orderbookAddress,
from: address,
data,
txOptions,
signer,
});
}
/**
* @dev Constructs a transaction for a sell limit order.
* @param signer - The signer instance.
* @param orderbookAddress - The address of the order book contract.
* @param price - The price of the order.
* @param size - The size of the order.
* @param postOnly - Whether the order is post-only.
* @param txOptions - Transaction options.
* @returns A promise that resolves to the transaction request object.
*/
static async constructSellOrderTransaction(signer, orderbookAddress, price, size, postOnly, txOptions) {
const address = await signer.getAddress();
const orderbookInterface = new ethers_1.ethers.Interface(OrderBook_json_1.default.abi);
const data = orderbookInterface.encodeFunctionData('addSellOrder', [price, size, postOnly]);
return (0, txConfig_1.default)({
to: orderbookAddress,
from: address,
data,
txOptions,
signer,
});
}
/**
* @dev Places a buy limit order on the order book.
*/
static async addBuyOrder(orderbook, price, size, postOnly, txOptions, providerOrSigner) {
try {
// Extract signer from contract or use provider/signer directly
const signer = providerOrSigner;
const tx = await GTC.constructBuyOrderTransaction(signer, await orderbook.getAddress(), price, size, postOnly, txOptions);
const transaction = await signer.sendTransaction(tx);
const receipt = await transaction.wait(1);
if (!receipt) {
throw new Error('Transaction receipt is null');
}
return receipt;
}
catch (e) {
console.log({ e });
if (!e.error) {
throw e;
}
throw (0, utils_1.extractErrorMessage)(e);
}
}
/**
* @dev Places a sell limit order on the order book.
*/
static async addSellOrder(orderbook, price, size, postOnly, txOptions, providerOrSigner) {
try {
// Extract signer from contract or use provider/signer directly
const signer = providerOrSigner;
const tx = await GTC.constructSellOrderTransaction(signer, await orderbook.getAddress(), price, size, postOnly, txOptions);
const transaction = await signer.sendTransaction(tx);
const receipt = await transaction.wait(1);
if (!receipt) {
throw new Error('Transaction receipt is null');
}
return receipt;
}
catch (e) {
console.log({ e });
if (!e.error) {
throw e;
}
throw (0, utils_1.extractErrorMessage)(e);
}
}
}
exports.GTC = GTC;
// ======================== INTERNAL HELPER FUNCTIONS ========================
async function estimateGasBuy(orderbook, price, size, postOnly) {
try {
const gasEstimate = await orderbook.addBuyOrder.estimateGas(price, size, postOnly);
return gasEstimate;
}
catch (e) {
if (!e.error) {
throw e;
}
throw (0, utils_1.extractErrorMessage)(e);
}
}
async function estimateGasSell(orderbook, price, size, postOnly) {
try {
const gasEstimate = await orderbook.addSellOrder.estimateGas(price, size, postOnly);
return gasEstimate;
}
catch (e) {
if (!e.error) {
throw e;
}
throw (0, utils_1.extractErrorMessage)(e);
}
}
//# sourceMappingURL=gtc.js.map