/
builder.go
198 lines (156 loc) · 6.04 KB
/
builder.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
package txDataBuilder
import (
"encoding/hex"
"math/big"
"github.com/ME-MotherEarth/me-core/core"
)
// txDataBuilder constructs a string to be used for transaction arguments
type txDataBuilder struct {
function string
elements []string
separator string
}
// NewBuilder creates a new txDataBuilder instance.
func NewBuilder() *txDataBuilder {
return &txDataBuilder{
function: "",
elements: make([]string, 0),
separator: "@",
}
}
// Clear resets the internal state of the txDataBuilder, allowing a new data
// string to be built.
func (builder *txDataBuilder) Clear() *txDataBuilder {
builder.function = ""
builder.elements = make([]string, 0)
return builder
}
// ToString returns the data as a string.
func (builder *txDataBuilder) ToString() string {
data := builder.function
for _, element := range builder.elements {
data = data + builder.separator + element
}
return data
}
// ToBytes returns the data as a slice of bytes.
func (builder *txDataBuilder) ToBytes() []byte {
return []byte(builder.ToString())
}
// GetLast returns the currently last element.
func (builder *txDataBuilder) GetLast() string {
if len(builder.elements) == 0 {
return ""
}
return builder.elements[len(builder.elements)-1]
}
// SetLast replaces the last element with the provided one.
func (builder *txDataBuilder) SetLast(element string) {
if len(builder.elements) == 0 {
builder.elements = []string{element}
}
builder.elements[len(builder.elements)-1] = element
}
// Func sets the function to be invoked by the data string.
func (builder *txDataBuilder) Func(function string) *txDataBuilder {
builder.function = function
return builder
}
// Byte appends a single byte to the data string.
func (builder *txDataBuilder) Byte(value byte) *txDataBuilder {
element := hex.EncodeToString([]byte{value})
builder.elements = append(builder.elements, element)
return builder
}
// Bytes appends a slice of bytes to the data string.
func (builder *txDataBuilder) Bytes(bytes []byte) *txDataBuilder {
element := hex.EncodeToString(bytes)
builder.elements = append(builder.elements, element)
return builder
}
// Str appends a string to the data string.
func (builder *txDataBuilder) Str(str string) *txDataBuilder {
element := hex.EncodeToString([]byte(str))
builder.elements = append(builder.elements, element)
return builder
}
// Int appends an integer to the data string.
func (builder *txDataBuilder) Int(value int) *txDataBuilder {
element := hex.EncodeToString(big.NewInt(int64(value)).Bytes())
builder.elements = append(builder.elements, element)
return builder
}
// Int64 appends an int64 to the data string.
func (builder *txDataBuilder) Int64(value int64) *txDataBuilder {
element := hex.EncodeToString(big.NewInt(value).Bytes())
builder.elements = append(builder.elements, element)
return builder
}
// True appends the string "true" to the data string.
func (builder *txDataBuilder) True() *txDataBuilder {
return builder.Str("true")
}
// False appends the string "false" to the data string.
func (builder *txDataBuilder) False() *txDataBuilder {
return builder.Str("false")
}
// Bool appends either "true" or "false" to the data string, depending on the
// `value` argument.
func (builder *txDataBuilder) Bool(value bool) *txDataBuilder {
if value {
return builder.True()
}
return builder.False()
}
// BigInt appends the bytes of a big.Int to the data string.
func (builder *txDataBuilder) BigInt(value *big.Int) *txDataBuilder {
return builder.Bytes(value.Bytes())
}
// IssueMECT appends to the data string all the elements required to request an MECT issuing.
func (builder *txDataBuilder) IssueMECT(token string, ticker string, supply int64, numDecimals byte) *txDataBuilder {
return builder.Func("issue").Str(token).Str(ticker).Int64(supply).Byte(numDecimals)
}
// TransferMECT appends to the data string all the elements required to request an MECT transfer.
func (builder *txDataBuilder) TransferMECT(token string, value int64) *txDataBuilder {
return builder.Func(core.BuiltInFunctionMECTTransfer).Str(token).Int64(value)
}
// TransferMECTNFT appends to the data string all the elements required to request an MECT NFT transfer.
func (builder *txDataBuilder) TransferMECTNFT(token string, nonce int, value int64) *txDataBuilder {
return builder.Func(core.BuiltInFunctionMECTNFTTransfer).Str(token).Int(nonce).Int64(value)
}
// BurnMECT appends to the data string all the elements required to burn MECT tokens.
func (builder *txDataBuilder) BurnMECT(token string, value int64) *txDataBuilder {
return builder.Func(core.BuiltInFunctionMECTBurn).Str(token).Int64(value)
}
// CanFreeze appends "canFreeze" followed by the provided boolean value.
func (builder *txDataBuilder) CanFreeze(prop bool) *txDataBuilder {
return builder.Str("canFreeze").Bool(prop)
}
// CanWipe appends "canWipe" followed by the provided boolean value.
func (builder *txDataBuilder) CanWipe(prop bool) *txDataBuilder {
return builder.Str("canWipe").Bool(prop)
}
// CanPause appends "canPause" followed by the provided boolean value.
func (builder *txDataBuilder) CanPause(prop bool) *txDataBuilder {
return builder.Str("canPause").Bool(prop)
}
// CanMint appends "canMint" followed by the provided boolean value.
func (builder *txDataBuilder) CanMint(prop bool) *txDataBuilder {
return builder.Str("canMint").Bool(prop)
}
// CanBurn appends "canBurn" followed by the provided boolean value.
func (builder *txDataBuilder) CanBurn(prop bool) *txDataBuilder {
return builder.Str("canBurn").Bool(prop)
}
// CanTransferNFTCreateRole appends "canTransferNFTCreateRole" followed by the provided boolean value.
func (builder *txDataBuilder) CanTransferNFTCreateRole(prop bool) *txDataBuilder {
return builder.Str("canTransferNFTCreateRole").Bool(prop)
}
// CanAddSpecialRoles appends "canAddSpecialRoles" followed by the provided boolean value.
func (builder *txDataBuilder) CanAddSpecialRoles(prop bool) *txDataBuilder {
return builder.Str("canAddSpecialRoles").Bool(prop)
}
// IsInterfaceNil returns true if there is no value under the interface
func (builder *txDataBuilder) IsInterfaceNil() bool {
return builder == nil
}