This article is supposed to be a primer on designing a exchange/DEX/aggregator API that partners of LI.FI can work with. This article doesn’t aim to educate on how to build RESTful APIs, but we recommend to take a deep look into all the different parts that improve the quality of an API from compression, caching, versioning, following best practices.
As an aggregation protocol we’ve seen and assed many APIs and feel to be in a good position to suggest the following endpoints.
LI.FI integrates all exchanges in a backend, requests quotes for the user and parses them in the same way. The user gets a transaction to execute and checks a status endpoint in the LI.FI API which again will forward information of the swap that happened.
On-Chain the user sends a transaction to the LI.FI Diamond contract, which forwards tokens and parameters to the exchange. The contract needs to be able to verify that the exchange contract address and the method called.
Rate-Limits:
Request:
POST /{chainId}/quote // chainId based on <https://github.com/lifinance/types/blob/main/src/chains/base.ts#L67>
{
fromToken: string, // use `0x0000000000000000000000000000000000000000` for native assets on EVM
toToken: string, // use `0x0000000000000000000000000000000000000000` for native assets on EVM
fromAmount: string, // input token amount with all decimals, e.g. 12.6 USDC = '12600000'
slippage?: float, // The maximum allowed slippage for the transaction. 0.005 represents 0.5%, when not provided it is up to the exchange to set a good slippage limit for the passed token combination
}
Response:
{
fromAmount: string,
toAmount: string,
toAmountMin: string, // toAmount - slippage, limit should be enforced in the callData
slippage: float, // applied slippage limit
approveToAddress?: string, // where should ERC20 tokens be approved to (optional on other ecosystems)
gasEstimation: number, // gas units required to execute the transaction
transaction: {
to: string, // contract to call
value: string, // native tokens to attach to the transaction
callData: string, // callData to execute the swap
}
}
toAmountMin
).