⛏️executeSwap

Description

The executeSwap function enables token swaps between two chains using a third-party API.

It takes in the user's private key, source/destination chain, tokens, amount, receives address and slippage as input and returns the transaction receipt for the swap.

Parameters

NameTypeDescription

privateKey

string

The private key of the user's wallet for the source chain

srcChain

string

The name of the source blockchain network (e.g., ETH, AVA, BSC, Polygon, Klaytn), and all specified networks are on the mainnet.

destChain

string

The name of the destination blockchain network (e.g., ETH, AVA, BSC, Polygon, Klaytn), and all specified networks are on the mainnet.

fromToken

string

The address of the token to be swapped on the source blockchain

toToken

string

The address of the token to receive on the destination blockchain

amount

string

The amount of the token to be swapped (in the smallest unit)

receiveAddress

string

The address to receive the swapped tokens on the destination chain

slippage

string

The acceptable slippage rate (in percentage)

Response

PropertyTypeDescription

Promise<Object>

object

The receipt of the transaction after the token swap is done

Example Request and Response

Prerequisites

Before making requests with Nest SDK, you must have it installed.

You can install Nest SDK using either npm or yarn. Use the following commands to install Nest SDK:

npm install @nest25/xy-finance-lib
OR
yarn add @nest25/xy-finance-lib

Request

Here is an example of how to make a executeSwap request using the Nest SDK:

// import Nest SDK library
const { xySwap } = require('@nest25/xy-finance-lib');

// create xySwap instance
const swap = new xySwap();

async function main() {
  // execute swap
  const response = await swap.executeSwap(
    'your-private-key',
    'klaytn',
    'klaytn',
    '0xCF87f94fD8F6B6f0b479771F10dF672f99eADa63',
    '0x5c74070FDeA071359b86082bd9f9b3dEaafbe32b',
    '0.1',
    '0x9f658964c3658257545b13483e2c9a59b6faa98a',
    1,
  );

  console.log(`Contract hash is: ${response.hash}`);
}
// run main function
main();

Response

Contract hash is: 0x08a2be0e4c0019c34f9705b07cb4f5991d7c8710964b0baca32fd378281ea1a8

Use Cases

  • Cross-chain token swaps: The executeSwap function can be used by decentralized exchanges to facilitate cross-chain token swaps, allowing users to trade their tokens across different blockchain networks.

  • Liquidity providers: The function can also be used by liquidity providers to move their funds across different chains and earn higher yields.

Last updated