# ORDER

## Prepare

```typescript
if(!process.env.MNEMONIC) {
    throw new Error('MNEMINIC IS REQUIRED!');
}

// init wallet
const mnemonic = process.env.MNEMONIC.split(" ");
const keys = await mnemonicToPrivateKey(mnemonic);

const wallet = tonClient.open(
    WalletContractV4.create({
        workchain: 0,
        publicKey: keys.publicKey
    })
);

// get config
const apiConfig = await getApiConfig();

// get listed assets
const apiAssets = await getApiAssets();

// init order factory
const orderFactory = tonClient.open(OrderFactory.createFromAddress(apiConfig.order_factory));
```

## Create order

```typescript
// select assets
const asset1_id = Address.parse(apiAssets[2].asset_id); // bolt
const asset2_id = Address.parse(apiAssets[1].asset_id); // usdt, in this case

// create order TON -> JETTON
await orderFactory.sendCreateOrderTon(
    wallet.sender(keys.secretKey),
    123, // query_id
    toNano('10'), // amount
    asset2_id, // trade ton for
    calculateRate(toNano('10'), toNano('20')), // rate 1 : 2
    false, // lock order?
    3000, // 3000 seconds vesting time
);

// create order JETTON -> TON/JETTON
await orderFactory.sendCreateOrderJetton(
    wallet.sender(keys.secretKey),
    123,
    toNano('20'),
    asset1_id, // BOLT
    asset2_id, // USDT in this case. Put null if you want BOLT -> TON
    calculateRate(toNano('20'), 5000000n),
    true, // order locked, can't be cancelled
    0 // no vesting
);

// Use calculateRate. 1st amount - you send, 2nd - you wish to receive.
```

## Fill order

```typescript
// fill order with ton
await order.sendOrderFillTon(
    wallet.sender(keys.secretKey),
    123, // query_id
    toNano('10'), // amount
    someAddress, // recipient address, will receive fill result
    someCell, // recipient payload
    someOtherAddress, // reject address, will receive refund on any fails
);

// fill order with jetton
const jettonMaster = tonClient.open(JettonMinter.createFromAddress(asset2_id));
const jettonWallet = await jettonMaster.getWallet(wallet.address);

await order.sendOrderFillJetton(
    wallet.sender(keys.secretKey),
    jettonWallet, // your jetton wallet 
    123, // query_id
    13000000n, // amount
    wallet.address, // recipient address
    null, // recipient payload
    wallet.address // reject address
);
```

## Cancel order

```typescript
// init order
const order = tonClient.open(Order.createFromAddress(orderAddr));

// cancel order
await order.sendCancel(
    wallet.sender(keys.secretKey),
    123, // query_id
);
```

## Vesting trigger

```typescript
// You can manually send vesting trigger

await order.sendExternalVestingTrigger();
```

## Getters

```typescript
const orderData = await order.getOrder(); // full data in one call
const fillOut = await order.getFillOut(100000n); // fill result
const vestingData = await order.getVestingData(); // will fail if order status != 7.
const orderStatus = await order.getStatus();
const orderAmount = await order.getAmount();
const orderAssets = await order.getAssets();
const orderWallets = await order.getWallets();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.moon.cx/developers/sdk/order.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
