Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/fix/sdk-fetchmany-order' into fi…
Browse files Browse the repository at this point in the history
…x/sdk-fetchmany-order
  • Loading branch information
lklimek committed Oct 4, 2024
2 parents 050751f + 2fcd6ca commit 0278c2b
Show file tree
Hide file tree
Showing 23 changed files with 247 additions and 162 deletions.
Binary file added .yarn/cache/fsevents-patch-19706e7e35-10.zip
Binary file not shown.
37 changes: 19 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,50 @@ const MIN_ASSET_UNLOCK_CORE_FEE_PER_BYTE = 1;
const MINIMAL_WITHDRAWAL_AMOUNT = ASSET_UNLOCK_TX_SIZE * MIN_ASSET_UNLOCK_CORE_FEE_PER_BYTE * 1000;

type WithdrawalOptions = {
signingKeyIndex: number
signingKeyIndex?: number
toAddress?: string
};

/** Creates platform credits withdrawal request
* @param identity - identity to withdraw from
* @param amount - amount of credits to withdraw
* @param to - Dash L1 address
* @param options - withdrawal options
* @param [options] - withdrawal options
* @param [options.toAddress] - withdrawal destination address
*/
export async function creditWithdrawal(
this: Platform,
identity: Identity,
amount: number,
to: string,
options: WithdrawalOptions = {
signingKeyIndex: 3,
},
options: WithdrawalOptions = { },
): Promise<Metadata> {
await this.initialize();

// eslint-disable-next-line no-param-reassign
options = {
...options,
signingKeyIndex: 3,
};

const { dpp } = this;

let toAddress: Address;
try {
toAddress = new Address(to, this.client.network);
} catch (e) {
throw new Error(`Invalid core recipient "${to}" for network ${this.client.network}`);
let outputScriptBytes: Buffer | undefined;
if (options.toAddress) {
let toAddress: Address;
try {
toAddress = new Address(options.toAddress, this.client.network);
} catch (e) {
throw new Error(`Invalid core recipient "${options.toAddress}" for network ${this.client.network}`);
}

const outputScript = Script.buildPublicKeyHashOut(toAddress);
// @ts-ignore
outputScriptBytes = outputScript.toBuffer();

this.logger.debug(`[Identity#creditWithdrawal] credits withdrawal from ${identity.getId().toString()} to ${toAddress.toString()} with amount ${amount}`);
} else {
this.logger.debug(`[Identity#creditWithdrawal] credits withdrawal from ${identity.getId().toString()} to recent withdrawal address with amount ${amount}`);
}
this.logger.debug(`[Identity#creditWithdrawal] credits withdrawal from ${identity.getId().toString()} to ${toAddress.toString()} with amount ${amount}`);

const outputScript = Script.buildPublicKeyHashOut(toAddress);

const balance = identity.getBalance();
if (amount > balance) {
Expand Down Expand Up @@ -88,8 +101,7 @@ export async function creditWithdrawal(
BigInt(amount),
coreFeePerByte,
DEFAULT_POOLING,
// @ts-ignore
outputScript.toBuffer(),
outputScriptBytes,
BigInt(identityNonce),
);

Expand Down
2 changes: 1 addition & 1 deletion packages/rs-dpp/src/identity/identity_facade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl IdentityFacade {
amount: u64,
core_fee_per_byte: u32,
pooling: Pooling,
output_script: CoreScript,
output_script: Option<CoreScript>,
identity_nonce: u64,
) -> Result<IdentityCreditWithdrawalTransition, ProtocolError> {
self.factory.create_identity_credit_withdrawal_transition(
Expand Down
65 changes: 53 additions & 12 deletions packages/rs-dpp/src/identity/identity_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ use crate::state_transition::identity_credit_transfer_transition::IdentityCredit
#[cfg(all(feature = "state-transitions", feature = "client"))]
use crate::state_transition::identity_credit_withdrawal_transition::v0::IdentityCreditWithdrawalTransitionV0;
#[cfg(all(feature = "state-transitions", feature = "client"))]
use crate::state_transition::identity_credit_withdrawal_transition::v1::IdentityCreditWithdrawalTransitionV1;
#[cfg(all(feature = "state-transitions", feature = "client"))]
use crate::state_transition::identity_credit_withdrawal_transition::IdentityCreditWithdrawalTransition;
#[cfg(all(feature = "state-transitions", feature = "client"))]
use crate::state_transition::identity_topup_transition::accessors::IdentityTopUpTransitionAccessorsV0;
Expand Down Expand Up @@ -233,22 +235,61 @@ impl IdentityFactory {
amount: u64,
core_fee_per_byte: u32,
pooling: Pooling,
output_script: CoreScript,
output_script: Option<CoreScript>,
identity_nonce: IdentityNonce,
) -> Result<IdentityCreditWithdrawalTransition, ProtocolError> {
let identity_credit_withdrawal_transition = IdentityCreditWithdrawalTransitionV0 {
identity_id,
amount,
core_fee_per_byte,
pooling,
output_script,
nonce: identity_nonce,
..Default::default()
let platform_version = PlatformVersion::get(self.protocol_version)?;

let identity_credit_withdrawal_transition = match platform_version
.dpp
.state_transitions
.identities
.credit_withdrawal
.default_constructor
{
0 => {
let output_script = output_script.ok_or_else(|| {
ProtocolError::Generic(
"Output script is required for IdentityCreditWithdrawalTransitionV0"
.to_string(),
)
})?;

let transition = IdentityCreditWithdrawalTransitionV0 {
identity_id,
amount,
core_fee_per_byte,
pooling,
output_script,
nonce: identity_nonce,
..Default::default()
};

IdentityCreditWithdrawalTransition::from(transition)
}
1 => {
let transition = IdentityCreditWithdrawalTransitionV1 {
identity_id,
amount,
core_fee_per_byte,
pooling,
output_script,
nonce: identity_nonce,
..Default::default()
};

IdentityCreditWithdrawalTransition::from(transition)
}
version => {
return Err(ProtocolError::UnknownVersionMismatch {
method: "create_identity_credit_withdrawal_transition".to_string(),
known_versions: vec![0, 1],
received: version,
});
}
};

Ok(IdentityCreditWithdrawalTransition::from(
identity_credit_withdrawal_transition,
))
Ok(identity_credit_withdrawal_transition)
}

#[cfg(all(feature = "state-transitions", feature = "client"))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ impl IdentityCreditWithdrawalTransition {
pub fn default_versioned(platform_version: &PlatformVersion) -> Result<Self, ProtocolError> {
match platform_version
.dpp
.identity_versions
.identity_structure_version
.state_transitions
.identities
.credit_withdrawal
.default_constructor
{
0 => Ok(IdentityCreditWithdrawalTransition::V0(
IdentityCreditWithdrawalTransitionV0::default(),
Expand Down
12 changes: 6 additions & 6 deletions packages/rs-drive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ enum-map = { version = "2.0.3", optional = true }
intmap = { version = "2.0.0", features = ["serde"], optional = true }
chrono = { version = "0.4.35", optional = true }
itertools = { version = "0.11.0", optional = true }
grovedb = { version = "2.0.3", optional = true, default-features = false }
grovedb-costs = { version = "2.0.3", optional = true }
grovedb-path = { version = "2.0.3" }
grovedb-storage = { version = "2.0.3", optional = true }
grovedb-version = { version = "2.0.3"}
grovedb-epoch-based-storage-flags = { version = "2.0.3"}
grovedb = { version = "2.1.0", optional = true, default-features = false }
grovedb-costs = { version = "2.1.0", optional = true }
grovedb-path = { version = "2.1.0" }
grovedb-storage = { version = "2.1.0", optional = true }
grovedb-version = { version = "2.1.0"}
grovedb-epoch-based-storage-flags = { version = "2.1.0"}

[dev-dependencies]
criterion = "0.3.5"
Expand Down
Loading

0 comments on commit 0278c2b

Please sign in to comment.