Skip to main content

Check Transactions (Javascript)

In this step, let's write code to directly retrieve the transaction data created in the previous step.

Preview

The JavaScript code to fetch data looks like below. Replace the hash value of the transaction you created in line 9 and your wallet address in line 12. Modify and run the code, and you'll see the execution result on the right side after a short wait.

const { LCDClient } = require("@xpla/xpla.js");

const lcd = new LCDClient({
  chainID: 'cube_47-5',
  URL: 'https://cube-lcd.xpla.dev'
});

async function main() {
  const txInfo = await lcd.tx.txInfo("ECDBC35B66384DEE25987AA0DDAE8CF946D1F4907B04A0E77939988474013353"); // Transaction data
  console.log(JSON.stringify(txInfo, null, 2));

  const balance = await lcd.bank.balance("xpla1f6hcc3hpxyg8rst9d5sg393e5jaj7453t0xmz8"); // Balance details
  console.log(JSON.stringify(balance, null, 2));
}
main()

If you've understood the Preview Code, you can skip reading the following content and proceed to the next step.

Fetching Transaction Data with JavaScript

Let's use JavaScript code to fetch the transaction data created in the previous step.

Executing the Code

  1. Create an example-3.js file.

  2. Copy the code below and paste it into the example-3.js file, then save it.

    const { LCDClient } = require("@xpla/xpla.js");

    const lcd = new LCDClient({
    chainID: 'cube_47-5',
    URL: 'https://cube-lcd.xpla.dev'
    });

    async function main() {
    const txInfo = await lcd.tx.txInfo("ECDBC35B66384DEE25987AA0DDAE8CF946D1F4907B04A0E77939988474013353"); // Transaction data
    console.log(JSON.stringify(txInfo, null, 2));

    const balance = await lcd.bank.balance("xpla1f6hcc3hpxyg8rst9d5sg393e5jaj7453t0xmz8"); // Balance details
    console.log(JSON.stringify(balance, null, 2));
    }
    main()
  3. Replace ECDBC35B66384DEE25987AA0DDAE8CF946D1F4907B04A0E77939988474013353 in line 9 with the hash value of your created transaction. Also, replace xpla1f6hcc3hpxyg8rst9d5sg393e5jaj7453t0xmz8 in line 12 with your wallet address.

  4. Enter the command below in the VSCode Terminal.

    node example-3.js
  5. Check the results in the terminal. You'll see the transaction data and your wallet information.

Explanation

We're using the LCDClient's txInfo function to retrieve transaction data for the given hash value. When you examine the execution result, you'll find details like the height of the block where the transaction was created (Height), the transaction hash value (txhash), and the timestamp of transaction creation (timestamp). You can extract the necessary information from the result.

const txInfo = await lcd.tx.txInfo("ECDBC35B66384DEE25987AA0DDAE8CF946D1F4907B04A0E77939988474013353"); // Transaction data
console.log(JSON.stringify(txInfo, null, 2));

We're also using the LCDClient's balance function to fetch the balance of the provided address. Although you received 100 XPLA from the Faucet, you sent a lower amount in the transaction. Check if the amount is displayed correctly.

const balance = await lcd.bank.balance("xpla1f6hcc3hpxyg8rst9d5sg393e5jaj7453t0xmz8"); // Balance details
console.log(JSON.stringify(balance, null, 2));
info

Is the balance displaying a large number? That's because the balance is in aXPLA units. Remember, 1018 aXPLA is equivalent to 1 XPLA.

Compare your results with the Preview Code's outcome.

Wrapping Up

The results obtained through the code can also be verified in the XPLA Explorer. Now you have the ability to perform various tasks using code.

With this, you've learned how to create transactions on the XPLA blockchain and retrieve data!