API specification

Parameters, input/output format

Curl sample

curl --location --request GET 'https://router.firebird.finance/aggregator/v1/route?chainId=250&from=0x04068da6c83afcfa0e13ba15a6696662335d5b75&to=0xf24bcf4d1e507740041c9cfd2dddb29585adce1e&amount=10000000&receiver=0x3FD5e7C6A2950691ffbf9358d0C093de30f54432&source=discord_swap_bot'

Request query parameters

get
URL:
https://router.firebird.finance/aggregator/v1/route

Response payload

Field
Sub field
Type
Description
encodedData
json object
router
string
Firebird Router smart contract address The address is returned here because it's frequently updated
data
string
Data encoded for given quote. Client just need to submit this data to given provided contract address returned at "router"
maxReturn
json object
from
string
ERC20 token contract address to sell
to
string
ERC20 token contract address to buy
totalFrom
string
buy amount of ERC20 tokens in wei
totalTo
numeric string
best sell amount of ERC20 tokens in wei
totalGas
number
gasPrice
numeric string
paths
array of object
swap paths, for display purpose

Integration example

1
const Web3 = require('web3')
2
const axios = require('axios')
3
4
const { BN } = Web3.utils
5
const MAX_UINT = '115792089237316195423570985008687907853269984665640564039457584007913129639935'
6
7
const PRIVATE_KEY = '' // TODO: config your private key
8
const ROUTER_API = 'https://router.firebird.finance/aggregator/v1/route'
9
const RPC_URL = 'https://rpc.ftm.tools/'
10
11
const ERC20_ABI = [
12
{
13
"constant": true,
14
"inputs": [{ "name": "_owner", "type": "address" }, { "name": "_spender", "type": "address" }],
15
"name": "allowance",
16
"outputs": [{ "name": "remaining", "type": "uint256" }],
17
"payable": false,
18
"stateMutability": "view",
19
"type": "function"
20
},
21
{
22
"constant": false,
23
"inputs": [{ "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" }],
24
"name": "approve",
25
"outputs": [],
26
"payable": false,
27
"stateMutability": "nonpayable",
28
"type": "function"
29
}
30
]
31
32
const web3 = new Web3(new Web3.providers.HttpProvider(RPC_URL))
33
34
// PRIVATE_KEY variable defined
35
const account = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY)
36
37
function sendTransaction(tx) {
38
const signPromise = web3.eth.accounts.signTransaction(tx, PRIVATE_KEY)
39
return new Promise((resolve, reject) => {
40
signPromise.then((signedTx) => {
41
// raw transaction string may be available in .raw or
42
// .rawTransaction depending on which signTransaction
43
// function was called
44
const sentTx = web3.eth.sendSignedTransaction(signedTx.raw || signedTx.rawTransaction)
45
sentTx.on("receipt", receipt => {
46
// do something when receipt comes back
47
resolve(receipt)
48
})
49
sentTx.on("error", err => {
50
// do something on transaction error
51
reject(err)
52
})
53
}).catch((err) => {
54
// do something when promise fails
55
reject(err)
56
})
57
})
58
}
59
60
async function swap(params) {
61
// fetch encoded swap transaction data
62
const { data: { encodedData } } = await axios.get(ROUTER_API, {
63
headers: { 'Content-Type': 'application/json' },
64
params
65
})
66
67
const tokenIn = params.from
68
const routerContract = encodedData.router // router contract address
69
const currencyAIsEth = tokenIn.toLowerCase() === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
70
const amountIn = params.amount
71
72
if (!currencyAIsEth) {
73
// check approval
74
const contract = new web3.eth.Contract(ERC20_ABI, tokenIn, {
75
from: account.address
76
})
77
const allowance = await contract.methods.allowance(account.address, routerContract).call()
78
if (new BN(allowance).lt(new BN(amountIn))) {
79
console.log('# APPROVING...')
80
const approvalData = contract.methods.approve(routerContract, MAX_UINT).encodeABI()
81
const tx = {
82
from: account.address,
83
to: tokenIn,
84
gas: '1000000',
85
data: approvalData,
86
}
87
const approvalReceipt = await sendTransaction(tx, PRIVATE_KEY).catch()
88
console.log('# APPROVED TXN:', approvalReceipt.transactionHash)
89
}
90
}
91
92
const tx = {
93
from: account.address, // signer address
94
to: routerContract,
95
gas: '1000000',
96
data: encodedData.data, // encoded contract data
97
value: currencyAIsEth ? amountIn : undefined,
98
}
99
100
console.log('# SWAPPING...')
101
sendTransaction(tx)
102
.then((receipt) => {
103
console.log('# SWAP SUCCESSFULLY:')
104
// do something when receipt comes back
105
console.log(receipt)
106
})
107
.catch((err) => {
108
console.log('# SWAP FAILED:')
109
// do something on transaction error
110
console.error(err)
111
})
112
}
113
114
// swap USDC to FTM
115
swap({
116
chainId: 250,
117
from: '0x6c021ae822bea943b2e66552bde1d2696a53fbb7',
118
to: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
119
amount: '1000000',
120
receiver: account.address,
121
source: 'sample'
122
})
123
Copied!