Skip to content

Types

Enums are written the way NEAR contracts usually write them in JSON: a variant without fields is a string ("OutputOfLastIn"), a variant with fields is an object with one key ({ "ExactIn": "1000" }). Optional fields can be null or left out, except referral_fees in a DepositMessage, which can only be left out.

Type Format Example
U128 Integer string, in the asset’s smallest unit "1000000000000000000000000"
AccountId NEAR account ID "alice.near"
Base64 Base64 string of bytes "AQIDBA=="
PublicKey Curve and base58 key "ed25519:7EK1MhudQrXZBZjvnvvcNXiQ2ByYBkXg8rUhbKL69CGo"

A string:

Format Asset
near Native NEAR
nep141:<contract> NEP-141 fungible token, for example nep141:usdt.tether-token.near
nep171:<contract>:<token id> NEP-171 NFT. Its balance is "0" or "1"
nep245:<contract>:<token id> NEP-245 multi token

These are the same IDs the DEX Aggregator uses after intear-dex:.

A string, <deployer>/<name>, for example "slimedragon.near/xyk".

An owner of inner balances:

{ "Account": "alice.near" }
{ "Dex": "slimedragon.near/xyk" }
Value Meaning
{ "ExactIn": "<U128>" } Pay exactly this much asset_in
{ "ExactOut": "<U128>" } Receive exactly this much asset_out

The amount of a SwapSimple or SwapAdvanced operation:

Value Meaning
{ "Amount": SwapRequestAmount } A fixed amount, for example { "Amount": { "ExactIn": "1000" } }
"OutputOfLastIn" Exact input of the output of the last swap in the list, SwapSimple or SwapAdvanced. Its asset_out must match this asset_in
"EntireBalanceIn" Exact input of everything available of asset_in

The amount of the withdraw method:

Value Meaning
{ "Exact": "<U128>" } A fixed amount
{ "Full": { "at_least": "<U128>" | null } } The whole balance. Fails if it’s less than at_least

The amount of a Withdraw operation: the same as DirectWithdrawAmount, plus:

Value Meaning
"PreviousSwapOutput" The output of the last swap in the list, SwapSimple or SwapAdvanced. Its asset_out must match this asset_id

One step of execute_operations or of a deposit message.

{
"SwapSimple": {
"dex_id": "slimedragon.near/xyk",
"message": "<base64>",
"asset_in": "near",
"asset_out": "nep141:usdt.tether-token.near",
"amount": "EntireBalanceIn",
"constraint": "4800000"
}
}
Operation Fields What it does
SwapSimple dex_id: DexId, message: Base64, asset_in: AssetId, asset_out: AssetId, amount: SwapOperationAmount, constraint?: U128, quote_improvement_fee?: QuoteImprovementFee A swap. constraint is the minimum output for an exact input, or the maximum input for an exact output. quote_improvement_fee is a quote improvement fee
SwapAdvanced asset_in: AssetId, asset_out: AssetId, amount: SwapOperationAmount, constraint?: U128, quote_improvement_fee?: QuoteImprovementFee, options: SwapOption[], mode: "FindBest" | "FallbackQueue" A swap on one of several DEXs or routes: the best of them, or the first that works. See Best of several DEXs
Withdraw asset_id: AssetId, amount: WithdrawAmount, to?: AccountId, rescue_address?: AccountId Sends the asset to to (default: you). rescue_address is only used in deposit messages, see Withdrawing from a deposit
TransferAsset to: AccountOrDexId, asset_id: AssetId, amount: U128 Moves the asset to another inner balance
DexCall dex_id: DexId, method: string, args: Base64, attached_assets: { [AssetId]: U128 } Calls a DEX method
StorageDeposit amount: U128, for: AccountOrDexId Turns NEAR into a storage balance of for. At least 0.005 NEAR. for is required
RegisterAssets asset_ids: AssetId[], for?: AccountOrDexId Registers assets. Only in execute_operations
OracleRequest oracle_key: PublicKey, dex_id: DexId, request: Base64, fee: [AssetId, U128], swap?: OracleFeeSwap Asks the oracle with oracle_key to respond to request, which dex_id reads, and runs the operations after it once the oracle responds. Only as the first operation. fee is what the oracle takes for a request in that asset, exactly as its oracle_fees say. It’s taken from your inner balance, or from a deposit, which must be of that asset unless swap buys the fee with it. The request is at most 1024 bytes

A DEX that a SwapAdvanced operation can swap on, and the message to pass to it, like the dex_id and message of a SwapSimple:

{ "dex_id": "slimedragon.near/xyk", "message": "<base64>" }

A swap that buys exactly the fee of an OracleRequest with asset_in, before the request, for traders who don’t have an asset the oracle takes fees in. asset_in can’t be the asset of the fee, constraint is the most it can take of asset_in, and it pays no referral fees:

