-
Notifications
You must be signed in to change notification settings - Fork 44
feat(platform): token last claim query #2559
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
Merged
Merged
Changes from 2 commits
Commits
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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
69 changes: 69 additions & 0 deletions
69
...ages/rs-drive-abci/src/query/token_queries/token_perpetual_distribution_last_claim/mod.rs
This file contains hidden or 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,69 @@ | ||
| use crate::error::query::QueryError; | ||
| use crate::error::Error; | ||
| use crate::platform_types::platform::Platform; | ||
| use crate::platform_types::platform_state::PlatformState; | ||
| use crate::query::QueryValidationResult; | ||
|
|
||
| use dapi_grpc::platform::v0::get_token_perpetual_distribution_last_claim_request::Version as RequestVersion; | ||
| use dapi_grpc::platform::v0::get_token_perpetual_distribution_last_claim_response::Version as ResponseVersion; | ||
| use dapi_grpc::platform::v0::{ | ||
| GetTokenPerpetualDistributionLastClaimRequest, GetTokenPerpetualDistributionLastClaimResponse, | ||
| }; | ||
| use dpp::version::PlatformVersion; | ||
|
|
||
| mod v0; | ||
|
|
||
| impl<C> Platform<C> { | ||
| /// Query the last perpetual distribution claim for a given token and identity | ||
| pub fn query_token_perpetual_distribution_last_claim( | ||
| &self, | ||
| GetTokenPerpetualDistributionLastClaimRequest { version }: GetTokenPerpetualDistributionLastClaimRequest, | ||
| platform_state: &PlatformState, | ||
| platform_version: &PlatformVersion, | ||
| ) -> Result<QueryValidationResult<GetTokenPerpetualDistributionLastClaimResponse>, Error> { | ||
| let Some(version) = version else { | ||
| return Ok(QueryValidationResult::new_with_error( | ||
| QueryError::DecodingError( | ||
| "could not decode token perpetual distribution last claim query".to_string(), | ||
| ), | ||
| )); | ||
| }; | ||
|
|
||
| let feature_version_bounds = &platform_version | ||
| .drive_abci | ||
| .query | ||
| .token_queries | ||
| .token_perpetual_distribution_last_claim; | ||
|
|
||
| let feature_version = match &version { | ||
| RequestVersion::V0(_) => 0, | ||
| }; | ||
|
|
||
| if !feature_version_bounds.check_version(feature_version) { | ||
| return Ok(QueryValidationResult::new_with_error( | ||
| QueryError::UnsupportedQueryVersion( | ||
| "token_perpetual_distribution_last_claim".to_string(), | ||
| feature_version_bounds.min_version, | ||
| feature_version_bounds.max_version, | ||
| platform_version.protocol_version, | ||
| feature_version, | ||
| ), | ||
| )); | ||
| } | ||
|
|
||
| match version { | ||
| RequestVersion::V0(request_v0) => { | ||
| let result = self.query_token_perpetual_distribution_last_claim_v0( | ||
| request_v0, | ||
| platform_state, | ||
| platform_version, | ||
| )?; | ||
| Ok(result.map( | ||
| |response_v0| GetTokenPerpetualDistributionLastClaimResponse { | ||
| version: Some(ResponseVersion::V0(response_v0)), | ||
| }, | ||
| )) | ||
| } | ||
| } | ||
| } | ||
| } |
171 changes: 171 additions & 0 deletions
171
...s/rs-drive-abci/src/query/token_queries/token_perpetual_distribution_last_claim/v0/mod.rs
This file contains hidden or 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,171 @@ | ||
| use crate::error::query::QueryError; | ||
| use crate::error::Error; | ||
| use crate::platform_types::platform::Platform; | ||
| use crate::platform_types::platform_state::PlatformState; | ||
| use crate::query::QueryValidationResult; | ||
|
|
||
| use dapi_grpc::platform::v0::get_token_perpetual_distribution_last_claim_request::{ContractTokenInfo, GetTokenPerpetualDistributionLastClaimRequestV0}; | ||
| use dapi_grpc::platform::v0::get_token_perpetual_distribution_last_claim_response::{ | ||
| get_token_perpetual_distribution_last_claim_response_v0, | ||
| GetTokenPerpetualDistributionLastClaimResponseV0, | ||
| }; | ||
| use dapi_grpc::platform::v0::get_token_perpetual_distribution_last_claim_response::get_token_perpetual_distribution_last_claim_response_v0::{last_claim_info, LastClaimInfo}; | ||
| use dpp::check_validation_result_with_data; | ||
| use dpp::data_contract::accessors::v1::DataContractV1Getters; | ||
| use dpp::data_contract::associated_token::token_configuration::accessors::v0::TokenConfigurationV0Getters; | ||
| use dpp::data_contract::associated_token::token_distribution_rules::accessors::v0::TokenDistributionRulesV0Getters; | ||
| use dpp::data_contract::associated_token::token_perpetual_distribution::methods::v0::TokenPerpetualDistributionV0Accessors; | ||
| use dpp::data_contract::associated_token::token_perpetual_distribution::reward_distribution_moment::RewardDistributionMoment; | ||
| use dpp::identifier::Identifier; | ||
| use dpp::validation::ValidationResult; | ||
| use dpp::version::PlatformVersion; | ||
|
|
||
| impl<C> Platform<C> { | ||
| pub(super) fn query_token_perpetual_distribution_last_claim_v0( | ||
| &self, | ||
| GetTokenPerpetualDistributionLastClaimRequestV0 { | ||
| token_id, | ||
| contract_info, | ||
| identity_id, | ||
| prove, | ||
| }: GetTokenPerpetualDistributionLastClaimRequestV0, | ||
| platform_state: &PlatformState, | ||
| platform_version: &PlatformVersion, | ||
| ) -> Result<QueryValidationResult<GetTokenPerpetualDistributionLastClaimResponseV0>, Error> | ||
| { | ||
| // ── Basic argument validation ────────────────────────────────────────── | ||
| let token_id: Identifier = | ||
| check_validation_result_with_data!(token_id.try_into().map_err(|_| { | ||
| QueryError::InvalidArgument( | ||
| "token_id must be a valid identifier (32 bytes long)".to_string(), | ||
| ) | ||
| })); | ||
|
|
||
| let identity_id: Identifier = | ||
| check_validation_result_with_data!(identity_id.try_into().map_err(|_| { | ||
| QueryError::InvalidArgument( | ||
| "identity_id must be a valid identifier (32 bytes long)".to_string(), | ||
| ) | ||
| })); | ||
|
|
||
| let response = if prove { | ||
| let proof = check_validation_result_with_data!(self | ||
| .drive | ||
| .prove_perpetual_distribution_last_paid_moment( | ||
| token_id.into_buffer(), | ||
| identity_id, | ||
| None, | ||
| platform_version, | ||
| )); | ||
|
|
||
| GetTokenPerpetualDistributionLastClaimResponseV0 { | ||
| result: Some( | ||
| get_token_perpetual_distribution_last_claim_response_v0::Result::Proof( | ||
| self.response_proof_v0(platform_state, proof), | ||
| ), | ||
| ), | ||
| metadata: Some(self.response_metadata_v0(platform_state)), | ||
| } | ||
| } else if let Some(ContractTokenInfo { | ||
| contract_id, | ||
| token_contract_position, | ||
| }) = contract_info | ||
| { | ||
| let contract_id: Identifier = | ||
| check_validation_result_with_data!(contract_id.try_into().map_err(|_| { | ||
| QueryError::InvalidArgument( | ||
| "contract_id must be a valid identifier (32 bytes long)".to_string(), | ||
| ) | ||
| })); | ||
| let Some(contract) = check_validation_result_with_data!(self | ||
| .drive | ||
| .get_contract_with_fetch_info( | ||
| contract_id.into_buffer(), | ||
| false, | ||
| None, | ||
| platform_version | ||
| ) | ||
| .map_err(QueryError::Drive)) | ||
| else { | ||
| return Ok(QueryValidationResult::new_with_error(QueryError::NotFound( | ||
| format!("contract with identifier {} not found", contract_id), | ||
| ))); | ||
| }; | ||
|
|
||
| if token_contract_position > u16::MAX as u32 { | ||
| return Ok(QueryValidationResult::new_with_error( | ||
| QueryError::InvalidArgument( | ||
| "token_contract_position must be less than u16::MAX".to_string(), | ||
| ), | ||
| )); | ||
| } | ||
|
|
||
| let token = check_validation_result_with_data!(contract | ||
| .contract | ||
| .expected_token_configuration(token_contract_position as u16) | ||
| .map_err(QueryError::Protocol)); | ||
| let token_distribution_rules = token.distribution_rules(); | ||
| let Some(token_perpetual_distribution_rules) = | ||
| token_distribution_rules.perpetual_distribution() | ||
| else { | ||
| return Ok(QueryValidationResult::new_with_error( | ||
| QueryError::InvalidArgument(format!( | ||
| "contract with identifier {} does not have perpetual distribution rules", | ||
| contract_id | ||
| )), | ||
| )); | ||
| }; | ||
|
|
||
| let paid_at = self | ||
| .drive | ||
| .fetch_perpetual_distribution_last_paid_moment( | ||
| token_id.into_buffer(), | ||
| identity_id, | ||
| token_perpetual_distribution_rules.distribution_type(), | ||
| None, | ||
| platform_version, | ||
| )? | ||
| .map(|moment| match moment { | ||
| RewardDistributionMoment::BlockBasedMoment(height) => { | ||
| last_claim_info::PaidAt::BlockHeight(height) | ||
| } | ||
| RewardDistributionMoment::TimeBasedMoment(timestamp) => { | ||
| last_claim_info::PaidAt::TimestampMs(timestamp) | ||
| } | ||
| RewardDistributionMoment::EpochBasedMoment(epoch) => { | ||
| last_claim_info::PaidAt::Epoch(epoch as u32) | ||
| } | ||
| }); | ||
QuantumExplorer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| GetTokenPerpetualDistributionLastClaimResponseV0 { | ||
| result: Some( | ||
| get_token_perpetual_distribution_last_claim_response_v0::Result::LastClaim( | ||
| LastClaimInfo { paid_at }, | ||
| ), | ||
| ), | ||
| metadata: Some(self.response_metadata_v0(platform_state)), | ||
| } | ||
| } else { | ||
| let paid_at = self | ||
| .drive | ||
| .fetch_perpetual_distribution_last_paid_moment_raw( | ||
| token_id.into_buffer(), | ||
| identity_id, | ||
| None, | ||
| platform_version, | ||
| )? | ||
| .map(|moment| last_claim_info::PaidAt::RawBytes(moment)); | ||
|
Check failure on line 157 in packages/rs-drive-abci/src/query/token_queries/token_perpetual_distribution_last_claim/v0/mod.rs
|
||
|
|
||
| GetTokenPerpetualDistributionLastClaimResponseV0 { | ||
| result: Some( | ||
| get_token_perpetual_distribution_last_claim_response_v0::Result::LastClaim( | ||
| LastClaimInfo { paid_at }, | ||
| ), | ||
| ), | ||
| metadata: Some(self.response_metadata_v0(platform_state)), | ||
| } | ||
| }; | ||
|
|
||
| Ok(QueryValidationResult::new_with_data(response)) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.