Skip to content
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

[pos] Add epoch argument to vp::pos::rewards #4196

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions crates/apps_lib/src/cli.rs
Original file line number Diff line number Diff line change
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,10 @@ 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)."
)))
}
}

Expand Down Expand Up @@ -7567,6 +7574,7 @@ pub mod args {
type Data = PathBuf;
type DatedSpendingKey = WalletDatedSpendingKey;
type DatedViewingKey = WalletDatedViewingKey;
type Epoch = Epoch;
type EthereumAddress = String;
type Keypair = WalletKeypair;
type MaspIndexerAddress = String;
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
1 change: 1 addition & 0 deletions crates/sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ namada_proof_of_stake = { path = "../proof_of_stake", default-features = false,
namada_state = { path = "../state", features = ["testing"] }
namada_storage = { path = "../storage", features = ["testing"] }
namada_token = { path = "../token", features = ["testing", "masp"] }
namada_trans_token = { path = "../trans_token" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't be needed - the symbols used are re-exported from namada_token

namada_tx = { path = "../tx", features = ["testing"]}
namada_vm = { path = "../vm" }
namada_vote_ext = { path = "../vote_ext" }
Expand Down
5 changes: 5 additions & 0 deletions crates/sdk/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub trait NamadaTypes: Clone + std::fmt::Debug {
type MaspIndexerAddress: Clone + std::fmt::Debug;
/// Represents a block height
type BlockHeight: Clone + std::fmt::Debug;
/// Represents an epoch
type Epoch: Clone + std::fmt::Debug;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this addition is not needed - you can refer to the concrete Epoch type from both impls (I see you probably followed what's been done and there are already bunch of types here that don't need to be generic)

}

/// The concrete types being used in Namada SDK
Expand Down Expand Up @@ -128,6 +130,7 @@ impl NamadaTypes for SdkTypes {
type Data = Vec<u8>;
type DatedSpendingKey = DatedSpendingKey;
type DatedViewingKey = DatedViewingKey;
type Epoch = namada_core::chain::Epoch;
type EthereumAddress = ();
type Keypair = namada_core::key::common::SecretKey;
type MaspIndexerAddress = String;
Expand Down Expand Up @@ -2545,6 +2548,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<C::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