/
output_builder_account.go
275 lines (217 loc) · 8.74 KB
/
output_builder_account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package builder
import (
"github.com/iotaledger/hive.go/ierrors"
iotago "github.com/iotaledger/iota.go/v4"
)
// NewAccountOutputBuilder creates a new AccountOutputBuilder with the address and base token amount.
func NewAccountOutputBuilder(targetAddr iotago.Address, amount iotago.BaseToken) *AccountOutputBuilder {
return &AccountOutputBuilder{output: &iotago.AccountOutput{
Amount: amount,
Mana: 0,
AccountID: iotago.EmptyAccountID,
FoundryCounter: 0,
UnlockConditions: iotago.AccountOutputUnlockConditions{
&iotago.AddressUnlockCondition{Address: targetAddr},
},
Features: iotago.AccountOutputFeatures{},
ImmutableFeatures: iotago.AccountOutputImmFeatures{},
}}
}
// NewAccountOutputBuilderFromPrevious creates a new AccountOutputBuilder starting from a copy of the previous iotago.AccountOutput.
func NewAccountOutputBuilderFromPrevious(previous *iotago.AccountOutput) *AccountOutputBuilder {
return &AccountOutputBuilder{
prev: previous,
//nolint:forcetypeassert // we can safely assume that this is an AccountOutput
output: previous.Clone().(*iotago.AccountOutput),
}
}
// AccountOutputBuilder builds an iotago.AccountOutput.
type AccountOutputBuilder struct {
prev *iotago.AccountOutput
output *iotago.AccountOutput
}
// Amount sets the base token amount of the output.
func (builder *AccountOutputBuilder) Amount(amount iotago.BaseToken) *AccountOutputBuilder {
builder.output.Amount = amount
return builder
}
// Mana sets the mana of the output.
func (builder *AccountOutputBuilder) Mana(mana iotago.Mana) *AccountOutputBuilder {
builder.output.Mana = mana
return builder
}
// AccountID sets the iotago.AccountID of this output.
// Do not call this function if the underlying iotago.AccountOutput is not new.
func (builder *AccountOutputBuilder) AccountID(accountID iotago.AccountID) *AccountOutputBuilder {
builder.output.AccountID = accountID
return builder
}
// FoundriesToGenerate bumps the output's foundry counter by the amount of foundries to generate.
func (builder *AccountOutputBuilder) FoundriesToGenerate(count uint32) *AccountOutputBuilder {
builder.output.FoundryCounter += count
return builder
}
// Address sets/modifies an iotago.AddressUnlockCondition on the output.
func (builder *AccountOutputBuilder) Address(addr iotago.Address) *AccountOutputBuilder {
builder.output.UnlockConditions.Upsert(&iotago.AddressUnlockCondition{Address: addr})
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
}
// 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
}
// BlockIssuer sets/modifies an iotago.BlockIssuerFeature as a mutable feature on the output.
func (builder *AccountOutputBuilder) BlockIssuer(keys iotago.BlockIssuerKeys, expirySlot iotago.SlotIndex) *AccountOutputBuilder {
builder.output.Features.Upsert(&iotago.BlockIssuerFeature{
BlockIssuerKeys: keys,
ExpirySlot: expirySlot,
})
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]
}
builder.output.Features.Upsert(&iotago.StakingFeature{
StakedAmount: amount,
FixedCost: fixedCost,
StartEpoch: startEpoch,
EndEpoch: endEpoch,
})
return builder
}
// 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
}
// ImmutableMetadata sets/modifies an iotago.MetadataFeature as an immutable feature on the output.
// Only call this function on a new iotago.AccountOutput.
func (builder *AccountOutputBuilder) ImmutableMetadata(entries iotago.MetadataFeatureEntries) *AccountOutputBuilder {
builder.output.ImmutableFeatures.Upsert(&iotago.MetadataFeature{Entries: entries})
return builder
}
// Build builds the iotago.AccountOutput.
func (builder *AccountOutputBuilder) Build() (*iotago.AccountOutput, error) {
if builder.prev != nil {
if !builder.prev.ImmutableFeatures.Equal(builder.output.ImmutableFeatures) {
return nil, ierrors.New("immutable features are not allowed to be changed")
}
}
builder.output.UnlockConditions.Sort()
builder.output.Features.Sort()
builder.output.ImmutableFeatures.Sort()
return builder.output, nil
}
// MustBuild works like Build() but panics if an error is encountered.
func (builder *AccountOutputBuilder) MustBuild() *iotago.AccountOutput {
output, err := builder.Build()
if err != nil {
panic(err)
}
return output
}
// RemoveFeature removes a feature from the output.
func (builder *AccountOutputBuilder) RemoveFeature(featureType iotago.FeatureType) *AccountOutputBuilder {
builder.output.Features.Remove(featureType)
return builder
}
// BlockIssuerTransition narrows the builder functions to the ones available for an iotago.BlockIssuerFeature transition.
// If BlockIssuerFeature does not exist, it creates and sets an empty feature.
func (builder *AccountOutputBuilder) BlockIssuerTransition() *BlockIssuerTransition {
blockIssuerFeature := builder.output.FeatureSet().BlockIssuer()
if blockIssuerFeature == nil {
blockIssuerFeature = &iotago.BlockIssuerFeature{
BlockIssuerKeys: iotago.NewBlockIssuerKeys(),
ExpirySlot: 0,
}
}
return &BlockIssuerTransition{
feature: blockIssuerFeature,
builder: builder,
}
}
// StakingTransition narrows the builder functions to the ones available for an iotago.StakingFeature transition.
// If StakingFeature does not exist, it creates and sets an empty feature.
func (builder *AccountOutputBuilder) StakingTransition() *StakingTransition {
stakingFeature := builder.output.FeatureSet().Staking()
if stakingFeature == nil {
stakingFeature = &iotago.StakingFeature{
StakedAmount: 0,
FixedCost: 0,
StartEpoch: 0,
EndEpoch: 0,
}
}
return &StakingTransition{
feature: stakingFeature,
builder: builder,
}
}
type BlockIssuerTransition struct {
feature *iotago.BlockIssuerFeature
builder *AccountOutputBuilder
}
// AddKeys adds the keys of the BlockIssuerFeature.
func (trans *BlockIssuerTransition) AddKeys(keys ...iotago.BlockIssuerKey) *BlockIssuerTransition {
for _, blockIssuerKey := range keys {
trans.feature.BlockIssuerKeys.Add(blockIssuerKey)
}
return trans
}
// RemoveKey deletes the key of the iotago.BlockIssuerFeature.
func (trans *BlockIssuerTransition) RemoveKey(keyToDelete iotago.BlockIssuerKey) *BlockIssuerTransition {
trans.feature.BlockIssuerKeys.Remove(keyToDelete)
return trans
}
// Keys sets the keys of the iotago.BlockIssuerFeature.
func (trans *BlockIssuerTransition) Keys(keys iotago.BlockIssuerKeys) *BlockIssuerTransition {
trans.feature.BlockIssuerKeys = keys
return trans
}
// ExpirySlot sets the ExpirySlot of iotago.BlockIssuerFeature.
func (trans *BlockIssuerTransition) ExpirySlot(slot iotago.SlotIndex) *BlockIssuerTransition {
trans.feature.ExpirySlot = slot
return trans
}
// Builder returns the AccountOutputBuilder.
func (trans *BlockIssuerTransition) Builder() *AccountOutputBuilder {
return trans.builder
}
type StakingTransition struct {
feature *iotago.StakingFeature
builder *AccountOutputBuilder
}
// StakedAmount sets the StakedAmount of iotago.StakingFeature.
func (trans *StakingTransition) StakedAmount(amount iotago.BaseToken) *StakingTransition {
trans.feature.StakedAmount = amount
return trans
}
// FixedCost sets the FixedCost of iotago.StakingFeature.
func (trans *StakingTransition) FixedCost(fixedCost iotago.Mana) *StakingTransition {
trans.feature.FixedCost = fixedCost
return trans
}
// StartEpoch sets the StartEpoch of iotago.StakingFeature.
func (trans *StakingTransition) StartEpoch(epoch iotago.EpochIndex) *StakingTransition {
trans.feature.StartEpoch = epoch
return trans
}
// EndEpoch sets the EndEpoch of iotago.StakingFeature.
func (trans *StakingTransition) EndEpoch(epoch iotago.EpochIndex) *StakingTransition {
trans.feature.EndEpoch = epoch
return trans
}
// Builder returns the AccountOutputBuilder.
func (trans *StakingTransition) Builder() *AccountOutputBuilder {
return trans.builder
}