# OTC - Create OTC Order

The following code example is provided to help you understand the business process. For commercial use, you must handle parameter validation and exception handling on your own.

This example is tested on the **Polygon-Amoy** chain. Information used in the example can be retrieved from the **Source** directory. All assets involved have been pre-approved (authorized) to the contract `amoy_contract_OTCTrade`. Any amounts involved must be converted using `toWei(amount, "decimal")` for proper precision.

* **Offering Assets (OUT)**: Supports **ERC20**, **ERC721** (network native tokens are not supported).
* **Requiring Assets (GET)**: Supports **ERC20**, **ERC721**, and **network native tokens**.

***

#### **Code Example**

```javascript
javascriptCopy code// Import web3js library
import Web3 from 'web3';
import OTCTradeABI from '../abi/OTCTrade.json';

// This example is tested on the Polygon-Amoy chain
const amoy_chainId = 80002;
const amoy_chainName = "AMOY";
const amoy_chainRpcUrl = "https://polygon-amoy.infura.io/v3/4ba314367838400fb88f2a1d0e14d42d";
const amoy_contract_OTCTrade = "0x0C4A4390A1ae1186644a0F0354c49880595aD328";
const amoy_account = "0xA3932E6Dbf96983Ffdf43974c0BF7edE9fed76DF";

// Initialize web3 instance
// WalletProvider or HttpProvider
const web3 = new Web3("** Wallet **"); // window.ethereum
const Contract = new web3.eth.Contract(OTCTradeABI, amoy_contract_OTCTrade);

// 1. Order Parameters (Refer to the parameter explanation in the directory)
// Assets are only **ERC20** and **ERC721**. Ensure that all tokens are approved for the `amoy_contract_OTCTrade` contract. Check for sufficient balances and approvals. **ERC20** tokens must be converted to the correct precision using `toWei(amount, "decimal")`.

const maker = "0xA3932E6Dbf96983Ffdf43974c0BF7edE9fed76DF";
const taker = "0x0000000000000000000000000000000000000000"; // Open to all buyers

const asset = [
    {collection: "0x9A3fad316eB9cC7db65aB6f89672796574CD1B76", assetClass: "0x0000000000000000000000000000000000000721", amountOrID: "28308257", name: "Bored Ape Yacht Club", symbol: "BAYC", decimal: 0},
    {collection: "0x9A3fad316eB9cC7db65aB6f89672796574CD1B76", assetClass: "0x0000000000000000000000000000000000000721", amountOrID: "28308487", name: "Bored Ape Yacht Club", symbol: "BAYC", decimal: 0},
    {collection: "0x98700d8fF27Af5F16FdA3bE3bD30aa4585234DCa", assetClass: "0x0000000000000000000000000000000000000020", amountOrID: toWei(1, 9), name: "WBTC", symbol: "Wrapped BTC", decimal: 9},
    {collection: "0xc94BC02ecFf5f14b73fe1A3137bb587f5Fa62F5d", assetClass: "0x0000000000000000000000000000000000000020", amountOrID: toWei(1, 6), name: "USDT", symbol: "Tether USD", decimal: 6}
];

const currency = [
    {collection: "0x0000000000000000000000000000000000000001", assetClass: "0x0000000000000000000000000000000000000000", amountOrID: toWei(1, 18), name: "MATIC", symbol: "MATIC", decimal: 18}
];

const deadline = "1728955149"; // End of transaction validity (in seconds)

// 2. Retrieve the nonces from the contract
const nonce = await Contract.methods.nonces(amoy_account).call();

// 3. Sign the order information
// Note: Hardware wallet signatures may differ from web wallet signatures. Handle accordingly.
const signParams = {
    types: {
        EIP712Domain: [
            {name: 'name', type: 'string'},
            {name: 'version', type: 'string'},
            {name: 'chainId', type: 'uint256'},
            {name: 'verifyingContract', type: 'address'}
        ],
        Asset: [
            {name: "collection", type: "address"},
            {name: "assetClass", type: "address"},
            {name: "amountOrID", type: "uint256"}
        ],
        Order: [
            {name: 'maker', type: 'address'},
            {name: 'taker', type: 'address'},
            {name: 'asset', type: 'Asset[]'},
            {name: 'currency', type: 'Asset[]'},
            {name: 'deadline', type: 'uint256'},
            {name: 'nonce', type: 'uint256'}
        ]
    },
    domain: {
        name: "OTCTrade",
        version: "1",
        chainId: amoy_chainId,
        verifyingContract: amoy_contract_OTCTrade
    },
    primaryType: "Order",
    message: {
        maker: maker,
        taker: taker,
        asset: asset,
        currency: currency,
        deadline: deadline,
        nonce: nonce
    }
};

const signResult = await window.ethereum.send('eth_signTypedData_v4', [amoy_account, signParams]);
const sigStr = signResult.result;

// 4. Create an order
const orderParams = {
    chainname: amoy_chainName,
    chainid: amoy_chainId,
    useraddr: amoy_account,
    orderinfo: {
        maker: maker,
        taker: taker,
        asset: asset,
        currency: currency,
        deadline: deadline,
        nonce: nonce,
        sig: sigStr,
        startTime: parseInt(Date.now() / 1000),
        endTime: "0",
        ordertype: "SWAP"
    }
};

const formData = new URLSearchParams();
formData.append("p", JSON.stringify(orderParams));

fetch("https://test-api.paddlefi.com/api/dapp/createorder.do", {
    method: 'POST',
    headers: {
        "content-type": "application/x-www-form-urlencoded"
    },
    body: formData.toString()
}).then(res => res.json())
    .then(datas => {
        console.log(datas);
    }).catch(err => {
        console.log('Error', err);
    });
```

#### **Key Notes**:

1. **Assets**: Ensure the assets being sold are **ERC20** or **ERC721** and are approved for the `amoy_contract_OTCTrade` contract. Verify that all balances and approvals are sufficient. **ERC20** assets must be converted using `toWei(amount, "decimal")`.
2. **Currency**: The currency being required supports **ERC20**, **ERC721**, and network-native tokens. Ensure the correct conversion is applied for **ERC20** tokens.
3. **Signature**: Use the correct signature method based on the wallet (hardware vs. web wallet).
4. **Order Creation**: The order is created by submitting the signed data to the API endpoint, including information such as the asset, currency, and deadline.

This example demonstrates how to create an OTC order on the **Polygon-Amoy** chain. Adjust the parameters as needed for your specific requirements.
