engrave avatar

My contributions to dhive library | How to wait for transaction status

engrave

Published: 20 Feb 2022 › Updated: 20 Feb 2022My contributions to dhive library | How to wait for transaction status

My contributions to dhive library | How to wait for transaction status

While working on Hive Ledger wallet I needed additional functionalities that were not available in @hiveio/dhive javascript library and decided that it's a good moment to improve it a bit.

Transaction Status API

A few months ago, when the Splinterlands game got traction and started onboarding masses, witnesses realized that the current implementation of the broadcast method is blocking and may cause network lags. The library was changed to use asynchronous call which helped a lot but projects that rely on a transaction status had to add additional code to handle it. I decided to add a handy method to support Transaction Status API.

How to wait for a transaction until it's included in a block

const dhive = require("@hiveio/dhive");
const client = new dhive.Client("https://api.hive.blog");

const key = dhive.PrivateKey.fromString('PRIVATE KEY HERE');

const op = ["transfer", {
    amount: "0.001 HIVE",
    from: "engrave",
    to: "engrave.cold",
    memo: ""
}];

(async () => {
    try {
        const {id} = await client.broadcast.sendOperations([op], key);
        console.log(`Transaction ID: ${id}`);

        let tx = null;

        do {
            tx = await client.transaction.findTransaction(id);
            console.log(`Transaction status: ${tx.status}`);
            await wait(500);
        } while (tx.status == 'within_mempool')

        if (tx.status == 'within_reversible_block') {
            console.log('Transaction confirmed');
        } else {
            throw new Error(`Transaction failed with status: ${tx.status}`);
        }
    } catch (err) {
        console.error(err);
    }
})();

const wait = async (ms) => {
    return new Promise(resolve => setTimeout(resolve, ms));
};

The main point it so pool the API until we get anything but within_mempool and throw an error when received anything else than within_reversible_block.

Account By Key API

This is not a very popular API and I might be the only one using it, but it is very useful for a wallet supporting hardware wallets. The main reason is that you can only get the public key from the device, and you cannot store the information about the account, so every time you connect the hardware wallet, you need to search for associated accounts.

How to search for an account by the Boldpublic key

const dhive = require("@hiveio/dhive");
const client = new dhive.Client("https://api.hive.blog");

(async () => {
    try {
        const result = await client.keys.getKeyReferences(['TST65PUAPA4yC4RgPtGgsPupxT6yJtMhmT5JHFdsT3uoCbR8WJ25s'])
        console.log(result) // Will print [["hiveio"]]
    } catch (err) {
        console.error(err);
    }
})();

Serializer fix

I also encountered a small bug for update_proposal operation and fixed it.


Support me with your witness vote! Click on the image below:

banner_engrave 100.png

Leave My contributions to dhive library | How to wait for transaction status to:

Written by

Witness & Hive powered blogging platform with custom domains and themes. Visit dblog.org to get more from Hive!

Read more #development posts


Best Posts From engrave

We have not curated any of engrave's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From engrave