Skip to main content

Terra 2.0

feather.js

feather.js is a fork of terra.js with interchain capabilities. It can be used to interact with Cosmos blockchains like Terra within JavaScript runtimes, such as Node.js and the browser. To learn more about feather.js and installing the package, look here.

Setup

Getting started takes a few steps:

  • Importing feather.js and relevant classes
  • An LCDClient to connect to the Terra 2 blockchain
  • A MnemonicKey to sign and broadcast transactions
  • A wallet (inputs our MnemonicKey into our LCDClient)

_18
import { LCDClient, MnemonicKey, MsgExecuteContract, Coins, Fee } from '@terra-money/feather.js';
_18
_18
const lcd = new LCDClient({
_18
// key must be the chainID
_18
'pisco-1': {
_18
lcd: 'https://pisco-lcd.terra.dev', // testnet lcd
_18
chainID: 'pisco-1', // testnet chain-id
_18
gasAdjustment: 1.75,
_18
gasPrices: { uluna: 0.015 },
_18
prefix: 'terra', // bech32 prefix
_18
},
_18
});
_18
_18
const mk = new MnemonicKey({
_18
mnemonic: '',
_18
});
_18
_18
const wallet = lcd.wallet(mk);

Executing Messages

To execute a contract message, we create a new MsgExecuteContract object variable containing our wallet, the contract we are sending our message to, our message itself, and any Coins to send along with our transaction.

This object then gets passed into a tx variable which creates and signs the transaction. A custom fee variable can also be passed in.

Finally, our transaction gets broadcasted and the result is printed in the console.


_32
const contract_address = '<INSERT_CONTRACT_ADDRESS>'; // A terra address on pisco-1
_32
_32
const execute = new MsgExecuteContract(
_32
wallet.key.accAddress('terra'),
_32
contract_address,
_32
{
_32
"swap": {
_32
"offer_asset": {
_32
"info": {
_32
"native_token": {
_32
"denom": "uluna"
_32
}
_32
},
_32
"amount": "100000"
_32
}
_32
}
_32
}, // Example execute message
_32
new Coins({ uluna: '100000' }) // Coins to send with transaction
_32
);
_32
_32
// Broadcast transaction
_32
const fee = new Fee(500000, { uluna: 200000 }); //
_32
_32
const tx = await wallet.createAndSignTx({
_32
msgs: [execute],
_32
fee,
_32
chainID: 'pisco-1',
_32
});
_32
_32
const result = await lcd.tx.broadcast(tx, 'pisco-1');
_32
_32
console.log(result);

Querying Data

To query data from a contract, you just need to make an async call to the LCDClient containing the contract to query and the query itself. Note that you don't need a MnemonicKey or wallet to execute queries.


_8
const contract_address = '<INSERT_CONTRACT_ADDRESS>'; // A terra address on pisco-1
_8
_8
const query = await lcd.wasm.contractQuery(
_8
contract_address,
_8
{
_8
"config": {} // Example query
_8
}
_8
).then(result => { console.log(result) }); // Print result