Skip to content

Commit

Permalink
Add and use TransactionOptionsDto (#1715)
Browse files Browse the repository at this point in the history
* Add and use TransactionOptionsDto

* Use .transpose()

* Add tagged data payload example

* Add change file
  • Loading branch information
Thoralf-M authored Jan 9, 2023
1 parent ec38acf commit 5587a0b
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changes/tagged-data-payload.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nodejs-binding": patch
---

Fix tagged data payload type.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `AccountManager::get_participation_event_ids` method;
- `RequiredStorageDeposit::{alias(), basic(), foundry(), nft()}` getters;
- `TransactionOptionsDto`;

### Changed

- Updated dependencies;
- Message interface methods to accept `TransactionOptionsDto` instead of `TransactionOptions`;

## 1.0.0-rc.4 - 2022-12-23

Expand Down
44 changes: 44 additions & 0 deletions bindings/nodejs/examples/34-tagged-data-payload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* This example will send a transaction with a tagged data payload
*/
const getUnlockedManager = require('./account-manager');

async function run() {
try {
const manager = await getUnlockedManager();
const account = await manager.getAccount('Alice');

await account.sync();

//TODO: Replace with the address of your choice!
const address =
'rms1qrrv7flg6lz5cssvzv2lsdt8c673khad060l4quev6q09tkm9mgtupgf0h0';
const amount = '1000000';

const response = await account.sendAmount([
{
address,
amount,
},
],
{
taggedDataPayload: {
type: 5,
// 'Stardust' hex encoded
tag: "0x5374617264757374", data: "0x5374617264757374"
}
});

console.log(response);

console.log(
`Check your block on http://localhost:14265/api/core/v2/blocks/${response.blockId}`,
);
} catch (error) {
console.log('Error: ', error);
}
process.exit(0);
}


run();
2 changes: 1 addition & 1 deletion src/account/operations/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use iota_client::{
secret::types::InputSigningData,
};

pub use self::options::{RemainderValueStrategy, TransactionOptions};
pub use self::options::{RemainderValueStrategy, TransactionOptions, TransactionOptionsDto};
use crate::{
account::{
handle::AccountHandle,
Expand Down
42 changes: 41 additions & 1 deletion src/account/operations/transaction/options.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use iota_client::block::{output::OutputId, payload::tagged_data::TaggedDataPayload};
use iota_client::block::{
output::OutputId,
payload::{dto::TaggedDataPayloadDto, tagged_data::TaggedDataPayload},
DtoError,
};
use serde::{Deserialize, Serialize};

use crate::account::types::address::AccountAddress;
Expand All @@ -24,6 +28,42 @@ pub struct TransactionOptions {
pub note: Option<String>,
}

impl TransactionOptions {
/// Conversion from [`TransactionOptionsDto`] to [`TransactionOptions`].
pub fn try_from_dto(value: &TransactionOptionsDto) -> Result<Self, DtoError> {
Ok(TransactionOptions {
remainder_value_strategy: value.remainder_value_strategy.clone(),
tagged_data_payload: value
.tagged_data_payload
.as_ref()
.map(TaggedDataPayload::try_from)
.transpose()?,
custom_inputs: value.custom_inputs.clone(),
mandatory_inputs: value.mandatory_inputs.clone(),
allow_burning: value.allow_burning,
note: value.note.clone(),
})
}
}

/// Dto for transaction options
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TransactionOptionsDto {
#[serde(rename = "remainderValueStrategy", default)]
pub remainder_value_strategy: RemainderValueStrategy,
#[serde(rename = "taggedDataPayload", default)]
pub tagged_data_payload: Option<TaggedDataPayloadDto>,
// If custom inputs are provided only they are used. If also other additional inputs should be used,
// `mandatory_inputs` should be used instead.
#[serde(rename = "customInputs", default)]
pub custom_inputs: Option<Vec<OutputId>>,
#[serde(rename = "mandatoryInputs", default)]
pub mandatory_inputs: Option<Vec<OutputId>>,
#[serde(rename = "allowBurning", default)]
pub allow_burning: bool,
pub note: Option<String>,
}

#[allow(clippy::enum_variant_names)]
/// The strategy to use for the remainder value management when sending funds.
#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
36 changes: 18 additions & 18 deletions src/message_interface/account_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::{
},
},
prepare_output::OutputOptionsDto,
TransactionOptions,
TransactionOptionsDto,
},
},
},
Expand Down Expand Up @@ -125,7 +125,7 @@ pub enum AccountMethod {
/// To be burned amount
#[serde(rename = "burnAmount")]
burn_amount: U256Dto,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Burn an nft output. Outputs controlled by it will be swept before if they don't have a storage
/// deposit return, timelock or expiration unlock condition. This should be preferred over burning, because after
Expand All @@ -134,7 +134,7 @@ pub enum AccountMethod {
BurnNft {
#[serde(rename = "nftId")]
nft_id: NftIdDto,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Consolidate outputs.
/// Expected response: [`SentTransaction`](crate::message_interface::Response::SentTransaction)
Expand All @@ -148,7 +148,7 @@ pub enum AccountMethod {
CreateAliasOutput {
#[serde(rename = "aliasOutputOptions")]
alias_output_options: Option<AliasOutputOptionsDto>,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Destroy an alias output. Outputs controlled by it will be swept before if they don't have a
/// storage deposit return, timelock or expiration unlock condition. The amount and possible native tokens will be
Expand All @@ -157,15 +157,15 @@ pub enum AccountMethod {
DestroyAlias {
#[serde(rename = "aliasId")]
alias_id: AliasIdDto,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Function to destroy a foundry output with a circulating supply of 0.
/// Native tokens in the foundry (minted by other foundries) will be transacted to the controlling alias
/// Expected response: [`SentTransaction`](crate::message_interface::Response::SentTransaction)
DestroyFoundry {
#[serde(rename = "foundryId")]
foundry_id: FoundryId,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Generate new unused addresses.
/// Expected response: [`GeneratedAddress`](crate::message_interface::Response::GeneratedAddress)
Expand Down Expand Up @@ -242,7 +242,7 @@ pub enum AccountMethod {
/// To be melted amount
#[serde(rename = "meltAmount")]
melt_amount: U256Dto,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Calculate the minimum required storage deposit for an output.
/// Expected response:
Expand All @@ -259,21 +259,21 @@ pub enum AccountMethod {
mint_amount: U256Dto,
#[serde(rename = "increaseNativeTokenSupplyOptions")]
increase_native_token_supply_options: Option<IncreaseNativeTokenSupplyOptionsDto>,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Mint native token.
/// Expected response: [`MintTokenTransaction`](crate::message_interface::Response::MintTokenTransaction)
MintNativeToken {
#[serde(rename = "nativeTokenOptions")]
native_token_options: NativeTokenOptionsDto,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Mint nft.
/// Expected response: [`SentTransaction`](crate::message_interface::Response::SentTransaction)
MintNfts {
#[serde(rename = "nftsOptions")]
nfts_options: Vec<NftOptionsDto>,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Get account balance information.
/// Expected response: [`Balance`](crate::message_interface::Response::Balance)
Expand All @@ -282,20 +282,20 @@ pub enum AccountMethod {
/// Expected response: [`OutputDto`](crate::message_interface::Response::OutputDto)
PrepareOutput {
options: OutputOptionsDto,
transaction_options: Option<TransactionOptions>,
transaction_options: Option<TransactionOptionsDto>,
},
/// Prepare transaction.
/// Expected response: [`PreparedTransactionData`](crate::message_interface::Response::PreparedTransactionData)
PrepareTransaction {
outputs: Vec<OutputDto>,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Prepare send amount.
/// Expected response: [`PreparedTransactionData`](crate::message_interface::Response::PreparedTransactionData)
PrepareSendAmount {
#[serde(rename = "addressesWithAmount")]
addresses_with_amount: Vec<AddressWithAmountDto>,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Retries (promotes or reattaches) a transaction sent from the account for a provided transaction id until it's
/// included (referenced by a milestone). Returns the included block id.
Expand All @@ -322,28 +322,28 @@ pub enum AccountMethod {
SendAmount {
#[serde(rename = "addressesWithAmount")]
addresses_with_amount: Vec<AddressWithAmountDto>,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Send amount below minimum storage deposit.
/// Expected response: [`SentTransaction`](crate::message_interface::Response::SentTransaction)
SendMicroTransaction {
#[serde(rename = "addressesWithMicroAmount")]
addresses_with_micro_amount: Vec<AddressWithMicroAmountDto>,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Send native tokens.
/// Expected response: [`SentTransaction`](crate::message_interface::Response::SentTransaction)
SendNativeTokens {
#[serde(rename = "addressesNativeTokens")]
addresses_native_tokens: Vec<AddressNativeTokens>,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Send nft.
/// Expected response: [`SentTransaction`](crate::message_interface::Response::SentTransaction)
SendNft {
#[serde(rename = "addressesAndNftIds")]
addresses_nft_ids: Vec<AddressAndNftId>,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Set the alias of the account.
/// Expected response: [`Ok`](crate::message_interface::Response::Ok)
Expand All @@ -352,7 +352,7 @@ pub enum AccountMethod {
/// Expected response: [`SentTransaction`](crate::message_interface::Response::SentTransaction)
SendOutputs {
outputs: Vec<OutputDto>,
options: Option<TransactionOptions>,
options: Option<TransactionOptionsDto>,
},
/// Sign a prepared transaction.
/// Expected response: [`TransactionPayload`](crate::message_interface::Response::TransactionPayload)
Expand Down
Loading

0 comments on commit 5587a0b

Please sign in to comment.