Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Add getRewards API in rpc #719

Merged
merged 4 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/caver-rpc/src/klay.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Klay.Rewards>} An object includes reward distribution result.
*/
new Method({
name: 'getRewards',
call: 'klay_getRewards',
params: 1,
inputFormatter: [formatters.inputBlockNumberFormatter],
}),
/**
* An object defines the AccountKeyLegacy.
*
Expand Down
6 changes: 6 additions & 0 deletions rpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@
"params": 1,
"inputFormatter": ["inputBlockNumberFormatter"]
},
{
"name": "getRewards",
"call": "klay_getRewards",
"params": 1,
"inputFormatter": ["inputBlockNumberFormatter"]
},
{
"name": "getBlockTransactionCount",
"call": "klay_getBlockTransactionCountByNumber",
Expand Down
18 changes: 18 additions & 0 deletions test/packages/caver.rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
11 changes: 11 additions & 0 deletions types/packages/caver-core/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions types/packages/caver-klay/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
2 changes: 2 additions & 0 deletions types/packages/caver-rpc/src/klay.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { AccountKey } from '../../caver-account/src'
import {
Block,
Header,
Rewards,
BlockNumber,
TransactionReceipt,
BlockWithConsensusInfo,
Expand Down Expand Up @@ -133,6 +134,7 @@ export class Klay {
getHeader(blockNumber: BlockNumber, callback?: (error: Error, result: Header) => void): Promise<Header>
getHeaderByNumber(blockNumber: BlockNumber, callback?: (error: Error, result: Header) => void): Promise<Header>
getHeaderByHash(blockHash: string, callback?: (error: Error, result: Header) => void): Promise<Header>
getRewards(blockNumber: BlockNumber, callback?: (error: Error, result: Header) => void): Promise<Rewards>
getBlockReceipts(blockHash: string, callback?: (error: Error, result: TransactionReceipt[]) => void): Promise<TransactionReceipt[]>
getBlockTransactionCount(blockNumber: BlockNumber, callback?: (error: Error, result: string) => void): Promise<string>
getBlockTransactionCountByNumber(
Expand Down
5 changes: 5 additions & 0 deletions types/packages/caver-rtm/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import BN = require('bn.js')
import BigNumber from 'bignumber.js'
import {
Header,
Rewards,
Block,
BlockNumber,
TransactionReceipt,
Expand Down Expand Up @@ -125,6 +126,10 @@ export default class RpcCallToMethod {
blockHash: string,
callback?: (error: Error, result: Header) => void
): Promise<Header>
klay_getRewards(
blockNumberOrHash: BlockNumber | string,
callback?: (error: Error, result: Header) => void
): Promise<Rewards>
klay_getTransactionFromBlock(
blockNumber: BlockNumber,
index: number,
Expand Down