> For the complete documentation index, see [llms.txt](https://docs.nectarliquid.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.nectarliquid.com/builders/smart-contract-integration.md).

# Smart Contract & SDK Integration

Developers building custom frontends, trading terminals, Telegram bots, or programmatic arbitrage engines can submit orders directly to Nectar Liquid smart contracts on Asset Chain using the TypeScript SDK or direct contract calls.

***

### On-Chain Fee Mechanism: `uiFeeReceiver`

Nectar Liquid smart contracts natively support builder monetization through the **`uiFeeReceiver`** parameter:

* When creating an order (Increase, Decrease, or Swap), your application includes your registered payout wallet address as the `uiFeeReceiver`.
* The order is stored in the `OrderVault` with the encoded receiver address.
* When the automated keeper network executes the order on Asset Chain, the builder fee portion is directed to your receiver address or recorded for payout.

***

### Retrieving Protocol Builder Configuration

Before submitting orders, your application can fetch the protocol's active fee receiver configurations:

```http
GET /v1/builders/config?chainId=42420
Host: api.nectarliquid.com
```

#### Response

```json
{
  "chainId": 42420,
  "uiFeeReceiver": "0x284d323a81d12E01ac195D940Eb7Cb08a139B7dF",
  "defaultFeeRateBps": 0,
  "maxFeeRateBps": 5
}
```

***

### SDK Order Submission Examples

Install the Nectar Liquid SDK and Ethers:

```bash
yarn add @nectarliquid/sdk ethers@^5.7.2
```

#### 1. Perpetual Long Increase Order

The following TypeScript snippet demonstrates opening a $5,000 WETH long position with a custom `uiFeeReceiver`:

```typescript
import { 
  createIncreaseOrderTxn, 
  ExchangeRouter__factory,
  ASSETCHAIN
} from "@nectarliquid/sdk";
import { ethers } from "ethers";

// 1. Configure provider and signer
const provider = new ethers.providers.JsonRpcProvider("https://mainnet-rpc.assetchain.org");
const signer = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);
const traderAddress = await signer.getAddress();

// 2. Builder fee recipient
const BUILDER_FEE_RECEIVER = "0x284d323a81d12E01ac195D940Eb7Cb08a139B7dF";

// 3. Build Increase Order transaction
const orderTxn = await createIncreaseOrderTxn({
  account: traderAddress,
  marketAddress: "0x97D83eE4829D0ca1371144cBdCf3EB44c446460D", // WETH/USD PERP
  initialCollateralTokenAddress: "0x2B7C1342Cc64add10B2a79C8f9767d2667DE64B2", // USDC
  sizeDeltaUsd: ethers.utils.parseUnits("5000", 30), // $5,000 USD (30 decimals precision)
  initialCollateralDeltaAmount: ethers.utils.parseUnits("500", 6), // $500 USDC margin (10x leverage)
  isLong: true,
  acceptablePrice: ethers.utils.parseUnits("3500", 30), // Maximum acceptable entry price
  uiFeeReceiver: BUILDER_FEE_RECEIVER, // Encodes builder fee
});

// 4. Submit transaction to Asset Chain
const tx = await signer.sendTransaction(orderTxn);
console.log(`Submitted order. TxHash: ${tx.hash}`);

// 5. Await receipt
const receipt = await tx.wait();
console.log(`Confirmed in block: ${receipt.blockNumber}`);
```

#### 2. Perpetual Decrease / Close Order

To close or reduce a position:

```typescript
import { createDecreaseOrderTxn } from "@nectarliquid/sdk";

const decreaseTxn = await createDecreaseOrderTxn({
  account: traderAddress,
  marketAddress: "0x97D83eE4829D0ca1371144cBdCf3EB44c446460D",
  initialCollateralTokenAddress: "0x2B7C1342Cc64add10B2a79C8f9767d2667DE64B2",
  sizeDeltaUsd: ethers.utils.parseUnits("5000", 30), // Close full $5,000 size
  initialCollateralDeltaAmount: ethers.constants.Zero,
  isLong: true,
  acceptablePrice: ethers.utils.parseUnits("3300", 30), // Minimum acceptable exit price
  uiFeeReceiver: BUILDER_FEE_RECEIVER,
});

const tx = await signer.sendTransaction(decreaseTxn);
await tx.wait();
```

#### 3. Spot Token Swap Order

To execute a zero-price-impact spot swap:

```typescript
import { createSwapOrderTxn } from "@nectarliquid/sdk";

const swapTxn = await createSwapOrderTxn({
  account: traderAddress,
  initialCollateralTokenAddress: "0x2B7C1342Cc64add10B2a79C8f9767d2667DE64B2", // Swap in: USDC
  swapPath: ["0x3f454e0c4F88CaEEE58909A3e646b48f4B776dA0"], // WRWA/USDC pool
  initialCollateralDeltaAmount: ethers.utils.parseUnits("1000", 6), // 1,000 USDC
  minOutputAmount: ethers.utils.parseUnits("995", 18), // 0.5% max slippage floor
  uiFeeReceiver: BUILDER_FEE_RECEIVER,
});

const tx = await signer.sendTransaction(swapTxn);
await tx.wait();
```

***

### Order Execution Lifecycle

```
1. Client broadcasts orderTxn
   |
   v
2. Collateral transferred to OrderVault on Asset Chain
   |
   v
3. Automated Keeper watches for OrderCreated event
   |
   v
4. Keeper verifies oracle mark price against acceptablePrice
   |
   +---> If Price Valid: Keeper executes order
   |     - Position updated in DataStore
   |     - uiFeeReceiver credited
   |     - Unused gas refund returned to trader
   |
   +---> If Price Invalid / Expired: Keeper cancels order
         - Collateral returned to trader in full
```

For full contract addresses across Mainnet and Testnet, see the [Contract Directory](/contracts.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.nectarliquid.com/builders/smart-contract-integration.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
