A small blazing fast wrapper over the Via Router API for on-chain and cross-chain swaps. Our API allows you to find the best route for moving funds between chains!
Feature List
feature
remark
method
get routes
Swaps/bridges cheapest routes
getRoutes
check allowance
Allowance to check whether it's needed or not to approve
getAllowanceStatus
build approval transaction
The transaction that approves the VIA contract to spend your token
buildApprovalTx
build transaction
The transaction that will perform a swap/bridge according to the route
buildTx
check transaction
Check the status of the transaction
checkTx
Installing
npm i @viaprotocol/router-sdk
Usage
First you need to initialize a VIA Client instance:
constpagesNum=awaitcli.routesPages(); // cache me!constbaseParams= { fromChainId:1, fromTokenAddress:'0x0000000000000000000000000000000000000000', fromAmount:Math.pow(10,18), toChainId:56, toTokenAddress:'0x0000000000000000000000000000000000000000', fromAddress:'0x856cc59aaE47997a1C8D5472Fc8dfef27821235d',// might be null multiTx:false,// whether to return routes with multiple user transactions limit:1,};constparams= [...Array(pagesNum)].map( (_, i) => ({...baseParams, offset: i+1 }));constroutes=awaitPromise.allSettled(params.map(i =>cli.getRoutes(i)));
Request parameters description
Parameter
Description
fromChainId
Source chain id
fromTokenAddress
Source token address
fromAmount
Amount
toChainId
Target chain id
toTokenAddress
Target token address
fromAddress
Sender address
multiTx
whether to return routes with multiple user transactions
offset
Pagination offset
limit
Pagination limit
Pagination is needed because the request time for a specific page is faster than for all pages at once
Get allowance status
You must approve the contract to spend your token. You can get route_id from the route you like received above in the code snippet.
constfirstNonEmptyPage=routes.find(i =>i.value.routes.length>0).value;constroute=firstNonEmptyPage.routes[0];constrouteId=route.routeId;constchainId= fromChainId;constowner="YOUR_WALLET_ADDRESS";consttokenAddress= fromTokenAddress;constnumAction=0; // number of action in routesconstallowanceStatus=awaitcli.getAllowanceStatus( {owner, routeId, numAction});
Build approval transaction. Returns the transaction that approves the Via API to spend your token.
Now you can build the transaction that will perform a crosschain swap according to the route.
# amount out minimal, you can get it from get_routes asto_token_amountoutput =firstNonEmptyPage.routes[0].toTokenAmounttx =awaitcli.buildTx( { routeId, fromAddress, receiveAddress, numAction })
If you want to know the status of the transaction, then you need to tell us that you started it
With via.exchange it is possible to execute routes in several steps. This will help expand the number of available routes and, as a result, help you find the most profitable routes!
Multichain and Hyphen bridges don't support transfers from non-whitelist accounts. Therefore, it is necessary to filter routes with these bridges for users with multi0signature wallets.