Skip to content

Commit

Permalink
Add validator delegations query
Browse files Browse the repository at this point in the history
Add validator unbonding delegations endpoint

Added endpoint for validator-delegator delegation pair

Update delegators delegation endpoint to include all delegations
  • Loading branch information
freak12techno committed Jan 31, 2024
1 parent 5dd80a6 commit 1b1e6ec
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ default-run = "nomic"

[dependencies]
bitcoin = { version = "0.29.2", features = ["serde", "rand"] }
orga = { git = "https://github.com/nomic-io/orga.git", rev = "f35f1f6be228345e6e3a5d8617e049c6c203c8d1", features = [
orga = { git = "https://github.com/nomic-io/orga.git", rev = "38682a1eafc9c3566aa5de4601a1aff06eb79c4f", features = [
"merk-verify",
] }
thiserror = "1.0.30"
Expand Down
4 changes: 2 additions & 2 deletions rest/Cargo.lock

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

137 changes: 121 additions & 16 deletions rest/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[macro_use]
extern crate rocket;

use chrono::{Utc, TimeZone};
use nomic::{
app::{InnerApp, Nom},
bitcoin::Nbtc,
Expand Down Expand Up @@ -468,25 +469,26 @@ async fn staking_delegators_delegations(address: &str) -> Value {
.await
.unwrap();

let total_staked: u64 = delegations
.iter()
.map(|(_, d)| -> u64 { d.staked.into() })
.sum();
let mut entries = vec![];

json!({
"delegation_responses": [
{
for (validator_address, delegation) in delegations {
entries.push(json!({
"delegation": {
"delegator_address": "",
"validator_address": "",
"shares": "0"
"delegator_address": address.to_string(),
"validator_address": validator_address.to_string(),
"shares": delegation.staked.to_string(),
},
"balance": {
"denom": "unom",
"amount": total_staked.to_string(),
}
}
], "pagination": { "next_key": null, "total": "0" } })
"amount": delegation.staked.to_string(),
},
}))
}

json!({
"delegation_responses": entries,
"pagination": { "next_key": null, "total": entries.len().to_string() }
})
}

#[get("/staking/delegators/<address>/delegations")]
Expand Down Expand Up @@ -534,8 +536,8 @@ async fn staking_delegators_unbonding_delegations(address: &str) -> Value {
entries.push(json!({
"creation_height": "0", // TODO
"completion_time": t, // TODO
"initial_balance": "0", // TODO
"balance": "0" // TODO
"initial_balance": unbond.amount.to_string(),
"balance": unbond.amount.to_string()
}))
}
unbonds.push(json!({
Expand All @@ -553,6 +555,106 @@ fn staking_delegators_unbonding_delegations_2(_address: &str) -> Value {
json!({ "height": "0", "result": [] })
}

#[get("/cosmos/staking/v1beta1/validators/<address>/delegations")]
async fn staking_validators_delegations(address: &str) -> Value {
let validator_address: Address = address.parse().unwrap();
let delegations: Vec<(Address, DelegationInfo)> = app_client()
.query(|app: InnerApp| app.staking.validator_delegations(validator_address))
.await
.unwrap();

let mut entries = vec![];

for (delegator_address, delegation) in delegations {
entries.push(json!({
"delegation": {
"delegator_address": delegator_address.to_string(),
"validator_address": validator_address.to_string(),
"shares": delegation.staked.to_string(),
},
"balance": {
"denom": "unom",
"amount": delegation.staked.to_string(),
},
}))
}

json!({
"delegation_responses": entries,
"pagination": { "next_key": null, "total": entries.len().to_string() }
})
}

#[get("/cosmos/staking/v1beta1/validators/<validator_address>/delegations/<delegator_address>")]
async fn staking_validator_single_delegation(validator_address: &str, delegator_address: &str) -> Value {
let delegator_address: Address = delegator_address.parse().unwrap();
let validator_address: Address = validator_address.parse().unwrap();

let delegations: Vec<(Address, DelegationInfo)> = app_client()
.query(|app: InnerApp| app.staking.delegations(delegator_address))
.await
.unwrap();

let delegation: &DelegationInfo = delegations.iter().
find(|(validator, _delegation)| *validator == validator_address).
map(|(_validator, delegation)| delegation).
unwrap();

json!({
"delegation_response": {
"delegation": {
"delegator_address": delegator_address,
"validator_address": validator_address,
"shares": delegation.staked.to_string(),
},
"balance": {
"denom": "unom",
"amount": delegation.staked.to_string(),
}
}
})
}



#[get("/cosmos/staking/v1beta1/validators/<address>/unbonding_delegations")]
async fn staking_validators_unbonding_delegations(address: &str) -> Value {
let validator_address: Address = address.parse().unwrap();
let delegations: Vec<(Address, DelegationInfo)> = app_client()
.query(|app: InnerApp| app.staking.validator_delegations(validator_address))
.await
.unwrap();

let mut unbonds = vec![];

for (delegator_address, delegation) in delegations {
if delegation.unbonding.len() == 0 {
continue;
}

let mut entries = vec![];
for unbond in delegation.unbonding {
let t = Utc.timestamp_opt(unbond.start_seconds, 0).unwrap();
entries.push(json!({
"creation_height": "0", // TODO
"completion_time": t, // TODO
"initial_balance": unbond.amount.to_string(),
"balance": unbond.amount.to_string()
}))
}
unbonds.push(json!({
"delegator_address": delegator_address,
"validator_address": validator_address,
"entries": entries
}))
}

json!({
"unbonding_responses": unbonds,
"pagination": { "next_key": null, "total": unbonds.len().to_string()
} })
}

#[get("/cosmos/distribution/v1beta1/delegators/<address>/rewards")]
async fn distribution_delegators_rewards(address: &str) -> Value {
let address: Address = address.parse().unwrap();
Expand Down Expand Up @@ -1040,6 +1142,9 @@ fn rocket() -> _ {
staking_delegators_delegations_2,
staking_delegators_unbonding_delegations,
staking_delegators_unbonding_delegations_2,
staking_validators_delegations,
staking_validators_unbonding_delegations,
staking_validator_single_delegation,
distribution_delegators_rewards,
distribution_delegators_rewards_for_validator,
minting_inflation,
Expand Down

0 comments on commit 1b1e6ec

Please sign in to comment.