For the complete documentation index, see llms.txt. This page is also available as Markdown.

ExternalActionSwap

Overview

ExternalActionSwap defines a common swap action interface used by Hinkal external actions. It receives user intent from HinkalInLogic.handleRunExternalAction, executes a swap on a target router, pays the relay, returns tokens back to Hinkal, and reports resulting UTXOs.

Routing is delegated to an aggregator, so Hinkal does not maintain one adapter per DEX. On EVM chains the router is LI.FI, which internally splits the trade across the underlying DEXs and aggregators.

Concrete adapters:

  • LifiExternalAction - LI.FI Router arbitrary calldata (EVM swaps and cross-chain swaps)

Solana swaps do not use this contract; they are executed on the Solana program side against either the LI.FI API (ExternalActionId.Lifi, Public/Proxy accounts) or the OKX DEX API (ExternalActionId.Okx, shielded and Public accounts).

Inheritance

  • Transferer - ERC20/ETH helpers (approve, transfers, balances)

  • ExternalActionBaseV2 - recipient allowlist and ownership controls

Key Storage

  • IHinkalHelper public hinkalHelper - to calculate relay fee

  • IWrapper internal immutable wrapper - WETH/Native wrapper, available to adapters that need to wrap or unwrap ETH

  • address public immutable router - target aggregator router

  • uint256 public constant SWAP_DEADLINE_WINDOW = 10 minutes - max age of the proof's timestamp


Entry Point from Hinkal

function runAction(CircomData circomData, int256[] deltaAmounts)
    external onlyAllowedRecipient returns (UTXO[] utxoSet)
  • Delegates to swap(circomData, deltaAmounts).

  • Protected by onlyAllowedRecipient so only the registered Hinkal contract can call.

Flow:

  1. Read input token/amount from circomData.erc20TokenAddresses[0] and deltaAmounts[0].

  2. If feeToken == inputToken, subtract the flat fee from the input amount.

  3. Read output token from circomData.erc20TokenAddresses[1].

  4. Require slippageValues[1] != 0 ("swap output slippage floor not set").

  5. Require block.timestamp <= circomData.timeStamp + SWAP_DEADLINE_WINDOW ("swap expired").

  6. Execute the swap via callRouter.

  7. Pay the relay: relayFee is the flat fee, hinkalFee = hinkalHelper.calculateRelayFee(swappedAmount, 0, variableRate). If feeToken == outputToken, both are sent to the relay in the output token in one transfer; otherwise the flat fee is sent in feeToken and the variable fee in outputToken.

  8. Transfer the remaining output amount to msg.sender (Hinkal), and create a single UTXO with output token, amount, and stealth info.


Adapter

LifiExternalAction

  • Measures outputToken balance before the call.

  • If input is ETH (address(0)), forwards inputAmount as msg.value to router.call(externalActionMetadata); else approves the router unlimited allowance and calls without value.

  • Requires success with distinct revert messages for native ("LI.FI swap failed: native coin") vs ERC20 ("LI.FI swap failed: erc-20 token").

  • Computes swappedAmount as the post-call balance delta for outputToken.

Metadata: raw calldata produced off-chain by the LI.FI API for the specific chain, tokens and route. Fetch it with getEvmSwapPrices (TypeScript SDK) or GetSwapQuotesJSON (Go/mobile SDK); the returned lifiDataValue is the metadata, and the matching action id is ExternalActionId.Lifi.


Fees and Accounting

  • Input-side: If the feeToken is the input token, the flat fee is removed from the input before the swap.

  • Output-side: The variable Hinkal fee is computed on the swap result and deducted before returning to Hinkal; the flat relay fee is also deducted from the output when feeToken == outputToken.

  • Hinkal later enforces slippage and balance equations; this adapter focuses on execution and fee pass-through.


ETH Handling

  • The LI.FI router takes and returns native ETH directly, so the adapter passes msg.value on native input and reads native balances via getERC20OrETHBalance; no wrapping is performed in the swap path.

  • The wrapper (WETH) is available in the base contract for adapters and flows that do need wrapping; WrapperTokenExchange covers the standalone wrap/unwrap case.

  • Transfers via transferERC20TokenOrETH so ETH and ERC20 paths are unified.


Security Considerations

  • onlyAllowedRecipient prevents arbitrary callers from abusing approvals/funds.

  • No internal slippage check: circuit/Hinkal-level slippageValues enforce minimum outputs, and the action refuses to run when the output slippage floor is unset.

  • SWAP_DEADLINE_WINDOW bounds how stale the proof's timestamp can be at execution time.

  • Unlimited approval is given per-call to the router; this contract is trust-minimized by recipient gating and Hinkal balance checks.

  • Explicit revert messages for external call failures improve debuggability.


Example Integrations

  • LI.FI single-hop swap on one EVM chain, metadata produced by the LI.FI quote API.

  • LI.FI split-route aggregation across several DEXs, returned as one router call.

  • LI.FI cross-chain swap where the destination chain and recipient are encoded in the route calldata.

Last updated