swapWithExactIn()
SET UP THE SDK BEFORE YOU START:
- You can find the SDK setup instructions in the Quickstart page.
- We also created a tutorial to make it easier to understand how devs need to initialize the Nexus SDK in their project.
Use the swapWithExactIn() function to swap tokens with an exactly defined input amount.
For example, if you want to swap 1 ETH into whatever amount of USDC you can get on the destination chain.
Method signature
Typescript
async swapWithExactIn(
input: ExactInSwapInput,
options?: OnEventParam,
): Promise<SwapResult>Parameters
Typescript
export interface ExactOutSwapInput {
toChainId: number;
toTokenAddress: Hex;
toAmount: bigint;
}-
ExactInSwapInput: Parameters for using theswapWithExactIn()function.toChainId(number, required): The chain ID of the destination chain.toTokenAddress(Hex, required): The address of the token to be swapped to.toAmount(bigint, required): The amount of tokens to be swapped to.
-
options: Optional parameters for the event listener.
Example
Typescript
import type { ExactInSwapInput, OnEventParam, SwapResult } from '@avail-project/nexus-core';
const swapWithExactInResult = await sdk.swapWithExactIn(
{
from: [
{ chainId: 10, amount: 1_000_000n, tokenAddress: '0x...' },
],
toChainId: 8453,
toTokenAddress: '0x...',
},
{
onEvent: (event) => console.log('Swap event:', event),
},
);
console.log('Swap with exact in result:', swapWithExactInResult);Return Value
swapWithExactIn()
The return value is a SwapResult object.
Typescript
export type SwapResult =
| {
success: true;
result: SuccessfulSwapResult;
}
| { success: false; error: string };
export type SuccessfulSwapResult = {
sourceSwaps: ChainSwap[];
explorerURL: string;
destinationSwap: ChainSwap | null;
};
export type ChainSwap = {
chainId: number;
swaps: Swap[];
txHash: Hex;
};Last updated on