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 ExactInSwapInput {
from: {
chainId: number;
amount: bigint;
tokenAddress: Hex;
}[];
toChainId: number;
toTokenAddress: Hex;
}-
ExactInSwapInput: Parameters for using theswapWithExactIn()function.from: An array of objects with the following properties:chainId(number, required): The chain ID of the source chain.amount(bigint, required): The amount of tokens to be swapped from.tokenAddress(Hex, required): The address of the token to be swapped from.
toChainId(number, required): The chain ID of the destination chain.toTokenAddress(Hex, required): The address of the token to be swapped to.
-
options:OnEventParam(optional): Optional callback function to listen to the events emitted by the swap operation.onEvent: SDK emits these events as the transaction progresses. This is helpful to add when trying to display transaction progress to the user. With this you receive:SWAP_STEP_COMPLETE: A singleSwapStepTypethat is emitted for each completed step of the intent’s fulfillment.
- Either of the
Swapmethods don’t emitSTEPS_LISTevents. - You can check out the type definitions for each step type in the Swap Events page.
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
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;
};
export type Swap = {
inputAmount: bigint;
inputContract: Hex;
inputDecimals: number;
outputAmount: bigint;
outputContract: Hex;
outputDecimals: number;
};Last updated on