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 ourMnemonicKey
into ourLCDClient
)
_18import { LCDClient, MnemonicKey, MsgExecuteContract, Coins, Fee } from '@terra-money/feather.js';_18_18const 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_18const mk = new MnemonicKey({_18 mnemonic: '',_18});_18_18const 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.
_32const contract_address = '<INSERT_CONTRACT_ADDRESS>'; // A terra address on pisco-1_32_32const 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_32const fee = new Fee(500000, { uluna: 200000 }); // _32_32const tx = await wallet.createAndSignTx({_32 msgs: [execute],_32 fee,_32 chainID: 'pisco-1',_32});_32_32const result = await lcd.tx.broadcast(tx, 'pisco-1');_32_32console.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.
_8const contract_address = '<INSERT_CONTRACT_ADDRESS>'; // A terra address on pisco-1_8_8const query = await lcd.wasm.contractQuery(_8 contract_address, _8 {_8 "config": {} // Example query_8 }_8).then(result => { console.log(result) }); // Print result