-
Notifications
You must be signed in to change notification settings - Fork 95
/
output_foundry.go
283 lines (228 loc) · 7.92 KB
/
output_foundry.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
276
277
278
279
280
281
282
283
package iotago
import (
"context"
"encoding/binary"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/iota.go/v4/hexutil"
)
const (
FoundrySerialNumberLength = serializer.UInt32ByteSize
FoundryTokenSchemeLength = serializer.OneByte
// FoundryIDLength is the byte length of a FoundryID consisting out of the account address, serial number and token scheme.
FoundryIDLength = AccountAddressSerializedBytesSize + FoundrySerialNumberLength + FoundryTokenSchemeLength
)
var (
// ErrFoundryTransitionWithoutAccount gets returned when a foundry output is transitioned
// without an accompanying account on the input or output side.
ErrFoundryTransitionWithoutAccount = ierrors.New("foundry output transitioned without accompanying account on input or output side")
// ErrFoundrySerialInvalid gets returned when the foundry output's serial number is invalid.
ErrFoundrySerialInvalid = ierrors.New("foundry output serial number is invalid")
emptyFoundryID = [FoundryIDLength]byte{}
)
// FoundryID defines the identifier for a foundry consisting out of the address, serial number and TokenScheme.
type FoundryID [FoundryIDLength]byte
func FoundryIDFromAddressAndSerialNumberAndTokenScheme(addr Address, serialNumber uint32, tokenScheme TokenSchemeType) (NativeTokenID, error) {
serixAPI := CommonSerixAPI()
var foundryID FoundryID
addrBytes, err := serixAPI.Encode(context.Background(), addr)
if err != nil {
return foundryID, err
}
copy(foundryID[:], addrBytes)
binary.LittleEndian.PutUint32(foundryID[len(addrBytes):], serialNumber)
foundryID[len(foundryID)-1] = byte(tokenScheme)
return foundryID, nil
}
func (fID FoundryID) ToHex() string {
return hexutil.EncodeHex(fID[:])
}
func (fID FoundryID) Addressable() bool {
return false
}
// FoundrySerialNumber returns the serial number of the foundry.
func (fID FoundryID) FoundrySerialNumber() uint32 {
return binary.LittleEndian.Uint32(fID[AccountAddressSerializedBytesSize : AccountAddressSerializedBytesSize+FoundrySerialNumberLength])
}
func (fID FoundryID) Matches(other ChainID) bool {
otherFID, is := other.(FoundryID)
if !is {
return false
}
return fID == otherFID
}
func (fID FoundryID) AccountAddress() (*AccountAddress, error) {
var addr Address
if _, err := CommonSerixAPI().Decode(context.Background(), fID[:], &addr); err != nil {
return nil, err
}
accountAddr, isAccountAddr := addr.(*AccountAddress)
if !isAccountAddr {
return nil, ierrors.New("address is not an account address")
}
return accountAddr, nil
}
func (fID FoundryID) ToAddress() ChainAddress {
panic("foundry ID is not addressable")
}
func (fID FoundryID) Empty() bool {
return fID == emptyFoundryID
}
func (fID FoundryID) String() string {
return hexutil.EncodeHex(fID[:])
}
// FoundryOutputs is a slice of FoundryOutput(s).
type FoundryOutputs []*FoundryOutput
// FoundryOutputsSet is a set of FoundryOutput(s).
type FoundryOutputsSet map[FoundryID]*FoundryOutput
type (
FoundryOutputUnlockCondition interface{ UnlockCondition }
FoundryOutputFeature interface{ Feature }
FoundryOutputImmFeature interface{ Feature }
FoundryOutputUnlockConditions = UnlockConditions[FoundryOutputUnlockCondition]
FoundryOutputFeatures = Features[FoundryOutputFeature]
FoundryOutputImmFeatures = Features[FoundryOutputImmFeature]
)
// FoundryOutput is an output type which controls the supply of user defined native tokens.
type FoundryOutput struct {
// The amount of IOTA tokens held by the output.
Amount BaseToken `serix:""`
// The serial number of the foundry.
SerialNumber uint32 `serix:""`
// The token scheme this foundry uses.
TokenScheme TokenScheme `serix:""`
// The unlock conditions on this output.
UnlockConditions FoundryOutputUnlockConditions `serix:",omitempty"`
// The feature on the output.
Features FoundryOutputFeatures `serix:",omitempty"`
// The immutable feature on the output.
ImmutableFeatures FoundryOutputImmFeatures `serix:",omitempty"`
}
func (f *FoundryOutput) Clone() Output {
return &FoundryOutput{
Amount: f.Amount,
SerialNumber: f.SerialNumber,
TokenScheme: f.TokenScheme.Clone(),
UnlockConditions: f.UnlockConditions.Clone(),
Features: f.Features.Clone(),
ImmutableFeatures: f.ImmutableFeatures.Clone(),
}
}
func (f *FoundryOutput) Equal(other Output) bool {
otherOutput, isSameType := other.(*FoundryOutput)
if !isSameType {
return false
}
if f.Amount != otherOutput.Amount {
return false
}
if f.SerialNumber != otherOutput.SerialNumber {
return false
}
if !f.TokenScheme.Equal(otherOutput.TokenScheme) {
return false
}
if !f.UnlockConditions.Equal(otherOutput.UnlockConditions) {
return false
}
if !f.Features.Equal(otherOutput.Features) {
return false
}
if !f.ImmutableFeatures.Equal(otherOutput.ImmutableFeatures) {
return false
}
return true
}
func (f *FoundryOutput) Owner() Address {
return f.UnlockConditionSet().ImmutableAccount().Address
}
func (f *FoundryOutput) UnlockableBy(addr Address, pastBoundedSlotIndex SlotIndex, futureBoundedSlotIndex SlotIndex) bool {
ok, _ := outputUnlockableBy(f, nil, addr, pastBoundedSlotIndex, futureBoundedSlotIndex)
return ok
}
func (f *FoundryOutput) StorageScore(storageScoreStruct *StorageScoreStructure, _ StorageScoreFunc) StorageScore {
return storageScoreStruct.OffsetOutput +
storageScoreStruct.FactorData().Multiply(StorageScore(f.Size())) +
f.TokenScheme.StorageScore(storageScoreStruct, nil) +
f.UnlockConditions.StorageScore(storageScoreStruct, nil) +
f.Features.StorageScore(storageScoreStruct, nil) +
f.ImmutableFeatures.StorageScore(storageScoreStruct, nil)
}
func (f *FoundryOutput) WorkScore(workScoreParameters *WorkScoreParameters) (WorkScore, error) {
workScoreTokenScheme, err := f.TokenScheme.WorkScore(workScoreParameters)
if err != nil {
return 0, err
}
workScoreConditions, err := f.UnlockConditions.WorkScore(workScoreParameters)
if err != nil {
return 0, err
}
workScoreFeatures, err := f.Features.WorkScore(workScoreParameters)
if err != nil {
return 0, err
}
workScoreImmutableFeatures, err := f.ImmutableFeatures.WorkScore(workScoreParameters)
if err != nil {
return 0, err
}
return workScoreParameters.Output.Add(workScoreTokenScheme, workScoreConditions, workScoreFeatures, workScoreImmutableFeatures)
}
func (f *FoundryOutput) ChainID() ChainID {
foundryID, err := f.FoundryID()
if err != nil {
panic(err)
}
return foundryID
}
// FoundryID returns the FoundryID of this FoundryOutput.
func (f *FoundryOutput) FoundryID() (FoundryID, error) {
return FoundryIDFromAddressAndSerialNumberAndTokenScheme(f.Owner(), f.SerialNumber, f.TokenScheme.Type())
}
// MustFoundryID works like FoundryID but panics if an error occurs.
func (f *FoundryOutput) MustFoundryID() FoundryID {
id, err := f.FoundryID()
if err != nil {
panic(err)
}
return id
}
// MustNativeTokenID works like NativeTokenID but panics if there is an error.
func (f *FoundryOutput) MustNativeTokenID() NativeTokenID {
nativeTokenID, err := f.NativeTokenID()
if err != nil {
panic(err)
}
return nativeTokenID
}
// NativeTokenID returns the NativeTokenID this FoundryOutput operates on.
func (f *FoundryOutput) NativeTokenID() (NativeTokenID, error) {
return f.FoundryID()
}
func (f *FoundryOutput) FeatureSet() FeatureSet {
return f.Features.MustSet()
}
func (f *FoundryOutput) UnlockConditionSet() UnlockConditionSet {
return f.UnlockConditions.MustSet()
}
func (f *FoundryOutput) ImmutableFeatureSet() FeatureSet {
return f.ImmutableFeatures.MustSet()
}
func (f *FoundryOutput) BaseTokenAmount() BaseToken {
return f.Amount
}
func (f *FoundryOutput) StoredMana() Mana {
return 0
}
func (f *FoundryOutput) Type() OutputType {
return OutputFoundry
}
func (f *FoundryOutput) Size() int {
// OutputType
return serializer.OneByte +
BaseTokenSize +
FoundrySerialNumberLength +
f.TokenScheme.Size() +
f.UnlockConditions.Size() +
f.Features.Size() +
f.ImmutableFeatures.Size()
}