Skip to content

Commit

Permalink
Output changes
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez committed Nov 1, 2023
1 parent cc4c633 commit 37b89ee
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 106 deletions.
82 changes: 43 additions & 39 deletions bindings/nodejs/lib/types/block/output/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ enum OutputType {
Basic = 0,
/** An Account output. */
Account = 1,
/** An Anchor output. */
Anchor = 2,
/** A Foundry output. */
Foundry = 2,
Foundry = 3,
/** An NFT output. */
Nft = 3,
Nft = 4,
/** A Delegation output. */
Delegation = 4,
/** An Anchor output. */
Anchor = 5,
Delegation = 5,
}

/**
Expand Down Expand Up @@ -74,6 +74,8 @@ abstract class Output {
return plainToInstance(BasicOutput, data) as any as BasicOutput;
} else if (data.type == OutputType.Account) {
return plainToInstance(AccountOutput, data) as any as AccountOutput;
} else if (data.type == OutputType.Anchor) {
return plainToInstance(AnchorOutput, data) as any as AnchorOutput;
} else if (data.type == OutputType.Foundry) {
return plainToInstance(FoundryOutput, data) as any as FoundryOutput;
} else if (data.type == OutputType.Nft) {
Expand All @@ -83,8 +85,6 @@ abstract class Output {
DelegationOutput,
data,
) as any as DelegationOutput;
} else if (data.type == OutputType.Anchor) {
return plainToInstance(AnchorOutput, data) as any as AnchorOutput;
}
throw new Error('Invalid JSON');
}
Expand Down Expand Up @@ -144,6 +144,7 @@ abstract class CommonOutput extends Output {
return this.nativeTokens;
}
}

/**
* A Basic output.
*/
Expand Down Expand Up @@ -272,71 +273,73 @@ class AnchorOutput extends ImmutableFeaturesOutput {
}

