Utilities
Utilities
The SDK provides various utility functions for token balances, allowances, gas estimation, and token info. All utilities adapt to the configured clients and work seamlessly with viem or ethers signers.
estimateGasCost(gasParams, gasPercentage?)
: Estimates the gas required for a transaction and can optionally pad it by a percentage.getAllowence(token: string, spender: string)
: Retrieves the allowance granted tospender
for the specifiedtoken
.getTokenBalance(token?)
: Fetches the balance of a token for the connected account. Iftoken
is omitted, returns the native IP (via WIP) balance.getTokenInfo(address: string)
: Fetches the decimals, symbol, and name of an ERC-20 token.getPoolInfo(address: string)
: Retrieves pool information such as fee, state, liquidity, tick, ticks, sqrtPriceX96.getPool(token0: string, token1: string, fee: number )
: Retrieves the pool of a token pair.
Example:
import { getTokenBalance, getTokenInfo, estimateGasCost, getAllowence } from '@storyhunt/wrapper-sdk';
const balance = await getTokenBalance();
console.log('Native token balance:', balance.value.toString());
const info = await getTokenInfo(ADDRESSES.TOKENS.FATE.id);
console.log(`FATE: ${info.symbol} (${info.name}), decimals: ${info.decimals}`);
const allowance = await getAllowence(ADDRESSES.TOKENS.USDC.id, ADDRESSES.V3_SWAP_ROUTER_CONTRACT_ADDRESS);
console.log('USDC allowance for V3 Router:', allowance.toString());
const gas = await estimateGasCost({
address: ADDRESSES.V3_SWAP_ROUTER_CONTRACT_ADDRESS,
abi: [], // Provide ABI for the function youβre calling
functionName: 'someFunction',
args: []
});
console.log('Estimated gas:', gas?.toString());
Wrap IP
Function:
async function wrap(value: bigint): Promise<string>
Wraps native IP into WIP by calling the deposit
function on the WIP contract.
Parameters:
value
(bigint
): The amount of native IP to wrap, specified in wei. This value determines how much native IP will be converted into WIP.
Returns:
string
: The transaction hash of thedeposit
operation, representing the successful submission of the wrap transaction to the blockchain.
Example:
import { wrap } from '@storyhunt/wrapper-sdk';
async function wrapTokens() {
const value = BigInt('1000000000000000000'); // 1 IP in wei
const txHash = await wrap(value);
console.log('Transaction hash:', txHash);
}
wrapTokens();
UnWrap IP
Function:
async function unwrap(value: bigint): Promise<string>
Unwraps WIP back into native IP by calling the withdraw
function on the WIP contract.
Parameters:
value
(bigint
): The amount of WIP to unwrap, specified in wei. This value determines how much WIP will be converted back into native IP.
Returns:
string
: The transaction hash of thewithdraw
operation, representing the successful submission of the unwrap transaction to the blockchain.
Example:
import { unwrap } from '@storyhunt/wrapper-sdk';
async function unwrapTokens() {
const value = BigInt('1000000000000000000'); // 1 WIP in wei
const txHash = await unwrap(value);
console.log('Transaction hash:', txHash);
}
unwrapTokens();
Approvals
To conduct swaps or add liquidity, you may need to approve tokens for specific contracts.
V3 Swap Router Approval
Function: 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.
Example:
import { v3RoutertokenApproval, ADDRESSES } from '@storyhunt/wrapper-sdk';
await v3RoutertokenApproval(ADDRESSES.TOKENS.USDC.id);
console.log('USDC approved for V3 Router!');
V3 Non-Fungible Position Manager Approval
Function: v3PositionManagertokenApproval(token: string, amount?: bigint)
Approves the Non-Fungible Position Manager to spend your tokens. Defaults to MaxUint256
if amount
is not provided.
Example:
import { v3PositionManagertokenApproval, ADDRESSES } from '@storyhunt/wrapper-sdk';
await v3PositionManagertokenApproval(ADDRESSES.TOKENS.WIP.id);
console.log('WIP approved for Non-Fungible Position Manager!');
Wrap and Unwrap Functions
wrap
Function
Wraps the native IP token into WIP (Wrapped IP) by calling the deposit
method on the WIP contract. This is useful for interacting with smart contracts that require WIP instead of the native IP token.
Parameters
value
(bigint): The amount of native IP (in wei) to wrap.
Returns
A transaction hash as a string if the transaction is successful.
Example Usage
import { wrap } from '@storyhunt/wrapper-sdk';
async function wrapTokens() {
const value = BigInt('1000000000000000000'); // 1 IP in wei
const txHash = await wrap(value);
console.log('Transaction hash:', txHash);
}
wrapTokens();
unwrap
Function
Unwraps WIP (Wrapped IP) back into the native IP token by calling the withdraw
method on the WIP contract. This is useful when you need the native IP token from its wrapped version.
Parameters
value
(bigint): The amount of WIP (in wei) to unwrap.
Returns
A transaction hash as a string if the transaction is successful.
Example Usage
import { unwrap } from '@storyhunt/wrapper-sdk';
async function unwrapTokens() {
const value = BigInt('1000000000000000000'); // 1 WIP in wei
const txHash = await unwrap(value);
console.log('Transaction hash:', txHash);
}
unwrapTokens();
Constants and Addresses
ADDRESSES
and defaultChain
are available from constants.ts
. ADDRESSES
provides contract addresses for Odyssey Testnet, including token addresses and essential V3 contracts:
ADDRESSES.TOKENS.IP
andADDRESSES.TOKENS.WIP
represent the native IP token and its wrapped version (WIP).Additional tokens:
JUTSU
,vIP
,USDC
,FATE
.
You can use these addresses directly when calling functions that require token or contract addresses.