OneInch
The OneInch
class from @moonup/moon-api
provides a convenient way to interact with the 1inch API. This API allows you to perform various operations related to cryptocurrency swaps, approvals, and data retrieval.
Here's a detailed documentation for the OneInch
class:
Initialization
To use the OneInch
class, you need to create an instance of it with a configured HttpClient
. The HttpClient
is responsible for making HTTP requests to the 1inch API.
Here's an example of how to create an instance of the OneInch
class:
Class: OneInch
Methods
approveCallData(data: ApproveCallDataPayload, params?: RequestParams)
This method is used to get the call data for approving a token transfer.
Parameters:
data
: An object containing the token address and the amount to approve.params
: Optional request parameters.
Returns: A promise that resolves to the call data for approving the token transfer.
Example:
approveSpender(data: ApproveSpenderPayload, params?: RequestParams)
This method is used to approve a spender to transfer tokens on your behalf.
Parameters:
data
: An object containing the token address and the spender address.params
: Optional request parameters.
Returns: A promise that resolves to the transaction hash of the approval.
Example:
protocols(data: ProtocolsPayload, params?: RequestParams)
This method is used to get the list of supported protocols.
Parameters:
data
: An object containing the chain ID.params
: Optional request parameters.
Returns: A promise that resolves to the list of supported protocols.
Example:
quote(data: QuotePayload, params?: RequestParams)
This method is used to get a quote for a swap.
Parameters:
data
: An object containing the source token address, the destination token address, and the amount to swap.params
: Optional request parameters.
Returns: A promise that resolves to the quote for the swap.
Example:
swap(accountName: string, data: GetSwapDto, params?: RequestParams)
This method is used to perform a swap.
Parameters:
accountName
: The name of the account to use for the swap.data
: An object containing the swap details, such as the source token address, the destination token address, the amount to swap, and the slippage tolerance.params
: Optional request parameters.
Returns: A promise that resolves to the transaction hash of the swap.
Example:
tokens(data: TokensPayload, params?: RequestParams)
This method is used to get the list of supported tokens.
Parameters:
data
: An object containing the chain ID.params
: Optional request parameters.
Returns: A promise that resolves to the list of supported tokens.
Example:
Usage
To use the OneInch
class, you need to create an instance of it with a configured HttpClient
. The HttpClient
is responsible for making HTTP requests to the 1inch API.
You can then use the oneinch
instance to call the various methods of the OneInch
class. Make sure to handle any potential errors that may occur during the API calls using try-catch
blocks.
Last updated