# Quickstart

> Use Intear DEX call by call with near CLI: pay for storage, register assets, deposit NEAR, swap it for USDt, and withdraw the USDt to your wallet.

This page is part of the Intear DEX docs. Intear DEX is the `dex.intear.near` contract. https://docs.intear.tech/intear-dex/llms-full.txt has all of its pages in one file, and https://docs.intear.tech/llms.txt lists every page of the Intear docs.

This page swaps 1 NEAR for USDt on Intear DEX and withdraws the USDt to your wallet, one contract call at a time, starting from an account that has never used Intear DEX. Every call goes to `dex.intear.near`, except the token registration in step 6. Arguments are JSON, amounts are integer strings in the asset's smallest unit (yoctoNEAR for NEAR, so 1 NEAR is `"1000000000000000000000000"`).

:::tip
You don't have to make these calls yourself to swap. The [DEX Aggregator](https://docs.intear.tech/dex-aggregator.md) with `dexes=Plach` returns ready-to-sign transactions for the same swap, including storage and registration.
:::

## Before you start

The examples in these docs use [near CLI](https://github.com/near/near-cli-rs) and [jq](https://jqlang.org/). Install near CLI, and import the account you'll sign with:

```sh
npm install -g near-cli-rs
near account import-account
```

Replace `alice.near` in the examples with your account. The examples sign with `sign-with-keychain`, which uses the key that `import-account` saved.

## 1. Pay for storage

Your balances take storage in the engine contract, and you pay for it with `storage_deposit`. The minimum is 0.005 NEAR, but the storage balance itself and the two registrations of the next step take 0.00518 NEAR for alice.near, and more for longer account IDs. This attaches 0.01 NEAR:

```sh
near contract call-function as-transaction dex.intear.near storage_deposit \
  json-args '{}' \
  prepaid-gas '30.0 Tgas' attached-deposit '0.01 NEAR' \
  sign-as alice.near network-config mainnet sign-with-keychain send
```

The whole file, tested in CI: https://docs.intear.tech/snippets/cli/quickstart.sh

You only do this once. You can top it up later or withdraw what you don't use. See [Storage](https://docs.intear.tech/intear-dex/guides/storage.md).

## 2. Register the assets

An account can only hold assets that are registered for it. Register both sides of the swap, with 1 yoctoNEAR attached:

```sh
near contract call-function as-transaction dex.intear.near register_assets \
  json-args '{"asset_ids": ["near", "nep141:usdt.tether-token.near"]}' \
  prepaid-gas '30.0 Tgas' attached-deposit '1 yoctoNEAR' \
  sign-as alice.near network-config mainnet sign-with-keychain send
```

The whole file, tested in CI: https://docs.intear.tech/snippets/cli/quickstart.sh

Registering an asset that's already registered does nothing, so it's safe to always include it.

## 3. Deposit NEAR

Call `deposit_near` with the NEAR you want in your inner balance attached:

```sh
near contract call-function as-transaction dex.intear.near deposit_near \
  json-args '{}' \
  prepaid-gas '30.0 Tgas' attached-deposit '1 NEAR' \
  sign-as alice.near network-config mainnet sign-with-keychain send
```

The whole file, tested in CI: https://docs.intear.tech/snippets/cli/quickstart.sh

For a NEP-141 token, call `ft_transfer_call` on the token contract instead. See [Inner balances](https://docs.intear.tech/intear-dex/guides/inner-balances.md#deposit).

Check the balance with the `asset_balance_of` view method:

```sh
near contract call-function as-read-only dex.intear.near asset_balance_of \
  json-args '{"of": {"Account": "alice.near"}, "asset_id": "near"}' \
  network-config mainnet now
```

The whole file, tested in CI: https://docs.intear.tech/snippets/cli/quickstart.sh

It returns `"1000000000000000000000000"`, or `null` if the asset isn't registered.

## 4. Simulate the swap

`simulate_swap_simple` is a view method that runs the swap without changing anything, and returns `[amount_in, amount_out]`. Use its `amount_out` to set the least you accept:

```sh
AMOUNT_OUT=$(near contract call-function as-read-only dex.intear.near simulate_swap_simple \
  json-args '{
    "dex_id": "slimedragon.near/xyk",
    "message": "AQAAAEBCDwABAAAA3QAAAAA=",
    "trader": "alice.near",
    "asset_in": "near",
    "asset_out": "nep141:usdt.tether-token.near",
    "amount": {"ExactIn": "1000000000000000000000000"}
  }' \
  network-config mainnet now | jq -r '.[1]')
# Accept up to 1% less than the simulation
MIN_AMOUNT_OUT=$((AMOUNT_OUT * 99 / 100))
```

The whole file, tested in CI: https://docs.intear.tech/snippets/cli/quickstart.sh

`message` is passed to the DEX as is, and each DEX decides what goes there. For [Plach](https://docs.intear.tech/plach.md) (`slimedragon.near/xyk`), it's the [route](https://docs.intear.tech/plach/guides/swaps.md#the-message) in Borsh: `AQAAAEBCDwABAAAA3QAAAAA=` swaps the whole amount through pool 221, NEAR/USDt.

## 5. Swap

Call `swap_simple` with the same arguments, without `trader` (it's you), and with a `constraint`: the minimum amount you accept for an exact input, or the maximum amount you pay for an exact output. Attach 1 yoctoNEAR:

```sh
near contract call-function as-transaction dex.intear.near swap_simple \
  json-args "$(jq -n --arg min_amount_out "$MIN_AMOUNT_OUT" '{
    "dex_id": "slimedragon.near/xyk",
    "message": "AQAAAEBCDwABAAAA3QAAAAA=",
    "asset_in": "near",
    "asset_out": "nep141:usdt.tether-token.near",
    "amount": {"ExactIn": "1000000000000000000000000"},
    "constraint": $min_amount_out
  }')" \
  prepaid-gas '100.0 Tgas' attached-deposit '1 yoctoNEAR' \
  sign-as alice.near network-config mainnet sign-with-keychain send
```

The whole file, tested in CI: https://docs.intear.tech/snippets/cli/quickstart.sh

It returns `[amount_in, amount_out]`. If the output is lower than `constraint`, the call fails and nothing changes. See [Swaps](https://docs.intear.tech/intear-dex/guides/swaps.md).

## 6. Withdraw

Your wallet must be registered on the token contract (have a storage deposit there) to receive USDt. Skip this if you already hold USDt:

```sh
near contract call-function as-transaction usdt.tether-token.near storage_deposit \
  json-args '{"account_id": "alice.near", "registration_only": true}' \
  prepaid-gas '30.0 Tgas' attached-deposit '0.00125 NEAR' \
  sign-as alice.near network-config mainnet sign-with-keychain send
```

The whole file, tested in CI: https://docs.intear.tech/snippets/cli/quickstart.sh

Then send the USDt from your inner balance to your wallet with `withdraw`, 1 yoctoNEAR attached:

```sh
near contract call-function as-transaction dex.intear.near withdraw \
  json-args '{
    "asset_id": "nep141:usdt.tether-token.near",
    "amount": {"Full": {"at_least": null}}
  }' \
  prepaid-gas '50.0 Tgas' attached-deposit '1 yoctoNEAR' \
  sign-as alice.near network-config mainnet sign-with-keychain send
```

The whole file, tested in CI: https://docs.intear.tech/snippets/cli/quickstart.sh

`Full` withdraws the whole balance. Use `{"Exact": "..."}` for a specific amount. If the transfer fails, the tokens go back to your inner balance.

## In one transaction

The deposit, the swap and the withdrawal (steps 3, 5 and the `withdraw` of step 6) can be one call: [`execute_operations`](https://docs.intear.tech/intear-dex/guides/operations.md#execute_operations) with the NEAR attached, or `deposit_near` with `operations`. With a NEP-141 token as input, `ft_transfer_call` with a list of [operations](https://docs.intear.tech/intear-dex/guides/operations.md#operations-in-a-deposit) in `msg` deposits, swaps and withdraws in one transaction. You still simulate first to set the constraint, and register your wallet on the output token beforehand.
