Skip to content

Commit

Permalink
Merge branch '2.0' into address-converters-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Mar 7, 2024
2 parents a61fc00 + 23631d8 commit dace37b
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 129 deletions.
14 changes: 11 additions & 3 deletions bindings/nodejs/lib/types/client/burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

import { u256 } from '../utils';
import { AccountId, FoundryId, NftId, TokenId } from '../block/id';
import {
AccountId,
DelegationId,
FoundryId,
NftId,
TokenId,
} from '../block/id';

/** A DTO for [`Burn`] */
export interface Burn {
Expand All @@ -12,10 +18,12 @@ export interface Burn {
generatedMana?: boolean;
/** Accounts to burn */
accounts?: AccountId[];
/** NFTs to burn */
nfts?: NftId[];
/** Foundries to burn */
foundries?: FoundryId[];
/** NFTs to burn */
nfts?: NftId[];
/** Delegations to burn */
delegations?: DelegationId[];
/** Amounts of native tokens to burn */
nativeTokens?: Map<TokenId, u256>;
}
24 changes: 17 additions & 7 deletions bindings/python/iota_sdk/types/burn.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ class Burn:
mana: Whether initial excess mana should be burned (only from inputs/outputs that have been specified manually).
generated_mana: Whether generated mana should be burned.
accounts: The accounts to burn.
nfts: The NFTs to burn.
foundries: The foundries to burn.
nfts: The NFTs to burn.
delegations: The delegations to burn.
native_tokens: The native tokens to burn.
"""

mana: Optional[bool] = None
generated_mana: Optional[bool] = None
accounts: Optional[List[HexStr]] = None
nfts: Optional[List[HexStr]] = None
foundries: Optional[List[HexStr]] = None
nfts: Optional[List[HexStr]] = None
delegations: Optional[List[HexStr]] = None
native_tokens: Optional[List[NativeToken]] = None

def set_mana(self, burn_mana: bool) -> Burn:
Expand All @@ -49,6 +51,14 @@ def add_account(self, account: HexStr) -> Burn:
self.accounts.append(account)
return self

def add_foundry(self, foundry: HexStr) -> Burn:
"""Add a foundry to the burn.
"""
if self.foundries is None:
self.foundries = []
self.foundries.append(foundry)
return self

def add_nft(self, nft: HexStr) -> Burn:
"""Add an NFT to the burn.
"""
Expand All @@ -57,12 +67,12 @@ def add_nft(self, nft: HexStr) -> Burn:
self.nfts.append(nft)
return self

def add_foundry(self, foundry: HexStr) -> Burn:
"""Add a foundry to the burn.
def add_delegation(self, delegation: HexStr) -> Burn:
"""Add a delegation to the burn.
"""
if self.foundries is None:
self.foundries = []
self.foundries.append(foundry)
if self.delegations is None:
self.delegations = []
self.delegations.append(delegation)
return self

def add_native_token(self, native_token: NativeToken) -> Burn:
Expand Down
11 changes: 1 addition & 10 deletions sdk/src/client/api/block_builder/transaction_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,16 +347,7 @@ impl TransactionBuilder {
// Process all the requirements until there are no more.
while let Some(requirement) = self.requirements.pop() {
// Fulfill the requirement.
let inputs = self.fulfill_requirement(&requirement)?;

if !self.allow_additional_input_selection && !inputs.is_empty() {
return Err(TransactionBuilderError::AdditionalInputsRequired(requirement));
}

// Select suggested inputs.
for input in inputs {
self.select_input(input)?;
}
self.fulfill_requirement(&requirement)?;
}

let (input_mana, output_mana) = self.mana_sums(false)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use super::{Requirement, TransactionBuilder, TransactionBuilderError};
use crate::{
client::secret::types::InputSigningData,
types::block::output::{AccountId, Output, OutputId},
};
use crate::types::block::output::{AccountId, Output, OutputId};

/// Checks if an output is an account with output ID that matches the given account ID.
pub(crate) fn is_account_with_id(output: &Output, account_id: &AccountId, output_id: &OutputId) -> bool {
Expand All @@ -28,10 +25,7 @@ pub(crate) fn is_account_with_id_non_null(output: &Output, account_id: &AccountI

impl TransactionBuilder {
/// Fulfills an account requirement by selecting the appropriate account from the available inputs.
pub(crate) fn fulfill_account_requirement(
&mut self,
account_id: AccountId,
) -> Result<Vec<InputSigningData>, TransactionBuilderError> {
pub(crate) fn fulfill_account_requirement(&mut self, account_id: AccountId) -> Result<(), TransactionBuilderError> {
// Check if the requirement is already fulfilled.
if let Some(input) = self
.selected_inputs
Expand All @@ -42,7 +36,13 @@ impl TransactionBuilder {
"{account_id:?} requirement already fulfilled by {:?}",
input.output_id()
);
return Ok(Vec::new());
return Ok(());
}

if !self.allow_additional_input_selection {
return Err(TransactionBuilderError::AdditionalInputsRequired(Requirement::Account(
account_id,
)));
}

// Check if the requirement can be fulfilled.
Expand All @@ -58,6 +58,8 @@ impl TransactionBuilder {

log::debug!("{account_id:?} requirement fulfilled by {:?}", input.output_id());

Ok(vec![input])
self.select_input(input)?;

Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ pub(crate) fn sdruc_not_expired(
}

impl TransactionBuilder {
pub(crate) fn fulfill_amount_requirement(&mut self) -> Result<Vec<InputSigningData>, TransactionBuilderError> {
pub(crate) fn fulfill_amount_requirement(&mut self) -> Result<(), TransactionBuilderError> {
let (mut input_amount, mut output_amount) = self.amount_balance()?;
if input_amount >= output_amount {
log::debug!("Amount requirement already fulfilled");
return Ok(Vec::new());
return Ok(());
}

log::debug!("Fulfilling amount requirement with input amount {input_amount}, output amount {output_amount}");
Expand Down Expand Up @@ -82,7 +82,7 @@ impl TransactionBuilder {
}
}

Ok(Vec::new())
Ok(())
}

pub(crate) fn amount_sums(&self) -> (u64, u64, HashMap<Address, u64>, HashMap<Address, u64>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

use super::{TransactionBuilder, TransactionBuilderError};
use crate::{
client::secret::types::InputSigningData,
types::block::{
context_input::{BlockIssuanceCreditContextInput, CommitmentContextInput},
output::{AccountId, DelegationOutputBuilder, Output},
},
use crate::types::block::{
context_input::{BlockIssuanceCreditContextInput, CommitmentContextInput},
output::{AccountId, DelegationOutputBuilder, Output},
};

impl TransactionBuilder {
pub(crate) fn fulfill_context_inputs_requirement(
&mut self,
) -> Result<Vec<InputSigningData>, TransactionBuilderError> {
pub(crate) fn fulfill_context_inputs_requirement(&mut self) -> Result<(), TransactionBuilderError> {
let mut needs_commitment_context = false;

for input in &self.selected_inputs {
Expand Down Expand Up @@ -95,6 +90,6 @@ impl TransactionBuilder {
self.commitment_context_input
.replace(CommitmentContextInput::new(self.latest_slot_commitment_id));
}
Ok(Vec::new())
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use super::{Requirement, TransactionBuilder, TransactionBuilderError};
use crate::{
client::secret::types::InputSigningData,
types::block::output::{DelegationId, Output, OutputId},
};
use crate::types::block::output::{DelegationId, Output, OutputId};

/// Checks if an output is an delegation with a given delegation ID.
/// Assumes that the output delegation ID can be null and hashes the output ID.
Expand Down Expand Up @@ -34,7 +31,7 @@ impl TransactionBuilder {
pub(crate) fn fulfill_delegation_requirement(
&mut self,
delegation_id: DelegationId,
) -> Result<Vec<InputSigningData>, TransactionBuilderError> {
) -> Result<(), TransactionBuilderError> {
// Check if the requirement is already fulfilled.
if let Some(input) = self
.selected_inputs
Expand All @@ -45,7 +42,13 @@ impl TransactionBuilder {
"{delegation_id:?} requirement already fulfilled by {:?}",
input.output_id()
);
return Ok(Vec::new());
return Ok(());
}

if !self.allow_additional_input_selection {
return Err(TransactionBuilderError::AdditionalInputsRequired(
Requirement::Delegation(delegation_id),
));
}

// Check if the requirement can be fulfilled.
Expand All @@ -61,6 +64,8 @@ impl TransactionBuilder {

log::debug!("{delegation_id:?} requirement fulfilled by {:?}", input.output_id());

Ok(vec![input])
self.select_input(input)?;

Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ impl TransactionBuilder {
}

/// Fulfills an ed25519 sender requirement by selecting an available input that unlocks its address.
pub(crate) fn fulfill_ed25519_requirement(
&mut self,
address: &Address,
) -> Result<Vec<InputSigningData>, TransactionBuilderError> {
pub(crate) fn fulfill_ed25519_requirement(&mut self, address: &Address) -> Result<(), TransactionBuilderError> {
// Checks if the requirement is already fulfilled.
if let Some(input) = self
.selected_inputs
Expand All @@ -53,7 +50,13 @@ impl TransactionBuilder {
"{address:?} sender requirement already fulfilled by {:?}",
input.output_id()
);
return Ok(Vec::new());
return Ok(());
}

if !self.allow_additional_input_selection {
return Err(TransactionBuilderError::AdditionalInputsRequired(Requirement::Ed25519(
address.clone(),
)));
}

// Checks if the requirement can be fulfilled by a basic output.
Expand All @@ -78,7 +81,9 @@ impl TransactionBuilder {

log::debug!("{address:?} sender requirement fulfilled by {:?}", input.output_id(),);

Ok(vec![input])
self.select_input(input)?;

Ok(())
}
None => Err(TransactionBuilderError::UnfulfillableRequirement(Requirement::Ed25519(
address.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use super::{Requirement, TransactionBuilder, TransactionBuilderError};
use crate::{
client::secret::types::InputSigningData,
types::block::output::{FoundryId, Output},
};
use crate::types::block::output::{FoundryId, Output};

/// Checks if an output is a foundry with a given foundry ID.
pub(crate) fn is_foundry_with_id(output: &Output, foundry_id: &FoundryId) -> bool {
Expand All @@ -18,10 +15,7 @@ pub(crate) fn is_foundry_with_id(output: &Output, foundry_id: &FoundryId) -> boo

impl TransactionBuilder {
/// Fulfills a foundry requirement by selecting the appropriate foundry from the available inputs.
pub(crate) fn fulfill_foundry_requirement(
&mut self,
foundry_id: FoundryId,
) -> Result<Vec<InputSigningData>, TransactionBuilderError> {
pub(crate) fn fulfill_foundry_requirement(&mut self, foundry_id: FoundryId) -> Result<(), TransactionBuilderError> {
// Check if the requirement is already fulfilled.
if let Some(input) = self
.selected_inputs
Expand All @@ -32,7 +26,13 @@ impl TransactionBuilder {
"{foundry_id:?} requirement already fulfilled by {:?}",
input.output_id()
);
return Ok(Vec::new());
return Ok(());
}

if !self.allow_additional_input_selection {
return Err(TransactionBuilderError::AdditionalInputsRequired(Requirement::Foundry(
foundry_id,
)));
}

// Check if the requirement can be fulfilled.
Expand All @@ -48,6 +48,8 @@ impl TransactionBuilder {

log::debug!("{foundry_id:?} requirement fulfilled by {:?}", input.output_id());

Ok(vec![input])
self.select_input(input)?;

Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@
// SPDX-License-Identifier: Apache-2.0

use super::{Requirement, TransactionBuilder, TransactionBuilderError};
use crate::{client::secret::types::InputSigningData, types::block::address::Address};
use crate::types::block::address::Address;

impl TransactionBuilder {
/// Fulfills an issuer requirement by fulfilling the equivalent sender requirement.
/// Potentially converts the error for a more accurate one.
pub(crate) fn fulfill_issuer_requirement(
&mut self,
address: &Address,
) -> Result<Vec<InputSigningData>, TransactionBuilderError> {
pub(crate) fn fulfill_issuer_requirement(&mut self, address: &Address) -> Result<(), TransactionBuilderError> {
log::debug!("Treating {address:?} issuer requirement as a sender requirement");

match self.fulfill_sender_requirement(address) {
Ok(res) => Ok(res),
Err(TransactionBuilderError::UnfulfillableRequirement(Requirement::Sender(_))) => Err(
TransactionBuilderError::UnfulfillableRequirement(Requirement::Issuer(address.clone())),
),
Err(e) => Err(e),
}
self.fulfill_sender_requirement(address).map_err(|e| match e {
TransactionBuilderError::UnfulfillableRequirement(Requirement::Sender(_)) => {
TransactionBuilderError::UnfulfillableRequirement(Requirement::Issuer(address.clone()))
}
e => e,
})
}
}
Loading

0 comments on commit dace37b

Please sign in to comment.