{ "dex_id": "slimedragon.near/xyk", "message": "<base64>", "asset_in": "nep141:usdt.tether-token.near", "constraint": "10000" }

The msg of a token transfer to the engine, as a JSON string, or operations of deposit_near. Either a list of operations:

[{ "SwapSimple": { ... } }, { "Withdraw": { ... } }]

or an object with a referrer, referral fees, a fee discount, or any of them:

{ "operations": [{ "SwapSimple": { ... } }], "referrer": "myapp.near", "referral_fees": [["myapp.near", 5000]], "fee_discount": { ... } }

referrer can be null, but leave referral_fees out instead of setting it to null: null makes the whole message invalid, so deposit_near fails and a token deposit is refunded.

An array of [AccountId, u32] pairs: the accounts a swap pays referral fees to, out of its input, and the fee of each, where 1000000 is 100%. At most 5 of them, adding up to at most 50000 (5%).

[["myapp.near", 5000], ["partner.near", 1000]]

Takes what a swap does better than its quote expected: for an ExactIn swap, the output above quote_expected_amount, and for an ExactOut swap, how much its input and referral fees together are below it. The fee goes to the inner balance of quote_improvement_receiver, in asset_out or asset_in, so the trader gets at most, or pays at least, what the quote expected. If the receiver hasn’t registered that asset, it isn’t paid and the trader keeps it. Swaps log it in quote_improvement_fee_paid.

{ "quote_expected_amount": "4855607", "quote_improvement_receiver": "myapp.near" }

A discount on the pool and protocol fees of swaps, signed by the engine’s fee discount signer, the key that get_fee_discount_signer returns. The DEX Aggregator gives them out to traders who stake TEAR. A DEX reads the discount with the fee_discount host function, and DEXs by others can ignore it. It never reduces referral fees.

{
"discount": {
"ForTrader": { "trader_id": "alice.near", "fee_discount": 250000, "expiration_block_height": 226575858 }
},
"signature": "<base64>"
}

fee_discount is out of 1000000, which waives the fees. A discount is valid until the block expiration_block_height, and is one of:

Discount Fields Valid for
ForOperations operations_hash: string, trader_id?: AccountId, fee_discount: u32, expiration_block_height: u64 The swaps of the operations whose Borsh has this SHA-256, in base58. For swap_simple, that’s one SwapSimple operation of its arguments. Only for trader_id, the caller or the alleged sender of a deposit, if it’s set
ForTrader trader_id: AccountId, fee_discount: u32, expiration_block_height: u64 Every swap of trader_id in calls they make themselves, not in deposits. It can expire at most 1008000 blocks, about 7 days, after the swap

signature is a signature of the SHA-256 of the Borsh of { chain_id: String, engine_contract_id: AccountId, discount: FeeDiscount }: Ed25519, secp256k1 with the recovery ID (0 or 1) as the last byte, or ML-DSA-65.

What an oracle would respond, for simulate_swap_simple to quote a swap on a DEX that needs an oracle. request and response are at most 1024 bytes each:

{ "oracle_key": "ed25519:7EK1MhudQrXZBZjvnvvcNXiQ2ByYBkXg8rUhbKL69CGo", "request": "<base64>", "response": "<base64>" }

A DEX call that deploy_dex_code makes once the code is stored, as the deployer, with assets from their inner balance, for example to initialize the DEX:

{ "method": "new", "args": "<base64>", "attached_assets": {} }
{ "total": "10000000000000000000000", "available": "9000000000000000000000" }

total is everything deposited, available is the part not used for storage yet. Both in yoctoNEAR.

The engine sends DEXs swap and call requests and reads their responses in Borsh, and dex_events_borsh is Borsh too. Inside them, the swap message, the args of a call and the response of a DEX method are bytes in whatever format the DEX defines (Plach uses Borsh). In Borsh:

  • AssetId is an enum: Near (0), Nep141(AccountId) (1), Nep245(AccountId, String) (2), Nep171(AccountId, String) (3).
  • DexId is a struct of deployer: AccountId and id: String.
  • AccountId is a string.
  • Caller, what a DEX gets from the engine’s caller host function, is an enum: Direct(AccountId) (0), the account that called the engine, or OnTransfer { alleged_sender: AccountId, transfer_of: AssetId } (1), for operations in a deposit. See What a DEX sees.

In Rust, these types and the request and response types of DEXs are in the intear-dex-common crate, and the arguments and events of the engine, like Operation and DexEventData, are in intear-dex-types. They aren’t on crates.io, so add them from GitHub:

intear-dex-common = { git = "https://github.com/INTEARnear/dex" }
intear-dex-types = { git = "https://github.com/INTEARnear/dex" }

Outside a contract, also enable the non-contract-usage feature of near-sdk. DEX code enables the dex feature of intear-dex-common for the engine’s host functions, like env::caller() and env::dex_event().