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

feat(analytics): add nft and native token activity endpoints #560

Merged
merged 5 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'main' into feat/analytics/nft-native-endpoints
  • Loading branch information
Alexandcoats committed Aug 17, 2022
commit d33c729fbe98fa83a816317fe95e484d3b19e56a
51 changes: 51 additions & 0 deletions bin/inx-chronicle/src/api/stardust/analytics/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,54 @@ pub struct OutputDiffAnalyticsResponse {
}

impl_success_response!(OutputDiffAnalyticsResponse);

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RichestAddressesResponse {
pub top: Vec<AddressStatDto>,
pub ledger_index: u32,
}

impl_success_response!(RichestAddressesResponse);

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AddressStatDto {
pub address: AddressDto,
pub balance: String,
}

impl From<AddressStat> for AddressStatDto {
fn from(s: AddressStat) -> Self {
Self {
address: AddressDto::from(&bee_block_stardust::address::Address::from(s.address)),
balance: s.balance,
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TokenDistributionResponse {
pub distribution: Vec<DistributionStatDto>,
pub ledger_index: u32,
}

impl_success_response!(TokenDistributionResponse);

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DistributionStatDto {
pub range: Range<u64>,
pub address_count: String,
pub total_balance: String,
}

impl From<DistributionStat> for DistributionStatDto {
fn from(s: DistributionStat) -> Self {
Self {
range: 10_u64.pow(s.index)..10_u64.pow(s.index + 1),
address_count: s.address_count.to_string(),
total_balance: s.total_balance,
}
}
}
32 changes: 31 additions & 1 deletion bin/inx-chronicle/src/api/stardust/analytics/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::{
extractors::{LedgerIndex, MilestoneRange, RichestAddressesQuery},
responses::{
AddressAnalyticsResponse, BlockAnalyticsResponse, OutputAnalyticsResponse, OutputDiffAnalyticsResponse,
StorageDepositAnalyticsResponse,
RichestAddressesResponse, StorageDepositAnalyticsResponse, TokenDistributionResponse,
},
};
use crate::api::{ApiError, ApiResult};
Expand Down Expand Up @@ -165,3 +165,33 @@ async fn foundry_output_analytics(
burned_count: res.burned_count.to_string(),
})
}

async fn richest_addresses(
database: Extension<MongoDb>,
RichestAddressesQuery { top, ledger_index }: RichestAddressesQuery,
) -> ApiResult<RichestAddressesResponse> {
let res = database
.get_richest_addresses(ledger_index, top)
.await?
.ok_or(ApiError::NoResults)?;

Ok(RichestAddressesResponse {
top: res.top.into_iter().map(Into::into).collect(),
ledger_index: res.ledger_index.0,
})
}

async fn token_distribution(
database: Extension<MongoDb>,
LedgerIndex { ledger_index }: LedgerIndex,
) -> ApiResult<TokenDistributionResponse> {
let res = database
.get_token_distribution(ledger_index)
.await?
.ok_or(ApiError::NoResults)?;

Ok(TokenDistributionResponse {
distribution: res.distribution.into_iter().map(Into::into).collect(),
ledger_index: res.ledger_index.0,
})
}
You are viewing a condensed version of this merge commit. You can view the full changes here.