Swap
Routing
Function:
await swapRouterV3(
tokenIn: string,
tokenOut: string,
amount: bigint,
exactIn: boolean
);swapRouterV3(tokenIn: string, tokenOut: string, amount: bigint, exactIn: boolean)Finds the best liquidity route between two tokens, returning an array of Trade<TInput, TOutput, TTradeType> if successful, or an Error if no route is found.
Parameters:
tokenIn/tokenOut: Token addresses. UseWIPaddress if IP (native) is desired.amount: Abigintrepresenting the amount of tokens.exactIn: A boolean indicating whether this is an EXACT_INPUT or EXACT_OUTPUT trade.
Example:
import { swapRouterV3, ADDRESSES } from '@storyhunt/wrapper-sdk';
async function findRoute() {
const routes = await swapRouterV3(
ADDRESSES.TOKENS.WIP.id,
ADDRESSES.TOKENS.USDC.id,
BigInt(10 ** 15),
true
);
if (routes instanceof Error) {
console.error('Error finding routes:', routes.message);
return;
}
console.log('Available trades:', routes);
}
Returns:
Trade<TInput, TOutput, TTradeType>[]if routes are found, containing details of possible swap paths.Errorif routing fails.
Approval
To conduct swaps, you may need to approve tokens for the Story Hunts V3 Swap Router contract.
V3 Swap Router Approval
Function:
Approves the V3 Swap Router to spend your tokens. Defaults to MaxUint256 for unlimited approval if amount is not provided.
Parameters:
token : Token Address.
amount?: Spending amount limit. Defaults to MaxUint256
Example:
Swapping
Function:
Executes a swap transaction given a trade object obtained from swapRouterV3.
Parameters:
trade: ATradeobject representing the best route and amounts for the swap.
Example:
Returns:
Transaction hash of the swap if successful.
Throws an error if the swap cannot be executed.