/**
* An NFT output.
* A Foundry output.
*/
class NftOutput extends ImmutableFeaturesOutput {
class FoundryOutput extends ImmutableFeaturesOutput {
/**
* Unique identifier of the NFT, which is the BLAKE2b-256 hash of the Output ID that created it.
* Unless its newly minted, then the id is zeroed.
* The serial number of the Foundry with respect to the controlling alias.
*/
readonly nftId: NftId;
readonly serialNumber: number;

/**
* The amount of (stored) Mana held by the output.
* The token scheme for the Foundry.
*/
readonly mana: u64;
@Type(() => TokenScheme, {
discriminator: TokenSchemeDiscriminator,
})
readonly tokenScheme: TokenScheme;

/**
* @param amount The amount of the output.
* @param mana The amount of stored mana.
* @param nftId The NFT ID as hex-encoded string.
* @param serialNumber The serial number of the Foundry with respect to the controlling alias.
* @param unlockConditions The unlock conditions of the output.
* @param tokenScheme The token scheme for the Foundry.
*/
constructor(
amount: u64,
mana: u64,
nftId: NftId,
serialNumber: number,
unlockConditions: UnlockCondition[],
tokenScheme: TokenScheme,
) {
super(OutputType.Nft, amount, unlockConditions);
this.nftId = nftId;
this.mana = mana;
super(OutputType.Foundry, amount, unlockConditions);
this.serialNumber = serialNumber;
this.tokenScheme = tokenScheme;
}
}

/**
* A Foundry output.
* An NFT output.
*/
class FoundryOutput extends ImmutableFeaturesOutput {
class NftOutput extends ImmutableFeaturesOutput {
/**
* The serial number of the Foundry with respect to the controlling alias.
* Unique identifier of the NFT, which is the BLAKE2b-256 hash of the Output ID that created it.
* Unless its newly minted, then the id is zeroed.
*/
readonly serialNumber: number;
readonly nftId: NftId;

/**
* The token scheme for the Foundry.
* The amount of (stored) Mana held by the output.
*/
@Type(() => TokenScheme, {
discriminator: TokenSchemeDiscriminator,
})
readonly tokenScheme: TokenScheme;
readonly mana: u64;

/**
* @param amount The amount of the output.
* @param serialNumber The serial number of the Foundry with respect to the controlling alias.
* @param mana The amount of stored mana.
* @param nftId The NFT ID as hex-encoded string.
* @param unlockConditions The unlock conditions of the output.
* @param tokenScheme The token scheme for the Foundry.
*/
constructor(
amount: u64,
serialNumber: number,
mana: u64,
nftId: NftId,
unlockConditions: UnlockCondition[],
tokenScheme: TokenScheme,
) {
super(OutputType.Foundry, amount, unlockConditions);
this.serialNumber = serialNumber;
this.tokenScheme = tokenScheme;
super(OutputType.Nft, amount, unlockConditions);
this.nftId = nftId;
this.mana = mana;
}
}

/**
* A Delegation output.
*/
Expand Down Expand Up @@ -402,8 +405,9 @@ const OutputDiscriminator = {
subTypes: [
{ value: BasicOutput, name: OutputType.Basic as any },
{ value: AccountOutput, name: OutputType.Account as any },
{ value: NftOutput, name: OutputType.Nft as any },
{ value: AnchorOutput, name: OutputType.Anchor as any },
{ value: FoundryOutput, name: OutputType.Foundry as any },
{ value: NftOutput, name: OutputType.Nft as any },
{ value: DelegationOutput, name: OutputType.Delegation as any },
],
};
Expand All @@ -416,7 +420,7 @@ export {
BasicOutput,
AccountOutput,
AnchorOutput,
NftOutput,
FoundryOutput,
NftOutput,
DelegationOutput,
};
29 changes: 17 additions & 12 deletions bindings/python/iota_sdk/types/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ class OutputType(IntEnum):
Attributes:
Basic (0): A basic output.
Account (1): An account output.
Foundry (2): A foundry output.
Nft (3): An NFT output.
Delegation (4): A delegation output.
Anchor (5): An anchor output.
Anchor (2): An anchor output.
Foundry (3): A foundry output.
Nft (4): An NFT output.
Delegation (5): A delegation output.
"""
Basic = 0
Account = 1
Foundry = 2
Nft = 3
Delegation = 4
Anchor = 5
Anchor = 2
Foundry = 3
Nft = 4
Delegation = 5


@json
Expand Down Expand Up @@ -304,8 +305,12 @@ class DelegationOutput:
OutputType.Delegation), init=False)


Output: TypeAlias = Union[BasicOutput, AccountOutput,
FoundryOutput, NftOutput, DelegationOutput, AnchorOutput]
Output: TypeAlias = Union[BasicOutput,
AccountOutput,
AnchorOutput,
FoundryOutput,
NftOutput,
DelegationOutput]


def deserialize_output(d: Dict[str, Any]) -> Output:
Expand All @@ -320,14 +325,14 @@ def deserialize_output(d: Dict[str, Any]) -> Output:
return BasicOutput.from_dict(d)
if output_type == OutputType.Account:
return AccountOutput.from_dict(d)
if output_type == OutputType.Anchor:
return AnchorOutput.from_dict(d)
if output_type == OutputType.Foundry:
return FoundryOutput.from_dict(d)
if output_type == OutputType.Nft:
return NftOutput.from_dict(d)
if output_type == OutputType.Delegation:
return DelegationOutput.from_dict(d)
if output_type == OutputType.Anchor:
return AnchorOutput.from_dict(d)
raise Exception(f'invalid output type: {output_type}')


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ impl InputSelection {
Output::Account(output) => AccountOutputBuilder::from(&*output)
.with_amount(new_amount)
.finish_output(self.protocol_parameters.token_supply())?,
Output::Nft(output) => NftOutputBuilder::from(&*output)
Output::Foundry(output) => FoundryOutputBuilder::from(&*output)
.with_amount(new_amount)
.finish_output(self.protocol_parameters.token_supply())?,
Output::Foundry(output) => FoundryOutputBuilder::from(&*output)
Output::Nft(output) => NftOutputBuilder::from(&*output)
.with_amount(new_amount)
.finish_output(self.protocol_parameters.token_supply())?,
_ => panic!("only account, nft and foundry can be automatically created"),
Expand Down
24 changes: 12 additions & 12 deletions sdk/src/client/api/block_builder/input_selection/requirement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,6 @@ impl InputSelection {

is_created
}
// Add an nft requirement if the nft output is transitioning and then required in the inputs.
Output::Nft(nft_output) => {
let is_created = nft_output.nft_id().is_null();

if !is_created {
let requirement = Requirement::Nft(*nft_output.nft_id());
log::debug!("Adding {requirement:?} from output");
self.requirements.push(requirement);
}

is_created
}
// Add a foundry requirement if the foundry output is transitioning and then required in the inputs.
// Also add an account requirement since the associated account output needs to be transitioned.
Output::Foundry(foundry_output) => {
Expand All @@ -114,6 +102,18 @@ impl InputSelection {

is_created
}
// Add an nft requirement if the nft output is transitioning and then required in the inputs.
Output::Nft(nft_output) => {
let is_created = nft_output.nft_id().is_null();

if !is_created {
let requirement = Requirement::Nft(*nft_output.nft_id());
log::debug!("Adding {requirement:?} from output");
self.requirements.push(requirement);
}

is_created
}
_ => false,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ impl InputSelection {
pub(crate) fn transition_input(&mut self, input: &InputSigningData) -> Result<Option<Output>, Error> {
match &input.output {
Output::Account(account_input) => self.transition_account_input(account_input, input.output_id()),
Output::Nft(nft_input) => self.transition_nft_input(nft_input, input.output_id()),
Output::Foundry(foundry_input) => self.transition_foundry_input(foundry_input, input.output_id()),
Output::Nft(nft_input) => self.transition_nft_input(nft_input, input.output_id()),
_ => Ok(None),
}
}
Expand Down
3 changes: 1 addition & 2 deletions sdk/src/types/block/output/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,7 @@ pub struct AnchorOutput {

impl AnchorOutput {
/// The [`Output`](crate::types::block::output::Output) kind of an [`AnchorOutput`].
/// TODO
pub const KIND: u8 = 255;
pub const KIND: u8 = 2;
/// Maximum possible length in bytes of the state metadata.
pub const STATE_METADATA_LENGTH_MAX: u16 = 8192;
/// The set of allowed [`UnlockCondition`]s for an [`AnchorOutput`].
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/output/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ pub struct DelegationOutput {

impl DelegationOutput {
/// The [`Output`](crate::types::block::output::Output) kind of a [`DelegationOutput`].
pub const KIND: u8 = 4;
pub const KIND: u8 = 5;
/// The set of allowed [`UnlockCondition`]s for a [`DelegationOutput`].
pub const ALLOWED_UNLOCK_CONDITIONS: UnlockConditionFlags = UnlockConditionFlags::ADDRESS;

Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/output/foundry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ pub struct FoundryOutput {

impl FoundryOutput {
/// The [`Output`](crate::types::block::output::Output) kind of a [`FoundryOutput`].
pub const KIND: u8 = 2;
pub const KIND: u8 = 3;
/// The set of allowed [`UnlockCondition`]s for a [`FoundryOutput`].
pub const ALLOWED_UNLOCK_CONDITIONS: UnlockConditionFlags = UnlockConditionFlags::IMMUTABLE_ACCOUNT_ADDRESS;
/// The set of allowed [`Feature`]s for a [`FoundryOutput`].
Expand Down
Loading

0 comments on commit 37b89ee

Please sign in to comment.