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

Fix foundry output and account output builders #661

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions builder/output_builder_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,16 @@ func (builder *AccountOutputBuilder) Address(addr iotago.Address) *AccountOutput
return builder
}

// Staking sets/modifies an iotago.StakingFeature as a mutable feature on the output.
func (builder *AccountOutputBuilder) Staking(amount iotago.BaseToken, fixedCost iotago.Mana, startEpoch iotago.EpochIndex, optEndEpoch ...iotago.EpochIndex) *AccountOutputBuilder {
endEpoch := iotago.MaxEpochIndex
if len(optEndEpoch) > 0 {
endEpoch = optEndEpoch[0]
}
// Sender sets/modifies an iotago.SenderFeature as a mutable feature on the output.
func (builder *AccountOutputBuilder) Sender(senderAddr iotago.Address) *AccountOutputBuilder {
builder.output.Features.Upsert(&iotago.SenderFeature{Address: senderAddr})

builder.output.Features.Upsert(&iotago.StakingFeature{
StakedAmount: amount,
FixedCost: fixedCost,
StartEpoch: startEpoch,
EndEpoch: endEpoch,
})
return builder
}

// Metadata sets/modifies an iotago.MetadataFeature on the output.
func (builder *AccountOutputBuilder) Metadata(entries iotago.MetadataFeatureEntries) *AccountOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Entries: entries})

return builder
}
Expand All @@ -98,24 +95,27 @@ func (builder *AccountOutputBuilder) BlockIssuer(keys iotago.BlockIssuerKeys, ex
return builder
}

// Sender sets/modifies an iotago.SenderFeature as a mutable feature on the output.
func (builder *AccountOutputBuilder) Sender(senderAddr iotago.Address) *AccountOutputBuilder {
builder.output.Features.Upsert(&iotago.SenderFeature{Address: senderAddr})

return builder
}
// Staking sets/modifies an iotago.StakingFeature as a mutable feature on the output.
func (builder *AccountOutputBuilder) Staking(amount iotago.BaseToken, fixedCost iotago.Mana, startEpoch iotago.EpochIndex, optEndEpoch ...iotago.EpochIndex) *AccountOutputBuilder {
endEpoch := iotago.MaxEpochIndex
if len(optEndEpoch) > 0 {
endEpoch = optEndEpoch[0]
}

// ImmutableSender sets/modifies an iotago.SenderFeature as an immutable feature on the output.
// Only call this function on a new iotago.AccountOutput.
func (builder *AccountOutputBuilder) ImmutableSender(senderAddr iotago.Address) *AccountOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.SenderFeature{Address: senderAddr})
builder.output.Features.Upsert(&iotago.StakingFeature{
StakedAmount: amount,
FixedCost: fixedCost,
StartEpoch: startEpoch,
EndEpoch: endEpoch,
})

return builder
}

// Metadata sets/modifies an iotago.MetadataFeature on the output.
func (builder *AccountOutputBuilder) Metadata(entries iotago.MetadataFeatureEntries) *AccountOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Entries: entries})
// ImmutableIssuer sets/modifies an iotago.IssuerFeature as an immutable feature on the output.
// Only call this function on a new iotago.AccountOutput.
func (builder *AccountOutputBuilder) ImmutableIssuer(issuer iotago.Address) *AccountOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.IssuerFeature{Address: issuer})

return builder
}
Expand Down
34 changes: 8 additions & 26 deletions builder/output_builder_anchor.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,6 @@ func (builder *AnchorOutputBuilder) Governor(governor iotago.Address) *AnchorOut
return builder
}

// Sender sets/modifies an iotago.SenderFeature as a mutable feature on the output.
func (builder *AnchorOutputBuilder) Sender(senderAddr iotago.Address) *AnchorOutputBuilder {
builder.output.Features.Upsert(&iotago.SenderFeature{Address: senderAddr})
builder.govCtrlReq = true

return builder
}

// ImmutableSender sets/modifies an iotago.SenderFeature as an immutable feature on the output.
// Only call this function on a new iotago.AnchorOutput.
func (builder *AnchorOutputBuilder) ImmutableSender(senderAddr iotago.Address) *AnchorOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.SenderFeature{Address: senderAddr})

return builder
}

// Metadata sets/modifies an iotago.MetadataFeature on the output.
func (builder *AnchorOutputBuilder) Metadata(entries iotago.MetadataFeatureEntries) *AnchorOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Entries: entries})
Expand All @@ -109,6 +93,14 @@ func (builder *AnchorOutputBuilder) StateMetadata(entries iotago.StateMetadataFe
return builder
}

