How to Swap
On this page you will find a guide on how to use the Fanbase Aggregate Swap API.
Performing a successful swap consists of a series of requests to our API and executions on the blockchain which requires paying gas fees - fees vary depending on the blockchain. Our API supports a collection of blockchain networks which you find a full list here.
Step 1: Get quotes
The first step on swapping is to get the available exchange quotes. The endpoint and the returned data is the following:
Fetch Quotes
GET
https://agg20.fanbase.io/v1/quotes
Get quotes of a pair from available exchanges. In case of getting quotes between Native Coin (e.g. Ethereum) pass asinputAddress
or outputAddress
the following: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE
.
Query Parameters
chainId*
String
Network ID e.g.:1
inputAddress*
String
Contract address of a token to sell e.g.: 0xdAC17F958D2ee523a2206206994597C13D831ec7
outputAddress*
String
Contract address of a token to buy e.g.: 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984
inputAmount*
Big Number
Amount of input token to sell, in WEI e.g.:
1000000000000000000
[
{
"fromToken": {
"name": string,
"symbol": string,
"decimals": number
},
"toToken": {
"name": string,
"symbol": string,
"decimals": number
},
"fromTokenAmount": string - amoun in WEI,
"toTokenAmount": BigNumber,
"dexName": string
},
...
]
This endpoint returns an array of the available exchanges you can perform swaps to.
Step 2: Check approve allowance
Before approving a swap, it is a good practice to check the allowance of the tokens that user will spend. In cases where a user wants to swap X for Y, they need to approve the spending of X by the exchanges contracts. Sometimes users approve more than they spend so there are cases where the user doesn't have to approve their next swap.
Check Allowance
POST
https://agg20.fanbase.io/v1/approve/allowance
Check if a token spending is already approved. Only available when swapping Token
-> Token
or Token
-> Native
.
Request Body
chainId*
String
Network ID e.g.:1
inputAddress*
String
Contract address of a token to check allowance e.g.: 0xdAC17F958D2ee523a2206206994597C13D831ec7
dexName*
String
Name of DEX e.g.:
UNISWAP_V2
fromAddress*
String
Operator address e.g.: 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045
{
"statusCode": 200,
"message": "Successfully",
"data": {
"allowance": BigNumber
}
}
Step 3: Approve spending of tokens
Approving the spending of tokens is necessary and required when swapping tokens Token
-> Token
or Token
-> Native
. Native
-> Token
does NOT need approval.
Token
refers to any token that is a Smart Contract and follows the ERC-20 standard.
Native
refers to the native coin of a blockchain. Its is the coin that used to pay fees with. e.g. on Ethereum blockchain the native coin it ETH. On Polygon blockchain it is MATIC etc.
Approving requires to paying gas fees to the blockchain.
Generate Approve Data
POST
https://agg20.fanbase.io/v1/approve
In order for the Fanbase Aggregation protocol to access the tokens in your wallet, you need to create an approve transaction. This specifies that the fanbase router can exchange a specific amount of the chosen token.
Only available when swapping Token
-> Token
or Token
-> Native
. Native
-> Token
does NOT need approval.
Request Body
chainId*
Number
Network ID e.g.:1
inputAddress*
String
Contract address of a token to sell e.g.: 0xdAC17F958D2ee523a2206206994597C13D831ec7
outputAddress*
String
Contract address of a token to buy e.g.: 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984
inputAmount*
Big Number
Amount of input token to sell, in WEI e.g.:
1000000000000000000
dexName*
String
Name of DEX e.g.:
UNISWAP_V2
, UNISWAP_V3
, PANCAKESWAP
, SUSHISWAP
, VOLTAGE
, SPIRITSWAP
, SPOOKYSWAP
, 1INCH
, FANBASE
Step 4: Swap tokens
After the execution of /approve
- only for Token
spendings - you can proceed to swapping the tokens.
Also this endpoint can be called without calling /approve
endpoint and willl turn full transaction data set include approve transaction.
Generate Swap Data
POST
https://agg20.fanbase.io/v1/swap
Swap a pair of tokens. In case of Native -> Token there's no need for an /approve
to happen before /swap
. In any other case it's required.
Request Body
chainId*
Number
Network ID e.g.:1
inputAddress*
String
Contract address of a token to sell e.g.: 0xdAC17F958D2ee523a2206206994597C13D831ec7
outputAddress*
String
Contract address of a token to buy e.g.: 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984
fromAddress*
String
Operator address e.g.: 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045
inputAmount*
BigNumber
Amount of input token to sell, in WEI e.g.:
1000000000000000000
slippage*
Number
slippage tolerance eg: 1
slippage*
String
Name of DEX e.g.:
UNISWAP_V2
, UNISWAP_V3
, PANCAKESWAP
, SUSHISWAP
, VOLTAGE
, SPIRITSWAP
, SPOOKYSWAP
, 1INCH
, FANBASE
{
"statusCode": 200,
"message": "Successfully",
"data": {
"data": string,
"to": string,
// BigNumber is not returned when "dexName" is "1INCH"
"gasLimit": BigNumber,
// Following are returned when "dexName" is "1INCH"
"value": string,
"from": string,
"gas": number,
"gasPrice": string
}
}
General Step: Executing data
The API basically returns a 0x...
data string that the user needs to sign with their wallet and send the transaction to the blockchain.
This can be accomplished with different blockchain libraries like ethers
or web3
.
Last updated