Integration engineering

Preserve transaction value when executing a native swap

Keep native transaction value intact through API normalization, simulation and wallet submission.

Transaction value is a native-currency transfer attached to an EVM call. It is not the token input amount by default, and it must survive every stage between the provider response and the wallet.

The JSON-RPC transaction model separates value from calldata and gas. A provider may return quantities in a different serialization format from the wallet library. Convert the representation exactly while preserving the amount.

A common adapter failure

A developer copies to and data from a token-to-token example and omits value. The same adapter later receives a native-input route. Its calldata is correct, but the contract receives no native currency and the call reverts.

The 0x quickstart carries transaction.value into wallet submission. PancakeSwap's example explicitly converts its returned hex quantity to BigInt. Follow the schema of the selected interface rather than assuming every API returns decimal strings.

Validate the whole funding request

Compare value with the intended trade and the provider's documented behavior. Account for the network fee separately. A token-input route can still have protocol-specific native funding requirements, so neither zeroing value nor replacing it with sellAmount is a safe generic rule.

Simulate using the same value that will be submitted. If the UI edits the sell amount or switches native currency to its wrapped token, obtain a new route; do not patch value alone.

Fixtures should cover zero value, a nonzero decimal value, a nonzero hex value and a number beyond safe JavaScript integer precision. After normalization, exact integer comparison should prove that no representation change altered the intended transfer.

Sources & verification (3)

Source-check date is recorded in the article details. URLs are provided for manual verification. Use Copy to keep this page open.

  1. JSON-RPC API

    Transaction submission, receipt retrieval and transaction fields

    https://ethereum.org/developers/docs/apis/json-rpc/
  2. Get Started with Swap API

    AllowanceHolder sequence, request parameters and transaction payload

    https://docs.0x.org/docs/introduction/quickstart/swap-tokens-with-0x-swap-api
  3. TypeScript examples

    Different approval and execution paths for agg and pcsx candidates

    https://developer.pancakeswap.finance/contracts/unified-swap-api/typescript

Continue reading

Swap API integration: quote, approve, simulate, submit