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:
Leave My contributions to dhive library | How to wait for transaction status to:
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
- Help me design a decentralized version of hiveprojects.io
- Hive Projects update: 4 projects added, 202 listed in total!
- How to add missing authorities from a Ledger device
- Engrave witness update, HF28, Hive Ledger changes and 100k Power Up
- Low power local witness node
- Hive Projects update: 4 projects added, 198 listed in total!
- I've been voted into top20 just to be forced to vote on a Splinterlands proposal
- Hive Projects update: 4 projects added, 194 listed in total!
- The state of dBlog
- Hive Projects update: 3 projects added, 190 listed in total!