Skip to the article
Block Times

Crypto markets, protocols and policy

106abc

How to buy an exact amount of tokens on ParaSwap: Market BUY

Learn how to request an exact token output with a BUY quote, build the transaction, and preserve precision from token units through receipt.

Block Times Newsroom4 min read

How to buy an exact amount of tokens on ParaSwap: Market BUY

ParaSwap exact-output buying starts by fixing the destination-token amount in raw units, because every quote and transaction build depends on that target. Use the ParaSwap Market API to request a BUY quote and build the transaction; that route fits when the acquisition quantity must stay fixed. ParaSwap is a DeFi swap aggregator that finds routes across decentralized exchange liquidity, unlike a trade sent through only one venue.

Encode the destination amount before quoting

Start with the amount of the token you want to receive and its verified decimals. For 25 LINK with 18 decimals, the raw-unit amount is 25000000000000000000. This is the value to send as a string, not the human-readable “25.”

That conversion is where exact-output precision can quietly break. JavaScript Number cannot represent every large integer exactly. Converting a raw amount to Number and then back to text can change its final digits before the request even leaves your application. Keep the amount as a decimal string, or parse and calculate with BigInt; convert to the API’s expected string only at the boundary. BigInt cannot be serialized directly as JSON, so stringify it explicitly.

For this example, the prerequisites are verified LINK decimals and a supported BUY route for the chosen source token, LINK, and network. The decimals determine the raw target; the route determines whether the requested pair can be priced.

Request a BUY quote, then build against it

The Market API’s GET /prices request uses side=BUY and amount as the destination quantity in raw units. Include the source and destination token addresses and the network’s chain ID. The SDK’s rate parameters define amount as source-token units for SELL and destination-token units for BUY, and send the price request to /prices. See the official SDK rate parameters.

For the LINK example, amount is the string 25000000000000000000. The quote returns a route and an estimated source amount for acquiring that destination amount. While the request is pending, the API is finding a route that can meet the target. A missing supported route can stall the task; a returned route gives the transaction builder the quote to use.

Build with the same price route, destAmount set to 25000000000000000000, and your chosen slippage in basis points. The SDK’s TxInputAmountsPartBuy pairs destAmount with slippage and omits srcAmount. That combination keeps the destination target fixed while allowing the source spend to vary within the slippage bound. The SDK transaction types also validate that the BUY destination amount matches the quote. See the official SDK transaction types.

Authorize the spend and verify the receipt

Consider an integration that wants 25 LINK using a funded wallet’s WETH. It encodes the LINK target, requests a BUY quote, then builds with the matching destAmount and its slippage setting. The result is a transaction request; the wallet still needs enough WETH for the permitted source spend and native currency for gas.

Authorize the source token spend for the permitted amount, using the allowance flow required by your integration. Then submit the built transaction. During confirmation, the swap is pending on-chain; network congestion or transaction execution can delay the receipt. Reserve native gas separately from the swap input, since the transaction needs it to execute.

The final check is the receipt and destination-token balance change: confirm that the transaction succeeded and that the received LINK satisfies the target within the source-spend authorization. If the builder returns “Destination Amount Mismatch,” align the build’s destAmount with the quoted BUY route’s destination amount, then build again from that quote. A BUY fixes the requested output; a SELL instead fixes the source amount and leaves the output variable.

Which quantity must stay fixed?

Choose the route by the quantity the task requires you to hold constant:

  • BUY fixes the destination-token amount and allows bounded source-spend variation.
  • SELL fixes the source-token amount and allows the destination amount to vary.

If acquiring a specific token quantity is mandatory, choose the linked aggregator’s BUY route. The source cost and its dollar value can still vary with the route and market conditions; the fixed target is the token quantity.

Other exact-output questions

Can a limit order guarantee my purchase?

A limit sets acceptable terms; filling still depends on counterparties.

Does exact output fix the dollar cost?

It fixes token quantity; source-token cost and its dollar value can vary.

Can I buy fractional tokens exactly?

The target must fit the token’s smallest representable unit. Convert the desired fraction using the verified decimals before quoting. For a practical final safeguard, compare the raw destination amount as a string before and after any formatting or storage step; that catches display-rounding changes before they become a wrong quote.

Related coverage