Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing indexer tests + impl Into<U256> params #766

Merged
merged 10 commits into from
Jul 13, 2023
10 changes: 3 additions & 7 deletions bindings/core/src/method_handler/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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?;
Expand All @@ -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?;
Expand Down
7 changes: 3 additions & 4 deletions sdk/examples/client/output/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use iota_sdk::{
payload::{transaction::TransactionEssence, Payload},
},
};
use primitive_types::U256;

#[tokio::main]
async fn main() -> Result<()> {
Expand Down Expand Up @@ -105,7 +104,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,
Expand All @@ -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()
Expand Down Expand Up @@ -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)?,
Expand Down
7 changes: 3 additions & 4 deletions sdk/examples/client/output/all_automatic_input_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use iota_sdk::{
payload::{transaction::TransactionEssence, Payload},
},
};
use primitive_types::U256;

#[tokio::main]
async fn main() -> Result<()> {
Expand Down Expand Up @@ -98,7 +97,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,
Expand All @@ -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()
Expand Down Expand Up @@ -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)?,
Expand Down
30 changes: 9 additions & 21 deletions sdk/examples/client/output/foundry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use iota_sdk::{
payload::{transaction::TransactionEssence, Payload},
},
};
use primitive_types::U256;

#[tokio::main]
async fn main() -> Result<()> {
Expand Down Expand Up @@ -92,11 +91,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,
Expand All @@ -112,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)?,
];
Expand All @@ -135,16 +130,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())?;
Expand All @@ -158,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)?,
];

Expand Down Expand Up @@ -199,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)?,
];

Expand Down Expand Up @@ -234,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
Expand All @@ -259,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
Expand Down
10 changes: 2 additions & 8 deletions sdk/examples/client/output/native_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions sdk/examples/how_tos/native_tokens/burn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions sdk/examples/how_tos/native_tokens/melt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions sdk/examples/how_tos/native_tokens/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions sdk/examples/how_tos/outputs/unlock_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use iota_sdk::{
},
},
};
use primitive_types::U256;

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)?)?;
}
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/types/block/output/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -1050,15 +1050,15 @@ 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))
.with_immutable_features(rand_allowed_features(AliasOutput::ALLOWED_IMMUTABLE_FEATURES));
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))
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/types/block/output/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/types/block/output/foundry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand Down
4 changes: 3 additions & 1 deletion sdk/src/types/block/output/native_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ pub struct NativeToken {
impl NativeToken {
/// Creates a new [`NativeToken`].
#[inline(always)]
pub fn new(token_id: TokenId, amount: U256) -> Result<Self, Error> {
pub fn new(token_id: TokenId, amount: impl Into<U256>) -> Result<Self, Error> {
let amount = amount.into();

verify_amount::<true>(&amount, &())?;

Ok(Self { token_id, amount })
Expand Down
Loading
Loading