-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Capacity runtime api with list_unclaimed_rewards endpoint #2088
Merged
shannonwells
merged 3 commits into
feat/capacity-staking-rewards-impl
from
capacity-runtime-api-1698
Jul 19, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import '@frequency-chain/api-augment'; | ||
import assert from 'assert'; | ||
import { Extrinsic, ExtrinsicHelper } from '../scaffolding/extrinsicHelpers'; | ||
import { getFundingSource } from '../scaffolding/funding'; | ||
import { | ||
createKeys, | ||
createMsaAndProvider, | ||
stakeToProvider, | ||
CENTS, | ||
DOLLARS, | ||
createAndFundKeypair, | ||
createProviderKeysAndId, | ||
boostProvider, | ||
getNextEpochBlock, | ||
getNextRewardEraBlock, | ||
} from '../scaffolding/helpers'; | ||
import { isTestnet } from '../scaffolding/env'; | ||
import { KeyringPair } from '@polkadot/keyring/types'; | ||
import { UnclaimedRewardInfo } from '@frequency-chain/api-augment/interfaces'; | ||
import { Vec } from '@polkadot/types'; | ||
|
||
const fundingSource = getFundingSource('capacity-replenishment'); | ||
|
||
describe('Capacity: list_unclaimed_rewards', function() { | ||
const providerBalance = 2n * DOLLARS; | ||
|
||
const setUpForBoosting = async (boosterName: string, providerName: string): Promise<[number, KeyringPair]> => { | ||
const booster = await createAndFundKeypair(fundingSource, 5n * DOLLARS, boosterName); | ||
const providerKeys = createKeys(providerName); | ||
const provider = await createMsaAndProvider(fundingSource, providerKeys, providerName, providerBalance); | ||
await assert.doesNotReject(boostProvider(fundingSource, booster, provider, 1n * DOLLARS)); | ||
|
||
return [provider.toNumber(), booster]; | ||
}; | ||
|
||
it('can be called', async function() { | ||
const [_provider, booster] = await setUpForBoosting("booster1", "provider1"); | ||
const result = ExtrinsicHelper.api.rpc.state.call( | ||
'CapacityRuntimeApi_list_unclaimed_rewards', booster.address); | ||
let count = 0; | ||
const subscription = result.subscribe((x) => { | ||
count++; | ||
}); | ||
// Failing to do this results in "helpful" error: | ||
// `Bad input data provided to list_unclaimed_rewards: Input buffer has still data left after decoding!` | ||
subscription.unsubscribe(); | ||
assert(count === 0, `should have been empty but had ${count} items`); | ||
}); | ||
|
||
it('returns correct rewards after enough eras have passed', async function() { | ||
if (isTestnet()) { | ||
this.skip(); | ||
} | ||
const [_provider, booster] = await setUpForBoosting("booster2", "provider2"); | ||
console.debug(`Booster pubkey: ${booster.address}`); | ||
|
||
await ExtrinsicHelper.runToBlock(await getNextRewardEraBlock()); | ||
await ExtrinsicHelper.runToBlock(await getNextRewardEraBlock()); | ||
await ExtrinsicHelper.runToBlock(await getNextRewardEraBlock()); | ||
|
||
const encodedAddr = ExtrinsicHelper.api.registry.createType('AccountId32', booster.address); | ||
|
||
const result = await ExtrinsicHelper.apiPromise.rpc.state.call( | ||
'CapacityRuntimeApi_list_unclaimed_rewards', encodedAddr); | ||
|
||
const decResult: Vec<UnclaimedRewardInfo> = ExtrinsicHelper.api.registry.createType('Vec<UnclaimedRewardInfo>', result); | ||
let count = 0; | ||
assert(decResult.every(item => { | ||
count++; | ||
return item.staked_amount.toHuman() === '1.0000 UNIT'; | ||
})); | ||
assert.equal(count, 3); | ||
|
||
assert.equal(decResult[0].eligible_amount.toHuman(), '1.0000 UNIT'); | ||
assert.equal(decResult[0].earned_amount.toHuman(), '3.8000 mUNIT'); | ||
|
||
assert.equal(decResult[1].eligible_amount.toHuman(), '1.0000 UNIT'); | ||
assert.equal(decResult[1].earned_amount.toHuman(), '3.8000 mUNIT'); | ||
|
||
assert.equal(decResult[2].eligible_amount.toHuman(), '1.0000 UNIT'); | ||
assert.equal(decResult[2].earned_amount.toHuman(), '3.8000 mUNIT'); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export default { | ||
rpc: { | ||
dummy: { description: "This API has no custom RPCs", params: [], type: 'undefined' }, | ||
}, | ||
types: { | ||
RewardEra: 'u32', | ||
Balance: 'u128', | ||
BlockNumber: 'u32', | ||
UnclaimedRewardInfo: { | ||
reward_era: 'RewardEra', | ||
expires_at_block: 'BlockNumber', | ||
staked_amount: 'Balance', | ||
eligible_amount: 'Balance', | ||
earned_amount: 'Balance', | ||
} | ||
}, | ||
runtime: { | ||
CapacityRuntimeApi: [ | ||
{ | ||
methods: { | ||
list_unclaimed_rewards: { | ||
description: 'List any rewards that can be claimed up to the previous Reward Era', | ||
params: [ { name: 'address', type: 'AccountId' }, ], | ||
type: 'Vec<UnclaimedRewardInfo>', | ||
}, | ||
}, | ||
version: 1, | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[package] | ||
name = "pallet-capacity-runtime-api" | ||
version = "0.0.0" | ||
description = "A package that adds Runtime Api for the Capacity pallet" | ||
authors = ["Frequency"] | ||
license = "Apache-2.0" | ||
publish = false | ||
homepage = "https://frequency.xyz" | ||
repository = "https://github.com/frequency-chain/frequency/" | ||
edition = "2021" | ||
|
||
|
||
[dependencies] | ||
parity-scale-codec = { workspace = true, features = ["derive"] } | ||
sp-api = { workspace = true, default-features = false } | ||
sp-std = { workspace = true, default-features = false } | ||
sp-runtime = { workspace = true, default-features = false } | ||
common-primitives = { path="../../../../common/primitives", default-features = false} | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"parity-scale-codec/std", | ||
"sp-api/std", | ||
"sp-std/std", | ||
"sp-runtime/std" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
#![allow(clippy::too_many_arguments)] | ||
#![allow(clippy::unnecessary_mut_passed)] | ||
#![allow(rustdoc::bare_urls)] | ||
// Strong Documentation Lints | ||
#![deny( | ||
rustdoc::broken_intra_doc_links, | ||
rustdoc::missing_crate_level_docs, | ||
rustdoc::invalid_codeblock_attributes, | ||
missing_docs | ||
)] | ||
|
||
//! Runtime API definition for [Capacity](../pallet_capacity/index.html) | ||
//! | ||
//! This api must be implemented by the node runtime. | ||
//! Runtime APIs Provide: | ||
//! - An interface between the runtime and Custom RPCs. | ||
//! - Runtime interfaces for end users beyond just State Queries | ||
|
||
use common_primitives::capacity::UnclaimedRewardInfo; | ||
use parity_scale_codec::Codec; | ||
use sp_runtime::traits::MaybeDisplay; | ||
use sp_std::vec::Vec; | ||
|
||
// Here we declare the runtime API. It is implemented it the `impl` block in | ||
// runtime files (the `runtime` folder) | ||
sp_api::decl_runtime_apis! { | ||
/// Runtime Version for Capacity | ||
/// - MUST be incremented if anything changes | ||
/// - Also update in js/api-augment | ||
/// - See: https://paritytech.github.io/polkadot/doc/polkadot_primitives/runtime_api/index.html | ||
#[api_version(1)] | ||
/// Runtime APIs for [Capacity](../pallet_capacity/index.html) | ||
pub trait CapacityRuntimeApi<AccountId, Balance, BlockNumber> where | ||
AccountId: Codec + MaybeDisplay, | ||
Balance: Codec + MaybeDisplay, | ||
BlockNumber: Codec + MaybeDisplay, | ||
{ | ||
// state_call method: CapacityRuntimeApi_list_unclaimed_rewards | ||
/// Get the list of unclaimed rewards information for each eligible Reward Era. | ||
fn list_unclaimed_rewards(who: AccountId) -> Vec<UnclaimedRewardInfo<Balance, BlockNumber>>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. By the way, do this show up in PolkadotUI now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It won't until it merges to main and is released, and I think you also have to post a PR with the updated release to PolkadotJS ui