Get started with feather.js
This is an in-depth guide on how to use the feather.js
SDK.
In this tutorial, you'll learn how to:
- Set up your project
- Set up a Terra LCD (light client daemon)
- Create and connect a wallet
- Query a swap contract
- Create, sign, and broadcast a transaction
By the end of this guide, you'll be able to execute a token swap from your application using feather.js.
Prerequisites
- npm and node.js
- Station browser extension
1. Set up your project
- Create a new directory for your project:
- Enter your new project directory:
- Next, initialize npm, install the
feather.js
package, and create anindex.js
file to house the code:
- Open the
package.json
file in a code editor and add'type': 'module',
.
2. Initialize the LCD
Terra’s LCD or Light Client Daemon allows users to connect to the blockchain, make queries, create wallets, and submit transactions. It's the main workhorse behind feather.js
.
- Open your
index.js
file in a code editor and input the following to initialize the LCD:
The previous code block shows how to connect to the pisco testnet. To connect to LocalTerra, change the URL
to http://localhost:1317
. To connect to the phoenix-1 mainnet for production, use https://phoenix-lcd.terra.dev
.
You will also need to change the chainID
from pisco-1
to localterra
or phoenix-1
.
3. Create a pisco testnet wallet
-
You'll need a wallet to sign and submit transactions. Create a new wallet using the Station extension. Be sure to save your mnemonic key!
-
After creating your wallet, you will need to set it to use the testnet. Click the gear icon in the extension and change the network from
mainnet
totestnet
. -
Add the following code to your
index.js
file and input your mnemonic key:
Although this tutorial has you input your mnemonic directly, this practice should be avoided in production.
For security reasons, it's better to store your mnemonic key data in your environment by using process.env.SECRET_MNEMONIC
or process.env.SECRET_PRIV_KEY
. This practice is more secure than a hard-coded string.
- Request testnet funds for your wallet by navigating to the Terra faucet and inputting your wallet address. You'll need these funds to perform swaps and pay for gas fees. Once the funds are in your wallet, you’re ready to move on to the next step.
4. Find a contract address
To find the contract address for a specific Terraswap pair, visit https://app.terraswap.io/.
5. Query a Terraswap contract and set up the transaction
Before you can perform a swap, you will need a belief price. You can calculate the belief price of one token by querying the proportion of the two pooled tokens. The belief price +/- the max_spread
is the range of possible acceptable prices for this swap.
- Add the following code to your
index.js
file. Make sure the contract address is correct.
- Next, generate a message to broadcast to the network:
6. Broadcast the transaction
- Add the following code to
index.js
to create, sign, and broadcast the transaction. It's important to specifyuluna
as the fee denomination becuase Luna is the only denomination the faucet sends.
- Run the code in your terminal:
If successful, you'll see a log of the successful transaction and some new tokens in your wallet.
And that's it! You can find other pool addresses here to call other swaps. Be sure to use the correct testnet or mainnet contract address.
More examples
View the Common examples section for more information on using feather.js.