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

in HiveDevs3 years ago (edited)

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

Sort:  

Awesome keep up the good work, this is really amazing and a work well-done in the blockchain

Thanks :)

Great contribution as always!
What's the status of the Ledger App review ?

It passed the functional review and I'm waiting for security review. It is possible to install it via the Ledger Live already (by enabling developer mode) and hopefully, it will be officially released soon :)

Awesome, that will be a game changer!

Can't wait for that Ledger integration! Keep up with that bugfixing.

Good Job!