diff --git a/packages/caver-rpc/src/klay.js b/packages/caver-rpc/src/klay.js index 44d368d4..8dc01b52 100644 --- a/packages/caver-rpc/src/klay.js +++ b/packages/caver-rpc/src/klay.js @@ -150,6 +150,44 @@ 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.getRewards` + * const result = await caver.rpc.klay.getRewards(0) + * 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_getRewards', + params: 1, + inputFormatter: [formatters.inputBlockNumberFormatter], + }), /** * An object defines the AccountKeyLegacy. * diff --git a/rpc.json b/rpc.json index cf76418e..1b08f9ad 100644 --- a/rpc.json +++ b/rpc.json @@ -145,6 +145,12 @@ "params": 1, "inputFormatter": ["inputBlockNumberFormatter"] }, + { + "name": "getRewards", + "call": "klay_getRewards", + "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..daaea824 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_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_getRewards') + } else { + expect(data.method).to.equal('klay_getRewards') + } + 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-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-klay/src/index.d.ts b/types/packages/caver-klay/src/index.d.ts index 02d83605..8e335fc7 100644 --- a/types/packages/caver-klay/src/index.d.ts +++ b/types/packages/caver-klay/src/index.d.ts @@ -85,6 +85,7 @@ export class DeprecatedKlayRPC { getHeaderByNumber: RpcCallToMethod['klay_getHeaderByNumber'] getHeaderByHash: RpcCallToMethod['klay_getHeaderByHash'] getBlockReceipts: RpcCallToMethod['klay_getBlockReceipts'] + getRewards: RpcCallToMethod['klay_getRewards'] 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..972de933 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, @@ -133,6 +134,7 @@ 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 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..6517641a 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, @@ -125,6 +126,10 @@ 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_getTransactionFromBlock( blockNumber: BlockNumber, index: number,