3️⃣Use methods of AKKA
There are many methods in AKKA object that help you implement an app for swapping tokens.
All methods defined in AKKA take an optional input which is an instance of RequestOptions
.
export type RequestOptions = {
signal?: AbortSignal;
};
You can use this to customize your request.
Additionally, all the methods introduced in this page will also throw an instance of HTTPError
if the request fails.
getSpender method
The getSpender
method takes one mandatory input. It is an instance of SpenderRequest
.
export interface SpenderRequest {
chainId: ChainId;
};
This method will return a Promise
of SpenderResponse
which contains an attribute named address, and if the requested chainId is supported by AKKA it would be the address of the contract of AKKA for that chain.
export type SpenderResponse = {
address?: string;
};
Example usage:
const response = await akka.getSpender({
chainId: ChainId.CORE,
});
console.log(response?.address);
getApproveTransaction method
This method takes as input an instance of ApproveTransactionRequest
.
export interface ApproveTransactionRequest {
chainId: ChainId;
tokenAddress: string;
amount?: string;
};
It contains the chainId
, the tokenAddress
and optionally the amount
to be approved. If the amount is not present, it'll default to maximum possible amount. The return value of this method is a Promise of type ApproveTransactionResponse
.
export type ApproveTransactionResponse = {
data: string;
gasPrice: string;
to: string;
value: string;
};
It contains the data required to send an approve transaction with the given amount and the given token to the AKKA contract. This data needs to be signed by user wallet before being sent into an actual transaction.
Example usage:
const response = await akka.getApproveTransaction({
chainId: ChainId.CORE,
tokenAddress: '0x6E35fF7aC8eEB825DdB155515eF612ADcD66BCbC',
amount: '1000000000000',
});
console.log(response.data);
console.log(response.gasPrice);
console.log(response.to);
console.log(response.value);
getAllowance method
This method takes as input an object of type AllowanceRequest
, which has the following interface.
export interface AllowanceRequest {
chainId: ChainId;
tokenAddress: string;
walletAddress: string;
};
The return value is a Promise of type AllowanceResponse
and contains the amount of the given token from the given address that is allowed to spend by the AKKA contract.
export type AllowanceResponse = {
allowance?: number;
};
Example usage:
const response = await akka.getAllowance({
chainId: ChainId.CORE,
tokenAddress: '0x40375C92d9FAf44d2f9db9Bd9ba41a3317a2404f',
walletAddress: '0x2222222222222222222222222222222222222222',
});
console.log(response?.allowance);
getTokens method
The input of this method needs to be of type TokensRequest
.
export interface TokensRequest {
chainId: ChainId;
};
When invoked, this method will return a Promise of type TokensResponse
.
export type TokensResponse = {
tokens: TokensJsonResponse;
};
export type TokensJsonResponse = {
[key: string]: Token;
};
This response contains the data of the tokens that are supported by the AKKA contract in the given chainId.
Example usage:
const response = await akka.getTokens({
chainId: ChainId.CORE,
});
console.log(response.tokens);
getQuote method
This method must be called with an input of type QuoteRequest
.
export interface QuoteRequest {
chainId: ChainId;
src: string;
dst: string;
amount: string;
includeTokensInfo?: boolean;
includeProtocols?: boolean;
};
src
is the token address which the user has and wants to swap, dst
is the token address which they want to take and amount
is the amount of src
they want to exchange.
The return value is a Promise of QuoteResponse
, looking like this:
export type QuoteResponse = {
toAmount: string;
fromToken?: Token;
toToken?: Token;
protocols?: RouteProtocols[];
};
export type RouteProtocols = RouteProtocol[];
export type RouteProtocol = RoutePair[];
export type RoutePair = {
name: string;
part: number;
fromTokenAddress: string;
toTokenAddress: string;
};
It will always contain the toAmount
parameter, which tells the amount of the dst
token that is received if the swap is done through the suggested route of AKKA. Optionally, if the includeTokensInfo
parameter is set to true
, the response would also contain the information of src
and dst
tokens in fromToken
and toToken
parameter. And likewise, if the includeProtocols
parameter is set to true
, the response will also contain the suggested routes for making the swap inside the protocols
parameter.
Example usage:
const response = await akka.getQuote({
chainId: ChainId.CORE,
src: '0x74c7b589209Ce095A219f8bDF941De117656CBd6',
dst: '0x40375C92d9FAf44d2f9db9Bd9ba41a3317a2404f',
amount: '10000000000000000',
});
console.log(response.toAmount);
console.log(response?.fromToken);
console.log(response?.toToken);
console.log(response?.protocols);
getSwap method
The input of this method is of type OnChainSwapRequest
, and the interface for it is given below:
export interface OnChainSwapRequest {
chainId: ChainId;
src: string;
dst: string;
amount: string;
from: string;
slippage: number;
};
The first 4 parameters are the same as the previous method. from
is the address of the wallet which holds the src
token, and slippage
is a number between 0 and 50 indicating the allowed percentage of slippage in received amount of dst
token.
The return value is an object with the following type:
type OneChainSwapTx = {
from: string;
to: string;
data: string;
value: string;
gas: number;
gasPrice: string;
};
export type OnChainSwapResponse = {
toAmount: string;
tx: OneChainSwapTx;
};
The toAmount
is the same as the previous method. The tx
parameter contains the required data for sending into a transaction, after being signed of course.
Example usage:
const response = await akka.getSwap({
chainId: ChainId.CORE,
src: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
dst: '0x6E35fF7aC8eEB825DdB155515eF612ADcD66BCbC',
amount: '2000000000000000000',
from: '0x2222222222222222222222222222222222222222',
slippage: 0.05,
});
console.log(response.toAmount);
console.log(response.tx);
Last updated