aftermath-ts-sdk
Version:
Aftermath TypeScript SDK
191 lines (190 loc) • 7.44 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LimitOrders = void 0;
const caller_1 = require("../../general/utils/caller");
/**
* The `LimitOrders` class manages creation, cancellation, and querying of
* limit orders on the Aftermath platform. Limit orders allow you to buy or
* sell at a specified price, giving more control over your trades compared
* to market execution.
*
* @example
* ```typescript
* const afSdk = new Aftermath("MAINNET");
* await afSdk.init(); // initialize provider
*
* const limitOrders = afSdk.LimitOrders();
* ```
*/
class LimitOrders extends caller_1.Caller {
// =========================================================================
// Constructor
// =========================================================================
/**
* Creates a new `LimitOrders` instance for interacting with limit order functionality
* on Aftermath.
*
* @param config - Optional configuration, including network and access token.
*/
constructor(config) {
super(config, "limit-orders");
}
// =========================================================================
// Class Objects
// =========================================================================
/**
* Fetches the list of **active** limit orders for a given user. The user must
* provide a signature for identification.
*
* @param inputs - Contains the `walletAddress`, as well as `bytes` and `signature` if needed for auth.
* @returns A promise resolving to an array of `LimitOrderObject`, representing the active orders.
*
* @example
* ```typescript
* const activeOrders = await limitOrders.getActiveLimitOrders({
* walletAddress: "0x<address>",
* bytes: "0x<signed_bytes>",
* signature: "0x<signature>"
* });
* ```
*/
getActiveLimitOrders(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("active", inputs);
});
}
/**
* Fetches the list of **past** limit orders for a given user (e.g., completed, canceled, or expired).
*
* @param inputs - An object containing the `walletAddress`.
* @returns A promise resolving to an array of `LimitOrderObject` representing past orders.
*
* @example
* ```typescript
* const pastOrders = await limitOrders.getPastLimitOrders({
* walletAddress: "0x<address>",
* });
* ```
*/
getPastLimitOrders(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("past", inputs);
});
}
// =========================================================================
// Transactions
// =========================================================================
/**
* Constructs a limit order creation transaction on the Aftermath API, returning a `Transaction`
* object that can be signed and submitted to the network.
*
* @param inputs - The limit order details, including coin types, amounts, expiry, etc.
* @returns A promise resolving to a `Transaction` that can be locally signed and executed.
*
* @example
* ```typescript
* const tx = await limitOrders.getCreateLimitOrderTx({
* walletAddress: "0x<address>",
* allocateCoinType: "0x<coin>",
* allocateCoinAmount: BigInt(1000),
* buyCoinType: "0x<other_coin>",
* expiryDurationMs: 3600000, // 1 hour
* outputToInputExchangeRate: 0.5,
* });
* // sign and execute the transaction
* ```
*/
getCreateLimitOrderTx(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApiTransaction("transactions/create-order", inputs);
});
}
/**
* Cancels an existing limit order by sending a request to the Aftermath API
* with the user's signed cancellation message.
*
* @param inputs - Contains the user's `walletAddress`, plus `bytes` and `signature`.
* @returns A boolean indicating whether the cancellation was successful.
*
* @example
* ```typescript
* const success = await limitOrders.cancelLimitOrder({
* walletAddress: "0x<address>",
* bytes: "0x<signed_bytes>",
* signature: "0x<signature>",
* });
* ```
*/
cancelLimitOrder(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi(`cancel`, inputs);
});
}
// =========================================================================
// Interactions
// =========================================================================
/**
* Generates the JSON message needed to cancel one or more limit orders. The user
* signs this message (converted to bytes), and the resulting signature is passed
* to `cancelLimitOrder`.
*
* @param inputs - Object with `orderIds`, an array of order object IDs to cancel.
* @returns A JSON structure with the action and order IDs to be canceled.
*
* @example
* ```typescript
* const msg = limitOrders.cancelLimitOrdersMessageToSign({
* orderIds: ["0x<order1>", "0x<order2>"]
* });
* // user signs this JSON
* ```
*/
cancelLimitOrdersMessageToSign(inputs) {
return {
action: "CANCEL_LIMIT_ORDERS",
order_object_ids: inputs.orderIds,
};
}
// =========================================================================
// Configuration
// =========================================================================
/**
* Retrieves the minimum allowable order size (in USD) for limit orders on Aftermath.
*
* @returns A promise resolving to a `number` (USD value) or `undefined` if not configured.
*
* @example
* ```typescript
* const minSize = await limitOrders.getMinOrderSizeUsd();
* console.log("Minimum order size in USD:", minSize);
* ```
*/
getMinOrderSizeUsd() {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("min-order-size-usd", {});
});
}
}
exports.LimitOrders = LimitOrders;
// =========================================================================
// Constants
// =========================================================================
/**
* Static configuration constants, including a default gas amount for
* limit order transactions (50 SUI).
*/
LimitOrders.constants = {
/**
* The default gas budget for limit orders. This may be subject to change.
*/
gasAmount: BigInt(50000000),
};