Cross chain swap
How to execute cross chain swap using Magpie APIs
Last updated
How to execute cross chain swap using Magpie APIs
Last updated
Magpie provides two execution options for cross-chain swaps:
Users execute the transaction themselves and cover gas costs.
Magpie executes the transaction on behalf of the user and covers the gas fees, so no gas payment is required in the native currency.
Each cross-chain swap involves two main steps:
Quote-In: Get an estimated swap amount and route on the source chain.
Transaction Execution: Complete the swap by either self-execution or letting Magpie handle it.
In this step, you retrieve a quote that provides details for the cross-chain swap on the source chain, including the estimated output amount and swap route.
Endpoint: /aggregator/quote-in
Method: GET
fromTokenAddress: Address of the token on the source chain that you want to swap from.
toTokenAddress: Address of the token on the destination chain that you want to receive.
amount: The amount of fromToken
in the smallest unit (e.g., wei for ETH).
slippage: Allowed slippage percentage (e.g., 0.5
for 0.5%).
fromAddress: Wallet address initiating the swap.
toAddress: Wallet address receiving the swapped tokens on the destination chain.
gasless: Set true
to let Magpie handle gas costs (gasless mode) or false
if you will cover the gas cost.
Affiliate Fee: Users can implement their own affiliate fees and define their affiliate address, the fees will be deducted from their user from the from asset (asset sold by their user) and distributed to the affiliate address in the same transaction. They can set an affiliate fee by including affiliateAddress and affiliateFee in the quote request. affiliateFee is the percentage fee, such as 1 for 1%.
This request returns a quote-id
and details such as toTokenAmount
(estimated output) and estimatedGas
.
Based on the gasless
option chosen in Step 1, there are two ways to execute the transaction:
Self-Execution (gasless = false
): Users retrieve transaction data and broadcast it themselves.
Gasless Transaction (gasless = true
): Magpie executes the transaction on behalf of the user.
If gasless
is set to false
, users execute the cross-chain transaction on-chain themselves.
Endpoint: /aggregator/transaction-in
Method: GET
quoteId: Use the quote-id
from Step 1 to retrieve the transaction data.
If gasless
is set to true
, Magpie will execute the transaction on behalf of the user.
Endpoint: /user-manager/execute-swap-in
Method: POST
networkName: The target network, where the transaction takes place.
quoteId: The quote-id
from Step 1 is required to execute the swap.
swapSignature: Signed quote using EIP-712
permitSignature: Signed approval using EIP-2612
permitDeadline: The time at which the signature expires (unix time)
To generate a swapSignature
using Ethers v5, follow these steps:
Obtain Parameters: Retrieve the message
parameter from the response of quote API.
Generate the Signature: const signature = await signer._signTypedData(domain, types, message);
Magpie handles the swap and covers the gas fees. No additional action or gas payment is required from the user.
After initiating a swap, you can monitor the swap status through the following endpoints:
Get Swap Details
Endpoint: /user-manager/swap
Method: GET
Use this endpoint to retrieve details of a completed swap.
Get Swap Status Counts
Endpoint: /user-manager/status-counts
Method: GET
This endpoint provides counts of swaps that are in pending
, error
, or other states based on the wallet address.
To check the route and distribution of the swap on both source and destination chains, use the following endpoints with the quote-id
from Step 1:
Source Chain Distribution: /aggregator/distribution-in
Destination Chain Distribution: /aggregator/distribution-out