-
Notifications
You must be signed in to change notification settings - Fork 54
feat(platform)!: fees for data contract creation and update #2584
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 all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7a010ea
feat(platform): fees for data contract creation and update
QuantumExplorer a861ed2
clippy fixes
QuantumExplorer 4484bb7
fixed js test
QuantumExplorer 42083cf
fixed js
QuantumExplorer c5bb488
fixed js
QuantumExplorer b69944d
increase funding in js
QuantumExplorer ad6c71a
fix
QuantumExplorer 3c0aa42
fix
QuantumExplorer 868fa9c
another fix
QuantumExplorer 5609045
last fix
QuantumExplorer 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file renamed
BIN
+5.03 KB
...ase-x-npm-3.0.9-7b2588e106-957101d6fd.zip → ...se-x-npm-3.0.11-3798da0834-c2e3c443fd.zip
Binary file not shown.
Binary file not shown.
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
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
84 changes: 84 additions & 0 deletions
84
packages/rs-dpp/src/data_contract/methods/registration_cost/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,84 @@ | ||
| mod v1; | ||
|
|
||
| use crate::data_contract::serialized_version::DataContractInSerializationFormat; | ||
| use crate::data_contract::DataContract; | ||
| use crate::ProtocolError; | ||
| use platform_version::version::PlatformVersion; | ||
|
|
||
| impl DataContract { | ||
| /// Returns the registration cost of the data contract based on the current platform version | ||
| /// and the number of associated search keywords. | ||
| /// | ||
| /// This method dispatches to a version-specific implementation based on the | ||
| /// platform version configuration. If the version is unrecognized, it returns a version mismatch error. | ||
| /// | ||
| /// # Arguments | ||
| /// - `platform_version`: A reference to the platform version, used to determine which | ||
| /// registration cost algorithm to apply. | ||
| /// | ||
| /// # Returns | ||
| /// - `Ok(u64)`: The total registration cost in credits for this contract. | ||
| /// - `Err(ProtocolError)`: If the platform version is unrecognized or if the fee computation overflows. | ||
| /// | ||
| /// # Version Behavior | ||
| /// - Version 0: Always returns `0` (used before protocol version 9, ie before 2.0, where registration cost was not charged). | ||
| /// - Version 1: Uses a detailed cost model based on document types, indexes, tokens, and keyword count. | ||
| pub fn registration_cost( | ||
| &self, | ||
| platform_version: &PlatformVersion, | ||
| ) -> Result<u64, ProtocolError> { | ||
| match platform_version | ||
| .dpp | ||
| .contract_versions | ||
| .methods | ||
| .registration_cost | ||
| { | ||
| 0 => Ok(0), // Before 2.0 it's just 0 (There was some validation cost) | ||
| 1 => Ok(self.registration_cost_v1(platform_version)), | ||
| version => Err(ProtocolError::UnknownVersionMismatch { | ||
| method: "DataContract::registration_cost".to_string(), | ||
| known_versions: vec![0, 1], | ||
| received: version, | ||
| }), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl DataContractInSerializationFormat { | ||
| /// Returns the registration cost of the data contract based on the current platform version | ||
| /// and the number of associated search keywords. | ||
| /// | ||
| /// This method dispatches to a version-specific implementation based on the | ||
| /// platform version configuration. If the version is unrecognized, it returns a version mismatch error. | ||
| /// | ||
| /// # Arguments | ||
| /// - `platform_version`: A reference to the platform version, used to determine which | ||
| /// registration cost algorithm to apply. | ||
| /// | ||
| /// # Returns | ||
| /// - `Ok(u64)`: The total registration cost in credits for this contract. | ||
| /// - `Err(ProtocolError)`: If the platform version is unrecognized or if the fee computation overflows. | ||
| /// | ||
| /// # Version Behavior | ||
| /// - Version 0: Always returns `0` (used before protocol version 9, ie before 2.0, where registration cost was not charged). | ||
| /// - Version 1: Uses a detailed cost model based on document types, indexes, tokens, and keyword count. | ||
| pub fn registration_cost( | ||
| &self, | ||
| platform_version: &PlatformVersion, | ||
| ) -> Result<u64, ProtocolError> { | ||
| match platform_version | ||
| .dpp | ||
| .contract_versions | ||
| .methods | ||
| .registration_cost | ||
| { | ||
| 0 => Ok(0), // Before 2.0 it's just 0 (There was some validation cost) | ||
| 1 => Ok(self.registration_cost_v1(platform_version)), | ||
| version => Err(ProtocolError::UnknownVersionMismatch { | ||
| method: "DataContract::registration_cost".to_string(), | ||
| known_versions: vec![0, 1], | ||
| received: version, | ||
| }), | ||
| } | ||
| } | ||
| } | ||
159 changes: 159 additions & 0 deletions
159
packages/rs-dpp/src/data_contract/methods/registration_cost/v1/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,159 @@ | ||
| use crate::data_contract::accessors::v0::DataContractV0Getters; | ||
| use crate::data_contract::accessors::v1::DataContractV1Getters; | ||
| use crate::data_contract::associated_token::token_configuration::accessors::v0::TokenConfigurationV0Getters; | ||
| use crate::data_contract::associated_token::token_distribution_rules::accessors::v0::TokenDistributionRulesV0Getters; | ||
| use crate::data_contract::document_type::accessors::DocumentTypeV0Getters; | ||
| use crate::data_contract::document_type::Index; | ||
| use crate::data_contract::serialized_version::DataContractInSerializationFormat; | ||
| use crate::fee::Credits; | ||
| use crate::prelude::DataContract; | ||
| use platform_value::Value; | ||
| use platform_version::version::PlatformVersion; | ||
|
|
||
| impl DataContract { | ||
| /// Computes the registration cost of a data contract based on its document types, | ||
| /// indexes, token configurations, and keyword count. | ||
| /// | ||
| /// # Parameters | ||
| /// - `platform_version`: A reference to the current platform version providing fee parameters. | ||
| /// | ||
| /// # Returns | ||
| /// - `Ok(Credits)`: The total registration cost in credits if all additions are overflow-safe. | ||
| /// - `Err(ProtocolError)`: If any arithmetic operation results in overflow. | ||
| /// | ||
| /// # Fee Components | ||
| /// - Base contract registration fee. | ||
| /// - Per document type registration fee. | ||
| /// - Per index registration fee (unique and non-unique). | ||
| /// - Token registration fee per token. | ||
| /// - Additional fees for tokens using perpetual or pre-programmed distribution. | ||
| /// - Search keyword fees (`keyword_count * search_keyword_fee`). | ||
| pub(super) fn registration_cost_v1(&self, platform_version: &PlatformVersion) -> Credits { | ||
| let fee_version = &platform_version.fee_version.data_contract_registration; | ||
| let mut cost = fee_version.base_contract_registration_fee; | ||
|
|
||
| for document_type in self.document_types().values() { | ||
| cost = cost.saturating_add(fee_version.document_type_registration_fee); | ||
|
|
||
| for index in document_type.indexes().values() { | ||
| let base_index_fee = if index.contested_index.is_some() { | ||
| fee_version.document_type_base_contested_index_registration_fee | ||
| } else if index.unique { | ||
| fee_version.document_type_base_unique_index_registration_fee | ||
| } else { | ||
| fee_version.document_type_base_non_unique_index_registration_fee | ||
| }; | ||
|
|
||
| cost = cost.saturating_add(base_index_fee); | ||
| } | ||
| } | ||
|
|
||
| for token_config in self.tokens().values() { | ||
| cost = cost.saturating_add(fee_version.token_registration_fee); | ||
|
|
||
| if token_config | ||
| .distribution_rules() | ||
| .perpetual_distribution() | ||
| .is_some() | ||
| { | ||
| cost = cost.saturating_add(fee_version.token_uses_perpetual_distribution_fee); | ||
| } | ||
|
|
||
| if token_config | ||
| .distribution_rules() | ||
| .pre_programmed_distribution() | ||
| .is_some() | ||
| { | ||
| cost = cost.saturating_add(fee_version.token_uses_pre_programmed_distribution_fee); | ||
| } | ||
| } | ||
|
|
||
| let keyword_cost = fee_version | ||
| .search_keyword_fee | ||
| .saturating_mul(self.keywords().len() as u64); | ||
|
|
||
| cost = cost.saturating_add(keyword_cost); | ||
|
|
||
| cost | ||
| } | ||
|
QuantumExplorer marked this conversation as resolved.
|
||
| } | ||
|
|
||
| impl DataContractInSerializationFormat { | ||
| /// Computes the registration cost of a data contract based on its document types, | ||
| /// indexes, token configurations, and keyword count. | ||
| /// | ||
| /// # Parameters | ||
| /// - `platform_version`: A reference to the current platform version providing fee parameters. | ||
| /// | ||
| /// # Returns | ||
| /// - `Ok(Credits)`: The total registration cost in credits if all additions are overflow-safe. | ||
| /// - `Err(ProtocolError)`: If any arithmetic operation results in overflow. | ||
| /// | ||
| /// # Fee Components | ||
| /// - Base contract registration fee. | ||
| /// - Per document type registration fee. | ||
| /// - Per index registration fee (unique and non-unique). | ||
| /// - Token registration fee per token. | ||
| /// - Additional fees for tokens using perpetual or pre-programmed distribution. | ||
| /// - Search keyword fees (`keyword_count * search_keyword_fee`). | ||
| pub(super) fn registration_cost_v1(&self, platform_version: &PlatformVersion) -> Credits { | ||
| let fee_version = &platform_version.fee_version.data_contract_registration; | ||
| let mut cost = fee_version.base_contract_registration_fee; | ||
|
|
||
| for document_type_schema in self.document_schemas().values() { | ||
| cost = cost.saturating_add(fee_version.document_type_registration_fee); | ||
|
|
||
| // If this is not okay the registration will fail on basic validation | ||
| if let Ok(schema_map) = document_type_schema.to_map() { | ||
| // Initialize indices | ||
| if let Ok(Some(index_values)) = Value::inner_optional_array_slice_value( | ||
| schema_map, | ||
| crate::data_contract::document_type::property_names::INDICES, | ||
| ) { | ||
| for index_value in index_values { | ||
| if let Ok(index_value_map) = index_value.to_map() { | ||
| if let Ok(index) = Index::try_from(index_value_map.as_slice()) { | ||
| let base_index_fee = if index.contested_index.is_some() { | ||
| fee_version.document_type_base_contested_index_registration_fee | ||
| } else if index.unique { | ||
| fee_version.document_type_base_unique_index_registration_fee | ||
| } else { | ||
| fee_version.document_type_base_non_unique_index_registration_fee | ||
| }; | ||
| cost = cost.saturating_add(base_index_fee); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| for token_config in self.tokens().values() { | ||
| cost = cost.saturating_add(fee_version.token_registration_fee); | ||
|
|
||
| if token_config | ||
| .distribution_rules() | ||
| .perpetual_distribution() | ||
| .is_some() | ||
| { | ||
| cost = cost.saturating_add(fee_version.token_uses_perpetual_distribution_fee); | ||
| } | ||
|
|
||
| if token_config | ||
| .distribution_rules() | ||
| .pre_programmed_distribution() | ||
| .is_some() | ||
| { | ||
| cost = cost.saturating_add(fee_version.token_uses_pre_programmed_distribution_fee); | ||
| } | ||
| } | ||
|
|
||
| let keyword_cost = fee_version | ||
| .search_keyword_fee | ||
| .saturating_mul(self.keywords().len() as u64); | ||
|
|
||
| cost = cost.saturating_add(keyword_cost); | ||
|
|
||
| cost | ||
| } | ||
| } | ||
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
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.