Hinkal Mobile SDK

Swaps

A private swap trades one token for another entirely inside the private balance, with no public trace of the trade. It runs server-side through a Hinkal relayer.

SDK calls take an NSError out-parameter, wrapped here with the call helper from Getting Started.

Quote first

Get a quote before swapping. Use getEvmSwapPrices on EVM chains, getSolanaSwapPrices on Solana:

let quoteJSON = try call { hinkal.getEvmSwapPrices(chainId, inAmount: inWei, inTokenAddr: inToken, outTokenAddr: outToken, error: &$0) }

The quote returns the swap data you pass into swap.

Basic Swap

let txHash = try call {
    hinkal.swap(
        chainId,
        tokenAddrsJSON: "[\"\(inToken)\", \"\(outToken)\"]",
        amountsWeiJSON: "[\"\(inWei)\"]",
        actionID: actionID,
        swapData: swapData,
        feeToken: inToken,
        feeStructureJSON: "",
        error: &$0
    )
}
print("Swap tx:", txHash)

On Solana use swapSolana(...), which omits actionID:

let txHash = try call {
    hinkal.swapSolana(
        chainId,
        tokenAddrsJSON: "[\"\(inToken)\", \"\(outToken)\"]",
        amountsWeiJSON: "[\"\(inWei)\"]",
        swapData: swapData,
        feeToken: outToken,
        feeStructureJSON: "",
        error: &$0
    )
}

Parameters

ParameterTypeDescription
chainIDInt64Target chain id.
tokenAddrsJSONStringJSON array: input token, then output token.
amountsWeiJSONStringJSON array with the input amount in wei.
actionIDStringDEX action id from the quote (EVM only).
swapDataStringEncoded swap data from the quote.
feeTokenStringToken the fee is paid in.
feeStructureJSONStringFee quote, or empty for defaults.

Error Handling

do {
    let txHash = try call {
        hinkal.swap(chainId, tokenAddrsJSON: "[\"\(inToken)\", \"\(outToken)\"]", amountsWeiJSON: "[\"\(inWei)\"]",
                    actionID: actionID, swapData: swapData, feeToken: inToken, feeStructureJSON: "", error: &$0)
    }
} catch {
    print("Swap failed:", error.localizedDescription)
}

Best Practices

1. Always quote immediately before swapping

Quotes move. Fetch a fresh quote with getEvmSwapPrices / getSolanaSwapPrices right before calling swap, and pass its swapData unchanged.

let quote = try call { hinkal.getEvmSwapPrices(chainId, inAmount: inWei, inTokenAddr: inToken, outTokenAddr: outToken, error: &$0) }

2. Use the right method per chain

Use swap on EVM/Tron (it takes actionID) and swapSolana on Solana (no actionID). Branch on MobileIsSolanaChain(chainId).

3. Refresh the balance afterward

 = try call { hinkal.getTotalBalance(chainId, userKeysSignature: signature,
    ethAddress: address, resetCache: true, useBlockedUtxos: false, error: &$0) }