From e992d01febad9222b59c42af99ad50bccd0383dd Mon Sep 17 00:00:00 2001 From: Nohyun Nehemiah Kwak Date: Tue, 29 Nov 2022 00:45:02 +0900 Subject: [PATCH 1/4] Add getRewards API --- packages/caver-rpc/src/klay.js | 81 ++++++++++++++++++++++++ rpc.json | 7 ++ test/packages/caver.rpc.js | 18 ++++++ types/packages/caver-klay/src/index.d.ts | 3 + types/packages/caver-rpc/src/klay.d.ts | 3 + types/packages/caver-rtm/src/index.d.ts | 12 ++++ 6 files changed, 124 insertions(+) diff --git a/packages/caver-rpc/src/klay.js b/packages/caver-rpc/src/klay.js index 44d368d4..532c7ee2 100644 --- a/packages/caver-rpc/src/klay.js +++ b/packages/caver-rpc/src/klay.js @@ -150,6 +150,87 @@ class Klay { params: 1, inputFormatter: [formatters.inputBlockNumberFormatter], }), + /** + * An object for reward distribution from Klaytn. + * + * @example + * + * @typedef {object} Klay.Rewards + * @property {number} minted - The amount minted. + * @property {number} totalFee - Total tx fee spent. + * @property {number} burntFee - The amount burnt. + * @property {number} proposer - The amount for the block proposer. + * @property {number} stakers - Total amount for stakers. + * @property {number} kgf - The amount for KGF. + * @property {number} kir - The amount for KIR. + * @property {object} rewards - A mapping from reward recipient addresses to reward amounts. + */ + /** + * Returns the reward distribution result about a block. + * If parameter is hex string, this will use {@link Klay#getRewardsByHash|caver.rpc.klay.getRewardsByHash}, if paramter is number type, this will use {@link Klay#getRewardsByNumber|caver.rpc.klay.getRewardsByNumber}. + * + * @memberof Klay + * @method getRewards + * @instance + * + * @example + * // Use `caver.rpc.klay.getRewardsByNumber` + * const result = await caver.rpc.klay.getRewards(0) + * // Use `caver.rpc.klay.getRewardsByHash` + * const result = await caver.rpc.klay.getRewards('0x58482921af951cf42a069436ac9338de50fd963bdbea40e396f416f9ac96a08b') + * + * @param {string|number|BN|BigNumber} blockHashOrNumber The block hash or block number to query block header. + * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. + * @return {Promise} An object includes reward distribution result. + */ + new Method({ + name: 'getRewards', + call: 'klay_getRewardsByNumber', + hexCall: 'klay_getRewardsByHash', + params: 1, + inputFormatter: [formatters.inputBlockNumberFormatter], + }), + /** + * Returns the reward distribution result about a block by block number. + * + * @memberof Klay + * @method getRewardsByNumber + * @instance + * + * @example + * const result = await caver.rpc.klay.getRewardsByNumber(0) + * const result = await caver.rpc.klay.getRewardsByNumber('latest') + * + * @param {string|number|BN|BigNumber} blockNumberOrTag The block number or block tag string to query block header. + * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. + * @return {Promise} An object includes reward distribution result. + */ + new Method({ + name: 'getRewardsByNumber', + call: 'klay_getRewardsByNumber', + params: 1, + inputFormatter: [formatters.inputBlockNumberFormatter], + }), + /** + * Returns the reward distribution result about a block by block hash. + * + * @memberof Klay + * @method getRewardsByHash + * @instance + * + * @example + * const result = await caver.rpc.klay.getRewardsByHash('0x58482921af951cf42a069436ac9338de50fd963bdbea40e396f416f9ac96a08b') + * + * @param {string} blockHash The block hash to query the reward distribution result about a block. + * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. + * @return {Promise} An object includes reward distribution result. + */ + new Method({ + name: 'getRewardsByHash', + call: 'klay_getRewardsByHash', + params: 1, + inputFormatter: [formatters.inputBlockNumberFormatter], + }), /** * An object defines the AccountKeyLegacy. * diff --git a/rpc.json b/rpc.json index cf76418e..db04b459 100644 --- a/rpc.json +++ b/rpc.json @@ -145,6 +145,13 @@ "params": 1, "inputFormatter": ["inputBlockNumberFormatter"] }, + { + "name": "getRewards", + "call": "klay_getRewardsByNumber", + "hexCall": "klay_getRewardsByHash", + "params": 1, + "inputFormatter": ["inputBlockNumberFormatter"] + }, { "name": "getBlockTransactionCount", "call": "klay_getBlockTransactionCountByNumber", diff --git a/test/packages/caver.rpc.js b/test/packages/caver.rpc.js index fbe3358b..2e0c0263 100644 --- a/test/packages/caver.rpc.js +++ b/test/packages/caver.rpc.js @@ -674,6 +674,24 @@ describe('caver.rpc.klay', () => { await caver.rpc.klay.getHeader('0x489ef4696baa7f5c9548cb4affa1b969a5b18de221b0cc0ed2483a1b2f84ac69') }).timeout(100000) }) + + context('caver.rpc.klay.getRewards', () => { + it('CAVERJS-UNIT-RPC-030: caver.rpc.klay.getRewards should call correct RPC call depends on param type', async () => { + // Have to call klay_getHeaderByHash with hex string param + sandbox.stub(caver.rpc.klay._requestManager, 'send').callsFake((data, callback) => { + expect(data.params.length).to.equal(1) + if (caver.utils.isValidHash(data.params[0])) { + expect(data.method).to.equal('klay_getHeaderByHash') + } else { + expect(data.method).to.equal('klay_getHeaderByNumber') + } + callback(undefined, {}) + }) + await caver.rpc.klay.getRewards('latest') + await caver.rpc.klay.getRewards(0) + await caver.rpc.klay.getRewards('0x489ef4696baa7f5c9548cb4affa1b969a5b18de221b0cc0ed2483a1b2f84ac69') + }).timeout(100000) + }) }) describe('caver.rpc.governance', () => { diff --git a/types/packages/caver-klay/src/index.d.ts b/types/packages/caver-klay/src/index.d.ts index 02d83605..a0ae11a4 100644 --- a/types/packages/caver-klay/src/index.d.ts +++ b/types/packages/caver-klay/src/index.d.ts @@ -85,6 +85,9 @@ export class DeprecatedKlayRPC { getHeaderByNumber: RpcCallToMethod['klay_getHeaderByNumber'] getHeaderByHash: RpcCallToMethod['klay_getHeaderByHash'] getBlockReceipts: RpcCallToMethod['klay_getBlockReceipts'] + getRewards: RpcCallToMethod['klay_getRewards'] + getRewardsByNumber: RpcCallToMethod['klay_getRewardsByNumber'] + getRewardsByHash: RpcCallToMethod['klay_getRewardsByHash'] getBlockWithConsensusInfo: RpcCallToMethod['klay_getBlockWithConsensusInfo'] getBlockWithConsensusInfoByNumber: RpcCallToMethod['klay_getBlockWithConsensusInfoByNumber'] getBlockWithConsensusInfoByHash: RpcCallToMethod['klay_getBlockWithConsensusInfoByHash'] diff --git a/types/packages/caver-rpc/src/klay.d.ts b/types/packages/caver-rpc/src/klay.d.ts index bd31f5b9..d07f5c44 100644 --- a/types/packages/caver-rpc/src/klay.d.ts +++ b/types/packages/caver-rpc/src/klay.d.ts @@ -133,6 +133,9 @@ export class Klay { getHeader(blockNumber: BlockNumber, callback?: (error: Error, result: Header) => void): Promise
getHeaderByNumber(blockNumber: BlockNumber, callback?: (error: Error, result: Header) => void): Promise
getHeaderByHash(blockHash: string, callback?: (error: Error, result: Header) => void): Promise
+ getRewards(blockNumber: BlockNumber, callback?: (error: Error, result: Header) => void): Promise + getRewardsByNumber(blockNumber: BlockNumber, callback?: (error: Error, result: Header) => void): Promise + getRewardsByHash(blockHash: string, callback?: (error: Error, result: Header) => void): Promise getBlockReceipts(blockHash: string, callback?: (error: Error, result: TransactionReceipt[]) => void): Promise getBlockTransactionCount(blockNumber: BlockNumber, callback?: (error: Error, result: string) => void): Promise getBlockTransactionCountByNumber( diff --git a/types/packages/caver-rtm/src/index.d.ts b/types/packages/caver-rtm/src/index.d.ts index c638b276..1d5fa3bd 100644 --- a/types/packages/caver-rtm/src/index.d.ts +++ b/types/packages/caver-rtm/src/index.d.ts @@ -125,6 +125,18 @@ export default class RpcCallToMethod { blockHash: string, callback?: (error: Error, result: Header) => void ): Promise
+ klay_getRewards( + blockNumberOrHash: BlockNumber | string, + callback?: (error: Error, result: Header) => void + ): Promise + klay_getRewardsByNumber( + blockNumber: BlockNumber, + callback?: (error: Error, result: Header) => void + ): Promise + klay_getRewardsByHash( + blockHash: string, + callback?: (error: Error, result: Header) => void + ): Promise klay_getTransactionFromBlock( blockNumber: BlockNumber, index: number, From 2c97353bc2258646ba209b5a210b65da20e514b7 Mon Sep 17 00:00:00 2001 From: Nohyun Nehemiah Kwak Date: Tue, 29 Nov 2022 01:25:16 +0900 Subject: [PATCH 2/4] Debug testcase --- types/packages/caver-core/src/index.d.ts | 11 +++++++++++ types/packages/caver-rpc/src/klay.d.ts | 1 + types/packages/caver-rtm/src/index.d.ts | 1 + 3 files changed, 13 insertions(+) diff --git a/types/packages/caver-core/src/index.d.ts b/types/packages/caver-core/src/index.d.ts index 9459e765..8309eae4 100644 --- a/types/packages/caver-core/src/index.d.ts +++ b/types/packages/caver-core/src/index.d.ts @@ -46,6 +46,17 @@ export interface Header { baseFeePerGas?: string } +export interface Rewards { + minted: string + totalFee: string + burntFee: string + proposer: string + stakers: string + kgf: string + kir: string + rewards: any +} + export interface Block { blockscore: string extraData: string diff --git a/types/packages/caver-rpc/src/klay.d.ts b/types/packages/caver-rpc/src/klay.d.ts index d07f5c44..366bbfa6 100644 --- a/types/packages/caver-rpc/src/klay.d.ts +++ b/types/packages/caver-rpc/src/klay.d.ts @@ -22,6 +22,7 @@ import { AccountKey } from '../../caver-account/src' import { Block, Header, + Rewards, BlockNumber, TransactionReceipt, BlockWithConsensusInfo, diff --git a/types/packages/caver-rtm/src/index.d.ts b/types/packages/caver-rtm/src/index.d.ts index 1d5fa3bd..cbbcaa6d 100644 --- a/types/packages/caver-rtm/src/index.d.ts +++ b/types/packages/caver-rtm/src/index.d.ts @@ -17,6 +17,7 @@ import BN = require('bn.js') import BigNumber from 'bignumber.js' import { Header, + Rewards, Block, BlockNumber, TransactionReceipt, From 534b268ab3d294cfa13b6d3313c72b037a87c099 Mon Sep 17 00:00:00 2001 From: Nohyun Nehemiah Kwak Date: Thu, 1 Dec 2022 11:17:10 +0900 Subject: [PATCH 3/4] Debug API call --- packages/caver-rpc/src/klay.js | 47 +----------------------- rpc.json | 3 +- test/packages/caver.rpc.js | 6 +-- types/packages/caver-klay/src/index.d.ts | 2 - types/packages/caver-rtm/src/index.d.ts | 8 ---- 5 files changed, 6 insertions(+), 60 deletions(-) diff --git a/packages/caver-rpc/src/klay.js b/packages/caver-rpc/src/klay.js index 532c7ee2..8dc01b52 100644 --- a/packages/caver-rpc/src/klay.js +++ b/packages/caver-rpc/src/klay.js @@ -174,9 +174,8 @@ class Klay { * @instance * * @example - * // Use `caver.rpc.klay.getRewardsByNumber` + * // Use `caver.rpc.klay.getRewards` * const result = await caver.rpc.klay.getRewards(0) - * // Use `caver.rpc.klay.getRewardsByHash` * const result = await caver.rpc.klay.getRewards('0x58482921af951cf42a069436ac9338de50fd963bdbea40e396f416f9ac96a08b') * * @param {string|number|BN|BigNumber} blockHashOrNumber The block hash or block number to query block header. @@ -185,49 +184,7 @@ class Klay { */ new Method({ name: 'getRewards', - call: 'klay_getRewardsByNumber', - hexCall: 'klay_getRewardsByHash', - params: 1, - inputFormatter: [formatters.inputBlockNumberFormatter], - }), - /** - * Returns the reward distribution result about a block by block number. - * - * @memberof Klay - * @method getRewardsByNumber - * @instance - * - * @example - * const result = await caver.rpc.klay.getRewardsByNumber(0) - * const result = await caver.rpc.klay.getRewardsByNumber('latest') - * - * @param {string|number|BN|BigNumber} blockNumberOrTag The block number or block tag string to query block header. - * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. - * @return {Promise} An object includes reward distribution result. - */ - new Method({ - name: 'getRewardsByNumber', - call: 'klay_getRewardsByNumber', - params: 1, - inputFormatter: [formatters.inputBlockNumberFormatter], - }), - /** - * Returns the reward distribution result about a block by block hash. - * - * @memberof Klay - * @method getRewardsByHash - * @instance - * - * @example - * const result = await caver.rpc.klay.getRewardsByHash('0x58482921af951cf42a069436ac9338de50fd963bdbea40e396f416f9ac96a08b') - * - * @param {string} blockHash The block hash to query the reward distribution result about a block. - * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. - * @return {Promise} An object includes reward distribution result. - */ - new Method({ - name: 'getRewardsByHash', - call: 'klay_getRewardsByHash', + call: 'klay_getRewards', params: 1, inputFormatter: [formatters.inputBlockNumberFormatter], }), diff --git a/rpc.json b/rpc.json index db04b459..1b08f9ad 100644 --- a/rpc.json +++ b/rpc.json @@ -147,8 +147,7 @@ }, { "name": "getRewards", - "call": "klay_getRewardsByNumber", - "hexCall": "klay_getRewardsByHash", + "call": "klay_getRewards", "params": 1, "inputFormatter": ["inputBlockNumberFormatter"] }, diff --git a/test/packages/caver.rpc.js b/test/packages/caver.rpc.js index 2e0c0263..daaea824 100644 --- a/test/packages/caver.rpc.js +++ b/test/packages/caver.rpc.js @@ -677,13 +677,13 @@ describe('caver.rpc.klay', () => { context('caver.rpc.klay.getRewards', () => { it('CAVERJS-UNIT-RPC-030: caver.rpc.klay.getRewards should call correct RPC call depends on param type', async () => { - // Have to call klay_getHeaderByHash with hex string param + // Have to call klay_getRewards with hex string param sandbox.stub(caver.rpc.klay._requestManager, 'send').callsFake((data, callback) => { expect(data.params.length).to.equal(1) if (caver.utils.isValidHash(data.params[0])) { - expect(data.method).to.equal('klay_getHeaderByHash') + expect(data.method).to.equal('klay_getRewards') } else { - expect(data.method).to.equal('klay_getHeaderByNumber') + expect(data.method).to.equal('klay_getRewards') } callback(undefined, {}) }) diff --git a/types/packages/caver-klay/src/index.d.ts b/types/packages/caver-klay/src/index.d.ts index a0ae11a4..8e335fc7 100644 --- a/types/packages/caver-klay/src/index.d.ts +++ b/types/packages/caver-klay/src/index.d.ts @@ -86,8 +86,6 @@ export class DeprecatedKlayRPC { getHeaderByHash: RpcCallToMethod['klay_getHeaderByHash'] getBlockReceipts: RpcCallToMethod['klay_getBlockReceipts'] getRewards: RpcCallToMethod['klay_getRewards'] - getRewardsByNumber: RpcCallToMethod['klay_getRewardsByNumber'] - getRewardsByHash: RpcCallToMethod['klay_getRewardsByHash'] getBlockWithConsensusInfo: RpcCallToMethod['klay_getBlockWithConsensusInfo'] getBlockWithConsensusInfoByNumber: RpcCallToMethod['klay_getBlockWithConsensusInfoByNumber'] getBlockWithConsensusInfoByHash: RpcCallToMethod['klay_getBlockWithConsensusInfoByHash'] diff --git a/types/packages/caver-rtm/src/index.d.ts b/types/packages/caver-rtm/src/index.d.ts index cbbcaa6d..6517641a 100644 --- a/types/packages/caver-rtm/src/index.d.ts +++ b/types/packages/caver-rtm/src/index.d.ts @@ -130,14 +130,6 @@ export default class RpcCallToMethod { blockNumberOrHash: BlockNumber | string, callback?: (error: Error, result: Header) => void ): Promise - klay_getRewardsByNumber( - blockNumber: BlockNumber, - callback?: (error: Error, result: Header) => void - ): Promise - klay_getRewardsByHash( - blockHash: string, - callback?: (error: Error, result: Header) => void - ): Promise klay_getTransactionFromBlock( blockNumber: BlockNumber, index: number, From 55643e6fb416fca2ceb885f445fb90d48b0e1a7e Mon Sep 17 00:00:00 2001 From: Nohyun Nehemiah Kwak Date: Thu, 1 Dec 2022 11:39:27 +0900 Subject: [PATCH 4/4] Remove unused type --- types/packages/caver-rpc/src/klay.d.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/types/packages/caver-rpc/src/klay.d.ts b/types/packages/caver-rpc/src/klay.d.ts index 366bbfa6..972de933 100644 --- a/types/packages/caver-rpc/src/klay.d.ts +++ b/types/packages/caver-rpc/src/klay.d.ts @@ -135,8 +135,6 @@ export class Klay { getHeaderByNumber(blockNumber: BlockNumber, callback?: (error: Error, result: Header) => void): Promise
getHeaderByHash(blockHash: string, callback?: (error: Error, result: Header) => void): Promise
getRewards(blockNumber: BlockNumber, callback?: (error: Error, result: Header) => void): Promise - getRewardsByNumber(blockNumber: BlockNumber, callback?: (error: Error, result: Header) => void): Promise - getRewardsByHash(blockHash: string, callback?: (error: Error, result: Header) => void): Promise getBlockReceipts(blockHash: string, callback?: (error: Error, result: TransactionReceipt[]) => void): Promise getBlockTransactionCount(blockNumber: BlockNumber, callback?: (error: Error, result: string) => void): Promise getBlockTransactionCountByNumber(