Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions packages/dapi-grpc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn configure_platform(mut platform: MappingConfig) -> MappingConfig {
// Derive features for versioned messages
//
// "GetConsensusParamsRequest" is excluded as this message does not support proofs
const VERSIONED_REQUESTS: [&str; 42] = [
const VERSIONED_REQUESTS: [&str; 43] = [
"GetDataContractHistoryRequest",
"GetDataContractRequest",
"GetDataContractsRequest",
Expand Down Expand Up @@ -110,6 +110,7 @@ fn configure_platform(mut platform: MappingConfig) -> MappingConfig {
"GetIdentityTokenInfosRequest",
"GetIdentitiesTokenInfosRequest",
"GetTokenDirectPurchasePricesRequest",
"GetTokenContractInfoRequest",
"GetTokenStatusesRequest",
"GetTokenTotalSupplyRequest",
"GetGroupInfoRequest",
Expand All @@ -126,7 +127,7 @@ fn configure_platform(mut platform: MappingConfig) -> MappingConfig {
// - "GetIdentityByNonUniquePublicKeyHashResponse"
//
// "GetEvonodesProposedEpochBlocksResponse" is used for 2 Requests
const VERSIONED_RESPONSES: [&str; 40] = [
const VERSIONED_RESPONSES: [&str; 41] = [
"GetDataContractHistoryResponse",
"GetDataContractResponse",
"GetDataContractsResponse",
Expand Down Expand Up @@ -161,6 +162,7 @@ fn configure_platform(mut platform: MappingConfig) -> MappingConfig {
"GetIdentityTokenInfosResponse",
"GetIdentitiesTokenInfosResponse",
"GetTokenDirectPurchasePricesResponse",
"GetTokenContractInfoResponse",
"GetTokenStatusesResponse",
"GetTokenTotalSupplyResponse",
"GetGroupInfoResponse",
Expand Down
80 changes: 64 additions & 16 deletions packages/dapi-grpc/protos/platform/v0/platform.proto
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ service Platform {
returns (GetTokenStatusesResponse);
rpc getTokenDirectPurchasePrices(GetTokenDirectPurchasePricesRequest)
returns (GetTokenDirectPurchasePricesResponse);
rpc getTokenContractInfo(GetTokenContractInfoRequest)
returns (GetTokenContractInfoResponse);
rpc getTokenPreProgrammedDistributions(
GetTokenPreProgrammedDistributionsRequest)
returns (GetTokenPreProgrammedDistributionsResponse);
Expand Down Expand Up @@ -1370,6 +1372,27 @@ message GetTokenStatusesResponse {
oneof version { GetTokenStatusesResponseV0 v0 = 1; }
}

// Retrieve direct purchase prices defined for one or more tokens.
//
// Some tokens can have a direct purchase price defined using
// `TokenSetPriceForDirectPurchaseTransition` (see `dpp` crate for details).
// This request retrieves the direct purchase prices for those tokens and
// returns [GetTokenDirectPurchasePricesResponse].
message GetTokenDirectPurchasePricesRequest {
message GetTokenDirectPurchasePricesRequestV0 {
// List of token IDs to get prices for.
//
// The list must not be empty.
// Token IDs must have 32 bytes and be unique.
// Results for non-unique token IDs are undefined.
repeated bytes token_ids = 1;
// Whether to return proofs for the response, or just direct response.
bool prove = 2;
}
oneof version { GetTokenDirectPurchasePricesRequestV0 v0 = 1; }
}


// Response to GetTokenDirectPurchasePricesRequest, containing information about
// direct purchase prices defined for requested token IDs.
message GetTokenDirectPurchasePricesResponse {
Expand Down Expand Up @@ -1419,24 +1442,49 @@ message GetTokenDirectPurchasePricesResponse {
oneof version { GetTokenDirectPurchasePricesResponseV0 v0 = 1; }
}

// Retrieve direct purchase prices defined for one or more tokens.
//
// Some tokens can have a direct purchase price defined using
// `TokenSetPriceForDirectPurchaseTransition` (see `dpp` crate for details).
// This request retrieves the direct purchase prices for those tokens and
// returns [GetTokenDirectPurchasePricesResponse].
message GetTokenDirectPurchasePricesRequest {
message GetTokenDirectPurchasePricesRequestV0 {
// List of token IDs to get prices for.
//
// The list must not be empty.
// Token IDs must have 32 bytes and be unique.
// Results for non-unique token IDs are undefined.
repeated bytes token_ids = 1;
// Whether to return proofs for the response, or just direct response.
// Request to retrieve token contract info for a specific token ID.
message GetTokenContractInfoRequest {
message GetTokenContractInfoRequestV0 {
// The token ID to retrieve contract info for.
// Must be exactly 32 bytes.
bytes token_id = 1;

// Whether to return a cryptographic proof.
bool prove = 2;
}
oneof version { GetTokenDirectPurchasePricesRequestV0 v0 = 1; }

oneof version {
GetTokenContractInfoRequestV0 v0 = 1;
}
}

// Response to GetTokenContractInfoRequest.
message GetTokenContractInfoResponse {
message GetTokenContractInfoResponseV0 {
// Direct token contract info.
message TokenContractInfoData {
// The ID of the contract associated with the token.
bytes contract_id = 1;

// The position of the token within the contract.
uint32 token_contract_position = 2;
}

oneof result {
// Direct token contract data
TokenContractInfoData data = 1;

// Cryptographic proof of token contract info
Proof proof = 2;
}

// Metadata about the blockchain state at the time of the query
ResponseMetadata metadata = 3;
}

oneof version {
GetTokenContractInfoResponseV0 v0 = 1;
}
}

message GetTokenPreProgrammedDistributionsRequest {
Expand Down
30 changes: 30 additions & 0 deletions packages/rs-dpp/src/tokens/contract_info/methods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::data_contract::TokenContractPosition;
use crate::tokens::contract_info::v0::TokenContractInfoV0Accessors;
use crate::tokens::contract_info::TokenContractInfo;
use platform_value::Identifier;

impl TokenContractInfoV0Accessors for TokenContractInfo {
fn contract_id(&self) -> Identifier {
match self {
TokenContractInfo::V0(v0) => v0.contract_id(),
}
}

fn set_contract_id(&mut self, contract_id: Identifier) {
match self {
TokenContractInfo::V0(v0) => v0.set_contract_id(contract_id),
}
}

fn token_contract_position(&self) -> TokenContractPosition {
match self {
TokenContractInfo::V0(v0) => v0.token_contract_position(),
}
}

fn set_token_contract_position(&mut self, position: TokenContractPosition) {
match self {
TokenContractInfo::V0(v0) => v0.set_token_contract_position(position),
}
}
}
53 changes: 53 additions & 0 deletions packages/rs-dpp/src/tokens/contract_info/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::data_contract::TokenContractPosition;
use crate::tokens::contract_info::v0::TokenContractInfoV0;
use crate::ProtocolError;
use bincode::Encode;
use derive_more::From;
use platform_serialization::de::Decode;
use platform_serialization_derive::{PlatformDeserialize, PlatformSerialize};
use platform_value::Identifier;
use platform_version::version::PlatformVersion;
use platform_versioning::PlatformVersioned;

mod methods;
pub mod v0;

#[derive(
Debug,
Clone,
Encode,
Decode,
PlatformDeserialize,
PlatformSerialize,
PlatformVersioned,
From,
PartialEq,
)]
#[platform_serialize(unversioned)] //versioned directly, no need to use platform_version
pub enum TokenContractInfo {
V0(TokenContractInfoV0),
}

impl TokenContractInfo {
pub fn new(
contract_id: Identifier,
token_contract_position: TokenContractPosition,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError> {
match platform_version
.dpp
.token_versions
.token_contract_info_default_structure_version
{
0 => Ok(TokenContractInfo::V0(TokenContractInfoV0 {
contract_id,
token_contract_position,
})),
version => Err(ProtocolError::UnknownVersionMismatch {
method: "TokenContractInfo::new".to_string(),
known_versions: vec![0],
received: version,
}),
}
}
}
46 changes: 46 additions & 0 deletions packages/rs-dpp/src/tokens/contract_info/v0/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::data_contract::TokenContractPosition;
use bincode::{Decode, Encode};
use derive_more::From;
use platform_value::Identifier;

#[derive(Debug, Clone, Encode, Decode, From, PartialEq)]
/// Token contract info
pub struct TokenContractInfoV0 {
/// The contract id of the token
pub contract_id: Identifier,
/// The token contract position
pub token_contract_position: TokenContractPosition,
}

/// Accessor trait for `TokenContractInfoV0`
pub trait TokenContractInfoV0Accessors {
/// Returns a reference to the contract ID.
fn contract_id(&self) -> Identifier;

/// Sets the contract ID.
fn set_contract_id(&mut self, contract_id: Identifier);

/// Returns the token contract position.
fn token_contract_position(&self) -> TokenContractPosition;

/// Sets the token contract position.
fn set_token_contract_position(&mut self, position: TokenContractPosition);
}

impl TokenContractInfoV0Accessors for TokenContractInfoV0 {
fn contract_id(&self) -> Identifier {
self.contract_id
}

fn set_contract_id(&mut self, contract_id: Identifier) {
self.contract_id = contract_id;
}

fn token_contract_position(&self) -> TokenContractPosition {
self.token_contract_position
}

fn set_token_contract_position(&mut self, position: TokenContractPosition) {
self.token_contract_position = position;
}
}
1 change: 1 addition & 0 deletions packages/rs-dpp/src/tokens/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::prelude::{
use crate::util::hash::hash_double;

pub mod allowed_currency;
pub mod contract_info;
pub mod emergency_action;
pub mod errors;
pub mod gas_fees_paid_by;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ use drive::drive::prefunded_specialized_balances::prefunded_specialized_balances
use drive::drive::system::misc_path;
use drive::drive::tokens::paths::{
token_distributions_root_path, token_timed_distributions_path, tokens_root_path,
TOKEN_BALANCES_KEY, TOKEN_BLOCK_TIMED_DISTRIBUTIONS_KEY, TOKEN_DIRECT_SELL_PRICE_KEY,
TOKEN_DISTRIBUTIONS_KEY, TOKEN_EPOCH_TIMED_DISTRIBUTIONS_KEY, TOKEN_IDENTITY_INFO_KEY,
TOKEN_MS_TIMED_DISTRIBUTIONS_KEY, TOKEN_PERPETUAL_DISTRIBUTIONS_KEY,
TOKEN_BALANCES_KEY, TOKEN_BLOCK_TIMED_DISTRIBUTIONS_KEY, TOKEN_CONTRACT_INFO_KEY,
TOKEN_DIRECT_SELL_PRICE_KEY, TOKEN_DISTRIBUTIONS_KEY, TOKEN_EPOCH_TIMED_DISTRIBUTIONS_KEY,
TOKEN_IDENTITY_INFO_KEY, TOKEN_MS_TIMED_DISTRIBUTIONS_KEY, TOKEN_PERPETUAL_DISTRIBUTIONS_KEY,
TOKEN_PRE_PROGRAMMED_DISTRIBUTIONS_KEY, TOKEN_STATUS_INFO_KEY, TOKEN_TIMED_DISTRIBUTIONS_KEY,
};
use drive::drive::votes::paths::vote_end_date_queries_tree_path_vec;
Expand Down Expand Up @@ -415,6 +415,15 @@ impl<C> Platform<C> {
&platform_version.drive,
)?;

self.drive.grove_insert_if_not_exists(
(&path).into(),
&[TOKEN_CONTRACT_INFO_KEY],
Element::empty_tree(),
Some(transaction),
None,
&platform_version.drive,
)?;

// The token distribution trees

let token_distributions_path = token_distributions_root_path();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ mod token_selling_tests {
.drive
.fetch_identity_balance(buyer.id().to_buffer(), None, platform_version)
.expect("expected to fetch credit balance");
assert_eq!(buyer_credit_balance, Some(699_868_051_500)); // 10.0 - 3.0 spent - fees =~ 7 dash left
assert_eq!(buyer_credit_balance, Some(699_868_130_120)); // 10.0 - 3.0 spent - fees =~ 7 dash left
}

#[test]
Expand Down
22 changes: 17 additions & 5 deletions packages/rs-drive-abci/src/query/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ use dapi_grpc::platform::v0::{
GetPrefundedSpecializedBalanceResponse, GetProtocolVersionUpgradeStateRequest,
GetProtocolVersionUpgradeStateResponse, GetProtocolVersionUpgradeVoteStatusRequest,
GetProtocolVersionUpgradeVoteStatusResponse, GetStatusRequest, GetStatusResponse,
GetTokenDirectPurchasePricesRequest, GetTokenDirectPurchasePricesResponse,
GetTokenPerpetualDistributionLastClaimRequest, GetTokenPerpetualDistributionLastClaimResponse,
GetTokenPreProgrammedDistributionsRequest, GetTokenPreProgrammedDistributionsResponse,
GetTokenStatusesRequest, GetTokenStatusesResponse, GetTokenTotalSupplyRequest,
GetTokenTotalSupplyResponse, GetTotalCreditsInPlatformRequest,
GetTokenContractInfoRequest, GetTokenContractInfoResponse, GetTokenDirectPurchasePricesRequest,
GetTokenDirectPurchasePricesResponse, GetTokenPerpetualDistributionLastClaimRequest,
GetTokenPerpetualDistributionLastClaimResponse, GetTokenPreProgrammedDistributionsRequest,
GetTokenPreProgrammedDistributionsResponse, GetTokenStatusesRequest, GetTokenStatusesResponse,
GetTokenTotalSupplyRequest, GetTokenTotalSupplyResponse, GetTotalCreditsInPlatformRequest,
GetTotalCreditsInPlatformResponse, GetVotePollsByEndDateRequest, GetVotePollsByEndDateResponse,
WaitForStateTransitionResultRequest, WaitForStateTransitionResultResponse,
};
Expand Down Expand Up @@ -766,6 +766,18 @@ impl PlatformService for QueryService {
.await
}

async fn get_token_contract_info(
&self,
request: Request<GetTokenContractInfoRequest>,
) -> Result<Response<GetTokenContractInfoResponse>, Status> {
self.handle_blocking_query(
request,
Platform::<DefaultCoreRPC>::query_token_contract_info,
"get_token_contract_info",
)
.await
}

async fn get_token_perpetual_distribution_last_claim(
&self,
request: Request<GetTokenPerpetualDistributionLastClaimRequest>,
Expand Down
1 change: 1 addition & 0 deletions packages/rs-drive-abci/src/query/token_queries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod identities_token_balances;
mod identities_token_infos;
mod identity_token_balances;
mod identity_token_infos;
mod token_contract_info;
mod token_direct_purchase_prices;
mod token_perpetual_distribution_last_claim;
mod token_pre_programmed_distributions;
Expand Down
Loading
Loading