Skip to content

Exact input and exact output

Pass exactly one of amount_in or amount_out. Both are raw integers in the token’s smallest unit (use the token’s decimals from ft_metadata).

“I pay exactly this much, how much do I get?” This is the most common mode, and every DEX supports it.

amount_in=1000000000000000000000000

Quotes come back with output amounts:

"estimated_amount": { "amount_out": "4880007" },
"worst_case_amount": { "amount_out": "4831206" }

worst_case_amount is the minimum output the transactions accept. If the pools move so that the output would be lower, the swap fails and the input is refunded.

“I want to receive exactly this much, how much do I pay?”

amount_out=1000000

Quotes come back with input amounts:

"estimated_amount": { "amount_in": "205495745391126196067546" },
"worst_case_amount": { "amount_in": "210222147535122098577099" }

worst_case_amount is the most the trader can pay. The transactions send up to this much and return whatever isn’t used, so the trader needs to hold the worst-case amount, not just the estimate.

Not every DEX supports exact output:

DEX Exact output
Rhea ❌ No
Rhea DCL ✅ Yes
IntearDex ✅ Yes
Wrap ✅ Yes

DEXs that don’t support it just don’t return a quote, so you may get fewer quotes than with amount_in.

Traders holding or staking TEAR (token.intear.near) get a discount on Plach’s pool and protocol fees, signed into the route by the aggregator:

TEAR held or staked Fee discount
10 10%
1,000 25%
100,000 75%

The discount is signed for the trader, so pass trader_account_id to get it. The aggregator adds up the trader’s TEAR in the wallet and staked in stakeyourtears.intear.near, and reads it again when it’s over a minute old. The first quote for a trader comes without the discount while it’s read, so request a new quote before executing. Referral fees aren’t discounted. The signed discount expires after about 10 minutes (1000 blocks), and the swap fails after that, so refresh old quotes before executing them.

The quote’s discount says how much it saves, in the same direction as estimated_amount:

"estimated_amount": { "amount_out": "4880007" },
"worst_case_amount": { "amount_out": "4831206" },
"discount": { "amount_out": "1952" }

With amount_in, discount is the extra output the trader gets, so you can show it as a bonus. With amount_out, it’s {"amount_in": "..."}, how much less the trader pays.

estimated_amount and worst_case_amount already include the discount, so don’t add it again. It can be 0 when the discount doesn’t change the result, for example on very small amounts. Quotes without a fee discount, because they’re not IntearDex or Plach quotes, have no trader_account_id, are the first quote for the trader, or the trader holds and stakes less than 10 TEAR together, have no discount field.