DEX methods for swaping tokens:
async swapExactTokensForTokens
Takes in:
tokenAmountIn: TokenAmount, // Exact amount of token to be swaped
tokenAmountOutMin: TokenAmount, // get the quote from getAmountsOut function
to: string,
path: string[], // get the path from convertToDexPath function
deadline: number
returns a populated transaction
Example:
const path = convertToDexPath(
[tokenAmountA.token, tokenHop, tokenB],
weight
);
const amountsOutCall: BigNumber[] = await dex.getAmountsOut(
tokenAmountA,
path
);
const amountsOut = amountsOutCall[amountsOutCall.length - 1];
tokenAmountB = new TokenAmount(tokenB, amountsOut.toBigInt());
const tx = await dex.swapExactTokensForTokens(
tokenAmountA,
tokenAmountB,
signerWithProvider.address,
path,
getDeadline()
);
const signedTransaction = await signerWithProvider.sendTransaction(tx);
async swapTokensForExactTokens
Takes in:
tokenAmountOut: TokenAmount, // Exact amount of token to be swaped
tokenAmountInMax: TokenAmount, // get the quote from getAmountsIn function
to: string,
path: string[], // get the path from convertToDexPath function
deadline: number
returns a populated transaction
Example:
const path = convertToDexPath(
[tokenB, tokenHop, tokenAmountA.token],
weight
);
let amountsInCall: BigNumber[] = await dex.getAmountsIn(
tokenAmountA,
path
);
const amountsIn = amountsInCall[0];
tokenAmountB = new TokenAmount(tokenB, amountsIn.toBigInt());
const tx = await dex.swapTokensForExactTokens(
tokenAmountA,
tokenAmountB,
signerWithProvider.address,
path,
getDeadline()
);
const signedTransaction = await signerWithProvider.sendTransaction(tx);
async swapExactTokensForNative
Takes in:
tokenAmountIn: TokenAmount, // Exact amount of token to be swaped
nativeAmountOutMin: TokenAmount, // get the quote from getAmountsOut function
to: string,
path: string[], // get the path from convertToDexPath function
deadline: number
returns a populated transaction
Example:
const path = convertToDexPath(
[tokenAmountB.token, tokenHop, native], // native is the wraped Coin
weight
);
const amountsOutCall: BigNumber[] = await dex.getAmountsOut(
tokenAmountB,
path
);
const amountsOut = amountsOutCall[amountsOutCall.length - 1];
const nativeOut = new TokenAmount(native, amountsOut.toBigInt());
const tx = await dex.swapExactTokensForNative(
tokenAmountB,
nativeOut,
signerWithProvider.address,
path,
getDeadline()
);
const signedTransaction = await signerWithProvider.sendTransaction(tx);
async swapTokensForExactNative
Takes in:
nativeAmountOut: TokenAmount, // Exact amount of token to be swaped
tokenAmountInMax: TokenAmount, // get the quote from getAmountsIn function
to: string,
path: string[], // get the path from convertToDexPath function
deadline: number
returns a populated transaction
Example:
const path = convertToDexPath(
[tokenB, tokenHop, exactNativeOut.token], // native is the wraped Coin
weight
);
let amountsInCall: BigNumber[] = await dex.getAmountsIn(
exactNativeOut,
path
);
const amountsIn = amountsInCall[0];
tokenAmountB = new TokenAmount(tokenB, amountsIn.toBigInt());
const tx = await dex.swapTokensForExactNative(
exactNativeOut,
tokenAmountB,
signerWithProvider.address,
path,
getDeadline()
);
const signedTransaction = await signerWithProvider.sendTransaction(tx);
async swapNativeForExactTokens
Takes in:
tokenAmountOut: TokenAmount, // Exact amount of token to be swaped
nativeAmountInMax: TokenAmount, // get the quote from getAmountsIn function
to: string,
path: string[], // get the path from convertToDexPath function
deadline: number
returns a populated transaction
Example:
const path = convertToDexPath(
[native, tokenHop, tokenAmountB.token], // native is the wraped Coin
weight
);
const amountsInCall: BigNumber[] = await dex.getAmountsIn(
tokenAmountB,
path
);
const amountsIn = amountsInCall[0];
const nativeIn = new TokenAmount(native, amountsIn.toBigInt());
const tx = await dex.swapNativeForExactTokens(
tokenAmountB,
nativeIn,
signerWithProvider.address,
path,
getDeadline()
);
const signedTransaction = await signerWithProvider.sendTransaction(tx);
async swapExactNativeForTokens
Takes in:
nativeAmountIn: TokenAmount, // Exact amount of token to be swaped
tokenAmountOutMin: TokenAmount, // get the quote from getAmountsOut function
to: string,
path: string[], // get the path from convertToDexPath function
deadline: number
returns a populated transaction
Example:
const path = convertToDexPath(
[nativeExactIn.token, tokenHop, tokenB], // native is the wraped Coin
weight
);
const amountsOutCall: BigNumber[] = await dex.getAmountsOut(
nativeExactIn,
path
);
const amountsOut = amountsOutCall[amountsOutCall.length - 1];
tokenAmountB = new TokenAmount(tokenB, amountsOut.toBigInt());
const tx = await dex.swapExactNativeForTokens(
nativeExactIn,
tokenAmountB,
signerWithProvider.address,
path,
getDeadline()
);
const signedTransaction = await signerWithProvider.sendTransaction(tx);