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:
import {Via} from '@viaprotocol/router-sdk';
const DEFAULT_API_KEY = 'e3db93a3-ae1c-41e5-8229-b8c1ecef5583';
const cli = new Via({apiKey: DEFAULT_API_KEY, url: 'https://router-api.via.exchange', timeout: 30000});
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 as to_token_amount
output = firstNonEmptyPage.routes[0].toTokenAmount
tx = await cli.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
const v = new Via({apiKey: DEFAULT_API_KEY});
const wsProvider = v.getRoutesViaWs({
fromAddress: '0xD75183E452d6915356814454D2D64Df149853D38',
fromAmount: 148875000000000000,
fromChainId: 56,
toChainId: 56,
fromTokenAddress: '0x0000000000000000000000000000000000000000',
toTokenAddress: '0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d',
multiTx: true
});
wsProvider.onopen = function open() {
console.log('connected');
};
wsProvider.onclose = function close() {
console.log('disconnected');
};
wsProvider.onmessage = function incoming(data) {
const res = JSON.parse(data.data as string);
let route: IRoute[];
let status: IRouteFetchStatus;
if (Array.isArray(res)) {
route = res;
console.log(route);
} else {
status = res.status;
console.log(status);
if (status.finished === status.all){
wsProvider.close()
}
}
};
Response parameters description
Parameter
Description
retry
Time to retry in ms
event
Status of the transaction
data.started
Started time of the transaction on source chain
data.finished
Finished time of the transaction on source chain
data.txHash
Hash of the destination transaction
data.actualAmount
Received amount
Multi-tx
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.