Quick start
In this guide, we will provide an example token swap of 0.1 VULPEFI for DAI at the best possible rate on the market.
This process includes:
TIP
This example uses Binance Chain network; however, it can be replicated on any network supported by VulpeFI Aggregation Protocol
Checking allowances
Step 1: Set Up Your Environment
First, import the necessary Node.js libraries and set up your environment variables.
const Web3 = require('web3'); // Import the Web3 library for interacting with Ethereum
const fetch = require('node-fetch'); // Import the fetch library for making HTTP requests
const chainId = 56; // The chain ID for the Binance Smart Chain (BSC)
const web3RpcUrl = 'https://bsc-dataseed.binance.org'; // The URL for the BSC node you want to connect to
const walletAddress = '0x...xxx'; // Set your wallet address (replace '0x...xxx' with your actual wallet address)
const privateKey = '0x...xxx'; // Set the private key of your wallet (replace '0x...xxx' with your actual private key). NEVER SHARE THIS WITH ANYONE!Step 2: Define Your Swap Parameters
Next, define the parameters for the swap you want to perform.
Step 3: Define API URLs and Initialize Web3 Libraries
Now, define the API URLs and initialize Web3. Web3 is a collection of libraries which allow you to interact with a local or remote Ethereum node, using HTTP, IPC, or WebSocket.
Step 4: Define Helper Functions
Define a series of helper functions that will be used to interact with the VulpFI API.
Step 5: Check Token Allowance
Use the checkAllowance function to check the current allowance of the token.
At this point, you'll have set up your environment, defined your swap parameters, defined your API endpoints, and checked the allowance for the token you are selling.
Creating the token allowance (approval) transaction
In order for the VulpFI aggregation protocol to access tokens in your wallet, you must create an approval transaction. This specifies that the VulpFI router is allowed to swap a specific amount of the token chosen.
CAUTION
Approval transactions require payment of a blockchain gas fee! This amount is deducted in the form of native tokens from your wallet.
Step 1:
Similar to the Allowance Check, you'll first need to set up your environment, define swap parameters and API endpoints, and initialize Web3 libraries.
Step 2: Implement Helper Functions
Implement helper functions to interact with the VulpFI API, including constructing the API request URL, broadcasting raw transactions, signing and sending transactions, and preparing approval transactions considering the gas limit.
Step 3: Build the Body of the Transaction
Prepare the transaction data for approval using the buildTxForApproveTradeWithRouter function and store it in transactionForSign.
Step 4: Confirm and Send the Transaction
Prompt the user to confirm the transaction using the yesno library. If confirmed, sign and send the transaction. Otherwise, exit without signing.
After running this code in the console, you should see something like this:
> Approve tx hash: 0xc313c133e203fe66b487e27ab0afde71842dc34ab97aca60c147c7662505312a2
With the transaction hash, you can monitor its execution using the blockchain explorer.
For the Binance Chain, you can use bscscan.com:
BROKEN LINK
Making the Swap
CAUTION
Before proceeding, please confirm that your approval transaction has a status of Success!
Step 1: Set Up Environment and Define Swap Parameters
Step 2: Implement Helper Functions
Step 3: Build the Body of the Transaction
Step 4: Confirm and Send the Transaction
After running this code in the console, you should see something like this:
> Swap tx hash: 0xd991e17cc2b33e6a244fb2a98deb83b4659f94cf867ef7730b614d1feaa7cf94
Let's check the result of the transaction on the explorer:
BROKEN LINK
Last updated