forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
156 lines (124 loc) · 2.95 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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package common
import (
"context"
"time"
"github.com/VidarSolutions/avalanchego/ids"
"github.com/VidarSolutions/avalanchego/utils/set"
"github.com/VidarSolutions/avalanchego/vms/secp256k1fx"
)
const defaultPollFrequency = 100 * time.Millisecond
type Option func(*Options)
type Options struct {
ctx context.Context
customAddressesSet bool
customAddresses set.Set[ids.ShortID]
minIssuanceTimeSet bool
minIssuanceTime uint64
allowStakeableLocked bool
changeOwner *secp256k1fx.OutputOwners
memo []byte
assumeDecided bool
pollFrequencySet bool
pollFrequency time.Duration
}
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) 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 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 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
}
}