The following code example shows how to initialize the LCDClient for one or multiple chains. The rest of the examples assume that you have initialized the LCDClient using this example or similar code.
import{
MsgSend,
MnemonicKey,
Coins,
LCDClient,
}from'@terra-money/feather.js';
const lcd =newLCDClient({
'pisco-1':{
chainID:'pisco-1',
lcd:'https://pisco-lcd.terra.dev',
gasAdjustment:1.75,
gasPrices:{
uluna:0.015,
},
prefix:'terra',// bech32 prefix, used by the LCD to understand which is the right chain to query
},
});
To configure the LCD for multiple chains, pass an object with multiple keys, each key being the chainID.
const lcd =newLCDClient({
'osmosis-1':{
chainID:'osmosis-1',
lcd:'https://lcd.osmosis.zone',
gasAdjustment:1.75,
gasPrices:{
uosmo:0.025,
},
prefix:'osmo',
},
'phoenix-1':{
chainID:'phoenix-1',
lcd:'https://phoenix-lcd.terra.dev',
gasAdjustment:1.75,
gasPrices:{
uluna:0.015,
},
prefix:'terra',
},
});
You can also use the default configuration if you only want to connect to Terra.
const lcd = LCDClient.fromDefaultConfig('testnet');// connect to testnet
const lcd = LCDClient.fromDefaultConfig('mainnet');// connect to mainnet
'satisfy adjust timber high purchase tuition stool faith fine install that you unaware feed domain license impose boss human eager hat rent enjoy dawn',
});
const wallet = lcd.wallet(mk);
// Transfer 1 Luna.
const send =newMsgSend(
wallet.key.accAddress('terra'),// requires prefix as a parameter
The following code example shows how to send CW20 tokens:
import{
LCDClient,
MnemonicKey,
MsgExecuteContract,
}from'@terra-money/feather.js';
// const lcd = new LCDClient(...);
const mk =newMnemonicKey({
mnemonic:
'satisfy adjust timber high purchase tuition stool faith fine install that you unaware feed domain license impose boss human eager hat rent enjoy dawn',
});
const wallet = lcd.wallet(mk);
// Specify token address of desired token to send.
const tokenAddress ='<INSERT_TOKEN_ADDRESS>';
// Transfer 1 token.
const cw20Send =newMsgExecuteContract(
wallet.key.accAddress('terra'),
tokenAddress,
{
transfer:{
amount:'1000000',
recipient: wallet.key.accAddress('terra'),
},
},
);
const tx =await wallet.createAndSignTx({
msgs: [cw20Send],
chainID:'pisco-1',
});
const result =await lcd.tx.broadcast(tx,'pisco-1');
console.log(result);
Swapping a native Terra asset for a CW20 token using Terraswap​
The following code example shows how to swap a native asset for CW20 using Terraswap.
Run this example on mainnet.
import{
MsgExecuteContract,
MnemonicKey,
Coins,
LCDClient,
}from'@terra-money/feather.js';
// const lcd = new LCDClient(...);
const mk =newMnemonicKey({
mnemonic:
'satisfy adjust timber high purchase tuition stool faith fine install that you unaware feed domain license impose boss human eager hat rent enjoy dawn',
To validate bech32 encoded validator addresses, pubkey, etc. you can use the specific namespace:
ValAddress: for *valoper... prefixed validator operator address
ValConsAddress: for *valcons... prefixed validator consensus address
AccPubKey: for *pub... prefixed account public key
ValPubKey: for *valoperpub... prefixed validator public key
Avoid Status 500: timed out waiting for tx to be included in a block​
Occasionally, the broadcast function of feather.js and terra.py will throw the error Status 500: timed out waiting for tx to be included in a block. This occurs because the libraries use broadcast-mode = block by default, which causes the LCD (to which you are broadcasting the transaction) to send an http response to your request only when the transaction has been included in a block. If the chain is overloaded, the confirmation may take too long and trigger a timeout in the LCD.
To solve this problem, use broadcast-mode = sync and then iterate a request to the LCD with the txhash to ensure that it has been included in a block.