var img = document.createElement('img'); img.src = "https://terradocs.matomo.cloud//piwik.php?idsite=1&rec=1&url=https://docs.terra.money" + location.pathname; img.style = "border:0"; img.alt = "tracker"; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(img,s);
Skip to main content

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:

  1. Set up your project
  2. Set up a Terra LCD (light client daemon)
  3. Create and connect a wallet
  4. Query a swap contract
  5. 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

1. Set up your project

  1. Create a new directory for your project:

_1
mkdir my-feather-js-project

  1. Enter your new project directory:

_1
cd my-feather-js-project

  1. Next, initialize npm, install the feather.js package, and create an index.js file to house the code:

_3
npm init -y
_3
npm install @terra-money/feather.js
_3
touch index.js

  1. Open the package.json file in a code editor and add 'type': 'module',.

_5
{
_5
// ...
_5
"type": "module"
_5
// ...
_5
}

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.

  1. Open your index.js file in a code editor and input the following to initialize the LCD:

_12
import { LCDClient } from '@terra-money/feather.js';
_12
_12
const lcd = new LCDClient({
_12
// key must be the chainID
_12
'pisco-1': {
_12
lcd: 'https://pisco-lcd.terra.dev',
_12
chainID: 'pisco-1',
_12
gasAdjustment: 1.75,
_12
gasPrices: { uluna: 0.015 },
_12
prefix: 'terra', // bech32 prefix, used by the LCD to understand which is the right chain to query
_12
},
_12
});

📝Switching to LocalTerra or the mainnet

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

  1. 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!

  2. 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 to testnet.

  3. Add the following code to your index.js file and input your mnemonic key:


_5
import { MnemonicKey } from '@terra-money/feather.js';
_5
const mk = new MnemonicKey({
_5
mnemonic: '//Input your 24-word mnemonic key here//',
_5
});
_5
const wallet = lcd.wallet(mk);

☢️Mnemonic security

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.

  1. 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.

  1. Add the following code to your index.js file. Make sure the contract address is correct.

_3
const pool = '<INSERT_POOL_ADDRESS>'; // A terraswap contract address on pisco.
_3
const { assets } = await lcd.wasm.contractQuery(pool, { pool: {} }); // Fetch the amount of each asset in the pool.
_3
const beliefPrice = (assets[0].amount / assets[1].amount).toFixed(18); // Calculate belief price using proportion of pool balances.

  1. Next, generate a message to broadcast to the network:

_20
import { MsgExecuteContract } from '@terra-money/feather.js';
_20
const terraSwap = new MsgExecuteContract(
_20
wallet.key.accAddress('terra'),
_20
pool,
_20
{
_20
swap: {
_20
max_spread: '0.001',
_20
offer_asset: {
_20
info: {
_20
native_token: {
_20
denom: 'uluna',
_20
},
_20
},
_20
amount: '100000',
_20
},
_20
belief_price: beliefPrice,
_20
},
_20
},
_20
new Coins({ uluna: '100000' }),
_20
);

6. Broadcast the transaction

  1. Add the following code to index.js to create, sign, and broadcast the transaction. It's important to specify uluna as the fee denomination because Luna is the only denomination the faucet sends.

_7
const tx = await wallet.createAndSignTx({
_7
msgs: [terraSwap],
_7
feeDenoms: ['uluna'],
_7
chainID: 'pisco-1',
_7
});
_7
const result = await lcd.tx.broadcast(tx, 'pisco-1');
_7
console.log(result);

  1. Run the code in your terminal:

_1
node index.js

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.