Back to Tutorials
Beginner15 min

Sending Your First Transaction

Transfer tokens between accounts on Beatoz

Sending Your First Transaction

In this tutorial, you'll learn how to send your first transaction on the Beatoz blockchain.

Prerequisites

Make sure you have completed the previous tutorials:

What You'll Need

  • Two Beatoz accounts (sender and receiver)
  • Some BTZ tokens in the sender account (get from Faucet)

Step 1: Set Up Your Environment

const { Web3, TrxProtoBuilder } = require('@beatoz/web3');
require('dotenv').config();

// Connect to Testnet
const web3 = new Web3('https://rpc-testnet0.beatoz.io');

// Your accounts
const senderPrivateKey = process.env.SENDER_PRIVATE_KEY;
const receiverAddress = '0x...receiver_address';

const sender = web3.beatoz.accounts.privateKeyToAccount(senderPrivateKey);
console.log('Sender:', sender.address);

Security

Never hardcode your private key. Always use environment variables.

Step 2: Check Sender Balance

Before sending, make sure you have enough balance:

async function checkBalance(address) {
  const account = await web3.beatoz.getAccount(address);
  return web3.utils.fromFons(account.value.balance, 'beatoz');
}

const balance = await checkBalance(sender.address);
console.log('Sender Balance:', balance, 'BTZ');

Step 3: Get Account Nonce

Every transaction needs a nonce (sequence number):

async function getNonce(address) {
  const account = await web3.beatoz.getAccount(address);
  return account.value.nonce;
}

const nonce = await getNonce(sender.address);
console.log('Current Nonce:', nonce);

Step 4: Build the Transaction

Testnet

Use TrxProtoBuilder to create a transfer transaction:

// Amount to send (in BTZ)
const amount = '1'; // 1 BTZ
const amountInFons = web3.utils.toFons(amount, 'beatoz');

// Get chain info
const status = await web3.beatoz.status();
const chainId = status.node_info.network;

// Build transaction
const tx = TrxProtoBuilder.buildTransferTrxProto({
  from: sender.address,
  to: receiverAddress,
  nonce,
  gas: 100000,
  gasPrice: '48000000000',
  amount: amountInFons,
});

Step 5: Sign the Transaction

Sign the transaction with your private key:

// Sign transaction. The chain ID is included in the signature domain.
TrxProtoBuilder.signTrxProto(tx, sender, chainId);
console.log('Transaction signed!');

Step 6: Broadcast the Transaction

Send the signed transaction to the network:

async function sendTransaction(tx) {
  const result = await web3.beatoz.broadcastTxCommit(tx);
  
  if (result.deliver_tx?.code && result.deliver_tx.code !== 0) {
    throw new Error(`Transaction failed: ${result.deliver_tx.log}`);
  }
  
  return result.hash;
}

const txHash = await sendTransaction(tx);
console.log('Transaction Hash:', txHash);

Transaction Sent!

Your transaction has been submitted to the network!

Step 7: Verify the Transaction

Wait for confirmation and verify:

async function waitForTransaction(hash, maxRetries = 10) {
  for (let i = 0; i < maxRetries; i++) {
    try {
      const tx = await web3.beatoz.tx(hash);
      return tx;
    } catch (error) {
      console.log(`Waiting for confirmation... (${i + 1}/${maxRetries})`);
      await new Promise(resolve => setTimeout(resolve, 2000));
    }
  }
  throw new Error('Transaction not found');
}

const confirmedTx = await waitForTransaction(txHash);
console.log('Transaction confirmed!');
console.log('Block Height:', confirmedTx.height);

Complete Example

Here's the full code:

const { Web3, TrxProtoBuilder } = require('@beatoz/web3');
require('dotenv').config();

async function main() {
  const web3 = new Web3('https://rpc-testnet0.beatoz.io');
  
  // Setup accounts
  const sender = web3.beatoz.accounts.privateKeyToAccount(
    process.env.SENDER_PRIVATE_KEY
  );
  const receiverAddress = process.env.RECEIVER_ADDRESS;
  
  // Get account info
  const account = await web3.beatoz.getAccount(sender.address);
  const status = await web3.beatoz.status();
  
  // Build transaction
  const tx = TrxProtoBuilder.buildTransferTrxProto({
    from: sender.address,
    to: receiverAddress,
    nonce: account.value.nonce,
    gas: 100000,
    gasPrice: '48000000000',
    amount: web3.utils.toFons('1', 'beatoz'), // 1 BTZ
  });
  
  // Sign and send
  TrxProtoBuilder.signTrxProto(tx, sender, status.node_info.network);
  const result = await web3.beatoz.broadcastTxCommit(tx);
  
  console.log('Transaction Hash:', result.hash);
}

main().catch(console.error);

Next Steps

Beatoz Developer Portal