> For the complete documentation index, see [llms.txt](https://docs.aftermath.finance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aftermath.finance/for-developers/typescript-sdk/products/limit-orders.md). Agents can also search and read this documentation through its [MCP server](https://docs.aftermath.finance/~gitbook/mcp).

# 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

```typescript
const afSdk = await Aftermath.create({ network: "MAINNET" });

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

### Constants

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

### Order Management

#### Authorization

{% hint style="info" %}
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
  {% endhint %}

```typescript
const messageToSign = userData.createTermsAndConditionsMessage();

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

#### Fetch Orders

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

const pastOrders = await limit.getPastLimitOrders({
  walletAddress: "0x..",
});
```

```typescript
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

```typescript
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

```typescript
const messageToSign = userData.createTermsAndConditionsMessage();

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

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

`cancelLimitOrdersMessageToSign` is deprecated and its action-specific JSON is rejected by the current service. Sign the reusable terms message and send order object IDs as plain request fields.
