Sending transactions using InitiaJS is straightforward. First, we need to import the necessary classes and functions.

import { Wallet, LCDClient, MnemonicKey, MsgSend, Tx, WaitTxBroadcastResult } from '@initia/initia.js';

Next, we need to set up our environment variables. We will need the following:

  • MNEMONIC: The mnemonic of the account that will be sending the transaction.
  • LCD_URL: The URL of the Initia LCD.
  • GAS_PRICES: The gas prices for the transaction.
  • SENDER_ADDRESS: The address of the account that will be sending the transaction.
  • RECIPIENT_ADDRESS: The address of the account that will be receiving the transaction.
  • AMOUNT: The amount of tokens to send.
if (!process.env.MNEMONIC) {
  throw new Error('MNEMONIC environment variable is required');
}
const mnemonic = process.env.MNEMONIC;
const lcdUrl = process.env.LCD_URL || "https://lcd.testnet.initia.xyz";
const gasPrices = process.env.GAS_PRICES || "0.15move/944f8dd8dc49f96c25fea9849f16436dcfa6d564eec802f3ef7f8b3ea85368ff"; // Will be INIT for mainnet

const senderAddress = process.env.SENDER_ADDRESS || 'init1w4cqq6udjqtvl5xx0x6gjeyzgwtze8c05kysnu';
const recipientAddress = process.env.RECIPIENT_ADDRESS || 'init1w4cqq6udjqtvl5xx0x6gjeyzgwtze8c05kysnu';
const amount = process.env.AMOUNT || '1000uinit';

We can then use the use these variables to ultimately create the `wallet` client, as well as the `sendMsg` object. Since for this example we're making a token transfer, we'll use the `MsgSend` object. But the SDK provides many other message types for different actions related to staking, governance, and more.

```ts
const key: MnemonicKey = new MnemonicKey({ mnemonic }); // Create a key from the mnemonic
const lcd: LCDClient = new LCDClient(lcdUrl, { gasPrices });
const wallet: Wallet = new Wallet(lcd, key);

const sendMsg: MsgSend = new MsgSend(senderAddress, recipientAddress, amount);  

Finally, we can create the function to send the transaction.

  • We first create the transaction by calling wallet.createAndSignTx. This function takes in the raw message object, signs it using the wallet, and returns a signed transaction object.
  • We then broadcast the transaction by calling lcd.tx.broadcast. This function takes in the signed transaction object and broadcasts it to the network.
async function sendTransaction(): Promise<WaitTxBroadcastResult> {
  try {
    const signedTx: Tx = await wallet.createAndSignTx({
      msgs: [sendMsg],
      memo: 'memo',
    });
    
    const result: WaitTxBroadcastResult = await lcd.tx.broadcast(signedTx);
    console.log('Transaction successful');
    console.log('Transaction hash:', result.txhash);
    return result;
  } catch (error: unknown) {
    if (error instanceof Error) {
      console.error('Transaction failed:', error.message);
    } else {
      console.error('Transaction failed with an unknown error');
    }
    throw error;
  }
}

Finally, we can call the function to send the transaction.

sendTransaction()
  .then(() => process.exit(0))
  .catch(() => process.exit(1));

If you then run the script using npx ts-node src/sending-transactions.ts, you should see an output similar to the following:

Transaction successful
Transaction hash: 4F0B810D15FA7D6A2B9EC2B98B263B0A20E791A8DABCB549620445941B25C699

Full Example

src/sending-transactions.ts
  import { Wallet, LCDClient, MnemonicKey, MsgSend, Tx, WaitTxBroadcastResult } from '@initia/initia.js';

  const mnemonic = process.env.MNEMONIC;
  const lcdUrl = process.env.LCD_URL || "https://lcd.testnet.initia.xyz";
  const gasPrices = process.env.GAS_PRICES || "0.15move/944f8dd8dc49f96c25fea9849f16436dcfa6d564eec802f3ef7f8b3ea85368ff"; // Will be INIT for mainnet

  const senderAddress = process.env.SENDER_ADDRESS || 'init1w4cqq6udjqtvl5xx0x6gjeyzgwtze8c05kysnu';
  const recipientAddress = process.env.RECIPIENT_ADDRESS || 'init1w4cqq6udjqtvl5xx0x6gjeyzgwtze8c05kysnu';
  const amount = process.env.AMOUNT || '1000uinit';

  const key: MnemonicKey = new MnemonicKey({ mnemonic });
  const lcd: LCDClient = new LCDClient(lcdUrl, { gasPrices });
  const wallet: Wallet = new Wallet(lcd, key);
  
  const sendMsg: MsgSend = new MsgSend(senderAddress, recipientAddress, amount);

  async function sendTransaction(): Promise<WaitTxBroadcastResult> {
    try {
      const signedTx: Tx = await wallet.createAndSignTx({
        msgs: [sendMsg],
        memo: 'memo',
      });
      
      const result: WaitTxBroadcastResult = await lcd.tx.broadcast(signedTx);
      console.log('Transaction successful');
      console.log('Transaction hash:', result.txhash);
      return result;
    } catch (error: unknown) {
      if (error instanceof Error) {
        console.error('Transaction failed:', error.message);
      } else {
        console.error('Transaction failed with an unknown error');
      }
      throw error;
    }
  }

  sendTransaction()
    .then(() => process.exit(0))
    .catch(() => process.exit(1));