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. UseWIP
address if IP (native) is desired.amount
: Abigint
representing 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.Error
if 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:
await v3RoutertokenApproval(
token: string,
amount?: bigint
);
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:
import { v3RoutertokenApproval, ADDRESSES } from '@storyhunt/wrapper-sdk';
await v3RoutertokenApproval(ADDRESSES.TOKENS.USDC.id);
console.log('USDC approved for V3 Router!');
Swapping
Function:
await swapV3(
trade: Trade<TInput, TOutput, TTradeType>
);
Executes a swap transaction given a trade object obtained from swapRouterV3
.
Parameters:
trade
: ATrade
object representing the best route and amounts for the swap.
Example:
import { swapV3 } from '@storyhunt/wrapper-sdk';
// Assuming you have a Trade object from swapRouterV3
// const routes = await swapRouterV3(...);
// const bestRoute = routes[0]; // to pass to swapV3 as trade
async function executeSwap(trade: Trade<STORYHUNT.Token, STORYHUNT.Token, STORYHUNT.TradeType>) {
const txHash = await swapV3(trade); // bestRoute
console.log('Swap executed, txHash:', txHash);
}
Returns:
Transaction hash of the swap if successful.
Throws an error if the swap cannot be executed.