// ImmutableIssuer sets/modifies an iotago.IssuerFeature as an immutable feature on the output.
// Only call this function on a new iotago.AnchorOutput.
func (builder *AnchorOutputBuilder) ImmutableIssuer(issuer iotago.Address) *AnchorOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.IssuerFeature{Address: issuer})

return builder
}

// ImmutableMetadata sets/modifies an iotago.MetadataFeature as an immutable feature on the output.
// Only call this function on a new iotago.AnchorOutput.
func (builder *AnchorOutputBuilder) ImmutableMetadata(entries iotago.MetadataFeatureEntries) *AnchorOutputBuilder {
Expand Down Expand Up @@ -175,11 +167,6 @@ func (trans *AnchorStateTransition) StateMetadata(entries iotago.StateMetadataFe
return trans.builder.StateMetadata(entries).StateTransition()
}

// Sender sets/modifies an iotago.SenderFeature as a mutable feature on the output.
func (trans *AnchorStateTransition) Sender(senderAddr iotago.Address) *AnchorStateTransition {
return trans.builder.Sender(senderAddr).StateTransition()
}

// Builder returns the AnchorOutputBuilder.
func (trans *AnchorStateTransition) Builder() *AnchorOutputBuilder {
return trans.builder
Expand All @@ -206,11 +193,6 @@ func (trans *AnchorGovernanceTransition) Governor(governor iotago.Address) *Anch
return trans.builder.Governor(governor).GovernanceTransition()
}

// Sender sets/modifies an iotago.SenderFeature as a mutable feature on the output.
func (trans *AnchorGovernanceTransition) Sender(senderAddr iotago.Address) *AnchorGovernanceTransition {
return trans.builder.Sender(senderAddr).GovernanceTransition()
}

// Metadata sets/modifies an iotago.MetadataFeature as a mutable feature on the output.
func (trans *AnchorGovernanceTransition) Metadata(entries iotago.MetadataFeatureEntries) *AnchorGovernanceTransition {
return trans.builder.Metadata(entries).GovernanceTransition()
Expand Down
14 changes: 7 additions & 7 deletions builder/output_builder_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ func (builder *BasicOutputBuilder) Address(addr iotago.Address) *BasicOutputBuil
return builder
}

// NativeToken adds/modifies a native token to/on the output.
func (builder *BasicOutputBuilder) NativeToken(nt *iotago.NativeTokenFeature) *BasicOutputBuilder {
builder.output.Features.Upsert(nt)

return builder
}

// StorageDepositReturn sets/modifies an iotago.StorageDepositReturnUnlockCondition on the output.
func (builder *BasicOutputBuilder) StorageDepositReturn(returnAddr iotago.Address, amount iotago.BaseToken) *BasicOutputBuilder {
builder.output.UnlockConditions.Upsert(&iotago.StorageDepositReturnUnlockCondition{ReturnAddress: returnAddr, Amount: amount})
Expand Down Expand Up @@ -97,6 +90,13 @@ func (builder *BasicOutputBuilder) Tag(tag []byte) *BasicOutputBuilder {
return builder
}

// NativeToken adds/modifies a native token to/on the output.
func (builder *BasicOutputBuilder) NativeToken(nt *iotago.NativeTokenFeature) *BasicOutputBuilder {
builder.output.Features.Upsert(nt)

return builder
}

// Build builds the iotago.BasicOutput.
func (builder *BasicOutputBuilder) Build() (*iotago.BasicOutput, error) {
builder.output.UnlockConditions.Sort()
Expand Down
23 changes: 5 additions & 18 deletions builder/output_builder_foundry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

// NewFoundryOutputBuilder creates a new FoundryOutputBuilder with the account address, serial number, token scheme and base token amount.
func NewFoundryOutputBuilder(accountAddr *iotago.AccountAddress, tokenScheme iotago.TokenScheme, amount iotago.BaseToken) *FoundryOutputBuilder {
func NewFoundryOutputBuilder(accountAddr *iotago.AccountAddress, amount iotago.BaseToken, serialNumber uint32, tokenScheme iotago.TokenScheme) *FoundryOutputBuilder {
return &FoundryOutputBuilder{output: &iotago.FoundryOutput{
Amount: amount,
SerialNumber: 0,
SerialNumber: serialNumber,
TokenScheme: tokenScheme,
UnlockConditions: iotago.FoundryOutputUnlockConditions{
&iotago.ImmutableAccountUnlockCondition{Address: accountAddr},
Expand Down Expand Up @@ -41,15 +41,9 @@ func (builder *FoundryOutputBuilder) Amount(amount iotago.BaseToken) *FoundryOut
return builder
}

// SerialNumber sets the serial number of the output.
func (builder *FoundryOutputBuilder) SerialNumber(number uint32) *FoundryOutputBuilder {
if builder.prev != nil {
if builder.prev.SerialNumber != number {
panic(ierrors.New("serial number is not allowed to be changed"))
}
}

builder.output.SerialNumber = number
// Metadata sets/modifies an iotago.MetadataFeature on the output.
func (builder *FoundryOutputBuilder) Metadata(entries iotago.MetadataFeatureEntries) *FoundryOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Entries: entries})

return builder
}
Expand All @@ -61,13 +55,6 @@ func (builder *FoundryOutputBuilder) NativeToken(nt *iotago.NativeTokenFeature)
return builder
}

// Metadata sets/modifies an iotago.MetadataFeature on the output.
func (builder *FoundryOutputBuilder) Metadata(entries iotago.MetadataFeatureEntries) *FoundryOutputBuilder {
builder.output.Features.Upsert(&iotago.MetadataFeature{Entries: entries})

return builder
}

// ImmutableMetadata sets/modifies an iotago.MetadataFeature as an immutable feature on the output.
// Only call this function on a new iotago.FoundryOutput.
func (builder *FoundryOutputBuilder) ImmutableMetadata(entries iotago.MetadataFeatureEntries) *FoundryOutputBuilder {
Expand Down
30 changes: 15 additions & 15 deletions builder/output_builder_nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ func (builder *NFTOutputBuilder) Mana(mana iotago.Mana) *NFTOutputBuilder {
return builder
}

// Address sets/modifies an iotago.AddressUnlockCondition on the output.
func (builder *NFTOutputBuilder) Address(addr iotago.Address) *NFTOutputBuilder {
builder.output.UnlockConditions.Upsert(&iotago.AddressUnlockCondition{Address: addr})

return builder
}

// NFTID sets the iotago.NFTID of this output.
// Do not call this function if the underlying iotago.NFTID is not new.
func (builder *NFTOutputBuilder) NFTID(nftID iotago.NFTID) *NFTOutputBuilder {
Expand All @@ -63,6 +56,13 @@ func (builder *NFTOutputBuilder) NFTID(nftID iotago.NFTID) *NFTOutputBuilder {
return builder
}

// Address sets/modifies an iotago.AddressUnlockCondition on the output.
func (builder *NFTOutputBuilder) Address(addr iotago.Address) *NFTOutputBuilder {
builder.output.UnlockConditions.Upsert(&iotago.AddressUnlockCondition{Address: addr})

return builder
}

// StorageDepositReturn sets/modifies an iotago.StorageDepositReturnUnlockCondition on the output.
func (builder *NFTOutputBuilder) StorageDepositReturn(returnAddr iotago.Address, amount iotago.BaseToken) *NFTOutputBuilder {
builder.output.UnlockConditions.Upsert(&iotago.StorageDepositReturnUnlockCondition{ReturnAddress: returnAddr, Amount: amount})
Expand Down Expand Up @@ -98,14 +98,6 @@ func (builder *NFTOutputBuilder) Metadata(entries iotago.MetadataFeatureEntries)
return builder
}

// ImmutableMetadata sets/modifies an iotago.MetadataFeature as an immutable feature on the output.
// Only call this function on a new iotago.NFTOutput.
func (builder *NFTOutputBuilder) ImmutableMetadata(entries iotago.MetadataFeatureEntries) *NFTOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.MetadataFeature{Entries: entries})

return builder
}

// Tag sets/modifies an iotago.TagFeature on the output.
func (builder *NFTOutputBuilder) Tag(tag []byte) *NFTOutputBuilder {
builder.output.Features.Upsert(&iotago.TagFeature{Tag: tag})
Expand All @@ -121,6 +113,14 @@ func (builder *NFTOutputBuilder) ImmutableIssuer(issuer iotago.Address) *NFTOutp
return builder
}

// ImmutableMetadata sets/modifies an iotago.MetadataFeature as an immutable feature on the output.
// Only call this function on a new iotago.NFTOutput.
func (builder *NFTOutputBuilder) ImmutableMetadata(entries iotago.MetadataFeatureEntries) *NFTOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.MetadataFeature{Entries: entries})

return builder
}

// Build builds the iotago.FoundryOutput.
func (builder *NFTOutputBuilder) Build() (*iotago.NFTOutput, error) {
if builder.prev != nil {
Expand Down
23 changes: 12 additions & 11 deletions builder/output_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestAccountOutputBuilder(t *testing.T) {
amount iotago.BaseToken = 1337
metadataEntries = iotago.MetadataFeatureEntries{"data": []byte("123456")}
immMetadataEntries = iotago.MetadataFeatureEntries{"data": []byte("654321")}
immSender = tpkg.RandEd25519Address()
immIssuer = tpkg.RandEd25519Address()

blockIssuerKey1 = iotago.Ed25519PublicKeyBlockIssuerKeyFromPublicKey(tpkg.Rand32ByteArray())
blockIssuerKey2 = iotago.Ed25519PublicKeyBlockIssuerKeyFromPublicKey(tpkg.Rand32ByteArray())
Expand All @@ -66,7 +66,7 @@ func TestAccountOutputBuilder(t *testing.T) {
Staking(amount, 1, 1000).
BlockIssuer(iotago.NewBlockIssuerKeys(blockIssuerKey1, blockIssuerKey2, blockIssuerKey3), 100000).
ImmutableMetadata(immMetadataEntries).
ImmutableSender(immSender).
ImmutableIssuer(immIssuer).
FoundriesToGenerate(5).
Build()
require.NoError(t, err)
Expand All @@ -93,7 +93,7 @@ func TestAccountOutputBuilder(t *testing.T) {
},
},
ImmutableFeatures: iotago.AccountOutputImmFeatures{
&iotago.SenderFeature{Address: immSender},
&iotago.IssuerFeature{Address: immIssuer},
&iotago.MetadataFeature{Entries: immMetadataEntries},
},
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestAccountOutputBuilder(t *testing.T) {
},
},
ImmutableFeatures: iotago.AccountOutputImmFeatures{
&iotago.SenderFeature{Address: immSender},
&iotago.IssuerFeature{Address: immIssuer},
&iotago.MetadataFeature{Entries: immMetadataEntries},
},
}
Expand All @@ -158,13 +158,13 @@ func TestAnchorOutputBuilder(t *testing.T) {
amount iotago.BaseToken = 1337
stateMetadataEntries = iotago.StateMetadataFeatureEntries{"data": []byte("123456")}
immMetadataEntries = iotago.MetadataFeatureEntries{"data": []byte("654321")}
immSender = tpkg.RandEd25519Address()
immIssuer = tpkg.RandEd25519Address()
)

anchorOutput, err := builder.NewAnchorOutputBuilder(stateCtrl, gov, amount).
StateMetadata(stateMetadataEntries).
ImmutableMetadata(immMetadataEntries).
ImmutableSender(immSender).
ImmutableIssuer(immIssuer).
Build()
require.NoError(t, err)

Expand All @@ -179,7 +179,7 @@ func TestAnchorOutputBuilder(t *testing.T) {
&iotago.StateMetadataFeature{Entries: stateMetadataEntries},
},
ImmutableFeatures: iotago.AnchorOutputImmFeatures{
&iotago.SenderFeature{Address: immSender},
&iotago.IssuerFeature{Address: immIssuer},
&iotago.MetadataFeature{Entries: immMetadataEntries},
},
}
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestAnchorOutputBuilder(t *testing.T) {
&iotago.StateMetadataFeature{Entries: stateMetadataEntries},
},
ImmutableFeatures: iotago.AnchorOutputImmFeatures{
&iotago.SenderFeature{Address: immSender},
&iotago.IssuerFeature{Address: immIssuer},
&iotago.MetadataFeature{Entries: immMetadataEntries},
},
}
Expand Down Expand Up @@ -290,16 +290,17 @@ func TestFoundryOutputBuilder(t *testing.T) {
immMetadataEntries = iotago.MetadataFeatureEntries{"data": []byte("654321")}
)

foundryOutput, err := builder.NewFoundryOutputBuilder(accountAddr, tokenScheme, amount).
foundryOutput, err := builder.NewFoundryOutputBuilder(accountAddr, amount, 12345, tokenScheme).
NativeToken(nativeTokenFeature).
Metadata(metadataEntries).
ImmutableMetadata(immMetadataEntries).
Build()
require.NoError(t, err)

require.Equal(t, &iotago.FoundryOutput{
Amount: 1337,
TokenScheme: tokenScheme,
Amount: 1337,
SerialNumber: 12345,
TokenScheme: tokenScheme,
UnlockConditions: iotago.FoundryOutputUnlockConditions{
&iotago.ImmutableAccountUnlockCondition{Address: accountAddr},
},
Expand Down
Loading
Loading