-
Notifications
You must be signed in to change notification settings - Fork 651
/
options.go
207 lines (164 loc) · 4.06 KB
/
options.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package common
import (
"context"
"math/big"
"time"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
ethcommon "github.com/ethereum/go-ethereum/common"
)
const defaultPollFrequency = 100 * time.Millisecond
// Signature of the function that will be called after a transaction
// has been issued with the ID of the issued transaction.
type PostIssuanceFunc func(ids.ID)
type Option func(*Options)
type Options struct {
ctx context.Context
customAddressesSet bool
customAddresses set.Set[ids.ShortID]
customEthAddressesSet bool
customEthAddresses set.Set[ethcommon.Address]
baseFee *big.Int
minIssuanceTimeSet bool
minIssuanceTime uint64
allowStakeableLocked bool
changeOwner *secp256k1fx.OutputOwners
memo []byte
assumeDecided bool
pollFrequencySet bool
pollFrequency time.Duration
postIssuanceFunc PostIssuanceFunc
}
func NewOptions(ops []Option) *Options {
o := &Options{}
o.applyOptions(ops)
return o
}
func UnionOptions(first, second []Option) []Option {
firstLen := len(first)
newOptions := make([]Option, firstLen+len(second))
copy(newOptions, first)
copy(newOptions[firstLen:], second)
return newOptions
}
func (o *Options) applyOptions(ops []Option) {
for _, op := range ops {
op(o)
}
}
func (o *Options) Context() context.Context {
if o.ctx != nil {
return o.ctx
}
return context.Background()
}
func (o *Options) Addresses(defaultAddresses set.Set[ids.ShortID]) set.Set[ids.ShortID] {
if o.customAddressesSet {
return o.customAddresses
}
return defaultAddresses
}
func (o *Options) EthAddresses(defaultAddresses set.Set[ethcommon.Address]) set.Set[ethcommon.Address] {
if o.customEthAddressesSet {
return o.customEthAddresses
}
return defaultAddresses
}
func (o *Options) BaseFee(defaultBaseFee *big.Int) *big.Int {
if o.baseFee != nil {
return o.baseFee
}
return defaultBaseFee
}
func (o *Options) MinIssuanceTime() uint64 {
if o.minIssuanceTimeSet {
return o.minIssuanceTime
}
return uint64(time.Now().Unix())
}
func (o *Options) AllowStakeableLocked() bool {
return o.allowStakeableLocked
}
func (o *Options) ChangeOwner(defaultOwner *secp256k1fx.OutputOwners) *secp256k1fx.OutputOwners {
if o.changeOwner != nil {
return o.changeOwner
}
return defaultOwner
}
func (o *Options) Memo() []byte {
return o.memo
}
func (o *Options) AssumeDecided() bool {
return o.assumeDecided
}
func (o *Options) PollFrequency() time.Duration {
if o.pollFrequencySet {
return o.pollFrequency
}
return defaultPollFrequency
}
func (o *Options) PostIssuanceFunc() PostIssuanceFunc {
return o.postIssuanceFunc
}
func WithContext(ctx context.Context) Option {
return func(o *Options) {
o.ctx = ctx
}
}
func WithCustomAddresses(addrs set.Set[ids.ShortID]) Option {
return func(o *Options) {
o.customAddressesSet = true
o.customAddresses = addrs
}
}
func WithCustomEthAddresses(addrs set.Set[ethcommon.Address]) Option {
return func(o *Options) {
o.customEthAddressesSet = true
o.customEthAddresses = addrs
}
}
func WithBaseFee(baseFee *big.Int) Option {
return func(o *Options) {
o.baseFee = baseFee
}
}
func WithMinIssuanceTime(minIssuanceTime uint64) Option {
return func(o *Options) {
o.minIssuanceTimeSet = true
o.minIssuanceTime = minIssuanceTime
}
}
func WithStakeableLocked() Option {
return func(o *Options) {
o.allowStakeableLocked = true
}
}
func WithChangeOwner(changeOwner *secp256k1fx.OutputOwners) Option {
return func(o *Options) {
o.changeOwner = changeOwner
}
}
func WithMemo(memo []byte) Option {
return func(o *Options) {
o.memo = memo
}
}
func WithAssumeDecided() Option {
return func(o *Options) {
o.assumeDecided = true
}
}
func WithPollFrequency(pollFrequency time.Duration) Option {
return func(o *Options) {
o.pollFrequencySet = true
o.pollFrequency = pollFrequency
}
}
func WithPostIssuanceFunc(f PostIssuanceFunc) Option {
return func(o *Options) {
o.postIssuanceFunc = f
}
}