-
Notifications
You must be signed in to change notification settings - Fork 95
/
feat_native_token_test.go
93 lines (79 loc) · 2.5 KB
/
feat_native_token_test.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
//nolint:dupl
package iotago_test
import (
"math/big"
"testing"
"github.com/stretchr/testify/require"
"github.com/iotaledger/hive.go/serializer/v2/serix"
iotago "github.com/iotaledger/iota.go/v4"
"github.com/iotaledger/iota.go/v4/tpkg"
)
func TestNativeTokenDeSerialization(t *testing.T) {
ntIn := &iotago.NativeTokenFeature{
ID: tpkg.Rand38ByteArray(),
Amount: new(big.Int).SetUint64(1000),
}
ntBytes, err := tpkg.ZeroCostTestAPI.Encode(ntIn, serix.WithValidation())
require.NoError(t, err)
ntOut := &iotago.NativeTokenFeature{}
_, err = tpkg.ZeroCostTestAPI.Decode(ntBytes, ntOut, serix.WithValidation())
require.NoError(t, err)
require.EqualValues(t, ntIn, ntOut)
}
func TestNativeToken_SyntacticalValidation(t *testing.T) {
nativeTokenFeature := tpkg.RandNativeTokenFeature()
accountAddress, err := nativeTokenFeature.ID.AccountAddress()
require.NoError(t, err)
type test struct {
name string
nativeTokenFeature *iotago.NativeTokenFeature
wantErr error
}
tests := []*test{
{
name: "ok - NativeTokenFeature token ID == FoundryID",
nativeTokenFeature: nativeTokenFeature,
wantErr: nil,
},
{
name: "fail - NativeTokenFeature token ID != FoundryID",
nativeTokenFeature: tpkg.RandNativeTokenFeature(),
wantErr: iotago.ErrFoundryIDNativeTokenIDMismatch,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
foundryIn := &iotago.FoundryOutput{
Amount: 1000,
SerialNumber: nativeTokenFeature.ID.FoundrySerialNumber(),
TokenScheme: &iotago.SimpleTokenScheme{
MintedTokens: big.NewInt(100),
MeltedTokens: big.NewInt(0),
MaximumSupply: big.NewInt(100),
},
UnlockConditions: iotago.FoundryOutputUnlockConditions{
&iotago.ImmutableAccountUnlockCondition{
Address: accountAddress,
},
},
Features: iotago.FoundryOutputFeatures{
test.nativeTokenFeature,
},
ImmutableFeatures: iotago.FoundryOutputImmFeatures{},
}
foundryBytes, err := tpkg.ZeroCostTestAPI.Encode(foundryIn, serix.WithValidation())
if err == nil {
err = iotago.OutputsSyntacticalFoundry()(0, foundryIn)
}
if test.wantErr != nil {
require.ErrorIs(t, err, test.wantErr)
return
}
require.NoError(t, err)
foundryOut := &iotago.FoundryOutput{}
_, err = tpkg.ZeroCostTestAPI.Decode(foundryBytes, foundryOut, serix.WithValidation())
require.NoError(t, err)
require.True(t, foundryIn.Equal(foundryOut))
})
}
}