Limit Orders

Limit Orders allow you to set precise buy or sell conditions, enabling automated trades at your desired price levels. Secure better market entry or exit points and maintain control over your strategy,

Limit Orders

const afSdk = new Aftermath("MAINNET");
await afSdk.init(); // initialize provider

const dca = afSdk.Dca();
const limitOrders = afSdk.LimitOrders();
const userData = afSdk.UserData();

Constants

LimitOrders.constants = {
	gasAmount: BigInt(50_000_000),
};

Order Management

Authorization

To use the Aftermath Limit Orders service, you'll need to create a user account by signing a personal message in two cases:

  1. When creating your first order

  2. When fetching your active orders

This signature serves two important purposes:

  • It provides essential data that helps ensure the service works correctly

  • It verifies authorization identity

const messageToSign = userData.createUserAccountMessageToSign();

const signedMessage = await signPersonalMessage({
	message: new TextEncoder().encode(JSON.stringify(messageToSign)),
});

Fetch Orders

const activeOrders = await limit.getActiveLimitOrders({
  walletAddress: "0x..",
  bytes: signedMessage.bytes,
  signature: signedMessage.signature
});

const pastOrders = await limit.getPastLimitOrders({
  walletAddress: "0x..",
});
export interface LimitOrderObject {
	objectId: string;
	allocatedCoin: {
		coin: string;
		amount: string;
	};
	buyCoin: {
		coin: string;
		amount: string;
	};
	currentAmountSold: string;
	currentAmountBought: string;
	recipient: string;
	created: {
		timestamp: number;
		txnDigest: string;
	};
	finished?: {
		timestamp: number;
		txnDigest: string;
	};
	expiryTimestamp: number;
	status: 
	| "Active"
	| "Canceled"
	| "Failed"
	| "Filled"
	| "Expired"
	| "StopLossTriggered";
	error?: string;
	integratorFee?: {
        feeBps: number;
        feeRecipient: SuiAddress;
    };
	outputToInputStopLossExchangeRate?: number;
}

Create Order

const tx = await limitOrders.getCreateLimitOrderTx({
    walletAddress: "0x..",
    allocateCoinType:
        "0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN",
    allocateCoinAmount: BigInt(50000),
    buyCoinType:
        "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI",
    expiryDurationMs: 0,
    outputToInputExchangeRate: 0.45,
    outputToInputStopLossExchangeRate: 0,
    integratorFee: {
        feeBps: 6000, // 60%
        feeRecipient: "0x...",
    },
});

Cancel Order

const messageToSign = limit.cancelLimitOrderMessageToSign({
  orderIds: ["0x.."]
});

const signedMessage = await signPersonalMessage({
  message: new TextEncoder().encode(JSON.stringify(messageToSign)),
});

const success = await limit.cancelLimitOrder({
	...signedMessage,
	walletAddress: "0x..",
});

Last updated