A powerful and flexible Hive automation library.
WorkerBee, based on wax
, provides a simple yet powerful interface to interact with the Hive blockchain, allowing you to build sophisticated bots and automation scripts with ease.
- No More Endless Loops! π Just say what you want to listen to - WorkerBee does all the waiting and event handling behind the scenes.
- No Blockchain Headaches! π§© Forget complicated APIs and coding tricks. Write logic much like you would on regular web apps (think: βWhen a new post appears by Alice, send me a ping!β).
- Keep Your Sandbox Clean π§Ό WorkerBee shields your code from the messy details of blockchain data, so your app stays flexible and easy-to-maintain.
- One Interface, Many Sources ποΈ Switch from live blockchain data to a historical database or new data source (e.g. SQL) - all without changing your appβs logic!
- Easy to Expand π Start simple but add new events, rules, or channels as your needs grow.
- Fully typed βΉοΈ Library APIs have well defined types and functional interfaces with special support for IDE IntelliSense
You can find the high-level documentation with snippets for this library at https://hive.pages.syncad.com/workerbee-doc
This is a Node.js module available through the npm registry.
Before installing, download and install Node.js. Node.js 20 or higher is required.
Installation is done using the
npm install
command:
npm install @hiveio/workerbee
If you want to use development versions of our packages, set @hiveio
scope to use our GitLab registry:
echo @hiveio:registry=https://gitlab.syncad.com/api/v4/groups/136/-/packages/npm/ >> .npmrc
npm install @hiveio/workerbee
Here's a simple example of how to start listening for new blocks on the Hive blockchain:
import WorkerBee from "@hiveio/workerbee";
const bot = new WorkerBee();
await bot.start();
console.log("π Bot started! Waiting for new blocks...");
for await (const { id, number } of bot) {
console.log(`π Got block #${number} with ID: ${id}`);
}
WorkerBee offers a rich set of observers to monitor various activities on the Hive blockchain. Here are some common use cases:
You can use an observer to wait for the next block:
import WorkerBee from "@hiveio/workerbee";
const bot = new WorkerBee();
await bot.start();
console.log(`β³ Waiting for new blocks...`);
bot.observe.onBlock().subscribe({
next(data) {
console.log(`π¦ Block #${data.block.number} detected!`);
}
});
Monitor a specific account for new operations:
import WorkerBee from "@hiveio/workerbee";
const bot = new WorkerBee();
await bot.start();
console.log("π Listening for operations from 'gtg'...");
bot.observe.onImpactedAccounts("gtg").subscribe({
next(data) {
console.log("π₯ 'gtg' was involved in a new transaction!");
console.log(data.impactedAccounts["gtg"]);
}
});
Get notified when large transfers occur:
import WorkerBee from "@hiveio/workerbee";
const bot = new WorkerBee();
await bot.start();
// Set a threshold for what you consider a "whale" transfer (e.g., 1000 HIVE)
console.log(`π Watching for transfers larger than 1000 HIVE...`);
const amount = bot.chain!.hiveCoins(1000);
bot.observe.onWhaleAlert(amount).subscribe({
next(data) {
data.whaleOperations.forEach(({ operation }) => {
console.log(
bot.chain!.waxify`π¨ Whale Alert! ${operation.from} sent ${operation.amount} to ${operation.to}`
);
});
}
});
You can also process data from a specific range of past blocks. Here's how to find all posts by a specific author in a given block range:
import WorkerBee from "@hiveio/workerbee";
const bot = new WorkerBee();
await bot.start();
const author = "gtg";
const startBlock = 96549390;
const endBlock = 96549415;
console.log(`π Searching for posts by @${author} from block ${startBlock} to ${endBlock}...`);
bot.providePastOperations(startBlock, endBlock)
.onPosts(author)
.subscribe({
next(data) {
data.posts[author]?.forEach(({ operation }) => {
console.log(`βοΈ Found post: @${operation.author}/${operation.permlink}`);
});
},
error: console.error,
complete() {
console.log("β
Search complete.");
}
});
Important
providePastOperations
provides past data to the bot, allowing you to start processing historical operations and directly switch to the live data without losing any context.
You can subscribe to multiple events, and the observer will trigger when any of the specified conditions are met. This is useful for monitoring multiple accounts or operations without needing to write complex logic.
When multiple conditions are met at the same time with OR, WorkerBee is smart enough to only trigger the event once, preventing duplicate notifications. Moreover if multiple events occur in the same notification cycle, they will be processed together, ensuring that your logic can account for all relevant changes at once.
WorkerBee by default uses (implicit) OR between filters:
import WorkerBee from "@hiveio/workerbee";
const bot = new WorkerBee();
await bot.start();
bot.observe
.onImpactedAccounts("alice", "bob", "charlie")
.onPostsWithTags("gtg", "blocktrades")
.subscribe({
next(data) {
// This will trigger if any of the accounts have activity OR if posts are detected with specific author
console.log(data);
},
error: console.error
});
We can also add explicit OR between them:
import WorkerBee from "@hiveio/workerbee";
const bot = new WorkerBee();
await bot.start();
bot.observe
.onImpactedAccounts("alice", "bob", "charlie")
+ .or
.onPostsWithTags("gtg", "blocktrades")
.subscribe({
next(data) {
// This will trigger if any of the accounts have activity OR if posts are detected with specific author
console.log(data);
},
error: console.error
});
Yyou can also combine multiple observers using the .and
operator to create more complex conditions, where you can react to any one of several events occurring. The resulting observer will fire if all of the chained conditions are met.
Here's an example of how to get notified when either a specific account's manabar is full AND a new account is created:
import { EManabarType } from "@hiveio/wax";
import WorkerBee from "@hiveio/workerbee";
const bot = new WorkerBee();
await bot.start();
console.log("π Watching for 'initminer' to have a full RC manabar and for new account...");
bot.observe
.onAccountsFullManabar(EManabarType.RC, "initminer")
.and
.onNewAccount()
.subscribe({
next(data) {
console.log("π 'initminer' now has a full RC manabar!");
data.newAccounts.forEach(({ accountName }) => {
console.log(`π€ New account created: @${accountName}`);
});
},
error: console.error
});
Note
AND takes precedence over OR, so you can chain multiple .and
calls to create complex conditions
import { EManabarType } from "@hiveio/wax";
import WorkerBee from "@hiveio/workerbee";
const bot = new WorkerBee();
await bot.start();
bot.observe
.onAccountsFullManabar(EManabarType.RC, "initminer")
.or
.onAccountsBalanceChange(false, "initminer")
.and
.onNewAccount()
.subscribe({
next() {
console.log("π 'initminer' now has a full RC manabar or balance changed!");
console.log("π€ Also someone created a new account!");
},
error: console.error
});
Here's how you can create, sign, and broadcast a transaction:
import WorkerBee from "@hiveio/workerbee";
const bot = new WorkerBee();
await bot.start();
console.log("π‘ Broadcasting transaction...");
await bot.broadcast(transaction);
console.log("β
Transaction confirmed by the node!");
This will send the transaction to the Hive network and wait for confirmation from the node (transaction applied in the next block).
WorkerBee provides a set of predefined filter categories to help you easily subscribe to specific types of operations.
You can check the full list of available categories in the docs/predefined_filter_categories.md.
For a detailed API definition, please see our Wiki.
Clone the repository and install the dependencies:
git clone https://gitlab.syncad.com/hive/workerbee.git
cd workerbee
corepack enable
pnpm install
Compile the TypeScript source code:
pnpm build
Execute the test suite:
pnpm test
This project is licensed under the MIT License. See the LICENSE.md file for details.