-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathoptions.go
254 lines (216 loc) · 6.51 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package microstellar
import (
"context"
"time"
)
// SortOrder is used with WithSortOrder
type SortOrder int
// For use with WithSortOrder
const (
SortAscending = SortOrder(0)
SortDescending = SortOrder(1)
)
// MemoType sets the memotype field on the payment request.
type MemoType int
// Supported memo types.
const (
MemoNone = MemoType(0) // No memo
MemoID = MemoType(1) // ID memo
MemoText = MemoType(2) // Text memo (max 28 chars)
MemoHash = MemoType(3) // Hash memo
MemoReturn = MemoType(4) // Return hash memo
)
// Event denotes a specific event for a handler.
type Event int
// Supported events.
const (
EvBeforeSubmit = Event(0)
)
// TxHandler is a custom function that can be called when certain events occur. If
// false is returned or if the method returns an error, the caller stops processing the
// event immediately.
type TxHandler func(data ...interface{}) (bool, error)
// Options are additional parameters for a transaction. Use Opts() or NewOptions()
// to create a new instance.
type Options struct {
// Defaults to context.Background if unset.
ctx context.Context
handlers map[Event]*TxHandler
// Use With* methods to set these options
hasFee bool
fee uint32
hasTimeBounds bool
minTimeBound time.Time
maxTimeBound time.Time
// Used by all transactions.
memoType MemoType // defaults to no memo
memoText string // additional memo text
memoID uint64 // additional memo ID
memoHash [32]byte // additional memo ID
skipSignatures bool
signerSeeds []string
// Options for query methods (Watch*, Load*)
hasCursor bool
cursor string
hasLimit bool
limit uint
sortDescending bool
// For offer management.
passiveOffer bool
// for Path payments.
sourceAddress string
sendAsset *Asset
maxAmount string
path []*Asset
// for multi-op transactions
isMultiOp bool
multiOpSource string
}
// NewOptions creates a new options structure for Tx.
func NewOptions() *Options {
return &Options{
ctx: nil,
handlers: map[Event]*TxHandler{},
hasFee: false,
hasTimeBounds: false,
memoType: MemoNone,
hasCursor: false,
hasLimit: false,
sortDescending: false,
passiveOffer: false,
sourceAddress: "",
isMultiOp: false,
multiOpSource: "",
}
}
// Opts is just an alias for NewOptions
func Opts() *Options {
return NewOptions()
}
// mergeOptions takes a slice of Options and merges them.
func mergeOptions(opts []*Options) *Options {
// for now, just return the first option
if len(opts) > 0 {
return opts[0]
}
return NewOptions()
}
// WithMemoText sets the memoType and memoText fields on a Transaction. Used
// with all transactions.
func (o *Options) WithMemoText(text string) *Options {
o.memoType = MemoText
o.memoText = text
return o
}
// WithMemoID sets the memoType and memoID fields on a Transaction. Used
// with all transactions.
func (o *Options) WithMemoID(id uint64) *Options {
o.memoType = MemoID
o.memoID = id
return o
}
// WithMemoHash sets the memoType and memoHash fields on a Transaction. Used
// with all transactions.
func (o *Options) WithMemoHash(hash [32]byte) *Options {
o.memoType = MemoHash
o.memoHash = hash
return o
}
// WithMemoReturn sets the memoType and memoReturn fields on a Transaction. Used
// with all transactions.
func (o *Options) WithMemoReturn(hash [32]byte) *Options {
o.memoType = MemoReturn
o.memoHash = hash
return o
}
// WithSigner adds a signer to Payment. Used with all transactions.
func (o *Options) WithSigner(signerSeed string) *Options {
o.signerSeeds = append(o.signerSeeds, signerSeed)
return o
}
// WithContext sets the context.Context for the connection. Used with
// Watch* methods.
func (o *Options) WithContext(context context.Context) *Options {
o.ctx = context
return o
}
// WithCursor sets the cursor for watchers and queries. Used with Watch*
// methods and LoadOffers.
func (o *Options) WithCursor(cursor string) *Options {
o.hasCursor = true
o.cursor = cursor
return o
}
// WithLimit sets the limit for queries. Used with LoadOffers.
func (o *Options) WithLimit(limit uint) *Options {
o.hasLimit = true
o.limit = limit
return o
}
// WithSortOrder sets the sort order of the results. Used with LoadOffers.
func (o *Options) WithSortOrder(order SortOrder) *Options {
if order == SortDescending {
o.sortDescending = true
}
return o
}
// MakePassive turns this into a passive offer. Used with LoadOffers.
func (o *Options) MakePassive() *Options {
o.passiveOffer = true
return o
}
// WithAsset is used to setup a path payment. This makes the Pay method
// use "asset" as the sending asset, and sends no more than maxAmount units
// of the asset. Used with Pay and FindPaths.
//
// E.g.,
// ms.Pay(sourceSeed, address, "20", INR, Opts().WithAsset(NativeAsset, "20").Through(USD, EUR)
func (o *Options) WithAsset(asset *Asset, maxAmount string) *Options {
o.sendAsset = asset
o.maxAmount = maxAmount
return o
}
// Through adds "asset" as a routing point in the payment path.
//
// E.g.,
// ms.Pay(sourceSeed, address, "20", INR, Opts().WithAsset(NativeAsset, "20").Through(USD, EUR)
func (o *Options) Through(asset ...*Asset) *Options {
o.path = append(o.path, asset...)
return o
}
// FindPathFrom enables automatic path finding for path payments. Use sourceAddress
// to specify the address (not seed) for the source account.
func (o *Options) FindPathFrom(sourceAddress string) *Options {
o.sourceAddress = sourceAddress
return o
}
// MultiOp specifies that this is a multi-op transactions, and sets the fund source account
// to sourceAccount.
func (o *Options) MultiOp(sourceAccount string) *Options {
o.isMultiOp = true
o.multiOpSource = sourceAccount
return o
}
// On attaches a handler to an event. E.g.,
//
// Opts().On(microstellar.EvBeforeSubmit, func(tx) { log.Print(tx); return nil })
func (o *Options) On(event Event, handler *TxHandler) *Options {
o.handlers[event] = handler
return o
}
// SkipSignatures prevents Tx from signing transactions. This is typically done if the
// transaction is not meant to be submitted.
func (o *Options) SkipSignatures() *Options {
o.skipSignatures = true
return o
}
// WithTimeBounds attaches time bounds to the transaction. This means that the transaction
// can only be submitted between min and max time (as determined by the ledger.)
func (o *Options) WithTimeBounds(min time.Time, max time.Time) *Options {
o.hasTimeBounds = true
o.minTimeBound = min
o.maxTimeBound = max
return o
}
// TxOptions is a deprecated alias for TxOptoins
type TxOptions Options