Limit

To start working with orders, we need to parse them. The most convenient way to do this is by using the OrderFactory contract address, gathering outgoing transactions containing StateInit. There are a few ways to do this, like using third-party APIs. I believe the optimal solution would be to gather and categorize all available orders within your database.

Once we've collected all the order addresses, we'll need to check them out:

import { KeyPair, mnemonicToPrivateKey } from "@ton/crypto";
import { Address, OpenedContract, toNano, TonClient4, WalletContractV4 } from "@ton/ton";
import { JettonMaster, Order, OrderFactory } from "@mooncx/sdk";

const example = async () => {
    // Creating Wallet V4
    const tonClient = new TonClient4({ endpoint: "https://mainnet-v4.tonhubapi.com" });
    const mnemonic = ("Enter mnemonic here.").split(" ");
    const keys = await mnemonicToPrivateKey(mnemonic);

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

    // Order example
    const orderAddress = Address.parse("EQBB_SCrKPVsr3l7pru9nEDC7UzPbAKHcMupf-eGsdgqTUGS");
    const order = tonClient.open(Order.createFromAddress(orderAddress));

    // Getting info about order
    const orderInfo = await order.getOrderInfo();
}

orderInfo:

{
  index: 16, // Internal identifier
  status: 1, // Order active
  kind: 2, // JETTON to TON
  rate: 190400000000000000000000000000000000000n, 
  fee: 500, // 0.2%
  creator: UQDYzZmfsrGzhObKJUw4gzdeIxEai3jAFbiGKGwxvxHinf4K,    
  maker_amount: 115854388n, // 115.854388 (decimal = 6)
  maker_jetton: EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs // USDT
}

While the decimals value for Jettons is commonly set to 9, to ensure accurate calculations, it's recommended to retrieve the decimals value using the following method:

import { unpackJettonContentCell } from "@mooncx/sdk";

const jettonMasterAddress = Address.parse("EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs");
const jettonMaster = tonClient.open(JettonMaster.createFromAddress(jettonMasterAddress));

const jettonData = await jettonMaster.getJettonData();
const unpackedContent = await unpackJettonContentCell(jettonData.content);

With the information available, we can determine whether to proceed with the order. Below are several formulas for your convenience in calculations:

order_rate = taker_amount * 10^36 / maker_amount

maker_amount = taker_amount * 10^36 / order_rate

taker_amount = maker_amount * order_rate / 10^36 

max_taker_amount = order_balance * order_rate / 10^36

As a safety precaution, we advise manually confirming the token balance of the order contract before initiating a maker transaction.

Taker trade examples:

Order kind == 1 or 3 (Maker sending JETTON)

// jetton to send
const jettonMasterAddress = Address.parse("EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs");
const jettonWallet = tonClient.open(await jettonMaster.getWallet(wallet.address));

// prepare message body for transfer
const preparedBody = Order.createTakerJettonBody(
    60000000n, orderAddress, wallet.address, toNano('0.118'),
    {queryId: 777, recipientAddress: recipientAddr}
);

// sending jetton to order
await jettonWallet.sendTransferWithPreparedBody(
    wallet.sender(keys.secretKey), toNano("0.153"), preparedBody);

Order kind == 2 (Taker sending TON)

// sending ton to order
await order.sendTakerTransfer(
 wallet.sender(keys.secretKey), toNano('10.5'),
 {recipientAddress: recipientAddr});

If the recipientAddress is not specified, the funds will be sent to the Taker's address.

Last updated