Skip to content

Quickstart

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").

The examples in these docs use near CLI and jq. Install near CLI, and import the account you’ll sign with:

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.

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:

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

You only do this once. You can top it up later or withdraw what you don’t use. See Storage.

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

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

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

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

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

For a NEP-141 token, call ft_transfer_call on the token contract instead. See Inner balances.

Check the balance with the asset_balance_of view method:

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

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

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:

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))

message is passed to the DEX as is, and each DEX decides what goes there. For Plach (slimedragon.near/xyk), it’s the route in Borsh: AQAAAEBCDwABAAAA3QAAAAA= swaps the whole amount through pool 221, NEAR/USDt.

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:

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

It returns [amount_in, amount_out]. If the output is lower than constraint, the call fails and nothing changes. See Swaps.

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

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

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

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

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

The deposit, the swap and the withdrawal (steps 3, 5 and the withdraw of step 6) can be one call: 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 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.