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

Transactions

This document explains how to influence the blockchain's state by broadcasting transactions.

Transactions include:

  • A list of messages.
  • An optional memo.
  • A fee.
  • A signature from a key.

The messages included in a transaction contain the information that will be routed to a proper message handler in the node, which in turn parses the inputs and determines the next state of the blockchain.

Create transactions

Create a wallet

You will first want to create a wallet that you can use to sign transactions.

import { MnemonicKey, LCDClient } from '@terra-money/feather.js';

const mk = new MnemonicKey();
const terra = new LCDClient({
URL: 'https://pisco-lcd.terra.dev',
chainID: 'pisco-1',
});
const wallet = terra.wallet(mk);
Copy

Create messages

import { MsgSend } from '@terra-money/feather.js';

const send = new MsgSend(
wallet.key.accAddress('terra'),
'<random-terra-address>',
{
uluna: 1000,
},
);
Copy

Create and Sign Transaction

const tx = await wallet.createAndSignTx({
msgs: [send],
memo: 'Hello',
chainID: 'pisco-1',
});
Copy

Broadcast transaction

const txResult = await terra.tx.broadcast(tx, 'pisco-1');
Copy

The default broadcast mode is block, which waits until the transaction has been included in a block. This will give you the most information about the transaction, including events and errors while processing.

You can also use sync or async broadcast modes.

// const syncTxResult = await terra.tx.broadcastSync(tx, 'pisco-1');
// const asyncTxResult = await terra.tx.broadcastAsync(tx, 'pisco-1');
Copy

Check events

If you broadcasted the transaction with block, you can get the events emitted by your transaction.

import { isTxError } from '@terra-money/feather.js';

const txResult = terra.tx.broadcast(tx, 'pisco-1');

if (isTxError(txResult)) {
throw new Error(
`encountered an error while running the transaction: ${txResult.code} ${txResult.codespace}`,
);
}

// check for events from the first message
txResult.logs[0].eventsByType.store_code;
Copy