Skip to content

Commit

Permalink
Merge pull request #4302 from anoma/mergify/bp/maint-libs-0.47/pr-4196
Browse files Browse the repository at this point in the history
[pos] Add epoch argument to vp::pos::rewards (backport #4196)
  • Loading branch information
mergify[bot] authored Feb 3, 2025
2 parents 6064afc + 62fe537 commit 3599d9d
Show file tree
Hide file tree
Showing 9 changed files with 533 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/SDK/4196-rewards-at-past-epoch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Allow querying PoS rewards at a specified epoch
([\#4196](https://github.com/anoma/namada/pull/4196))
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,25 @@
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in library 'namada_sdk'",
"cargo": {
"args": [
"test",
"--no-run",
"--lib",
"--package=namada_sdk"
],
"filter": {
"name": "namada_sdk",
"kind": "lib"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
Expand Down
11 changes: 10 additions & 1 deletion crates/apps_lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2049,7 +2049,7 @@ pub mod cmds {
fn def() -> App {
App::new(Self::CMD)
.about(wrap!(
"Query the latest rewards available to claim for a given \
"Query the rewards available to claim for a given \
delegation (or self-bond)."
))
.add_args::<args::QueryRewards<args::CliTypes>>()
Expand Down Expand Up @@ -7309,6 +7309,7 @@ pub mod args {
query: self.query.to_sdk(ctx)?,
validator: ctx.borrow_chain_or_exit().get(&self.validator),
source: self.source.map(|x| ctx.borrow_chain_or_exit().get(&x)),
epoch: self.epoch,
})
}
}
Expand All @@ -7318,10 +7319,12 @@ pub mod args {
let query = Query::parse(matches);
let source = SOURCE_OPT.parse(matches);
let validator = VALIDATOR.parse(matches);
let epoch = EPOCH.parse(matches);
Self {
query,
source,
validator,
epoch,
}
}

Expand All @@ -7336,6 +7339,12 @@ pub mod args {
"Validator address for the rewards query."
)),
)
.arg(EPOCH.def().help(wrap!(
"The epoch at which to query (corresponding to the last \
committed block, if not specified). Note: when querying \
by epoch, this returns the accumulated rewards that were \
available to claim at the start of the epoch."
)))
}
}

Expand Down
17 changes: 13 additions & 4 deletions crates/apps_lib/src/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,8 +1503,11 @@ pub async fn query_rewards<C: Client + Sync>(
client: &C,
source: &Option<Address>,
validator: &Address,
epoch: &Option<Epoch>,
) -> token::Amount {
unwrap_sdk_result(rpc::query_rewards(client, source, validator).await)
unwrap_sdk_result(
rpc::query_rewards(client, source, validator, epoch).await,
)
}

/// Query token total supply.
Expand Down Expand Up @@ -1920,12 +1923,18 @@ pub async fn query_and_print_rewards<N: Namada>(
context: &N,
args: args::QueryRewards,
) {
let (source, validator) = (args.source, args.validator);
let (source, validator, epoch) = (args.source, args.validator, args.epoch);

let rewards = query_rewards(context.client(), &source, &validator).await;
let rewards =
query_rewards(context.client(), &source, &validator, &epoch).await;
display_line!(
context.io(),
"Current rewards available for claim: {} NAM",
"{}: {} NAM",
epoch
.map(|e| format!("Rewards at epoch {}", e))
.unwrap_or_else(
|| "Current rewards available for claim".to_string()
),
rewards.to_string_native()
);
}
Expand Down
2 changes: 2 additions & 0 deletions crates/sdk/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2545,6 +2545,8 @@ pub struct QueryRewards<C: NamadaTypes = SdkTypes> {
pub source: Option<C::Address>,
/// Address of the validator
pub validator: C::Address,
/// Epoch in which to find rewards
pub epoch: Option<Epoch>,
}

/// Query PoS delegations
Expand Down
2 changes: 1 addition & 1 deletion crates/sdk/src/queries/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ where
/// borsh-encoded types, it is safe to check `data.is_empty()` to see if the
/// value was found, except for unit - see `fn query_storage_value` in
/// `apps/src/lib/client/rpc.rs` for unit type handling via `storage_has_key`.
fn storage_value<D, H, V, T>(
pub fn storage_value<D, H, V, T>(
ctx: RequestCtx<'_, D, H, V, T>,
request: &RequestQuery,
storage_key: storage::Key,
Expand Down
Loading

0 comments on commit 3599d9d

Please sign in to comment.