Module 8 - Building AI Asset Transfer dApp
In this Tutorial, we’ll be Building an AI dApp which can execute cross-chain Transactions based on Prompts. To view the CodeBase, Click on the Link at the Bottom.
Running the Application
-
Clone the Repository
-
Install the necessary Packages
-
Replace Your_OpenAI_API_KEY with your OpenAI API Key
-
Run on Localhost
Congratulations!! Your application has started running.
Understanding the Code
Run the following commands in your Terminal to Download the necessary Libraries required in our Project.
Getting OpenAI API Key
-
Visit OpenAI and click on Explore API. Make sure to Create an account on OpenAI before Proceeding with the Step.
-
It will redirect you here. Click on Start Building.
-
It will redirect you to OpenAI Platform. Click on API Keys Option in the left SideBar and Create your new API Key.
Now, Copy the API Key somewhere as you’ll need it afterwards.
Imports
Import the Downloaded Libararies by -
Extract Variable Function
The extractVariables
function is designed to extract Specific Variables from a given sentence using the OpenAI API. It identifies and extracts the sourceToken
, sourceChain
, desToken
, desChain
, and amount
from the sentence based on Predefined sets of Possible Values.
Parameters
sentence
(String): The input sentence from which the variables need to be extracted.
Returns
-
A Promise that resolves to an object containing the extracted variables in the following format -
Usage
Function Details
-
Initialize OpenAI Instance: The function initializes an instance of the OpenAI Client using the Provided API Key.
-
Call OpenAI API: The function sends a request to the OpenAI API’s
chat.completions.create
endpoint, instructing it to extract the required variables from the input sentence. It uses thegpt-3.5-turbo
model. -
Parse and Return Result: The function parses the JSON response from the OpenAI API and returns the extracted variables.
Example Output
Given the input sentence “Transfer 100 USDT from Holsky to Fuji in exchange for USDC.”, the function might return -
Note: For sake for simplicity we are limiting this application for transferring AFTT Tokens between Amoy, Fuji and Holsky only but you can extend it for other tokens and chains as well. For more details please visit here.
Getting the Quote
Nitro enables you to interact with the Voyager Contract and initiate cross-chain Token Transfers. The first step in this Process is to request a Quote, which provides you with essential details about the proposed Token Transfer.
To request a Quote, follow these steps -
-
Define the PATH_FINDER_API_URL: Set the PATH_FINDER_API_URL variable to the URL of the Pathfinder API for the Voyager testnet. This is where you will send your Quote request.
-
Create the
getQuote
Function: This function handles the Quote request. It uses theaxios
library to make an HTTP GET request to the Voyager Pathfinder API. -
Call the
getQuote
Function: Use this function to request a Quote by passing appropriate Parameters. In this repository, this function is called using a button.These Parameters Define the details of the Token Transfer you wish to Execute. Let’s Break down what each Parameter represents -
-
‘fromTokenAddress’: This should specify the address of the Token you want to Transfer from (the Source Token).
-
‘toTokenAddress’: Provide the Address of the Token you want to Transfer to (the Dstination Token).
-
‘amount’: Set the Amount of the Token you wish to Transfer.
-
‘fromTokenChainId’: This Parameter Represents the Chain ID of the Source Blockchain. In this Case, it’s Set to “80001.”
-
‘toTokenChainId’: Similarly, this Parameter Specifies the Chain ID of the Destination Blockchain, which, in this example, is “43113” (Fuji).
-
‘widgetId’: This Parameter is used to identify the Widget Responsible for the Transfer. You’ll typically need to Obtain a Unique Widget ID through Contact with the Voyager Team, often via Telegram or other means. For now, let’s keep it as 0.
With these Parameters, you can now Call the getQuote Function with this params Object to initiate a Quote Request for your Specific Token Transfer.
-
Response
The getQuote
Function returns the Quote Data, which typically includes Details about the Token Transfer, such as Source and Destination Chains, Token Amount, Fees, and other Relevant Information. Click the Get Quote Button and go to console to see the Quote Data printed on console.
Check and Set Allowance
In Step 2 of using Router Nitro, you’ll Verify and Configure the Allowance for Token Transfers. This Process allows Router’s swap or Transfer Contract to safely move Tokens on your Behalf between Blockchain Networks.
-
Define ERC20 Contract ABI: We begin by Defining the ABI (Application Binary Interface) for ERC20 Tokens, specifically focusing on the “approve” and “allowance” Functions. This ABI is essential for interacting with ERC20 Token Contracts.
-
The checkAndSetAllowance Function: This Function Checks your Current Allowance and, if necessary, Sets a new Allowance. It first Checks if the Token is the Native Token (ETH), in which case no approval is needed.
-
Creating an ERC20 Contract: Using the Provided Token Address and the ERC20 ABI, we Create an Instance of the ERC20 Contract. This Contract represents the Token you want to Set an Allowance for.
-
Checking Current Allowance: We Retrieve your Current Allowance for the Token. The Allowance is the Maximum Amount the Voyager System (or any other address) can Withdraw from your Wallet.
-
Setting the Allowance: If the Current Allowance is less than the Desired Amount, we Proceed to set a new Allowance. We initiate an Approval Transaction to the ERC20 Contract, Granting Permission to Router’s Swap or Transfer Contract to Withdraw tokens on your Behalf.
Executing the Transaction
This Function is responsible for actually Executing the Transaction. It takes in the following Parameters -
- params: Parameters required for the Transaction, which should include the Source and Destination Token Addresses, Slippage Tolerance, Sender and Receiver Addresses, and the Widget ID.
- quoteData: Quote Data Obtained from Step 1.
When the Button is Clicked, It Performs the following tasks using the Function defined -
Create an Input Field
Now, create an Input field to enter the Prompt and the extract the variables from the Prompt by creating the following objects -
When the variables are extracted, the variables are mapped to the fields in the above Objects to get the Data in terms of Token Addresses and Chain IDs.
Submit Button
Next, we need to Create a Button on Click of which the following tasks take place.
- Extracting the Variables
- Getting the Quote
- Checking and Setting Allowance
- Executing the Transaction**
Check out the actual Code Implementation in the following Repository by Clicking here.