diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e34d61..1ca2b4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +4.1.0 / 2024-06-27 +================== + +* Add `onNewUnfinishedBlock()` method to full node api, requires a full node running 2.4.0 or greater. + 4.0.0 / 2023-12-29 ================== diff --git a/README.md b/README.md index a148b64..8e1d476 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ const { Connection, constants, ApiClient } = require('chia-api'); conn.onMessage((message) => { console.log(message); }); - conn.addService(constants.SERVICE().walletUi); + conn.ensureService(constants.makeServiceNames().walletUi); const fullNode = new ApiClient.FullNode({ connection: conn, origin: 'my-cool-service' }); await fullNode.init(); const blockchainState = await fullNode.getBlockchainState(); diff --git a/lib/api-client/base.js b/lib/api-client/base.js index ea4bf65..ba999c9 100644 --- a/lib/api-client/base.js +++ b/lib/api-client/base.js @@ -25,7 +25,7 @@ class Base { } async init() { - this.connection.addService(this.origin); + this.connection.ensureService(this.origin); if (!this.connection.connected) { await this.connection.connect(); } else { diff --git a/lib/api-client/daemon.js b/lib/api-client/daemon.js index b9a5961..5cb1961 100644 --- a/lib/api-client/daemon.js +++ b/lib/api-client/daemon.js @@ -1,10 +1,10 @@ const Base = require('./base'); const Message = require('../message'); -const { SERVICE } = require('../constants'); +const { makeServiceNames } = require('../constants'); class Daemon extends Base { get destination() { - return SERVICE(this.connection.coin).daemon; + return makeServiceNames(this.connection.coin).daemon; } async registerService(serviceName) { diff --git a/lib/api-client/farmer.js b/lib/api-client/farmer.js index 64aee0d..599280c 100644 --- a/lib/api-client/farmer.js +++ b/lib/api-client/farmer.js @@ -1,10 +1,10 @@ const Base = require('./base'); const Message = require('../message'); -const { SERVICE } = require('../constants'); +const { makeServiceNames } = require('../constants'); class Farmer extends Base { get destination() { - return SERVICE(this.connection.coin).farmer; + return makeServiceNames(this.connection.coin).farmer; } onNewFarmingInfo(cb) { diff --git a/lib/api-client/full-node.js b/lib/api-client/full-node.js index 7282e85..c3fe4d2 100644 --- a/lib/api-client/full-node.js +++ b/lib/api-client/full-node.js @@ -1,10 +1,10 @@ const Base = require('./base'); const Message = require('../message'); -const { SERVICE } = require('../constants'); +const { makeServiceNames } = require('../constants'); class FullNode extends Base { get destination() { - return SERVICE(this.connection.coin).fullNode; + return makeServiceNames(this.connection.coin).fullNode; } onNewBlockchainState(cb) { @@ -16,6 +16,16 @@ class FullNode extends Base { }); } + onNewUnfinishedBlock(cb) { + this.connection.ensureService(makeServiceNames(this.connection.coin).unfinishedBlockInfo) + this.connection.onMessage(message => { + if (message.command !== 'unfinished_block') { + return + } + cb(message.data) + }) + } + async getBlockchainState() { const res = await this.connection.send(new Message({ command: 'get_blockchain_state', diff --git a/lib/api-client/harvester.js b/lib/api-client/harvester.js index 8c8a606..69cd1dc 100644 --- a/lib/api-client/harvester.js +++ b/lib/api-client/harvester.js @@ -1,10 +1,10 @@ const Base = require('./base'); const Message = require('../message'); -const { SERVICE } = require('../constants'); +const { makeServiceNames } = require('../constants'); class Harvester extends Base { get destination() { - return SERVICE(this.connection.coin).harvester; + return makeServiceNames(this.connection.coin).harvester; } async getPlots() { diff --git a/lib/api-client/plotter.js b/lib/api-client/plotter.js index 0f7c145..7d149fb 100644 --- a/lib/api-client/plotter.js +++ b/lib/api-client/plotter.js @@ -1,13 +1,13 @@ const Base = require('./base'); -const { SERVICE } = require('../constants'); +const { makeServiceNames } = require('../constants'); class Plotter extends Base { get destination() { - return SERVICE(this.connection.coin).plotter; + return makeServiceNames(this.connection.coin).plotter; } async init() { - this.connection.addService(SERVICE(this.connection.coin).plotter); + this.connection.ensureService(makeServiceNames(this.connection.coin).plotter); if (!this.connection.connected) { await this.connection.connect(); } else { diff --git a/lib/api-client/wallet.js b/lib/api-client/wallet.js index f02b444..5abeca2 100644 --- a/lib/api-client/wallet.js +++ b/lib/api-client/wallet.js @@ -1,12 +1,12 @@ const Base = require('./base'); const Message = require('../message'); -const { SERVICE } = require('../constants'); +const { makeServiceNames } = require('../constants'); const BACKUP_HOST = 'https://backup.chia.net'; class Wallet extends Base { get destination() { - return SERVICE(this.connection.coin).wallet; + return makeServiceNames(this.connection.coin).wallet; } async getBalance({ walletId }) { diff --git a/lib/connection.js b/lib/connection.js index 4e01276..12b8003 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -23,7 +23,11 @@ class Connection { } addService(serviceName) { - this.services.set(serviceName, serviceName); + this.ensureService(serviceName) + } + + ensureService(serviceName) { + this.services.set(serviceName, serviceName) } async connect() { diff --git a/lib/constants.js b/lib/constants.js index 6d25fff..1a64272 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -1,4 +1,4 @@ -const SERVICE = (coin = 'chia') => { +const makeServiceNames = (coin = 'chia') => { return { daemon: 'daemon', fullNode: `${coin}_full_node`, @@ -7,8 +7,10 @@ const SERVICE = (coin = 'chia') => { wallet: `${coin}_wallet`, walletUi: 'wallet_ui', plotter: `${coin}_plotter`, - }; -}; + metrics: 'metrics', + unfinishedBlockInfo: 'unfinished_block_info', + } +} const SERVICE_TYPE = { fullNode: 1, harvester: 2, @@ -27,7 +29,8 @@ const PLOTTING_STATE = { }; module.exports = { - SERVICE, + SERVICE: makeServiceNames, + makeServiceNames, SERVICE_TYPE, PLOTTING_STATE, }; diff --git a/package.json b/package.json index a097647..76a84e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chia-api", - "version": "4.0.0", + "version": "4.1.0", "repository": "https://github.com/foxypool/chia-api.git", "bugs": "https://github.com/foxypool/chia-api/issues", "license": "GPL-3.0",