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 which you can use to sign transactions.
_8import { MnemonicKey, LCDClient } from "@terra-money/terra.js";_8_8const mk = new MnemonicKey();_8const terra = new LCDClient({_8 URL: "https://pisco-lcd.terra.dev",_8 chainID: "pisco-1",_8});_8const wallet = terra.wallet(mk);
Create messages
_5import { MsgSend } from "@terra-money/terra.js";_5_5const send = new MsgSend(wallet.key.accAddress, "<random-terra-address>", {_5 uluna: 1000,_5});
Create and Sign Transaction
_4const tx = await wallet.createAndSignTx({_4 msgs: [send],_4 memo: "Hello",_4});
Broadcast transaction
_1const txResult = await terra.tx.broadcast(tx);
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.
_2// const syncTxResult = await terra.tx.broadcastSync(tx);_2// const asyncTxResult = await terra.tx.broadcastAsync(tx);
Check events
If you broadcasted the transaction with block, you can get the events emitted by your transaction.
_12import { isTxError } from "@terra-money/terra.js";_12_12const txResult = terra.tx.broadcast(tx);_12_12if (isTxError(txResult)) {_12 throw new Error(_12 `encountered an error while running the transaction: ${txResult.code} ${txResult.codespace}`_12 );_12}_12_12// check for events from the first message_12txResult.logs[0].eventsByType.store_code;