diff --git a/packages/js-evo-sdk/tests/unit/facades/tokens.spec.ts b/packages/js-evo-sdk/tests/unit/facades/tokens.spec.ts index 7c184461dd1..cbd294c76dc 100644 --- a/packages/js-evo-sdk/tests/unit/facades/tokens.spec.ts +++ b/packages/js-evo-sdk/tests/unit/facades/tokens.spec.ts @@ -364,6 +364,21 @@ describe('TokensFacade', () => { expect(result.tokenId).to.equal(tokenId); expect(result.balance).to.equal(BigInt(100000000)); }); + + it('should pass a random amount range through to the mint', async () => { + const options = { + tokenId, + amount: BigInt(1000), + randomMinimumAmount: BigInt(100), + recipientId, + identityKey, + signer, + }; + + await client.tokens.mint(options); + + expect(tokenMintStub).to.be.calledOnceWithExactly(options); + }); }); describe('burn()', () => { @@ -402,6 +417,21 @@ describe('TokensFacade', () => { expect(result.senderBalance).to.equal(BigInt(40000000)); expect(result.recipientBalance).to.equal(BigInt(60000000)); }); + + it('should pass a random amount range through to the transfer', async () => { + const options = { + tokenId, + amount: BigInt(500), + randomMinimumAmount: BigInt(10), + recipientId, + identityKey, + signer, + }; + + await client.tokens.transfer(options); + + expect(tokenTransferStub).to.be.calledOnceWithExactly(options); + }); }); describe('freeze()', () => { diff --git a/packages/wasm-sdk/src/state_transitions/token.rs b/packages/wasm-sdk/src/state_transitions/token.rs index 6315b43295b..bda63f96151 100644 --- a/packages/wasm-sdk/src/state_transitions/token.rs +++ b/packages/wasm-sdk/src/state_transitions/token.rs @@ -184,10 +184,19 @@ export interface TokenMintOptions { tokenPosition: number; /** - * The amount of tokens to mint. + * The amount of tokens to mint, or with `randomMinimumAmount` the most a random mint + * can mint (what it reserves). */ amount: bigint; + /** + * Optional smallest amount of a random mint (protocol version 15). When set, the amount + * minted is drawn from `randomMinimumAmount` to `amount`, both included, from the random + * beacon of the block that accepts the mint. A random mint must name `recipientId` and + * cannot be a group action. + */ + randomMinimumAmount?: bigint; + /** * The identity ID of the minter. */ @@ -244,6 +253,8 @@ struct TokenMintOptionsInput { token_position: u16, amount: u64, #[serde(default)] + random_minimum_amount: Option, + #[serde(default)] public_note: Option, } @@ -376,6 +387,11 @@ impl WasmSdk { builder = builder.issued_to_identity_id(recipient_id); } + // Add an optional random amount + if let Some(random_minimum_amount) = parsed.random_minimum_amount { + builder = builder.with_random_minimum_amount(random_minimum_amount as TokenAmount); + } + // Add optional public note if let Some(note) = parsed.public_note { builder = builder.with_public_note(note); @@ -657,10 +673,18 @@ export interface TokenTransferOptions { tokenPosition: number; /** - * The amount of tokens to transfer. + * The amount of tokens to transfer, or with `randomMinimumAmount` the most a random + * transfer can move (what it reserves from the sender). */ amount: bigint; + /** + * Optional smallest amount of a random transfer (protocol version 15). When set, the amount + * transferred is drawn from `randomMinimumAmount` to `amount`, both included, from the + * random beacon of the block that accepts the transfer; the rest stays with the sender. + */ + randomMinimumAmount?: bigint; + /** * The sender's identity ID. */ @@ -708,6 +732,8 @@ struct TokenTransferOptionsInput { token_position: u16, amount: u64, #[serde(default)] + random_minimum_amount: Option, + #[serde(default)] public_note: Option, } @@ -854,6 +880,11 @@ impl WasmSdk { builder = builder.with_public_note(note); } + // Add an optional random amount + if let Some(random_minimum_amount) = parsed.random_minimum_amount { + builder = builder.with_random_minimum_amount(random_minimum_amount as TokenAmount); + } + // Add settings and user fee increase if let Some(ref settings) = settings { builder = builder.with_settings(*settings); @@ -2724,6 +2755,39 @@ mod tests { use dash_sdk::dpp::document::DocumentV0; use dash_sdk::dpp::group::group_action_status::GroupActionStatus; + /// `randomMinimumAmount` reaches the options a random transfer or mint is built from, and + /// its absence leaves the amount fixed: a name mismatch would silently make the operation a + /// fixed one. + #[test] + fn should_read_the_random_minimum_amount_of_a_transfer_and_a_mint() { + let transfer: TokenTransferOptionsInput = serde_json::from_value(serde_json::json!({ + "tokenPosition": 0, + "amount": 500, + "randomMinimumAmount": 10 + })) + .expect("transfer options parse"); + assert_eq!(transfer.amount, 500); + assert_eq!(transfer.random_minimum_amount, Some(10)); + + let mint: TokenMintOptionsInput = serde_json::from_value(serde_json::json!({ + "tokenPosition": 1, + "amount": 1000, + "randomMinimumAmount": 100 + })) + .expect("mint options parse"); + assert_eq!(mint.amount, 1000); + assert_eq!(mint.random_minimum_amount, Some(100)); + + let fixed: TokenTransferOptionsInput = + serde_json::from_value(serde_json::json!({ "tokenPosition": 0, "amount": 500 })) + .expect("fixed transfer options parse"); + assert_eq!(fixed.random_minimum_amount, None); + let fixed: TokenMintOptionsInput = + serde_json::from_value(serde_json::json!({ "tokenPosition": 0, "amount": 500 })) + .expect("fixed mint options parse"); + assert_eq!(fixed.random_minimum_amount, None); + } + #[test] fn should_preserve_closed_group_burn_without_remaining_balance() { let result = TokenBurnResultWasm::from_result(