github.com/MetalBlockchain/metalgo@v1.11.9/wallet/subnet/primary/common/options.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package common 5 6 import ( 7 "context" 8 "math/big" 9 "time" 10 11 "github.com/MetalBlockchain/metalgo/ids" 12 "github.com/MetalBlockchain/metalgo/utils/set" 13 "github.com/MetalBlockchain/metalgo/vms/secp256k1fx" 14 15 ethcommon "github.com/ethereum/go-ethereum/common" 16 ) 17 18 const defaultPollFrequency = 100 * time.Millisecond 19 20 // Signature of the function that will be called after a transaction 21 // has been issued with the ID of the issued transaction. 22 type PostIssuanceFunc func(ids.ID) 23 24 type Option func(*Options) 25 26 type Options struct { 27 ctx context.Context 28 29 customAddressesSet bool 30 customAddresses set.Set[ids.ShortID] 31 32 customEthAddressesSet bool 33 customEthAddresses set.Set[ethcommon.Address] 34 35 baseFee *big.Int 36 37 minIssuanceTimeSet bool 38 minIssuanceTime uint64 39 40 allowStakeableLocked bool 41 42 changeOwner *secp256k1fx.OutputOwners 43 44 memo []byte 45 46 assumeDecided bool 47 48 pollFrequencySet bool 49 pollFrequency time.Duration 50 51 postIssuanceFunc PostIssuanceFunc 52 } 53 54 func NewOptions(ops []Option) *Options { 55 o := &Options{} 56 o.applyOptions(ops) 57 return o 58 } 59 60 func UnionOptions(first, second []Option) []Option { 61 firstLen := len(first) 62 newOptions := make([]Option, firstLen+len(second)) 63 copy(newOptions, first) 64 copy(newOptions[firstLen:], second) 65 return newOptions 66 } 67 68 func (o *Options) applyOptions(ops []Option) { 69 for _, op := range ops { 70 op(o) 71 } 72 } 73 74 func (o *Options) Context() context.Context { 75 if o.ctx != nil { 76 return o.ctx 77 } 78 return context.Background() 79 } 80 81 func (o *Options) Addresses(defaultAddresses set.Set[ids.ShortID]) set.Set[ids.ShortID] { 82 if o.customAddressesSet { 83 return o.customAddresses 84 } 85 return defaultAddresses 86 } 87 88 func (o *Options) EthAddresses(defaultAddresses set.Set[ethcommon.Address]) set.Set[ethcommon.Address] { 89 if o.customEthAddressesSet { 90 return o.customEthAddresses 91 } 92 return defaultAddresses 93 } 94 95 func (o *Options) BaseFee(defaultBaseFee *big.Int) *big.Int { 96 if o.baseFee != nil { 97 return o.baseFee 98 } 99 return defaultBaseFee 100 } 101 102 func (o *Options) MinIssuanceTime() uint64 { 103 if o.minIssuanceTimeSet { 104 return o.minIssuanceTime 105 } 106 return uint64(time.Now().Unix()) 107 } 108 109 func (o *Options) AllowStakeableLocked() bool { 110 return o.allowStakeableLocked 111 } 112 113 func (o *Options) ChangeOwner(defaultOwner *secp256k1fx.OutputOwners) *secp256k1fx.OutputOwners { 114 if o.changeOwner != nil { 115 return o.changeOwner 116 } 117 return defaultOwner 118 } 119 120 func (o *Options) Memo() []byte { 121 return o.memo 122 } 123 124 func (o *Options) AssumeDecided() bool { 125 return o.assumeDecided 126 } 127 128 func (o *Options) PollFrequency() time.Duration { 129 if o.pollFrequencySet { 130 return o.pollFrequency 131 } 132 return defaultPollFrequency 133 } 134 135 func (o *Options) PostIssuanceFunc() PostIssuanceFunc { 136 return o.postIssuanceFunc 137 } 138 139 func WithContext(ctx context.Context) Option { 140 return func(o *Options) { 141 o.ctx = ctx 142 } 143 } 144 145 func WithCustomAddresses(addrs set.Set[ids.ShortID]) Option { 146 return func(o *Options) { 147 o.customAddressesSet = true 148 o.customAddresses = addrs 149 } 150 } 151 152 func WithCustomEthAddresses(addrs set.Set[ethcommon.Address]) Option { 153 return func(o *Options) { 154 o.customEthAddressesSet = true 155 o.customEthAddresses = addrs 156 } 157 } 158 159 func WithBaseFee(baseFee *big.Int) Option { 160 return func(o *Options) { 161 o.baseFee = baseFee 162 } 163 } 164 165 func WithMinIssuanceTime(minIssuanceTime uint64) Option { 166 return func(o *Options) { 167 o.minIssuanceTimeSet = true 168 o.minIssuanceTime = minIssuanceTime 169 } 170 } 171 172 func WithStakeableLocked() Option { 173 return func(o *Options) { 174 o.allowStakeableLocked = true 175 } 176 } 177 178 func WithChangeOwner(changeOwner *secp256k1fx.OutputOwners) Option { 179 return func(o *Options) { 180 o.changeOwner = changeOwner 181 } 182 } 183 184 func WithMemo(memo []byte) Option { 185 return func(o *Options) { 186 o.memo = memo 187 } 188 } 189 190 func WithAssumeDecided() Option { 191 return func(o *Options) { 192 o.assumeDecided = true 193 } 194 } 195 196 func WithPollFrequency(pollFrequency time.Duration) Option { 197 return func(o *Options) { 198 o.pollFrequencySet = true 199 o.pollFrequency = pollFrequency 200 } 201 } 202 203 func WithPostIssuanceFunc(f PostIssuanceFunc) Option { 204 return func(o *Options) { 205 o.postIssuanceFunc = f 206 } 207 }