-
Notifications
You must be signed in to change notification settings - Fork 43
feat(sdk): token purchase and set price transitions #2613
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
use crate::platform::transition::put_settings::PutSettings; | ||
use crate::platform::Identifier; | ||
use crate::{Error, Sdk}; | ||
use dpp::balances::credits::TokenAmount; | ||
use dpp::data_contract::accessors::v0::DataContractV0Getters; | ||
use dpp::data_contract::{DataContract, TokenContractPosition}; | ||
use dpp::fee::Credits; | ||
use dpp::identity::signer::Signer; | ||
use dpp::identity::IdentityPublicKey; | ||
use dpp::prelude::UserFeeIncrease; | ||
use dpp::state_transition::batch_transition::methods::v1::DocumentsBatchTransitionMethodsV1; | ||
use dpp::state_transition::batch_transition::methods::StateTransitionCreationOptions; | ||
use dpp::state_transition::batch_transition::BatchTransition; | ||
use dpp::state_transition::StateTransition; | ||
use dpp::tokens::calculate_token_id; | ||
use dpp::version::PlatformVersion; | ||
|
||
/// A builder to configure and broadcast token purchase transitions | ||
pub struct TokenDirectPurchaseTransitionBuilder<'a> { | ||
data_contract: &'a DataContract, | ||
token_position: TokenContractPosition, | ||
actor_id: Identifier, | ||
amount: TokenAmount, | ||
total_agreed_price: Credits, | ||
settings: Option<PutSettings>, | ||
user_fee_increase: Option<UserFeeIncrease>, | ||
} | ||
|
||
impl<'a> TokenDirectPurchaseTransitionBuilder<'a> { | ||
/// Start building a purchase tokens request for the provided DataContract. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `data_contract` - A reference to the data contract | ||
/// * `token_position` - The position of the token in the contract | ||
/// * `issuer_id` - The identifier of the issuer | ||
/// * `amount` - The amount of tokens to purchase | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Self` - The new builder instance | ||
pub fn new( | ||
data_contract: &'a DataContract, | ||
token_position: TokenContractPosition, | ||
actor_id: Identifier, | ||
amount: TokenAmount, | ||
total_agreed_price: Credits, | ||
) -> Self { | ||
// TODO: Validate token position | ||
|
||
Self { | ||
data_contract, | ||
token_position, | ||
actor_id, | ||
amount, | ||
total_agreed_price, | ||
settings: None, | ||
user_fee_increase: None, | ||
} | ||
} | ||
|
||
/// Adds a user fee increase to the token purchase transition | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `user_fee_increase` - The user fee increase to add | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Self` - The updated builder | ||
pub fn with_user_fee_increase(mut self, user_fee_increase: UserFeeIncrease) -> Self { | ||
self.user_fee_increase = Some(user_fee_increase); | ||
self | ||
} | ||
|
||
/// Adds settings to the token purchase transition | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `settings` - The settings to add | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Self` - The updated builder | ||
pub fn with_settings(mut self, settings: PutSettings) -> Self { | ||
self.settings = Some(settings); | ||
self | ||
} | ||
|
||
/// Signs the token purchase transition | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `sdk` - The SDK instance | ||
/// * `identity_public_key` - The public key of the identity | ||
/// * `signer` - The signer instance | ||
/// * `platform_version` - The platform version | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Result<StateTransition, Error>` - The signed state transition or an error | ||
pub async fn sign( | ||
&self, | ||
sdk: &Sdk, | ||
identity_public_key: &IdentityPublicKey, | ||
signer: &impl Signer, | ||
platform_version: &PlatformVersion, | ||
options: Option<StateTransitionCreationOptions>, | ||
) -> Result<StateTransition, Error> { | ||
let token_id = Identifier::from(calculate_token_id( | ||
self.data_contract.id().as_bytes(), | ||
self.token_position, | ||
)); | ||
|
||
Comment on lines
+110
to
+114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion No check that if self.token_position >= self.data_contract.tokens().len() {
return Err(Error::InvalidTokenPosition(self.token_position));
} 🤖 Prompt for AI Agents
|
||
let identity_contract_nonce = sdk | ||
.get_identity_contract_nonce( | ||
self.actor_id, | ||
self.data_contract.id(), | ||
true, | ||
self.settings, | ||
) | ||
.await?; | ||
|
||
let state_transition = BatchTransition::new_token_direct_purchase_transition( | ||
token_id, | ||
self.actor_id, | ||
self.data_contract.id(), | ||
self.token_position, | ||
self.amount, | ||
self.total_agreed_price, | ||
identity_public_key, | ||
identity_contract_nonce, | ||
self.user_fee_increase.unwrap_or_default(), | ||
signer, | ||
platform_version, | ||
options, | ||
)?; | ||
|
||
Ok(state_transition) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
use crate::platform::transition::put_settings::PutSettings; | ||
use crate::platform::Identifier; | ||
use crate::{Error, Sdk}; | ||
use dpp::data_contract::accessors::v0::DataContractV0Getters; | ||
use dpp::data_contract::{DataContract, TokenContractPosition}; | ||
use dpp::group::GroupStateTransitionInfoStatus; | ||
use dpp::identity::signer::Signer; | ||
use dpp::identity::IdentityPublicKey; | ||
use dpp::prelude::UserFeeIncrease; | ||
use dpp::state_transition::batch_transition::methods::v1::DocumentsBatchTransitionMethodsV1; | ||
use dpp::state_transition::batch_transition::methods::StateTransitionCreationOptions; | ||
use dpp::state_transition::batch_transition::BatchTransition; | ||
pauldelucia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use dpp::state_transition::StateTransition; | ||
use dpp::tokens::calculate_token_id; | ||
use dpp::tokens::token_pricing_schedule::TokenPricingSchedule; | ||
use dpp::version::PlatformVersion; | ||
|
||
/// A builder to configure and broadcast token change direct purchase price transitions | ||
pub struct TokenChangeDirectPurchasePriceTransitionBuilder<'a> { | ||
data_contract: &'a DataContract, | ||
token_position: TokenContractPosition, | ||
actor_id: Identifier, | ||
token_pricing_schedule: Option<TokenPricingSchedule>, | ||
public_note: Option<String>, | ||
settings: Option<PutSettings>, | ||
user_fee_increase: Option<UserFeeIncrease>, | ||
using_group_info: Option<GroupStateTransitionInfoStatus>, | ||
} | ||
|
||
impl<'a> TokenChangeDirectPurchasePriceTransitionBuilder<'a> { | ||
/// Start building a change direct purchase price tokens request for the provided DataContract. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `data_contract` - A reference to the data contract | ||
/// * `token_position` - The position of the token in the contract | ||
/// * `issuer_id` - The identifier of the issuer | ||
/// * `amount` - The amount of tokens to change direct purchase price | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Self` - The new builder instance | ||
pub fn new( | ||
data_contract: &'a DataContract, | ||
token_position: TokenContractPosition, | ||
issuer_id: Identifier, | ||
token_pricing_schedule: Option<TokenPricingSchedule>, | ||
) -> Self { | ||
// TODO: Validate token position | ||
|
||
Self { | ||
data_contract, | ||
token_position, | ||
actor_id: issuer_id, | ||
token_pricing_schedule, | ||
public_note: None, | ||
settings: None, | ||
user_fee_increase: None, | ||
using_group_info: None, | ||
} | ||
} | ||
|
||
/// Adds a public note to the token change direct purchase price transition | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `note` - The public note to add | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Self` - The updated builder | ||
pub fn with_public_note(mut self, note: String) -> Self { | ||
self.public_note = Some(note); | ||
self | ||
} | ||
|
||
/// Adds a user fee increase to the token change direct purchase price transition | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `user_fee_increase` - The user fee increase to add | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Self` - The updated builder | ||
pub fn with_user_fee_increase(mut self, user_fee_increase: UserFeeIncrease) -> Self { | ||
self.user_fee_increase = Some(user_fee_increase); | ||
self | ||
} | ||
|
||
/// Adds group information to the token change direct purchase price transition | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `group_info` - The group information to add | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Self` - The updated builder | ||
pub fn with_using_group_info(mut self, group_info: GroupStateTransitionInfoStatus) -> Self { | ||
self.using_group_info = Some(group_info); | ||
|
||
// TODO: Simplify group actions automatically find position if group action is required | ||
QuantumExplorer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
self | ||
} | ||
|
||
/// Adds settings to the token change direct purchase price transition | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `settings` - The settings to add | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Self` - The updated builder | ||
pub fn with_settings(mut self, settings: PutSettings) -> Self { | ||
self.settings = Some(settings); | ||
self | ||
} | ||
|
||
/// Signs the token change direct purchase price transition | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `sdk` - The SDK instance | ||
/// * `identity_public_key` - The public key of the identity | ||
/// * `signer` - The signer instance | ||
/// * `platform_version` - The platform version | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Result<StateTransition, Error>` - The signed state transition or an error | ||
pub async fn sign( | ||
&self, | ||
sdk: &Sdk, | ||
identity_public_key: &IdentityPublicKey, | ||
signer: &impl Signer, | ||
platform_version: &PlatformVersion, | ||
options: Option<StateTransitionCreationOptions>, | ||
) -> Result<StateTransition, Error> { | ||
let token_id = Identifier::from(calculate_token_id( | ||
self.data_contract.id().as_bytes(), | ||
self.token_position, | ||
)); | ||
|
||
let identity_contract_nonce = sdk | ||
.get_identity_contract_nonce( | ||
self.actor_id, | ||
self.data_contract.id(), | ||
true, | ||
self.settings, | ||
) | ||
.await?; | ||
|
||
let state_transition = BatchTransition::new_token_change_direct_purchase_price_transition( | ||
token_id, | ||
self.actor_id, | ||
self.data_contract.id(), | ||
self.token_position, | ||
self.token_pricing_schedule.clone(), | ||
self.public_note.clone(), | ||
self.using_group_info, | ||
identity_public_key, | ||
identity_contract_nonce, | ||
self.user_fee_increase.unwrap_or_default(), | ||
signer, | ||
platform_version, | ||
options, | ||
)?; | ||
|
||
Ok(state_transition) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.