# Onchain Integration

Call the LeverUp Diamond directly from your own account. You control the key, you pay gas, and
nothing sits between your code and the contracts.

If you want the protocol to relay and pay for your transactions instead, see
[Gasless Trading](/gasless/overview). The trade parameters are the same either way — this section is
worth reading first regardless of which path you take.

## Trading calls use one address

| | |
| :--- | :--- |
| Diamond | `0xea1b8E4aB7f14F7dCA68c5B214303B13078FC5ec` |
| Chain ID | `143` (Monad mainnet) |

Point every trading call at that address and supply only the ABI fragment for the function you
need. There is no separate address for limit orders, reads, or one-click trading. Standalone helper
contracts are called out explicitly; for example, minting LVMON uses the separate `LVMONMinter`.

For a single machine-readable ABI covering direct trading writes, current reads, lifecycle events,
and custom errors, see [Trading ABI](/onchain/trading-abi).

## What you can do

| Operation | Function | Oracle data | Page |
| :--- | :--- | :---: | :--- |
| Mint LVMON from MON or WMON | `LVMONMinter.mint` | no | [Minting LVMON](/onchain/minting-lvmon) |
| Open at market | `openMarketTradeV2` | yes | [Market Orders](/onchain/market-orders) |
| Open a limit order | `openLimitOrderV2` | yes | [Limit Orders](/onchain/limit-orders) |
| Cancel a limit order | `cancelLimitOrder`, `batchCancelLimitOrders` | no | [Limit Orders](/onchain/limit-orders#cancelling) |
| Change a limit order's TP/SL | `updateOrderTpAndSl` | no | [Limit Orders](/onchain/limit-orders#updating-tpsl) |
| Close fully | `closeTrade`, `batchCloseTrade` | no | [Closing](/onchain/closing) |
| Close partially | `closeTrade(hash, closeQty, broker)` | no | [Closing](/onchain/closing#partial-close) |
| Add margin | `addMargin` | no | [Managing a Position](/onchain/manage-position#add-margin) |
| Remove margin | `removeMargin` | yes | [Managing a Position](/onchain/manage-position#remove-margin) |
| Change a position's TP/SL | `updateTradeTpAndSl` | no | [Managing a Position](/onchain/manage-position#take-profit-and-stop-loss) |
| Create TP/SL orders | `batchCreateDecreaseOrders` | no | [TP/SL Orders](/onchain/tpsl-orders) |
| Update / cancel TP/SL orders | `batchUpdateDecreaseOrders`, `cancelDecreaseOrder` | no | [TP/SL Orders](/onchain/tpsl-orders) |
| Read positions, orders, markets | `getPositionsV4`, `getLimitOrders`, … | n/a | [Reading Data](/onchain/reading-data) |

"Oracle data: yes" means the call takes an `OracleUpdateData` argument and requires a native-token
fee as `msg.value`. See [Oracle updates](/introduction/concepts#oracle-updates).

## Two-phase execution

Opens and closes are requests, not immediate state changes:

```
your tx                        keeper tx
   │                               │
   ├─ openMarketTradeV2 ──────────►│
   │  collateral locked            │  oracle price delivered
   │  MarketPendingTrade emitted   ├─► position opens
   │                               │  OpenPosition / PositionIncreased emitted
```

A successful receipt on your own transaction confirms the request was accepted. Confirm the outcome
by watching [events](/onchain/events) or polling
[`getPositionsV4`](/onchain/reading-data#getpositionsv4).

Requests can also be **refunded** — if the filled price falls outside your acceptable price, the
market is closed, or a limit fails. A refund emits `PendingTradeRefund` with a reason code; see the
[Error Reference](/reference/errors#refund-reasons).

## Before your first write

1. **Approve the Diamond** for the collateral token you intend to use. Approve the Diamond address
   itself.
2. **Look up the pair** with [`GET /v1/pairs`](/api/pairs#list-pairs) — do not hardcode `pairBase`.
3. **Pair `tokenIn` with the right `lvToken`** —
   see [Collateral and lvToken](/introduction/concepts#collateral-and-lvtoken).
4. **Get the units right** — `qty` at 1e10, prices at 1e18.
   See [Precision & Units](/introduction/precision).
5. **If you route trades for others**, decide what to pass in `broker` before you start — it appears
   on opens, closes and TP/SL orders, and `0` is not a neutral value. See
   [Brokers & Referrals](/introduction/brokers).

## Shared snippets

All examples on the following pages assume the `config.ts` from the
[Quickstart](/introduction/quickstart#2-shared-setup), plus this oracle helper:

```ts
import { API } from './config'

export async function fetchOracleUpdate(
  pairBase: `0x${string}`,
  collateral: `0x${string}`,
) {
  const res = await fetch(`${API}/v1/oracle/price/updates/by-position`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      pairBase,
      collateral,
      blockChain: 'MONAD',
      options: { includeEncodingData: true, includeFee: true, includePrice: true },
    }),
  })
  if (!res.ok) throw new Error(`oracle: HTTP ${res.status}`)

  const data = await res.json()

  return {
    updateData: {
      pythPriceUpdateData: data.pythPriceUpdateData ?? [],
      pythProPriceUpdateData: data.pythProPriceUpdateData ?? [],
    },
    value: BigInt(data.updateFee ?? '0') + BigInt(data.verifition_fee ?? '0'),
  }
}
```

::: tip Native MON collateral
When `tokenIn` is native MON (`0x0`), request oracle data using the **WMON** address as
`collateral`, and set the transaction `value` to `amountIn + oracleValue` instead of just
`oracleValue`.
:::
