Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez committed Oct 16, 2023
1 parent cc54f8a commit 7bba80d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 49 deletions.
6 changes: 1 addition & 5 deletions sdk/src/types/block/output/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ impl From<&OutputId> for AccountId {
impl AccountId {
///
pub fn or_from_output_id(self, output_id: &OutputId) -> Self {
if self.is_null() {
Self::from(output_id)
} else {
self
}
if self.is_null() { Self::from(output_id) } else { self }
}
}

Expand Down
40 changes: 7 additions & 33 deletions sdk/src/wallet/account/operations/output_claiming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::{
address::Address,
output::{
unlock_condition::{AddressUnlockCondition, StorageDepositReturnUnlockCondition},
BasicOutputBuilder, MinimumStorageDepositBasicOutput, NativeTokens, NativeTokensBuilder, NftOutputBuilder,
Output, OutputId,
BasicOutputBuilder, MinimumStorageDepositBasicOutput, NativeTokensBuilder, NftOutputBuilder, Output,
OutputId,
},
slot::SlotIndex,
},
Expand Down Expand Up @@ -249,13 +249,8 @@ where
let mut new_native_tokens = NativeTokensBuilder::new();
// check native tokens
for output_data in &outputs_to_claim {
if let Some(native_tokens) = output_data.output.native_tokens() {
// Skip output if the max native tokens count would be exceeded
if get_new_native_token_count(&new_native_tokens, native_tokens)? > NativeTokens::COUNT_MAX.into() {
log::debug!("[OUTPUT_CLAIMING] skipping output to not exceed the max native tokens count");
continue;
}
new_native_tokens.add_native_tokens(native_tokens.clone())?;
if let Some(native_token) = output_data.output.native_token() {
new_native_tokens.add_native_token(native_token.clone())?;
}
if let Some(sdr) = sdr_not_expired(&output_data.output, slot_index) {
// for own output subtract the return amount
Expand Down Expand Up @@ -332,22 +327,13 @@ where
// the required storage deposit
required_amount = required_amount_for_nfts
+ MinimumStorageDepositBasicOutput::new(rent_structure, token_supply)
.with_native_tokens(option_native_token)
.with_native_token(option_native_token)
.finish()?;

if available_amount < required_amount {
if !additional_inputs_used.contains(&output_data.output_id) {
if let Some(native_tokens) = output_data.output.native_tokens() {
// Skip input if the max native tokens count would be exceeded
if get_new_native_token_count(&new_native_tokens, native_tokens)?
> NativeTokens::COUNT_MAX.into()
{
log::debug!(
"[OUTPUT_CLAIMING] skipping input to not exceed the max native tokens count"
);
continue;
}
new_native_tokens.add_native_tokens(native_tokens.clone())?;
if let Some(native_token) = output_data.output.native_token() {
new_native_tokens.add_native_token(native_token.clone())?;
}
available_amount += output_data.output.amount();
additional_inputs.push(output_data.output_id);
Expand Down Expand Up @@ -430,15 +416,3 @@ pub(crate) fn sdr_not_expired(output: &Output, slot_index: SlotIndex) -> Option<
})
})
}

// Helper function to calculate the native token count without duplicates, when new native tokens are added
// Might be possible to refactor the sections where it's used to remove the clones
pub(crate) fn get_new_native_token_count(
native_tokens_builder: &NativeTokensBuilder,
native_tokens: &NativeTokens,
) -> crate::wallet::Result<usize> {
// Clone to get the new native token count without actually modifying it
let mut native_tokens_count = native_tokens_builder.clone();
native_tokens_count.add_native_tokens(native_tokens.clone())?;
Ok(native_tokens_count.len())
}
15 changes: 4 additions & 11 deletions sdk/src/wallet/account/operations/output_consolidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use crate::{
types::block::{
address::Bech32Address,
input::INPUT_COUNT_MAX,
output::{
unlock_condition::AddressUnlockCondition, BasicOutputBuilder, NativeTokens, NativeTokensBuilder, Output,
},
output::{unlock_condition::AddressUnlockCondition, BasicOutputBuilder, NativeTokensBuilder, Output},
slot::SlotIndex,
},
};
Expand All @@ -31,7 +29,7 @@ use crate::wallet::account::constants::DEFAULT_LEDGER_OUTPUT_CONSOLIDATION_THRES
use crate::wallet::{
account::{
constants::DEFAULT_OUTPUT_CONSOLIDATION_THRESHOLD,
operations::{helpers::time::can_output_be_unlocked_now, output_claiming::get_new_native_token_count},
operations::helpers::time::can_output_be_unlocked_now,
types::{OutputData, Transaction},
Account, AddressWithUnspentOutputs, TransactionOptions,
},
Expand Down Expand Up @@ -241,13 +239,8 @@ where
let mut total_native_tokens = NativeTokensBuilder::new();

for output_data in outputs_to_consolidate.iter().take(max_inputs.into()) {
if let Some(native_tokens) = output_data.output.native_tokens() {
// Skip output if the max native tokens count would be exceeded
if get_new_native_token_count(&total_native_tokens, native_tokens)? > NativeTokens::COUNT_MAX.into() {
log::debug!("[OUTPUT_CONSOLIDATION] skipping output to not exceed the max native tokens count");
continue;
}
total_native_tokens.add_native_tokens(native_tokens.clone())?;
if let Some(native_token) = output_data.output.native_token() {
total_native_tokens.add_native_token(native_token.clone())?;
};
total_amount += output_data.output.amount();

Expand Down

0 comments on commit 7bba80d

Please sign in to comment.