aftermath-ts-sdk
Version:
Aftermath TypeScript SDK
231 lines (230 loc) • 9.37 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.Dca = void 0;
const caller_1 = require("../../general/utils/caller");
/**
* The `Dca` class provides functionality for automating Dollar-Cost Averaging
* (DCA) strategies on the Aftermath platform. It allows you to create, query,
* and close DCA orders that execute periodic trades based on user-defined
* parameters.
*
* @example
* ```typescript
* const afSdk = new Aftermath("MAINNET");
* await afSdk.init(); // initialize provider
*
* const dca = afSdk.Dca();
* ```
*/
class Dca extends caller_1.Caller {
// =========================================================================
// Constructor
// =========================================================================
/**
* Creates a new instance of the `Dca` class, responsible for
* managing DCA orders (querying, creating, closing).
*
* @param config - Optional caller configuration, such as network and access token.
*/
constructor(config) {
super(config, "dca");
}
// =========================================================================
// Class Objects
// =========================================================================
/**
* **Deprecated**. Fetches both active and past DCA orders for a given user in one response.
* Use `getActiveDcaOrders` and `getPastDcaOrders` for a more explicit approach.
*
* @param inputs - Object containing the user's `walletAddress`.
* @returns A `DcaOrdersObject` grouping active and past orders.
*
* @deprecated Please use `getActiveDcaOrders` & `getPastDcaOrders` instead.
* @example
* ```typescript
* // Old usage:
* const allOrders = await dca.getAllDcaOrders({ walletAddress: "0x..." });
* console.log(allOrders.active, allOrders.past);
* ```
*/
getAllDcaOrders(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("orders", inputs);
});
}
/**
* Retrieves the currently active DCA orders for a specific user.
*
* @param inputs - An object containing the user's `walletAddress`.
* @returns A promise that resolves to an array of `DcaOrderObject` for the active orders.
*
* @example
* ```typescript
* const activeOrders = await dca.getActiveDcaOrders({ walletAddress: "0x..." });
* console.log(activeOrders); // Array of active DCA orders
* ```
*/
getActiveDcaOrders(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("active", inputs);
});
}
/**
* Retrieves the past (completed or canceled) DCA orders for a specific user.
*
* @param inputs - An object containing the user's `walletAddress`.
* @returns A promise that resolves to an array of `DcaOrderObject` for the past orders.
*
* @example
* ```typescript
* const pastOrders = await dca.getPastDcaOrders({ walletAddress: "0x..." });
* console.log(pastOrders); // Array of past DCA orders
* ```
*/
getPastDcaOrders(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("past", inputs);
});
}
// =========================================================================
// Transactions
// =========================================================================
/**
* Builds a transaction block on the Aftermath API to create a new DCA order.
* The resulting `Transaction` can then be signed and executed by the user.
*
* @param inputs - The parameters describing the DCA order (coin types, amounts, frequency, etc.).
* @returns A `Transaction` object that can be signed and submitted to the Sui network.
*
* @example
* ```typescript
* const createOrderTx = await dca.getCreateDcaOrderTx({
* walletAddress: "0x<user>",
* allocateCoinType: "0x2::sui::SUI",
* allocateCoinAmount: BigInt(1_000_000_000),
* buyCoinType: "0x<coin>",
* frequencyMs: 3600000, // Every hour
* tradesAmount: 5,
* // ...other fields...
* });
* // sign & send the transaction
* ```
*/
getCreateDcaOrderTx(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApiTransaction("transactions/create-order", inputs);
});
}
/**
* Closes (cancels) an existing DCA order by sending a transaction with user signature.
* Typically used after generating a message to sign with `closeDcaOrdersMessageToSign`.
*
* @param inputs - Contains the user's `walletAddress`, plus the `bytes` and `signature` from message signing.
* @returns A boolean indicating success or failure (true if canceled).
*
* @example
* ```typescript
* const success = await dca.closeDcaOrder({
* walletAddress: "0x...",
* bytes: "0x<signed_bytes>",
* signature: "0x<signature>",
* });
* ```
*/
closeDcaOrder(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi(`cancel`, inputs);
});
}
// =========================================================================
// Interactions
// =========================================================================
/**
* Generates a JSON object representing the message to sign for canceling one or more DCA orders.
* The user can sign this message (converted to bytes) locally, then submit the signature to
* `closeDcaOrder`.
*
* @param inputs - An object containing `orderIds`, an array of order object IDs to cancel.
* @returns An object with `action: "CANCEL_DCA_ORDERS"` and the `order_object_ids`.
*
* @example
* ```typescript
* const msg = dca.closeDcaOrdersMessageToSign({ orderIds: ["0x<order1>", "0x<order2>"] });
* console.log(msg);
* // sign this as JSON or string-encode, then pass to closeDcaOrder
* ```
*/
closeDcaOrdersMessageToSign(inputs) {
return {
action: "CANCEL_DCA_ORDERS",
order_object_ids: inputs.orderIds,
};
}
// =========================================================================
// Interactions - Deprecated
// =========================================================================
/**
* **Deprecated**. Generates a message object used in older flows to create
* a DCA user account. Use the `userData` package for user key storage or account creation.
*
* @deprecated Please use method from `userData` package instead.
* @returns An object with `action: "CREATE_DCA_ACCOUNT"`.
*/
createUserAccountMessageToSign() {
return {
action: "CREATE_DCA_ACCOUNT",
};
}
// =========================================================================
// User Public Key
// =========================================================================
/**
* **Deprecated**. Fetches the user's public key from the older DCA system.
* Please use `getUserPublicKey` from the `userData` package instead.
*
* @deprecated Use `userData` package method instead
* @param inputs - Contains the user's `walletAddress`.
* @returns The public key as a string or `undefined`.
*/
getUserPublicKey(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi(`user/get`, inputs);
});
}
/**
* **Deprecated**. Creates the user's public key in the older DCA system.
* Please use `createUserPublicKey` from the `userData` package instead.
*
* @deprecated Use `userData` package method instead
* @param inputs - Body containing the user address, bytes, and signature.
* @returns `true` if the public key was successfully stored, otherwise `false`.
*/
createUserPublicKey(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi(`/user/add`, inputs);
});
}
}
exports.Dca = Dca;
// =========================================================================
// Constants
// =========================================================================
/**
* Contains static values related to DCA on the Aftermath platform, such as
* default gas usage for DCA transactions.
*/
Dca.constants = {
/**
* The default gas budget for DCA-related transactions (50 SUI).
*/
gasAmount: BigInt(50000000),
};