From f9c7aef1a02b797dc0e12a17c0ca93109097f3c9 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 12 Jul 2023 09:57:40 +0200 Subject: [PATCH 1/9] Reorg node_api tests --- .../client/{node_api.rs => node_api/core.rs} | 159 +----------------- sdk/tests/client/node_api/mod.rs | 110 ++++++++++++ sdk/tests/client/node_api/mqtt.rs | 59 +++++++ 3 files changed, 174 insertions(+), 154 deletions(-) rename sdk/tests/client/{node_api.rs => node_api/core.rs} (64%) create mode 100644 sdk/tests/client/node_api/mod.rs create mode 100644 sdk/tests/client/node_api/mqtt.rs diff --git a/sdk/tests/client/node_api.rs b/sdk/tests/client/node_api/core.rs similarity index 64% rename from sdk/tests/client/node_api.rs rename to sdk/tests/client/node_api/core.rs index 284dc8b644..5628c2a6d3 100644 --- a/sdk/tests/client/node_api.rs +++ b/sdk/tests/client/node_api/core.rs @@ -4,110 +4,17 @@ // These are E2E test samples, so they are ignored by default. use iota_sdk::{ - client::{ - api::GetAddressesOptions, bech32_to_hex, node_api::indexer::query_parameters::QueryParameter, - request_funds_from_faucet, secret::SecretManager, Client, NodeInfoWrapper, - }, + client::{api::GetAddressesOptions, node_api::indexer::query_parameters::QueryParameter, Client, NodeInfoWrapper}, types::block::{ - address::ToBech32Ext, output::{Output, OutputId}, - payload::{transaction::TransactionId, Payload}, - Block, BlockId, + payload::Payload, + Block, }, }; use packable::PackableExt; -use crate::client::common::{setup_client_with_node_health_ignored, FAUCET_URL, NODE_LOCAL}; - -// THIS SEED SERVES FOR TESTING PURPOSES! DON'T USE THIS SEED IN PRODUCTION! -const DEFAULT_DEVELOPMENT_SEED: &str = "0x256a818b2aac458941f7274985a410e57fb750f3a3a67969ece5bd9ae7eef5b2"; - -// Sends a tagged data block to the node to test against it. -async fn setup_tagged_data_block() -> BlockId { - let client = setup_client_with_node_health_ignored().await; - - client - .build_block() - .with_tag(b"Hello".to_vec()) - .with_data(b"Tangle".to_vec()) - .finish() - .await - .unwrap() - .id() -} - -pub fn setup_secret_manager() -> SecretManager { - SecretManager::try_from_hex_seed(DEFAULT_DEVELOPMENT_SEED.to_owned()).unwrap() -} - -// Sends a transaction block to the node to test against it. -async fn setup_transaction_block() -> (BlockId, TransactionId) { - let client = setup_client_with_node_health_ignored().await; - let secret_manager = setup_secret_manager(); - - let addresses = secret_manager - .generate_ed25519_addresses( - GetAddressesOptions::from_client(&client) - .await - .unwrap() - .with_range(0..2), - ) - .await - .unwrap(); - println!( - "{}", - request_funds_from_faucet(FAUCET_URL, &addresses[0]).await.unwrap() - ); - - // Continue only after funds are received - for _ in 0..30 { - tokio::time::sleep(std::time::Duration::from_secs(1)).await; - let output_ids_response = client - .basic_output_ids([ - QueryParameter::Address(addresses[0]), - QueryParameter::HasExpiration(false), - QueryParameter::HasTimelock(false), - QueryParameter::HasStorageDepositReturn(false), - ]) - .await - .unwrap(); - - if !output_ids_response.items.is_empty() { - break; - } - } - - let block_id = client - .build_block() - .with_secret_manager(&secret_manager) - .with_output_hex( - // Send funds back to the sender. - &bech32_to_hex(addresses[1].to_bech32(client.get_bech32_hrp().await.unwrap())).unwrap(), - // The amount to spend, cannot be zero. - 1_000_000, - ) - .await - .unwrap() - .finish() - .await - .unwrap() - .id(); - - let block = setup_client_with_node_health_ignored() - .await - .get_block(&block_id) - .await - .unwrap(); - - let transaction_id = match block.payload() { - Some(Payload::Transaction(t)) => t.id(), - _ => unreachable!(), - }; - - let _ = client.retry_until_included(&block.id(), None, None).await.unwrap(); - - (block_id, transaction_id) -} +use super::{setup_secret_manager, setup_tagged_data_block, setup_transaction_block}; +use crate::client::common::{setup_client_with_node_health_ignored, NODE_LOCAL}; #[ignore] #[tokio::test] @@ -433,62 +340,6 @@ async fn test_get_included_block_raw() { assert_eq!(block, block_raw); } -#[ignore] -#[tokio::test] -#[cfg(feature = "mqtt")] -async fn test_mqtt() { - use iota_sdk::client::mqtt::{MqttEvent, MqttPayload, Topic}; - use tokio::sync::mpsc::error::TrySendError; - - const BUFFER_SIZE: usize = 10; - - let client = setup_client_with_node_health_ignored().await; - - let (tx, mut rx) = tokio::sync::mpsc::channel(BUFFER_SIZE); - - client - .subscribe( - [ - Topic::new("milestone-info/latest").unwrap(), - Topic::new("blocks").unwrap(), - ], - move |evt| { - match &evt.payload { - MqttPayload::Block(_) => { - assert_eq!(evt.topic, "blocks"); - } - MqttPayload::Json(_) => { - assert_eq!(evt.topic, "milestone-info/latest"); - } - _ => panic!("unexpected mqtt payload type: {:?}", evt), - } - match tx.try_send(()) { - Ok(_) | Err(TrySendError::Full(_)) => (), - e => e.unwrap(), - } - }, - ) - .await - .unwrap(); - - // Wait for messages to come through - for i in 0..BUFFER_SIZE { - tokio::select! { - _ = rx.recv() => { - if i == 7 { - client.unsubscribe([Topic::new("blocks").unwrap()]).await.unwrap(); - } - } - _ = async { - client.mqtt_event_receiver().await.wait_for(|msg| *msg == MqttEvent::Disconnected).await.unwrap(); - } => { - panic!("mqtt disconnected"); - } - } - } - client.subscriber().disconnect().await.unwrap(); -} - #[ignore] #[tokio::test] async fn test_call_plugin_route() { diff --git a/sdk/tests/client/node_api/mod.rs b/sdk/tests/client/node_api/mod.rs new file mode 100644 index 0000000000..41c192050b --- /dev/null +++ b/sdk/tests/client/node_api/mod.rs @@ -0,0 +1,110 @@ +// Copyright 2023 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +mod core; +#[cfg(feature = "mqtt")] +mod mqtt; + +use iota_sdk::{ + client::{ + api::GetAddressesOptions, bech32_to_hex, node_api::indexer::query_parameters::QueryParameter, + request_funds_from_faucet, secret::SecretManager, + }, + types::block::{ + address::ToBech32Ext, + payload::{transaction::TransactionId, Payload}, + BlockId, + }, +}; + +use crate::client::common::{setup_client_with_node_health_ignored, FAUCET_URL}; + +// THIS SEED SERVES FOR TESTING PURPOSES! DON'T USE THIS SEED IN PRODUCTION! +const DEFAULT_DEVELOPMENT_SEED: &str = "0x256a818b2aac458941f7274985a410e57fb750f3a3a67969ece5bd9ae7eef5b2"; + +// Sends a tagged data block to the node to test against it. +async fn setup_tagged_data_block() -> BlockId { + let client = setup_client_with_node_health_ignored().await; + + client + .build_block() + .with_tag(b"Hello".to_vec()) + .with_data(b"Tangle".to_vec()) + .finish() + .await + .unwrap() + .id() +} + +pub fn setup_secret_manager() -> SecretManager { + SecretManager::try_from_hex_seed(DEFAULT_DEVELOPMENT_SEED.to_owned()).unwrap() +} + +// Sends a transaction block to the node to test against it. +async fn setup_transaction_block() -> (BlockId, TransactionId) { + let client = setup_client_with_node_health_ignored().await; + let secret_manager = setup_secret_manager(); + + let addresses = secret_manager + .generate_ed25519_addresses( + GetAddressesOptions::from_client(&client) + .await + .unwrap() + .with_range(0..2), + ) + .await + .unwrap(); + println!( + "{}", + request_funds_from_faucet(FAUCET_URL, &addresses[0]).await.unwrap() + ); + + // Continue only after funds are received + for _ in 0..30 { + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + let output_ids_response = client + .basic_output_ids([ + QueryParameter::Address(addresses[0]), + QueryParameter::HasExpiration(false), + QueryParameter::HasTimelock(false), + QueryParameter::HasStorageDepositReturn(false), + ]) + .await + .unwrap(); + + if !output_ids_response.items.is_empty() { + break; + } + } + + let block_id = client + .build_block() + .with_secret_manager(&secret_manager) + .with_output_hex( + // Send funds back to the sender. + &bech32_to_hex(addresses[1].to_bech32(client.get_bech32_hrp().await.unwrap())).unwrap(), + // The amount to spend, cannot be zero. + 1_000_000, + ) + .await + .unwrap() + .finish() + .await + .unwrap() + .id(); + + let block = setup_client_with_node_health_ignored() + .await + .get_block(&block_id) + .await + .unwrap(); + + let transaction_id = match block.payload() { + Some(Payload::Transaction(t)) => t.id(), + _ => unreachable!(), + }; + + let _ = client.retry_until_included(&block.id(), None, None).await.unwrap(); + + (block_id, transaction_id) +} diff --git a/sdk/tests/client/node_api/mqtt.rs b/sdk/tests/client/node_api/mqtt.rs new file mode 100644 index 0000000000..ff93d98b71 --- /dev/null +++ b/sdk/tests/client/node_api/mqtt.rs @@ -0,0 +1,59 @@ +// Copyright 2023 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +use crate::client::common::setup_client_with_node_health_ignored; + +#[ignore] +#[tokio::test] +async fn test_mqtt() { + use iota_sdk::client::mqtt::{MqttEvent, MqttPayload, Topic}; + use tokio::sync::mpsc::error::TrySendError; + + const BUFFER_SIZE: usize = 10; + + let client = setup_client_with_node_health_ignored().await; + + let (tx, mut rx) = tokio::sync::mpsc::channel(BUFFER_SIZE); + + client + .subscribe( + [ + Topic::new("milestone-info/latest").unwrap(), + Topic::new("blocks").unwrap(), + ], + move |evt| { + match &evt.payload { + MqttPayload::Block(_) => { + assert_eq!(evt.topic, "blocks"); + } + MqttPayload::Json(_) => { + assert_eq!(evt.topic, "milestone-info/latest"); + } + _ => panic!("unexpected mqtt payload type: {:?}", evt), + } + match tx.try_send(()) { + Ok(_) | Err(TrySendError::Full(_)) => (), + e => e.unwrap(), + } + }, + ) + .await + .unwrap(); + + // Wait for messages to come through + for i in 0..BUFFER_SIZE { + tokio::select! { + _ = rx.recv() => { + if i == 7 { + client.unsubscribe([Topic::new("blocks").unwrap()]).await.unwrap(); + } + } + _ = async { + client.mqtt_event_receiver().await.wait_for(|msg| *msg == MqttEvent::Disconnected).await.unwrap(); + } => { + panic!("mqtt disconnected"); + } + } + } + client.subscriber().disconnect().await.unwrap(); +} From cb7fb5d4f5c47e002c1636bd5e349bd24a6c02f4 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 12 Jul 2023 10:24:38 +0200 Subject: [PATCH 2/9] get_alias_output_id_test --- sdk/tests/client/node_api/indexer.rs | 48 +++++++++++++++++++++++ sdk/tests/client/node_api/mod.rs | 57 +++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 sdk/tests/client/node_api/indexer.rs diff --git a/sdk/tests/client/node_api/indexer.rs b/sdk/tests/client/node_api/indexer.rs new file mode 100644 index 0000000000..ba8a795e84 --- /dev/null +++ b/sdk/tests/client/node_api/indexer.rs @@ -0,0 +1,48 @@ +// Copyright 2023 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +use iota_sdk::{ + client::{api::GetAddressesOptions, Result}, + types::block::output::{ + unlock_condition::{GovernorAddressUnlockCondition, StateControllerAddressUnlockCondition}, + AliasId, AliasOutputBuilder, + }, +}; + +use super::get_alias_output_id; +use crate::client::common::create_client_and_secret_manager_with_funds; + +#[ignore] +#[tokio::test] +async fn get_alias_output_id_test() -> Result<()> { + let (client, secret_manager) = create_client_and_secret_manager_with_funds(None).await?; + let protocol_parameters = client.get_protocol_parameters().await?; + let alias_id = AliasId::null(); + + let address = secret_manager + .generate_ed25519_addresses(GetAddressesOptions::from_client(&client).await?.with_range(0..3)) + .await?[0]; + + let alias_output = + AliasOutputBuilder::new_with_minimum_storage_deposit(*protocol_parameters.rent_structure(), alias_id) + .with_state_metadata([1, 2, 3]) + .add_unlock_condition(StateControllerAddressUnlockCondition::new(address)) + .add_unlock_condition(GovernorAddressUnlockCondition::new(address)) + .finish_output(protocol_parameters.token_supply())?; + + let block = client + .build_block() + .with_secret_manager(&secret_manager) + .with_outputs([alias_output])? + .finish() + .await?; + + client.retry_until_included(&block.id(), None, None).await?; + + let output_id_0 = get_alias_output_id(block.payload().unwrap())?; + let output_id_1 = client.alias_output_id(alias_id).await?; + + assert_eq!(output_id_0, output_id_1); + + Ok(()) +} diff --git a/sdk/tests/client/node_api/mod.rs b/sdk/tests/client/node_api/mod.rs index 41c192050b..e6eb2abef8 100644 --- a/sdk/tests/client/node_api/mod.rs +++ b/sdk/tests/client/node_api/mod.rs @@ -2,17 +2,22 @@ // SPDX-License-Identifier: Apache-2.0 mod core; +mod indexer; #[cfg(feature = "mqtt")] mod mqtt; use iota_sdk::{ client::{ api::GetAddressesOptions, bech32_to_hex, node_api::indexer::query_parameters::QueryParameter, - request_funds_from_faucet, secret::SecretManager, + request_funds_from_faucet, secret::SecretManager, Result, }, types::block::{ address::ToBech32Ext, - payload::{transaction::TransactionId, Payload}, + output::{Output, OutputId}, + payload::{ + transaction::{TransactionEssence, TransactionId}, + Payload, + }, BlockId, }, }; @@ -108,3 +113,51 @@ async fn setup_transaction_block() -> (BlockId, TransactionId) { (block_id, transaction_id) } + +// helper function to get the output id for the first alias output +fn get_alias_output_id(payload: &Payload) -> Result { + match payload { + Payload::Transaction(tx_payload) => { + let TransactionEssence::Regular(regular) = tx_payload.essence(); + for (index, output) in regular.outputs().iter().enumerate() { + if let Output::Alias(_alias_output) = output { + return Ok(OutputId::new(tx_payload.id(), index.try_into().unwrap())?); + } + } + panic!("No alias output in transaction essence") + } + _ => panic!("No tx payload"), + } +} + +// // helper function to get the output id for the first foundry output +// fn get_foundry_output_id(payload: &Payload) -> Result { +// match payload { +// Payload::Transaction(tx_payload) => { +// let TransactionEssence::Regular(regular) = tx_payload.essence(); +// for (index, output) in regular.outputs().iter().enumerate() { +// if let Output::Foundry(_foundry_output) = output { +// return Ok(OutputId::new(tx_payload.id(), index.try_into().unwrap())?); +// } +// } +// panic!("No foundry output in transaction essence") +// } +// _ => panic!("No tx payload"), +// } +// } + +// // helper function to get the output id for the first NFT output +// fn get_nft_output_id(payload: &Payload) -> Result { +// match payload { +// Payload::Transaction(tx_payload) => { +// let TransactionEssence::Regular(regular) = tx_payload.essence(); +// for (index, output) in regular.outputs().iter().enumerate() { +// if let Output::Nft(_nft_output) = output { +// return Ok(OutputId::new(tx_payload.id(), index.try_into().unwrap())?); +// } +// } +// panic!("No nft output in transaction essence") +// } +// _ => panic!("No tx payload"), +// } +// } From 549d0ea0acb0c94cf9f515c95c12bf47a44743e3 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 12 Jul 2023 11:01:04 +0200 Subject: [PATCH 3/9] Fix test --- sdk/tests/client/node_api/indexer.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sdk/tests/client/node_api/indexer.rs b/sdk/tests/client/node_api/indexer.rs index ba8a795e84..60c7d34dcb 100644 --- a/sdk/tests/client/node_api/indexer.rs +++ b/sdk/tests/client/node_api/indexer.rs @@ -17,14 +17,13 @@ use crate::client::common::create_client_and_secret_manager_with_funds; async fn get_alias_output_id_test() -> Result<()> { let (client, secret_manager) = create_client_and_secret_manager_with_funds(None).await?; let protocol_parameters = client.get_protocol_parameters().await?; - let alias_id = AliasId::null(); let address = secret_manager .generate_ed25519_addresses(GetAddressesOptions::from_client(&client).await?.with_range(0..3)) .await?[0]; let alias_output = - AliasOutputBuilder::new_with_minimum_storage_deposit(*protocol_parameters.rent_structure(), alias_id) + AliasOutputBuilder::new_with_minimum_storage_deposit(*protocol_parameters.rent_structure(), AliasId::null()) .with_state_metadata([1, 2, 3]) .add_unlock_condition(StateControllerAddressUnlockCondition::new(address)) .add_unlock_condition(GovernorAddressUnlockCondition::new(address)) @@ -40,7 +39,7 @@ async fn get_alias_output_id_test() -> Result<()> { client.retry_until_included(&block.id(), None, None).await?; let output_id_0 = get_alias_output_id(block.payload().unwrap())?; - let output_id_1 = client.alias_output_id(alias_id).await?; + let output_id_1 = client.alias_output_id(AliasId::from(&output_id_0)).await?; assert_eq!(output_id_0, output_id_1); From 9e72f4e90b521c3173db3ebe4c4b7cbb7d445a8c Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 12 Jul 2023 11:23:08 +0200 Subject: [PATCH 4/9] Add get_nft_output_id_test --- sdk/tests/client/node_api/indexer.rs | 41 ++++++++++++++++++++++++++-- sdk/tests/client/node_api/mod.rs | 30 ++++++++++---------- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/sdk/tests/client/node_api/indexer.rs b/sdk/tests/client/node_api/indexer.rs index 60c7d34dcb..ecc2cb697b 100644 --- a/sdk/tests/client/node_api/indexer.rs +++ b/sdk/tests/client/node_api/indexer.rs @@ -4,12 +4,15 @@ use iota_sdk::{ client::{api::GetAddressesOptions, Result}, types::block::output::{ - unlock_condition::{GovernorAddressUnlockCondition, StateControllerAddressUnlockCondition}, - AliasId, AliasOutputBuilder, + unlock_condition::{ + AddressUnlockCondition, GovernorAddressUnlockCondition, StateControllerAddressUnlockCondition, + UnlockCondition, + }, + AliasId, AliasOutputBuilder, NftId, NftOutputBuilder, }, }; -use super::get_alias_output_id; +use super::{get_alias_output_id, get_nft_output_id}; use crate::client::common::create_client_and_secret_manager_with_funds; #[ignore] @@ -45,3 +48,35 @@ async fn get_alias_output_id_test() -> Result<()> { Ok(()) } + +#[ignore] +#[tokio::test] +async fn get_nft_output_id_test() -> Result<()> { + let (client, secret_manager) = create_client_and_secret_manager_with_funds(None).await?; + let protocol_parameters = client.get_protocol_parameters().await?; + + let address = secret_manager + .generate_ed25519_addresses(GetAddressesOptions::from_client(&client).await?.with_range(0..3)) + .await?[0]; + + let nft_output = + NftOutputBuilder::new_with_minimum_storage_deposit(*protocol_parameters.rent_structure(), NftId::null()) + .with_unlock_conditions([UnlockCondition::Address(AddressUnlockCondition::new(address))]) + .finish_output(protocol_parameters.token_supply())?; + + let block = client + .build_block() + .with_secret_manager(&secret_manager) + .with_outputs([nft_output])? + .finish() + .await?; + + client.retry_until_included(&block.id(), None, None).await?; + + let output_id_0 = get_nft_output_id(block.payload().unwrap())?; + let output_id_1 = client.nft_output_id(NftId::from(&output_id_0)).await?; + + assert_eq!(output_id_0, output_id_1); + + Ok(()) +} diff --git a/sdk/tests/client/node_api/mod.rs b/sdk/tests/client/node_api/mod.rs index e6eb2abef8..4403082d66 100644 --- a/sdk/tests/client/node_api/mod.rs +++ b/sdk/tests/client/node_api/mod.rs @@ -146,18 +146,18 @@ fn get_alias_output_id(payload: &Payload) -> Result { // } // } -// // helper function to get the output id for the first NFT output -// fn get_nft_output_id(payload: &Payload) -> Result { -// match payload { -// Payload::Transaction(tx_payload) => { -// let TransactionEssence::Regular(regular) = tx_payload.essence(); -// for (index, output) in regular.outputs().iter().enumerate() { -// if let Output::Nft(_nft_output) = output { -// return Ok(OutputId::new(tx_payload.id(), index.try_into().unwrap())?); -// } -// } -// panic!("No nft output in transaction essence") -// } -// _ => panic!("No tx payload"), -// } -// } +// helper function to get the output id for the first NFT output +fn get_nft_output_id(payload: &Payload) -> Result { + match payload { + Payload::Transaction(tx_payload) => { + let TransactionEssence::Regular(regular) = tx_payload.essence(); + for (index, output) in regular.outputs().iter().enumerate() { + if let Output::Nft(_nft_output) = output { + return Ok(OutputId::new(tx_payload.id(), index.try_into().unwrap())?); + } + } + panic!("No nft output in transaction essence") + } + _ => panic!("No tx payload"), + } +} From 9594655eae8ed91a2a2f9f856f064070a6a07fba Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 12 Jul 2023 20:31:11 +0200 Subject: [PATCH 5/9] get_foundry_output_id_test --- sdk/tests/client/node_api/indexer.rs | 82 ++++++++++++++++++++++++++-- sdk/tests/client/node_api/mod.rs | 30 +++++----- 2 files changed, 91 insertions(+), 21 deletions(-) diff --git a/sdk/tests/client/node_api/indexer.rs b/sdk/tests/client/node_api/indexer.rs index ecc2cb697b..d1e99caa6e 100644 --- a/sdk/tests/client/node_api/indexer.rs +++ b/sdk/tests/client/node_api/indexer.rs @@ -3,16 +3,21 @@ use iota_sdk::{ client::{api::GetAddressesOptions, Result}, - types::block::output::{ - unlock_condition::{ - AddressUnlockCondition, GovernorAddressUnlockCondition, StateControllerAddressUnlockCondition, - UnlockCondition, + types::block::{ + address::AliasAddress, + output::{ + unlock_condition::{ + AddressUnlockCondition, GovernorAddressUnlockCondition, ImmutableAliasAddressUnlockCondition, + StateControllerAddressUnlockCondition, UnlockCondition, + }, + AliasId, AliasOutputBuilder, FoundryId, FoundryOutputBuilder, NftId, NftOutputBuilder, SimpleTokenScheme, + TokenScheme, }, - AliasId, AliasOutputBuilder, NftId, NftOutputBuilder, }, }; +use primitive_types::U256; -use super::{get_alias_output_id, get_nft_output_id}; +use super::{get_alias_output_id, get_foundry_output_id, get_nft_output_id}; use crate::client::common::create_client_and_secret_manager_with_funds; #[ignore] @@ -80,3 +85,68 @@ async fn get_nft_output_id_test() -> Result<()> { Ok(()) } + +#[ignore] +#[tokio::test] +async fn get_foundry_output_id_test() -> Result<()> { + let (client, secret_manager) = create_client_and_secret_manager_with_funds(None).await?; + let protocol_parameters = client.get_protocol_parameters().await?; + + let address = secret_manager + .generate_ed25519_addresses(GetAddressesOptions::from_client(&client).await?.with_range(0..3)) + .await?[0]; + + let alias_output_0 = + AliasOutputBuilder::new_with_minimum_storage_deposit(*protocol_parameters.rent_structure(), AliasId::null()) + .with_state_metadata([1, 2, 3]) + .add_unlock_condition(StateControllerAddressUnlockCondition::new(address)) + .add_unlock_condition(GovernorAddressUnlockCondition::new(address)) + .finish_output(protocol_parameters.token_supply())?; + + let block = client + .build_block() + .with_secret_manager(&secret_manager) + .with_outputs([alias_output_0.clone()])? + .finish() + .await?; + + client.retry_until_included(&block.id(), None, None).await?; + + let alias_id = AliasId::from(&get_alias_output_id(block.payload().unwrap())?); + + let alias_output_1 = AliasOutputBuilder::from(alias_output_0.as_alias()) + .with_alias_id(alias_id) + .with_state_index(alias_output_0.as_alias().state_index() + 1) + .with_foundry_counter(alias_output_0.as_alias().foundry_counter() + 1) + .finish_output(protocol_parameters.token_supply())?; + + let foundry_id = FoundryId::build( + &AliasAddress::new(alias_id), + alias_output_0.as_alias().foundry_counter() + 1, + SimpleTokenScheme::KIND, + ); + + let foundry_output = FoundryOutputBuilder::new_with_minimum_storage_deposit( + *protocol_parameters.rent_structure(), + alias_output_0.as_alias().foundry_counter() + 1, + TokenScheme::Simple(SimpleTokenScheme::new(U256::from(100), U256::from(0), U256::from(500))?), + ) + .add_unlock_condition(ImmutableAliasAddressUnlockCondition::new(AliasAddress::from(alias_id))) + .finish_output(protocol_parameters.token_supply())?; + + let block = client + .build_block() + .with_secret_manager(&secret_manager) + .with_outputs([alias_output_1, foundry_output])? + .finish() + .await?; + + client.retry_until_included(&block.id(), None, None).await?; + + let output_id_0 = get_foundry_output_id(block.payload().unwrap())?; + let output_id_1 = client.foundry_output_id(foundry_id).await?; + + assert_eq!(output_id_0, output_id_1); + + Ok(()) +} diff --git a/sdk/tests/client/node_api/mod.rs b/sdk/tests/client/node_api/mod.rs index 4403082d66..be4bc67a5e 100644 --- a/sdk/tests/client/node_api/mod.rs +++ b/sdk/tests/client/node_api/mod.rs @@ -130,21 +130,21 @@ fn get_alias_output_id(payload: &Payload) -> Result { } } -// // helper function to get the output id for the first foundry output -// fn get_foundry_output_id(payload: &Payload) -> Result { -// match payload { -// Payload::Transaction(tx_payload) => { -// let TransactionEssence::Regular(regular) = tx_payload.essence(); -// for (index, output) in regular.outputs().iter().enumerate() { -// if let Output::Foundry(_foundry_output) = output { -// return Ok(OutputId::new(tx_payload.id(), index.try_into().unwrap())?); -// } -// } -// panic!("No foundry output in transaction essence") -// } -// _ => panic!("No tx payload"), -// } -// } +// helper function to get the output id for the first foundry output +fn get_foundry_output_id(payload: &Payload) -> Result { + match payload { + Payload::Transaction(tx_payload) => { + let TransactionEssence::Regular(regular) = tx_payload.essence(); + for (index, output) in regular.outputs().iter().enumerate() { + if let Output::Foundry(_foundry_output) = output { + return Ok(OutputId::new(tx_payload.id(), index.try_into().unwrap())?); + } + } + panic!("No foundry output in transaction essence") + } + _ => panic!("No tx payload"), + } +} // helper function to get the output id for the first NFT output fn get_nft_output_id(payload: &Payload) -> Result { From 751c1ed4082d22c978578baaf72be5ed4fab792f Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 12 Jul 2023 20:42:35 +0200 Subject: [PATCH 6/9] Add impl Into U256 param --- sdk/examples/client/output/all.rs | 2 +- .../output/all_automatic_input_selection.rs | 2 +- sdk/examples/client/output/foundry.rs | 19 ++---- .../how_tos/outputs/unlock_conditions.rs | 3 +- .../types/block/output/token_scheme/simple.rs | 10 ++- .../high_level/minting/create_native_token.rs | 2 +- sdk/tests/client/input_selection/burn.rs | 15 ++--- .../client/input_selection/foundry_outputs.rs | 67 +++++++++---------- sdk/tests/client/node_api/indexer.rs | 3 +- sdk/tests/types/foundry_id.rs | 4 +- .../types/transaction_regular_essence.rs | 3 +- 11 files changed, 57 insertions(+), 73 deletions(-) diff --git a/sdk/examples/client/output/all.rs b/sdk/examples/client/output/all.rs index c684868f33..4d409f115c 100644 --- a/sdk/examples/client/output/all.rs +++ b/sdk/examples/client/output/all.rs @@ -105,7 +105,7 @@ async fn main() -> Result<()> { let nft_output_id = get_nft_output_id(block.payload().unwrap())?; let nft_id = NftId::from(&nft_output_id); - let token_scheme = TokenScheme::Simple(SimpleTokenScheme::new(U256::from(50), U256::from(0), U256::from(100))?); + let token_scheme = TokenScheme::Simple(SimpleTokenScheme::new(50, 0, 100)?); let foundry_id = FoundryId::build( &AliasAddress::from(AliasId::from(&alias_output_id)), 1, diff --git a/sdk/examples/client/output/all_automatic_input_selection.rs b/sdk/examples/client/output/all_automatic_input_selection.rs index c841eb7e05..7721eb0e56 100644 --- a/sdk/examples/client/output/all_automatic_input_selection.rs +++ b/sdk/examples/client/output/all_automatic_input_selection.rs @@ -98,7 +98,7 @@ async fn main() -> Result<()> { let alias_id = AliasId::from(&alias_output_id_1); let nft_output_id = get_nft_output_id(block.payload().unwrap())?; let nft_id = NftId::from(&nft_output_id); - let token_scheme = TokenScheme::Simple(SimpleTokenScheme::new(U256::from(50), U256::from(0), U256::from(100))?); + let token_scheme = TokenScheme::Simple(SimpleTokenScheme::new(50, 0, 100)?); let foundry_id = FoundryId::build( &AliasAddress::from(AliasId::from(&alias_output_id_1)), 1, diff --git a/sdk/examples/client/output/foundry.rs b/sdk/examples/client/output/foundry.rs index 926bdfa38e..633f673c00 100644 --- a/sdk/examples/client/output/foundry.rs +++ b/sdk/examples/client/output/foundry.rs @@ -92,11 +92,7 @@ async fn main() -> Result<()> { let alias_output_id = get_alias_output_id(block.payload().unwrap())?; let alias_id = AliasId::from(&alias_output_id); - let token_scheme = TokenScheme::Simple(SimpleTokenScheme::new( - U256::from(70u8), - U256::from(0u8), - U256::from(100u8), - )?); + let token_scheme = TokenScheme::Simple(SimpleTokenScheme::new(70, 0, 100)?); let foundry_id = FoundryId::build( &AliasAddress::from(AliasId::from(&alias_output_id)), 1, @@ -135,16 +131,9 @@ async fn main() -> Result<()> { // melt 20 native token ////////////////////////////////// - let foundry_output_builder = FoundryOutputBuilder::new_with_amount( - 1_000_000, - 1, - TokenScheme::Simple(SimpleTokenScheme::new( - U256::from(70u8), - U256::from(20u8), - U256::from(100u8), - )?), - ) - .add_unlock_condition(ImmutableAliasAddressUnlockCondition::new(AliasAddress::from(alias_id))); + let foundry_output_builder = + FoundryOutputBuilder::new_with_amount(1_000_000, 1, TokenScheme::Simple(SimpleTokenScheme::new(70, 20, 100)?)) + .add_unlock_condition(ImmutableAliasAddressUnlockCondition::new(AliasAddress::from(alias_id))); let alias_output_id = get_alias_output_id(block.payload().unwrap())?; let foundry_output_id = get_foundry_output_id(block.payload().unwrap())?; diff --git a/sdk/examples/how_tos/outputs/unlock_conditions.rs b/sdk/examples/how_tos/outputs/unlock_conditions.rs index 239ef8ea9c..14dbdfdd9b 100644 --- a/sdk/examples/how_tos/outputs/unlock_conditions.rs +++ b/sdk/examples/how_tos/outputs/unlock_conditions.rs @@ -23,7 +23,6 @@ use iota_sdk::{ }, }, }; -use primitive_types::U256; #[tokio::main] async fn main() -> Result<()> { @@ -41,7 +40,7 @@ async fn main() -> Result<()> { let address = Address::try_from_bech32("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy")?; let alias_address = Address::try_from_bech32("rms1pr59qm43mjtvhcajfmupqf23x29llam88yecn6pyul80rx099krmv2fnnux")?; - let token_scheme = TokenScheme::Simple(SimpleTokenScheme::new(U256::from(50), U256::from(0), U256::from(100))?); + let token_scheme = TokenScheme::Simple(SimpleTokenScheme::new(50, 0, 100)?); let basic_output_builder = BasicOutputBuilder::new_with_minimum_storage_deposit(rent_structure) .add_unlock_condition(AddressUnlockCondition::new(address)); diff --git a/sdk/src/types/block/output/token_scheme/simple.rs b/sdk/src/types/block/output/token_scheme/simple.rs index 99ba912e56..b8153518e4 100644 --- a/sdk/src/types/block/output/token_scheme/simple.rs +++ b/sdk/src/types/block/output/token_scheme/simple.rs @@ -29,7 +29,15 @@ impl SimpleTokenScheme { /// Creates a new [`SimpleTokenScheme`]. #[inline(always)] - pub fn new(minted_tokens: U256, melted_tokens: U256, maximum_supply: U256) -> Result { + pub fn new( + minted_tokens: impl Into, + melted_tokens: impl Into, + maximum_supply: impl Into, + ) -> Result { + let minted_tokens = minted_tokens.into(); + let melted_tokens = melted_tokens.into(); + let maximum_supply = maximum_supply.into(); + verify_supply(&minted_tokens, &melted_tokens, &maximum_supply)?; Ok(Self { diff --git a/sdk/src/wallet/account/operations/transaction/high_level/minting/create_native_token.rs b/sdk/src/wallet/account/operations/transaction/high_level/minting/create_native_token.rs index 24142507b3..36f9cae1f9 100644 --- a/sdk/src/wallet/account/operations/transaction/high_level/minting/create_native_token.rs +++ b/sdk/src/wallet/account/operations/transaction/high_level/minting/create_native_token.rs @@ -164,7 +164,7 @@ where alias_output.foundry_counter() + 1, TokenScheme::Simple(SimpleTokenScheme::new( params.circulating_supply, - U256::from(0u8), + 0, params.maximum_supply, )?), ) diff --git a/sdk/tests/client/input_selection/burn.rs b/sdk/tests/client/input_selection/burn.rs index 5d5838f65b..9ef9136a04 100644 --- a/sdk/tests/client/input_selection/burn.rs +++ b/sdk/tests/client/input_selection/burn.rs @@ -14,7 +14,6 @@ use iota_sdk::{ protocol::protocol_parameters, }, }; -use primitive_types::U256; use crate::client::{ addresses, build_inputs, build_outputs, is_remainder_or_return, unsorted_eq, @@ -598,7 +597,7 @@ fn burn_foundry_present() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(0), U256::from(0), U256::from(10)).unwrap(), + SimpleTokenScheme::new(0, 0, 10).unwrap(), None, ), Alias( @@ -679,7 +678,7 @@ fn burn_foundry_absent() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(0), U256::from(0), U256::from(10)).unwrap(), + SimpleTokenScheme::new(0, 0, 10).unwrap(), None, )])[0] .output @@ -736,14 +735,14 @@ fn burn_foundries_present() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(0), U256::from(0), U256::from(10)).unwrap(), + SimpleTokenScheme::new(0, 0, 10).unwrap(), None, ), Foundry( 1_000_000, alias_id_1, 2, - SimpleTokenScheme::new(U256::from(0), U256::from(0), U256::from(10)).unwrap(), + SimpleTokenScheme::new(0, 0, 10).unwrap(), None, ), Alias( @@ -816,7 +815,7 @@ fn burn_foundry_in_outputs() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(0), U256::from(0), U256::from(10)).unwrap(), + SimpleTokenScheme::new(0, 0, 10).unwrap(), None, ), Basic(1_000_000, BECH32_ADDRESS_ED25519_0, None, None, None, None, None, None), @@ -826,7 +825,7 @@ fn burn_foundry_in_outputs() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(0), U256::from(0), U256::from(10)).unwrap(), + SimpleTokenScheme::new(0, 0, 10).unwrap(), None, ), Basic(1_000_000, BECH32_ADDRESS_ED25519_0, None, None, None, None, None, None), @@ -896,7 +895,7 @@ fn burn_foundry_and_its_alias() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(0), U256::from(0), U256::from(10)).unwrap(), + SimpleTokenScheme::new(0, 0, 10).unwrap(), None, ), Alias( diff --git a/sdk/tests/client/input_selection/foundry_outputs.rs b/sdk/tests/client/input_selection/foundry_outputs.rs index a202593ec0..f6684359be 100644 --- a/sdk/tests/client/input_selection/foundry_outputs.rs +++ b/sdk/tests/client/input_selection/foundry_outputs.rs @@ -19,7 +19,6 @@ use iota_sdk::{ rand::{block::rand_block_id, output::rand_output_id}, }, }; -use primitive_types::U256; use crate::client::{ addresses, build_inputs, build_outputs, is_remainder_or_return, unsorted_eq, @@ -46,7 +45,7 @@ fn missing_input_alias_for_foundry() { 1_000_000, alias_id_2, 1, - SimpleTokenScheme::new(U256::from(0), U256::from(0), U256::from(10)).unwrap(), + SimpleTokenScheme::new(0, 0, 10).unwrap(), None, )]); @@ -84,7 +83,7 @@ fn existing_input_alias_for_foundry_alias() { 1_000_000, alias_id_2, 1, - SimpleTokenScheme::new(U256::from(0), U256::from(0), U256::from(10)).unwrap(), + SimpleTokenScheme::new(0, 0, 10).unwrap(), None, )]); @@ -132,7 +131,7 @@ fn minted_native_tokens_in_new_remainder() { 1_000_000, alias_id_2, 1, - SimpleTokenScheme::new(U256::from(10), U256::from(0), U256::from(10)).unwrap(), + SimpleTokenScheme::new(10, 0, 10).unwrap(), None, )]); @@ -156,7 +155,7 @@ fn minted_native_tokens_in_new_remainder() { } if let Output::Basic(basic_output) = &output { // Basic output remainder has the minted native tokens - assert_eq!(basic_output.native_tokens().first().unwrap().amount(), U256::from(10)); + assert_eq!(basic_output.native_tokens().first().unwrap().amount().as_u32(), 10); } }); } @@ -187,7 +186,7 @@ fn minted_native_tokens_in_provided_output() { 1_000_000, alias_id_2, 1, - SimpleTokenScheme::new(U256::from(100), U256::from(0), U256::from(100)).unwrap(), + SimpleTokenScheme::new(100, 0, 100).unwrap(), None, ), Basic( @@ -229,7 +228,7 @@ fn melt_native_tokens() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(10), U256::from(0), U256::from(10)).unwrap(), + SimpleTokenScheme::new(10, 0, 10).unwrap(), Some(vec![( "0x0811111111111111111111111111111111111111111111111111111111111111110100000000", 10, @@ -256,7 +255,7 @@ fn melt_native_tokens() { alias_id_1, 1, // Melt 5 native tokens - SimpleTokenScheme::new(U256::from(10), U256::from(5), U256::from(10)).unwrap(), + SimpleTokenScheme::new(10, 5, 10).unwrap(), None, )]); @@ -280,7 +279,7 @@ fn melt_native_tokens() { } if let Output::Basic(basic_output) = &output { // Basic output remainder has the remaining native tokens - assert_eq!(basic_output.native_tokens().first().unwrap().amount(), U256::from(5)); + assert_eq!(basic_output.native_tokens().first().unwrap().amount().as_u32(), 5); } }); } @@ -302,13 +301,7 @@ fn destroy_foundry_with_alias_state_transition() { None, None, ), - Foundry( - 52_800, - alias_id_2, - 1, - SimpleTokenScheme::new(U256::from(10), U256::from(10), U256::from(10)).unwrap(), - None, - ), + Foundry(52_800, alias_id_2, 1, SimpleTokenScheme::new(10, 10, 10).unwrap(), None), ]); let alias_output = AliasOutputBuilder::from(inputs[0].output.as_alias()) .with_amount(103_100) @@ -354,7 +347,7 @@ fn destroy_foundry_with_alias_governance_transition() { 1_000_000, alias_id_2, 1, - SimpleTokenScheme::new(U256::from(10), U256::from(10), U256::from(10)).unwrap(), + SimpleTokenScheme::new(10, 10, 10).unwrap(), None, ), ]); @@ -396,7 +389,7 @@ fn destroy_foundry_with_alias_burn() { 1_000_000, alias_id_2, 1, - SimpleTokenScheme::new(U256::from(10), U256::from(10), U256::from(10)).unwrap(), + SimpleTokenScheme::new(10, 10, 10).unwrap(), None, ), ]); @@ -451,7 +444,7 @@ fn prefer_basic_to_foundry() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(10), U256::from(10), U256::from(10)).unwrap(), + SimpleTokenScheme::new(10, 10, 10).unwrap(), None, ), Basic(1_000_000, BECH32_ADDRESS_ED25519_0, None, None, None, None, None, None), @@ -492,7 +485,7 @@ fn simple_foundry_transition_basic_not_needed() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(10), U256::from(10), U256::from(10)).unwrap(), + SimpleTokenScheme::new(10, 10, 10).unwrap(), None, ), ]); @@ -517,7 +510,7 @@ fn simple_foundry_transition_basic_not_needed() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(10), U256::from(10), U256::from(10)).unwrap(), + SimpleTokenScheme::new(10, 10, 10).unwrap(), None, )]); @@ -567,7 +560,7 @@ fn simple_foundry_transition_basic_not_needed_with_remainder() { 2_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(10), U256::from(10), U256::from(10)).unwrap(), + SimpleTokenScheme::new(10, 10, 10).unwrap(), None, ), ]); @@ -591,7 +584,7 @@ fn simple_foundry_transition_basic_not_needed_with_remainder() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(10), U256::from(10), U256::from(10)).unwrap(), + SimpleTokenScheme::new(10, 10, 10).unwrap(), None, )]); @@ -650,7 +643,7 @@ fn simple_foundry_transition_basic_not_needed_with_remainder() { // inputs.extend(build_input_signing_data_foundry_outputs([( // alias_id_1, // 2_000_000, -// SimpleTokenScheme::new(U256::from(10), U256::from(10), U256::from(10)).unwrap(), +// SimpleTokenScheme::new(10, 10, 10).unwrap(), // None, // )])); // inputs.extend(build_inputs([( @@ -718,7 +711,7 @@ fn mint_and_burn_at_the_same_time() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(100), U256::from(0), U256::from(200)).unwrap(), + SimpleTokenScheme::new(100, 0, 200).unwrap(), Some(vec![(&token_id.to_string(), 100)]), )]); let alias_output = AliasOutputBuilder::new_with_amount(2_000_000, alias_id_1) @@ -742,7 +735,7 @@ fn mint_and_burn_at_the_same_time() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(120), U256::from(0), U256::from(200)).unwrap(), + SimpleTokenScheme::new(120, 0, 200).unwrap(), Some(vec![(&token_id.to_string(), 110)]), )]); @@ -774,7 +767,7 @@ fn take_amount_from_alias_and_foundry_to_fund_basic() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(100), U256::from(0), U256::from(200)).unwrap(), + SimpleTokenScheme::new(100, 0, 200).unwrap(), Some(vec![(&token_id.to_string(), 100)]), ), ]); @@ -848,7 +841,7 @@ fn create_native_token_but_burn_alias() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(0), U256::from(0), U256::from(100)).unwrap(), + SimpleTokenScheme::new(0, 0, 100).unwrap(), None, ), ]); @@ -856,7 +849,7 @@ fn create_native_token_but_burn_alias() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(100), U256::from(0), U256::from(100)).unwrap(), + SimpleTokenScheme::new(100, 0, 100).unwrap(), Some(vec![(&token_id.to_string(), 100)]), )]); @@ -898,7 +891,7 @@ fn melted_tokens_not_provided() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(100), U256::from(0), U256::from(100)).unwrap(), + SimpleTokenScheme::new(100, 0, 100).unwrap(), None, ), ]); @@ -906,7 +899,7 @@ fn melted_tokens_not_provided() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(100), U256::from(100), U256::from(100)).unwrap(), + SimpleTokenScheme::new(100, 100, 100).unwrap(), None, )]); @@ -924,7 +917,7 @@ fn melted_tokens_not_provided() { token_id, found, required, - }) if token_id == token_id_1 && found == U256::from(0) && required == U256::from(100))); + }) if token_id == token_id_1 && found.as_u32() == 0 && required.as_u32() == 100)); } #[test] @@ -950,7 +943,7 @@ fn burned_tokens_not_provided() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(100), U256::from(0), U256::from(100)).unwrap(), + SimpleTokenScheme::new(100, 0, 100).unwrap(), None, ), ]); @@ -958,7 +951,7 @@ fn burned_tokens_not_provided() { 1_000_000, alias_id_1, 1, - SimpleTokenScheme::new(U256::from(100), U256::from(0), U256::from(100)).unwrap(), + SimpleTokenScheme::new(100, 0, 100).unwrap(), None, )]); @@ -977,7 +970,7 @@ fn burned_tokens_not_provided() { token_id, found, required, - }) if token_id == token_id_1 && found == U256::from(0) && required == U256::from(100))); + }) if token_id == token_id_1 && found.as_u32() == 0 && required.as_u32() == 100)); } #[test] @@ -989,7 +982,7 @@ fn foundry_in_outputs_and_required() { 1_000_000, alias_id_2, 1, - SimpleTokenScheme::new(U256::from(0), U256::from(0), U256::from(10)).unwrap(), + SimpleTokenScheme::new(0, 0, 10).unwrap(), None, )]); let alias_output = AliasOutputBuilder::new_with_amount(1_251_500, alias_id_2) @@ -1012,7 +1005,7 @@ fn foundry_in_outputs_and_required() { 1_000_000, alias_id_2, 1, - SimpleTokenScheme::new(U256::from(0), U256::from(0), U256::from(10)).unwrap(), + SimpleTokenScheme::new(0, 0, 10).unwrap(), None, )]); diff --git a/sdk/tests/client/node_api/indexer.rs b/sdk/tests/client/node_api/indexer.rs index d1e99caa6e..30435d108c 100644 --- a/sdk/tests/client/node_api/indexer.rs +++ b/sdk/tests/client/node_api/indexer.rs @@ -15,7 +15,6 @@ use iota_sdk::{ }, }, }; -use primitive_types::U256; use super::{get_alias_output_id, get_foundry_output_id, get_nft_output_id}; use crate::client::common::create_client_and_secret_manager_with_funds; @@ -129,7 +128,7 @@ async fn get_foundry_output_id_test() -> Result<()> { let foundry_output = FoundryOutputBuilder::new_with_minimum_storage_deposit( *protocol_parameters.rent_structure(), alias_output_0.as_alias().foundry_counter() + 1, - TokenScheme::Simple(SimpleTokenScheme::new(U256::from(100), U256::from(0), U256::from(500))?), + TokenScheme::Simple(SimpleTokenScheme::new(100, 0, 500)?), ) .add_unlock_condition(ImmutableAliasAddressUnlockCondition::new(AliasAddress::from(alias_id))) .finish_output(protocol_parameters.token_supply())?; diff --git a/sdk/tests/types/foundry_id.rs b/sdk/tests/types/foundry_id.rs index 8781af29de..422b2ce44d 100644 --- a/sdk/tests/types/foundry_id.rs +++ b/sdk/tests/types/foundry_id.rs @@ -7,7 +7,6 @@ use iota_sdk::types::block::{ address::AliasAddress, output::{AliasId, FoundryId, SimpleTokenScheme, TokenScheme}, }; -use primitive_types::U256; #[test] fn getters() { @@ -15,8 +14,7 @@ fn getters() { AliasId::from_str("0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649").unwrap(), ); let serial_number = 42; - let token_scheme = - TokenScheme::from(SimpleTokenScheme::new(U256::from(100u8), U256::from(0u8), U256::from(100u8)).unwrap()); + let token_scheme = TokenScheme::from(SimpleTokenScheme::new(100, 0, 100).unwrap()); let foundry_id = FoundryId::build(&alias_address, serial_number, token_scheme.kind()); assert_eq!(foundry_id.alias_address(), alias_address); diff --git a/sdk/tests/types/transaction_regular_essence.rs b/sdk/tests/types/transaction_regular_essence.rs index e4d7cc22e1..82e6bb77c6 100644 --- a/sdk/tests/types/transaction_regular_essence.rs +++ b/sdk/tests/types/transaction_regular_essence.rs @@ -451,8 +451,7 @@ fn duplicate_output_foundry() { .finish_output(protocol_parameters.token_supply()) .unwrap(); let alias_id = AliasId::from(bytes); - let token_scheme = - TokenScheme::Simple(SimpleTokenScheme::new(U256::from(70u8), U256::from(0u8), U256::from(100u8)).unwrap()); + let token_scheme = TokenScheme::Simple(SimpleTokenScheme::new(70, 0, 100).unwrap()); let foundry_id = FoundryId::build(&AliasAddress::from(alias_id), 1, token_scheme.kind()); let token_id = TokenId::from(foundry_id); let foundry = FoundryOutput::build_with_amount(1_000_000, 1, token_scheme) From 50f2d6291113e8928720d42127785c70803e7d73 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 12 Jul 2023 20:57:51 +0200 Subject: [PATCH 7/9] Some more impl into --- sdk/examples/client/output/all.rs | 5 ++--- .../client/output/all_automatic_input_selection.rs | 5 ++--- sdk/examples/client/output/foundry.rs | 11 +++++------ sdk/examples/client/output/native_tokens.rs | 10 ++-------- sdk/examples/how_tos/native_tokens/burn.rs | 7 +++---- .../core/requirement/native_tokens.rs | 2 +- sdk/src/types/block/output/alias.rs | 6 +++--- sdk/src/types/block/output/basic.rs | 6 +++--- sdk/src/types/block/output/foundry.rs | 6 +++--- sdk/src/types/block/output/native_token.rs | 4 +++- sdk/src/types/block/output/nft.rs | 6 +++--- sdk/src/wallet/message_interface/message_handler.rs | 5 ++--- sdk/tests/client/mod.rs | 13 +++++-------- sdk/tests/types/transaction_regular_essence.rs | 3 +-- sdk/tests/wallet/output_preparation.rs | 3 +-- 15 files changed, 39 insertions(+), 53 deletions(-) diff --git a/sdk/examples/client/output/all.rs b/sdk/examples/client/output/all.rs index 4d409f115c..edd63a5900 100644 --- a/sdk/examples/client/output/all.rs +++ b/sdk/examples/client/output/all.rs @@ -28,7 +28,6 @@ use iota_sdk::{ payload::{transaction::TransactionEssence, Payload}, }, }; -use primitive_types::U256; #[tokio::main] async fn main() -> Result<()> { @@ -127,7 +126,7 @@ async fn main() -> Result<()> { foundry_output_builder .clone() // Mint native tokens - .add_native_token(NativeToken::new(token_id, U256::from(50))?) + .add_native_token(NativeToken::new(token_id, 50)?) .finish_output(token_supply)?, nft_output_builder .clone() @@ -172,7 +171,7 @@ async fn main() -> Result<()> { // with native token basic_output_builder .clone() - .add_native_token(NativeToken::new(token_id, U256::from(50))?) + .add_native_token(NativeToken::new(token_id, 50)?) .finish_output(token_supply)?, // with most simple output basic_output_builder.clone().finish_output(token_supply)?, diff --git a/sdk/examples/client/output/all_automatic_input_selection.rs b/sdk/examples/client/output/all_automatic_input_selection.rs index 7721eb0e56..f70784a06d 100644 --- a/sdk/examples/client/output/all_automatic_input_selection.rs +++ b/sdk/examples/client/output/all_automatic_input_selection.rs @@ -25,7 +25,6 @@ use iota_sdk::{ payload::{transaction::TransactionEssence, Payload}, }, }; -use primitive_types::U256; #[tokio::main] async fn main() -> Result<()> { @@ -120,7 +119,7 @@ async fn main() -> Result<()> { foundry_output_builder .clone() // Mint native tokens - .add_native_token(NativeToken::new(token_id, U256::from(50))?) + .add_native_token(NativeToken::new(token_id, 50)?) .finish_output(token_supply)?, nft_output_builder .clone() @@ -160,7 +159,7 @@ async fn main() -> Result<()> { // with native token basic_output_builder .clone() - .add_native_token(NativeToken::new(token_id, U256::from(50))?) + .add_native_token(NativeToken::new(token_id, 50)?) .finish_output(token_supply)?, // with most simple output basic_output_builder.clone().finish_output(token_supply)?, diff --git a/sdk/examples/client/output/foundry.rs b/sdk/examples/client/output/foundry.rs index 633f673c00..d7501ae74e 100644 --- a/sdk/examples/client/output/foundry.rs +++ b/sdk/examples/client/output/foundry.rs @@ -30,7 +30,6 @@ use iota_sdk::{ payload::{transaction::TransactionEssence, Payload}, }, }; -use primitive_types::U256; #[tokio::main] async fn main() -> Result<()> { @@ -108,7 +107,7 @@ async fn main() -> Result<()> { .with_foundry_counter(1) .finish_output(token_supply)?, FoundryOutputBuilder::new_with_amount(1_000_000, 1, token_scheme) - .add_native_token(NativeToken::new(token_id, U256::from(70u8))?) + .add_native_token(NativeToken::new(token_id, 70)?) .add_unlock_condition(ImmutableAliasAddressUnlockCondition::new(AliasAddress::from(alias_id))) .finish_output(token_supply)?, ]; @@ -147,7 +146,7 @@ async fn main() -> Result<()> { .finish_output(token_supply)?, foundry_output_builder .clone() - .add_native_token(NativeToken::new(token_id, U256::from(50u8))?) + .add_native_token(NativeToken::new(token_id, 50)?) .finish_output(token_supply)?, ]; @@ -188,7 +187,7 @@ async fn main() -> Result<()> { foundry_output_builder.finish_output(token_supply)?, basic_output_builder .clone() - .add_native_token(NativeToken::new(token_id, U256::from(50u8))?) + .add_native_token(NativeToken::new(token_id, 50)?) .finish_output(token_supply)?, ]; @@ -223,7 +222,7 @@ async fn main() -> Result<()> { let basic_output_id = get_basic_output_id_with_native_tokens(block.payload().unwrap())?; let outputs = [basic_output_builder .clone() - .add_native_token(NativeToken::new(token_id, U256::from(50u8))?) + .add_native_token(NativeToken::new(token_id, 50)?) .finish_output(token_supply)?]; let block = client @@ -248,7 +247,7 @@ async fn main() -> Result<()> { let basic_output_id = get_basic_output_id_with_native_tokens(block.payload().unwrap())?; let outputs = [basic_output_builder - .add_native_token(NativeToken::new(token_id, U256::from(30u8))?) + .add_native_token(NativeToken::new(token_id, 30)?) .finish_output(token_supply)?]; let block = client diff --git a/sdk/examples/client/output/native_tokens.rs b/sdk/examples/client/output/native_tokens.rs index 264e528745..18c0828bb2 100644 --- a/sdk/examples/client/output/native_tokens.rs +++ b/sdk/examples/client/output/native_tokens.rs @@ -65,19 +65,13 @@ async fn main() -> Result<()> { // tokens BasicOutputBuilder::new_with_amount(1_000_000) .add_unlock_condition(AddressUnlockCondition::new(receiver_address)) - .add_native_token(NativeToken::new( - TokenId::new(token_id), - primitive_types::U256::from(10), - )?) + .add_native_token(NativeToken::new(TokenId::new(token_id), 10)?) .finish_output(token_supply)?, // With StorageDepositReturnUnlockCondition, the receiver can consume the output to get the native tokens, but // he needs to send the amount back BasicOutputBuilder::new_with_amount(1_000_000) .add_unlock_condition(AddressUnlockCondition::new(receiver_address)) - .add_native_token(NativeToken::new( - TokenId::new(token_id), - primitive_types::U256::from(10), - )?) + .add_native_token(NativeToken::new(TokenId::new(token_id), 10)?) // Return the full amount. .add_unlock_condition(StorageDepositReturnUnlockCondition::new( sender_address, diff --git a/sdk/examples/how_tos/native_tokens/burn.rs b/sdk/examples/how_tos/native_tokens/burn.rs index c74f0b52f8..19a93af81e 100644 --- a/sdk/examples/how_tos/native_tokens/burn.rs +++ b/sdk/examples/how_tos/native_tokens/burn.rs @@ -19,7 +19,7 @@ use iota_sdk::{ types::block::output::{NativeToken, TokenId}, wallet::Result, - Wallet, U256, + Wallet, }; // The minimum available native token amount to search for in the account @@ -49,7 +49,7 @@ async fn main() -> Result<()> { .unwrap_or_else(|| TokenId::from(*balance.foundries().first().unwrap())); if let Some(native_token_balance) = balance.native_tokens().iter().find(|native_token| { - native_token.token_id() == &token_id && native_token.available() >= U256::from(MIN_AVAILABLE_AMOUNT) + native_token.token_id() == &token_id && native_token.available().as_u64() >= MIN_AVAILABLE_AMOUNT }) { println!("Balance before burning: {native_token_balance:#?}"); @@ -59,8 +59,7 @@ async fn main() -> Result<()> { .await?; // Burn a native token - let burn_amount = U256::from(BURN_AMOUNT); - let transaction = account.burn(NativeToken::new(token_id, burn_amount)?, None).await?; + let transaction = account.burn(NativeToken::new(token_id, BURN_AMOUNT)?, None).await?; println!("Transaction sent: {}", transaction.transaction_id); let block_id = account diff --git a/sdk/src/client/api/block_builder/input_selection/core/requirement/native_tokens.rs b/sdk/src/client/api/block_builder/input_selection/core/requirement/native_tokens.rs index e82c733f72..f306be299e 100644 --- a/sdk/src/client/api/block_builder/input_selection/core/requirement/native_tokens.rs +++ b/sdk/src/client/api/block_builder/input_selection/core/requirement/native_tokens.rs @@ -72,7 +72,7 @@ pub(crate) fn get_minted_and_melted_native_tokens( if initial_creation { let circulating_supply = output_foundry_simple_ts.circulating_supply(); - if circulating_supply != U256::from(0) { + if !circulating_supply.is_zero() { minted_native_tokens .add_native_token(NativeToken::new(output_foundry.token_id(), circulating_supply)?)?; } diff --git a/sdk/src/types/block/output/alias.rs b/sdk/src/types/block/output/alias.rs index 02320d5963..7f08621674 100644 --- a/sdk/src/types/block/output/alias.rs +++ b/sdk/src/types/block/output/alias.rs @@ -937,7 +937,7 @@ mod tests { let issuer_2 = rand_issuer_feature(); let mut builder = AliasOutput::build_with_amount(0, alias_id) - .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000.into()).unwrap()) + .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .add_unlock_condition(gov_address_1) .add_unlock_condition(state_address_1) .add_feature(sender_1) @@ -1050,7 +1050,7 @@ mod tests { }; let builder = AliasOutput::build_with_amount(100, alias_id) - .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000.into()).unwrap()) + .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .add_unlock_condition(gov_address) .add_unlock_condition(state_address) .with_features(rand_allowed_features(AliasOutput::ALLOWED_FEATURES)) @@ -1058,7 +1058,7 @@ mod tests { test_split_dto(builder); let builder = AliasOutput::build_with_minimum_storage_deposit(*protocol_parameters.rent_structure(), alias_id) - .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000.into()).unwrap()) + .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .add_unlock_condition(gov_address) .add_unlock_condition(state_address) .with_features(rand_allowed_features(AliasOutput::ALLOWED_FEATURES)) diff --git a/sdk/src/types/block/output/basic.rs b/sdk/src/types/block/output/basic.rs index ab561b1c59..24b656e8cc 100644 --- a/sdk/src/types/block/output/basic.rs +++ b/sdk/src/types/block/output/basic.rs @@ -471,7 +471,7 @@ mod tests { let sender_2 = rand_sender_feature(); let mut builder = BasicOutput::build_with_amount(0) - .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000.into()).unwrap()) + .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .add_unlock_condition(address_1) .add_feature(sender_1) .replace_feature(sender_2); @@ -553,13 +553,13 @@ mod tests { }; let builder = BasicOutput::build_with_amount(100) - .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000.into()).unwrap()) + .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .add_unlock_condition(address) .with_features(rand_allowed_features(BasicOutput::ALLOWED_FEATURES)); test_split_dto(builder); let builder = BasicOutput::build_with_minimum_storage_deposit(*protocol_parameters.rent_structure()) - .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000.into()).unwrap()) + .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .add_unlock_condition(address) .with_features(rand_allowed_features(BasicOutput::ALLOWED_FEATURES)); test_split_dto(builder); diff --git a/sdk/src/types/block/output/foundry.rs b/sdk/src/types/block/output/foundry.rs index 4a7f620120..1fc17d8a28 100644 --- a/sdk/src/types/block/output/foundry.rs +++ b/sdk/src/types/block/output/foundry.rs @@ -793,7 +793,7 @@ mod tests { let mut builder = FoundryOutput::build_with_amount(0, 234, rand_token_scheme()) .with_serial_number(85) - .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000.into()).unwrap()) + .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .with_unlock_conditions([alias_1]) .add_feature(metadata_1.clone()) .replace_feature(metadata_2.clone()) @@ -868,7 +868,7 @@ mod tests { }; let builder = FoundryOutput::build_with_amount(100, 123, rand_token_scheme()) - .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000.into()).unwrap()) + .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .add_unlock_condition(ImmutableAliasAddressUnlockCondition::new(rand_alias_address())) .add_immutable_feature(rand_metadata_feature()) .with_features(rand_allowed_features(FoundryOutput::ALLOWED_FEATURES)); @@ -879,7 +879,7 @@ mod tests { 123, rand_token_scheme(), ) - .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000.into()).unwrap()) + .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .add_unlock_condition(ImmutableAliasAddressUnlockCondition::new(rand_alias_address())) .add_immutable_feature(rand_metadata_feature()) .with_features(rand_allowed_features(FoundryOutput::ALLOWED_FEATURES)); diff --git a/sdk/src/types/block/output/native_token.rs b/sdk/src/types/block/output/native_token.rs index 906d83191b..bb07db9812 100644 --- a/sdk/src/types/block/output/native_token.rs +++ b/sdk/src/types/block/output/native_token.rs @@ -30,7 +30,9 @@ pub struct NativeToken { impl NativeToken { /// Creates a new [`NativeToken`]. #[inline(always)] - pub fn new(token_id: TokenId, amount: U256) -> Result { + pub fn new(token_id: TokenId, amount: impl Into) -> Result { + let amount = amount.into(); + verify_amount::(&amount, &())?; Ok(Self { token_id, amount }) diff --git a/sdk/src/types/block/output/nft.rs b/sdk/src/types/block/output/nft.rs index 6e85de6628..1c052e2662 100644 --- a/sdk/src/types/block/output/nft.rs +++ b/sdk/src/types/block/output/nft.rs @@ -656,7 +656,7 @@ mod tests { let issuer_2 = rand_issuer_feature(); let mut builder = NftOutput::build_with_amount(0, NftId::null()) - .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000.into()).unwrap()) + .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .add_unlock_condition(address_1) .add_feature(sender_1) .replace_feature(sender_2) @@ -741,7 +741,7 @@ mod tests { }; let builder = NftOutput::build_with_amount(100, NftId::null()) - .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000.into()).unwrap()) + .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .add_unlock_condition(rand_address_unlock_condition()) .with_features(rand_allowed_features(NftOutput::ALLOWED_FEATURES)) .with_immutable_features(rand_allowed_features(NftOutput::ALLOWED_IMMUTABLE_FEATURES)); @@ -749,7 +749,7 @@ mod tests { let builder = NftOutput::build_with_minimum_storage_deposit(*protocol_parameters.rent_structure(), NftId::null()) - .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000.into()).unwrap()) + .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .add_unlock_condition(rand_address_unlock_condition()) .with_features(rand_allowed_features(NftOutput::ALLOWED_FEATURES)) .with_immutable_features(rand_allowed_features(NftOutput::ALLOWED_IMMUTABLE_FEATURES)); diff --git a/sdk/src/wallet/message_interface/message_handler.rs b/sdk/src/wallet/message_interface/message_handler.rs index bbb8b4988f..e1c8b12ef2 100644 --- a/sdk/src/wallet/message_interface/message_handler.rs +++ b/sdk/src/wallet/message_interface/message_handler.rs @@ -12,7 +12,6 @@ use std::{ use backtrace::Backtrace; use crypto::keys::slip10::Chain; use futures::{Future, FutureExt}; -use primitive_types::U256; use zeroize::Zeroize; #[cfg(feature = "events")] @@ -692,7 +691,7 @@ impl WalletMessageHandler { let transaction = account .melt_native_token( token_id, - U256::try_from(&melt_amount).map_err(|_| Error::InvalidField("melt_amount"))?, + melt_amount, options.map(TransactionOptions::try_from_dto).transpose()?, ) .await?; @@ -709,7 +708,7 @@ impl WalletMessageHandler { let transaction = account .mint_native_token( token_id, - U256::try_from(&mint_amount).map_err(|_| Error::InvalidField("mint_amount"))?, + mint_amount, options.map(TransactionOptions::try_from_dto).transpose()?, ) .await?; diff --git a/sdk/tests/client/mod.rs b/sdk/tests/client/mod.rs index 868526408c..36a237abbe 100644 --- a/sdk/tests/client/mod.rs +++ b/sdk/tests/client/mod.rs @@ -40,7 +40,6 @@ use iota_sdk::{ rand::{block::rand_block_id, transaction::rand_transaction_id}, }, }; -use primitive_types::U256; const TOKEN_SUPPLY: u64 = 1_813_620_509_061_365; const ALIAS_ID_0: &str = "0x0000000000000000000000000000000000000000000000000000000000000000"; @@ -115,7 +114,7 @@ fn build_basic_output( builder = builder.with_native_tokens( native_tokens .into_iter() - .map(|(id, amount)| NativeToken::new(TokenId::from_str(id).unwrap(), U256::from(amount)).unwrap()), + .map(|(id, amount)| NativeToken::new(TokenId::from_str(id).unwrap(), amount).unwrap()), ); } @@ -157,7 +156,7 @@ fn build_nft_output( builder = builder.with_native_tokens( native_tokens .into_iter() - .map(|(id, amount)| NativeToken::new(TokenId::from_str(id).unwrap(), U256::from(amount)).unwrap()), + .map(|(id, amount)| NativeToken::new(TokenId::from_str(id).unwrap(), amount).unwrap()), ); } @@ -201,7 +200,7 @@ fn build_alias_output( builder = builder.with_native_tokens( native_tokens .into_iter() - .map(|(id, amount)| NativeToken::new(TokenId::from_str(id).unwrap(), U256::from(amount)).unwrap()), + .map(|(id, amount)| NativeToken::new(TokenId::from_str(id).unwrap(), amount).unwrap()), ); } @@ -230,7 +229,7 @@ fn build_foundry_output( builder = builder.with_native_tokens( native_tokens .into_iter() - .map(|(id, amount)| NativeToken::new(TokenId::from_str(id).unwrap(), U256::from(amount)).unwrap()), + .map(|(id, amount)| NativeToken::new(TokenId::from_str(id).unwrap(), amount).unwrap()), ); } @@ -380,9 +379,7 @@ fn is_remainder_or_return( let native_tokens = NativeTokens::from_set( native_tokens .into_iter() - .map(|(token_id, amount)| { - NativeToken::new(TokenId::from_str(token_id).unwrap(), U256::from(amount)).unwrap() - }) + .map(|(token_id, amount)| NativeToken::new(TokenId::from_str(token_id).unwrap(), amount).unwrap()) .collect::>(), ) .unwrap(); diff --git a/sdk/tests/types/transaction_regular_essence.rs b/sdk/tests/types/transaction_regular_essence.rs index 82e6bb77c6..091a4d7319 100644 --- a/sdk/tests/types/transaction_regular_essence.rs +++ b/sdk/tests/types/transaction_regular_essence.rs @@ -26,7 +26,6 @@ use iota_sdk::types::block::{ Error, }; use packable::bounded::TryIntoBoundedU16Error; -use primitive_types::U256; const TRANSACTION_ID: &str = "0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649"; const ED25519_ADDRESS_1: &str = "0xd56da1eb7726ed482dfe9d457cf548c2ae2a6ce3e053dbf82f11223be476adb9"; @@ -455,7 +454,7 @@ fn duplicate_output_foundry() { let foundry_id = FoundryId::build(&AliasAddress::from(alias_id), 1, token_scheme.kind()); let token_id = TokenId::from(foundry_id); let foundry = FoundryOutput::build_with_amount(1_000_000, 1, token_scheme) - .add_native_token(NativeToken::new(token_id, U256::from(70u8)).unwrap()) + .add_native_token(NativeToken::new(token_id, 70).unwrap()) .add_unlock_condition(ImmutableAliasAddressUnlockCondition::new(AliasAddress::from(alias_id))) .finish_output(protocol_parameters.token_supply()) .unwrap(); diff --git a/sdk/tests/wallet/output_preparation.rs b/sdk/tests/wallet/output_preparation.rs index b75c805d23..48dd862b22 100644 --- a/sdk/tests/wallet/output_preparation.rs +++ b/sdk/tests/wallet/output_preparation.rs @@ -12,7 +12,6 @@ use iota_sdk::{ account::{Assets, Features, OutputParams, ReturnStrategy, StorageDeposit, Unlocks}, MintNftParams, Result, }, - U256, }; use crate::wallet::common::{create_accounts_with_funds, make_wallet, setup, tear_down}; @@ -68,7 +67,7 @@ async fn output_preparation() -> Result<()> { let native_token = NativeToken::new( TokenId::from_str("0x08847bd287c912fadedb6bf38900bda9f2d377b75b2a0bece8738699f56ebca4130100000000")?, - U256::from(10u32), + 10, )?; let output = account .prepare_output( From d47fe677956b579299de8b46ee9bc381b8eac944 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 12 Jul 2023 21:13:37 +0200 Subject: [PATCH 8/9] More impl into --- bindings/core/src/method_handler/account.rs | 10 +++------- sdk/examples/how_tos/native_tokens/melt.rs | 6 +++--- sdk/examples/how_tos/native_tokens/mint.rs | 5 ++--- .../high_level/burning_melting/melt_native_token.rs | 4 ++-- .../high_level/minting/mint_native_token.rs | 7 ++++--- sdk/tests/wallet/native_tokens.rs | 4 +--- 6 files changed, 15 insertions(+), 21 deletions(-) diff --git a/bindings/core/src/method_handler/account.rs b/bindings/core/src/method_handler/account.rs index 00fff32c7f..529c707d2f 100644 --- a/bindings/core/src/method_handler/account.rs +++ b/bindings/core/src/method_handler/account.rs @@ -9,15 +9,11 @@ use iota_sdk::{ input_selection::Burn, PreparedTransactionData, PreparedTransactionDataDto, SignedTransactionData, SignedTransactionDataDto, }, - types::block::{ - output::{dto::OutputDto, Output}, - Error, - }, + types::block::output::{dto::OutputDto, Output}, wallet::account::{ types::TransactionDto, Account, OutputDataDto, PreparedCreateNativeTokenTransactionDto, TransactionOptions, }, }; -use primitive_types::U256; use crate::{method::AccountMethod, Response, Result}; @@ -143,7 +139,7 @@ pub(crate) async fn call_account_method_internal(account: &Account, method: Acco let data = account .prepare_melt_native_token( token_id, - U256::try_from(&melt_amount).map_err(|_| Error::InvalidField("melt_amount"))?, + melt_amount, options.map(TransactionOptions::try_from_dto).transpose()?, ) .await?; @@ -166,7 +162,7 @@ pub(crate) async fn call_account_method_internal(account: &Account, method: Acco let data = account .prepare_mint_native_token( token_id, - U256::try_from(&mint_amount).map_err(|_| Error::InvalidField("mint_amount"))?, + mint_amount, options.map(TransactionOptions::try_from_dto).transpose()?, ) .await?; diff --git a/sdk/examples/how_tos/native_tokens/melt.rs b/sdk/examples/how_tos/native_tokens/melt.rs index 61364f0a5a..67198a48e3 100644 --- a/sdk/examples/how_tos/native_tokens/melt.rs +++ b/sdk/examples/how_tos/native_tokens/melt.rs @@ -16,7 +16,7 @@ //! cargo run --release --all-features --example melt_native_token [TOKEN_ID] //! ``` -use iota_sdk::{types::block::output::TokenId, wallet::Result, Wallet, U256}; +use iota_sdk::{types::block::output::TokenId, wallet::Result, Wallet}; // The amount of native tokens to melt const MELT_AMOUNT: u64 = 10; @@ -59,8 +59,8 @@ async fn main() -> Result<()> { .await?; // Melt some of the circulating supply - let melt_amount = U256::from(MELT_AMOUNT); - let transaction = account.melt_native_token(token_id, melt_amount, None).await?; + + let transaction = account.melt_native_token(token_id, MELT_AMOUNT, None).await?; println!("Transaction sent: {}", transaction.transaction_id); let block_id = account diff --git a/sdk/examples/how_tos/native_tokens/mint.rs b/sdk/examples/how_tos/native_tokens/mint.rs index 8c3b3ea37e..0280f55676 100644 --- a/sdk/examples/how_tos/native_tokens/mint.rs +++ b/sdk/examples/how_tos/native_tokens/mint.rs @@ -16,7 +16,7 @@ //! cargo run --release --all-features --example mint_native_token [TOKEN_ID] //! ``` -use iota_sdk::{types::block::output::TokenId, wallet::Result, Wallet, U256}; +use iota_sdk::{types::block::output::TokenId, wallet::Result, Wallet}; // The amount of native tokens to mint const MINT_AMOUNT: u64 = 10; @@ -59,8 +59,7 @@ async fn main() -> Result<()> { .await?; // Mint some more native tokens - let mint_amount = U256::from(MINT_AMOUNT); - let transaction = account.mint_native_token(token_id, mint_amount, None).await?; + let transaction = account.mint_native_token(token_id, MINT_AMOUNT, None).await?; println!("Transaction sent: {}", transaction.transaction_id); let block_id = account diff --git a/sdk/src/wallet/account/operations/transaction/high_level/burning_melting/melt_native_token.rs b/sdk/src/wallet/account/operations/transaction/high_level/burning_melting/melt_native_token.rs index 4f81212b9f..aa7c35b0b3 100644 --- a/sdk/src/wallet/account/operations/transaction/high_level/burning_melting/melt_native_token.rs +++ b/sdk/src/wallet/account/operations/transaction/high_level/burning_melting/melt_native_token.rs @@ -26,7 +26,7 @@ where pub async fn melt_native_token( &self, token_id: TokenId, - melt_amount: U256, + melt_amount: impl Into + Send, options: impl Into> + Send, ) -> crate::wallet::Result { let options = options.into(); @@ -42,7 +42,7 @@ where pub async fn prepare_melt_native_token( &self, token_id: TokenId, - melt_amount: U256, + melt_amount: impl Into + Send, options: impl Into> + Send, ) -> crate::wallet::Result { log::debug!("[TRANSACTION] prepare_melt_native_token"); diff --git a/sdk/src/wallet/account/operations/transaction/high_level/minting/mint_native_token.rs b/sdk/src/wallet/account/operations/transaction/high_level/minting/mint_native_token.rs index 6ad8f96871..2f684081da 100644 --- a/sdk/src/wallet/account/operations/transaction/high_level/minting/mint_native_token.rs +++ b/sdk/src/wallet/account/operations/transaction/high_level/minting/mint_native_token.rs @@ -23,7 +23,7 @@ where /// ```ignore /// let tx = account.mint_native_token( /// TokenId::from_str("08e68f7616cd4948efebc6a77c4f93aed770ac53860100000000000000000000000000000000")?, - /// U256::from(100), + /// 100, /// None /// ).await?; /// println!("Transaction created: {}", tx.transaction_id); @@ -34,7 +34,7 @@ where pub async fn mint_native_token( &self, token_id: TokenId, - mint_amount: U256, + mint_amount: impl Into + Send, options: impl Into> + Send, ) -> crate::wallet::Result { let options = options.into(); @@ -51,11 +51,12 @@ where pub async fn prepare_mint_native_token( &self, token_id: TokenId, - mint_amount: U256, + mint_amount: impl Into + Send, options: impl Into> + Send, ) -> crate::wallet::Result { log::debug!("[TRANSACTION] mint_native_token"); + let mint_amount = mint_amount.into(); let account_details = self.details().await; let token_supply = self.client().get_token_supply().await?; let existing_foundry_output = account_details.unspent_outputs().values().find(|output_data| { diff --git a/sdk/tests/wallet/native_tokens.rs b/sdk/tests/wallet/native_tokens.rs index 51a606ec52..67761e0dc2 100644 --- a/sdk/tests/wallet/native_tokens.rs +++ b/sdk/tests/wallet/native_tokens.rs @@ -50,9 +50,7 @@ async fn create_and_mint_native_token() -> Result<()> { U256::from(50) ); - let tx = account - .mint_native_token(create_tx.token_id, U256::from(50), None) - .await?; + let tx = account.mint_native_token(create_tx.token_id, 50, None).await?; account .retry_transaction_until_included(&tx.transaction_id, None, None) .await?; From 0619bad64d856c4adcd2a3c66a810495374b151a Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Thu, 13 Jul 2023 09:57:05 +0200 Subject: [PATCH 9/9] Remove cast --- sdk/examples/how_tos/native_tokens/burn.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/examples/how_tos/native_tokens/burn.rs b/sdk/examples/how_tos/native_tokens/burn.rs index 19a93af81e..95d25f061d 100644 --- a/sdk/examples/how_tos/native_tokens/burn.rs +++ b/sdk/examples/how_tos/native_tokens/burn.rs @@ -19,7 +19,7 @@ use iota_sdk::{ types::block::output::{NativeToken, TokenId}, wallet::Result, - Wallet, + Wallet, U256, }; // The minimum available native token amount to search for in the account @@ -49,7 +49,7 @@ async fn main() -> Result<()> { .unwrap_or_else(|| TokenId::from(*balance.foundries().first().unwrap())); if let Some(native_token_balance) = balance.native_tokens().iter().find(|native_token| { - native_token.token_id() == &token_id && native_token.available().as_u64() >= MIN_AVAILABLE_AMOUNT + native_token.token_id() == &token_id && native_token.available() >= U256::from(MIN_AVAILABLE_AMOUNT) }) { println!("Balance before burning: {native_token_balance:#?}");