import { Router as FibrousRouter, getBestRouteParams } from "fibrous-router-sdk";
import { parseUnits } from "ethers";
// Create a new router instance with v2 API
const fibrous = new FibrousRouter({
apiKey: "your-api-key", // optional, required for integrator features
apiVersion: "v2", // optional, v2 is the latest version
});
// Refresh and get supported chains
const chains = await fibrous.refreshSupportedChains();
const chainId = chains.find(chain => chain.chain_name == "hyperevm")?.chain_id;
if (!chainId) {
throw new Error("Chain not supported");
}
// Build route options
const tokens = await fibrous.supportedTokens(chainId);
// Get input token from tokens map
const inputToken = tokens.get("usdc");
if (!inputToken) {
throw new Error("Input token not found");
}
// Get output token by address (for unverified tokens)
const outputToken = await fibrous.getToken(
"0xb8ce59fc3717ada4c02eadf9682a9e934f625ebb",
chainId,
);
if (!outputToken) {
throw new Error("Output token not found");
}
const tokenInAddress = inputToken.address;
const tokenOutAddress = outputToken.address;
const tokenInDecimals = Number(inputToken.decimals);
const inputAmount = BigInt(parseUnits("4", tokenInDecimals)); // 4 USDC
const getBestRouteParams: getBestRouteParams = {
amount: inputAmount,
tokenInAddress: tokenInAddress,
tokenOutAddress: tokenOutAddress,
chainId: chainId,
options: {
reverse: false,
},
};
const route = await fibrous.getBestRoute(getBestRouteParams);
// returns route type (src/types/route.ts)