swap()

function swap(client, props): Promise<`0x${string}`>

Swaps a token for another token using the router contract.

Parameters

ParameterTypeDescription
clientWriteClientThe write client to use for the contract call.
propsRouterSwapPropsAn object containing the properties for the function.
type RouterSwapProps: WriteContractBaseProps & {
  address: Address;
  raisingToken: Address;
  payAddress: Address;
  payIndex: bigint;
  payAmount: bigint;
  receiveAddress: Address;
  receiveIndex: bigint;
  minReceiveAmount: bigint;
  timeout: bigint;
};

Returns

Promise<`0x${string}`>

A promise that resolves to the result of the write contract.

Example

router.ts
import { router } from '@solio/core';
import { createWalletClient, custom } from 'viem';
import { sepolia } from 'viem/chains';
const writeClient = createWalletClient({
 chain: sepolia,
 transport: custom(window.ethereum),
});
const hash = await  router.write.swap(writeClient, {
 address: '0x...', // Router contract address
 raisingToken: '0x...',
 payAddress: '0x...',
 payIndex: 0n,
 payAmount: 100n,
 receiveAddress: '0x...',
 receiveIndex: 0n,
 minReceiveAmount: 100n
});