This repository has been archived by the owner on Jun 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
events.go
628 lines (535 loc) · 20.4 KB
/
events.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
// Copyright 2014 Tjerk Santegoeds
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oanda
import (
"encoding/json"
"fmt"
"net/url"
"sync"
"time"
)
///////////////////////////////////////////////////////////////////////////////////////////////////
// Events
type evtTradeDetailData struct {
TradeId Id `json:"id"`
Units int `json:"units"`
Pl float64 `json:"pl"`
Interest float64 `json:"interest"`
}
type evtTradeDetail struct{ content *evtTradeDetailData }
func (td *evtTradeDetail) TradeId() Id { return td.content.TradeId }
func (td *evtTradeDetail) Units() int { return td.content.Units }
func (td *evtTradeDetail) Pl() float64 { return td.content.Pl }
func (td *evtTradeDetail) Interest() float64 { return td.content.Interest }
type evtHeaderContent struct {
TranId Id `json:"id"`
AccountId Id `json:"accountId"`
Time Time `json:"time"`
Type string `json:"type"`
}
type evtHeader struct {
content *evtHeaderContent
}
type evtBody struct {
Instrument string `json:"instrument"`
Side string `json:"side"`
Units int `json:"units"`
Price float64 `json:"price"`
Expiry Time `json:"expiry"`
Reason string `json:"reason"`
LowerBound float64 `json:"lowerBound"`
UpperBound float64 `json:"upperBound"`
TakeProfitPrice float64 `json:"takeProfitPrice"`
StopLossPrice float64 `json:"stopLossPrice"`
TrailingStopLossDistance float64 `json:"trailingStopLossDistance"`
Pl float64 `json:"pl"`
Interest float64 `json:"interest"`
AccountBalance float64 `json:"accountBalance"`
MarginRate float64 `json:"marginRate"`
Amount float64 `json:"amount"`
TradeId Id `json:"tradeId"`
OrderId Id `json:"orderId"`
TradeOpened *evtTradeDetailData `json:"tradeOpened"`
TradeReduced *evtTradeDetailData `json:"tradeReduced"`
HomeCurrency string `json:"homeCurrency"`
}
type Event interface {
TranId() Id
AccountId() Id
Time() Time
Type() string
}
func (t *evtHeader) TranId() Id { return t.content.TranId }
func (t *evtHeader) AccountId() Id { return t.content.AccountId }
func (t *evtHeader) Time() Time { return t.content.Time }
func (t *evtHeader) Type() string { return t.content.Type }
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t *evtHeader) UnmarshalJSON(data []byte) (err error) {
return json.Unmarshal(data, &t.content)
}
// String implementes the fmt.Stringer interface.
func (t evtHeader) String() string {
return fmt.Sprintf("Event{TranId: %d, AccountId: %d Type: %s}",
t.TranId(), t.AccountId(), t.Type())
}
// EventFromJSON returns an OANDA Event object given JSON input.
func EventFromJSON(data []byte) (Event, error) {
rawEvent := struct {
*evtHeaderContent
*evtBody
}{}
if err := json.Unmarshal(data, &rawEvent); err != nil {
return nil, err
}
evt, err := asEvent(rawEvent.evtHeaderContent, rawEvent.evtBody)
if err != nil {
return nil, err
}
return evt, nil
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// CREATE
// AccountCreateEvent represents an event of type CREATE.
type AccountCreateEvent struct {
evtHeader
body *evtBody
}
func (t *AccountCreateEvent) HomeCurrency() string { return t.body.HomeCurrency }
func (t *AccountCreateEvent) Reason() string { return t.body.Reason }
///////////////////////////////////////////////////////////////////////////////////////////////////
// MARKET_ORDER_CREATE
// TradeCreateEvent represents an event of type MARKET_ORDER_CREATE
type TradeCreateEvent struct {
evtHeader
body *evtBody
}
func (t *TradeCreateEvent) Instrument() string { return t.body.Instrument }
func (t *TradeCreateEvent) Side() string { return t.body.Side }
func (t *TradeCreateEvent) Units() int { return t.body.Units }
func (t *TradeCreateEvent) Price() float64 { return t.body.Price }
func (t *TradeCreateEvent) Pl() float64 { return t.body.Pl }
func (t *TradeCreateEvent) Interest() float64 { return t.body.Interest }
func (t *TradeCreateEvent) LowerBound() float64 { return t.body.LowerBound }
func (t *TradeCreateEvent) UpperBound() float64 { return t.body.UpperBound }
func (t *TradeCreateEvent) AccountBalance() float64 { return t.body.AccountBalance }
func (t *TradeCreateEvent) StopLossPrice() float64 { return t.body.StopLossPrice }
func (t *TradeCreateEvent) TakeProfitPrice() float64 { return t.body.TakeProfitPrice }
func (t *TradeCreateEvent) TrailingStopLossDistance() float64 {
return t.body.TrailingStopLossDistance
}
func (t *TradeCreateEvent) TradeOpened() *evtTradeDetail {
if t.body.TradeOpened != nil {
return &evtTradeDetail{t.body.TradeOpened}
}
return nil
}
func (t *TradeCreateEvent) TradeReduced() *evtTradeDetail {
if t.body.TradeReduced != nil {
return &evtTradeDetail{t.body.TradeReduced}
}
return nil
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// LIMIT_ORDER_CREATE, STOP_ORDER_CREATE, MARKET_IF_TOUCHED_CREATE
// OrderCreateEvent represents an event of type LIMIT_ORDER_CREATE, STOP_ORDER_CREATE or
// MARKET_IF_TOUCHED_CREATE
type OrderCreateEvent struct {
evtHeader
body *evtBody
}
func (t *OrderCreateEvent) Instrument() string { return t.body.Instrument }
func (t *OrderCreateEvent) Side() string { return t.body.Side }
func (t *OrderCreateEvent) Units() int { return t.body.Units }
func (t *OrderCreateEvent) Price() float64 { return t.body.Price }
func (t *OrderCreateEvent) Expiry() Time { return t.body.Expiry }
func (t *OrderCreateEvent) Reason() string { return t.body.Reason }
func (t *OrderCreateEvent) LowerBound() float64 { return t.body.LowerBound }
func (t *OrderCreateEvent) UpperBound() float64 { return t.body.UpperBound }
func (t *OrderCreateEvent) TakeProfitPrice() float64 { return t.body.TakeProfitPrice }
func (t *OrderCreateEvent) StopLossPrice() float64 { return t.body.StopLossPrice }
func (t *OrderCreateEvent) TrailingStopLossDistance() float64 {
return t.body.TrailingStopLossDistance
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// ORDER_UPDATE
// OrderUpdateEvent represents an event of type ORDER_UPDATE
type OrderUpdateEvent struct {
evtHeader
body *evtBody
}
func (t *OrderUpdateEvent) Instrument() string { return t.body.Instrument }
func (t *OrderUpdateEvent) Side() string { return t.body.Side }
func (t *OrderUpdateEvent) Units() int { return t.body.Units }
func (t *OrderUpdateEvent) Reason() string { return t.body.Reason }
func (t *OrderUpdateEvent) LowerBound() float64 { return t.body.LowerBound }
func (t *OrderUpdateEvent) UpperBound() float64 { return t.body.UpperBound }
func (t *OrderUpdateEvent) TakeProfitPrice() float64 { return t.body.TakeProfitPrice }
func (t *OrderUpdateEvent) StopLossPrice() float64 { return t.body.StopLossPrice }
func (t *OrderUpdateEvent) TrailingStopLossDistance() float64 {
return t.body.TrailingStopLossDistance
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// ORDER_CANCEL
// OrderCancelEvent represents and event of type ORDER_CANCEL.
type OrderCancelEvent struct {
evtHeader
body *evtBody
}
func (t *OrderCancelEvent) OrderId() Id { return t.body.OrderId }
func (t *OrderCancelEvent) Reason() string { return t.body.Reason }
///////////////////////////////////////////////////////////////////////////////////////////////////
// ORDER_FILLED
// OrderFilledEvent represents an event of type ORDER_FILLED.
type OrderFilledEvent struct {
evtHeader
body *evtBody
}
func (t *OrderFilledEvent) OrderId() Id { return t.body.OrderId }
///////////////////////////////////////////////////////////////////////////////////////////////////
// TRADE_UPDATE
// TradeUpdateEvent represents an event of type TRADE_UPDATE
type TradeUpdateEvent struct {
evtHeader
body *evtBody
}
func (t *TradeUpdateEvent) Instrument() string { return t.body.Instrument }
func (t *TradeUpdateEvent) Units() int { return t.body.Units }
func (t *TradeUpdateEvent) Side() string { return t.body.Side }
func (t *TradeUpdateEvent) TradeId() Id { return t.body.TradeId }
func (t *TradeUpdateEvent) TakeProfitPrice() float64 { return t.body.TakeProfitPrice }
func (t *TradeUpdateEvent) StopLossPrice() float64 { return t.body.StopLossPrice }
func (t *TradeUpdateEvent) TailingStopLossDistance() float64 { return t.body.TrailingStopLossDistance }
///////////////////////////////////////////////////////////////////////////////////////////////////
// TRADE_CLOSE, MIGRATE_TRADE_CLOSE, TAKE_PROFIT_FILLED, STOP_LOSS_FILLED, TRAILING_STOP_FILLED,
// MARGIN_CLOSEOUT
// TradeCloseEvent represents an event of type TRADE_CLOSE, MIGRATE_TRADE_CLOSE, TAKE_PROFIT_FILLED,
// STOP_LOSS_FILLED, TRAILING_STOP_FILLED or MARGIN_CLOSEOUT.
type TradeCloseEvent struct {
evtHeader
body *evtBody
}
func (t *TradeCloseEvent) Instrument() string { return t.body.Instrument }
func (t *TradeCloseEvent) Units() int { return t.body.Units }
func (t *TradeCloseEvent) Side() string { return t.body.Side }
func (t *TradeCloseEvent) Price() float64 { return t.body.Price }
func (t *TradeCloseEvent) Pl() float64 { return t.body.Pl }
func (t *TradeCloseEvent) Interest() float64 { return t.body.Interest }
func (t *TradeCloseEvent) AccountBalance() float64 { return t.body.AccountBalance }
func (t *TradeCloseEvent) TradeId() Id { return t.body.TradeId }
///////////////////////////////////////////////////////////////////////////////////////////////////
// MIGRATE_TRADE_OPEN
// MigrateTradeOpenEvent represents an event of type MIGRATE_TRADE_OPEN
type MigrateTradeOpenEvent struct {
evtHeader
body *evtBody
}
func (t *MigrateTradeOpenEvent) Instrument() string { return t.body.Instrument }
func (t *MigrateTradeOpenEvent) Side() string { return t.body.Side }
func (t *MigrateTradeOpenEvent) Units() int { return t.body.Units }
func (t *MigrateTradeOpenEvent) Price() float64 { return t.body.Price }
func (t *MigrateTradeOpenEvent) TakeProfitPrice() float64 { return t.body.TakeProfitPrice }
func (t *MigrateTradeOpenEvent) StopLossPrice() float64 { return t.body.StopLossPrice }
func (t *MigrateTradeOpenEvent) TrailingStopLossDistance() float64 {
return t.body.TrailingStopLossDistance
}
func (t *MigrateTradeOpenEvent) TradeOpened() *evtTradeDetail {
return &evtTradeDetail{t.body.TradeOpened}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// SET_MARGIN_RATE
// SetMarginRateEvent represent and event of type SET_MARGIN_RATE.
type SetMarginRateEvent struct {
evtHeader
body *evtBody
}
func (t *SetMarginRateEvent) MarginRate() float64 { return t.body.MarginRate }
///////////////////////////////////////////////////////////////////////////////////////////////////
// TRANSFER_FUNDS
// TransferFundsEvent represents an event of type TRANSFER_FUNDS.
type TransferFundsEvent struct {
evtHeader
body *evtBody
}
func (t *TransferFundsEvent) Amount() float64 { return t.body.Amount }
///////////////////////////////////////////////////////////////////////////////////////////////////
// DAILY_INTEREST
// DailyInterestEvent represents an event of type DAILY_INTEREST.
type DailyInterestEvent struct {
evtHeader
body *evtBody
}
func (t *DailyInterestEvent) Interest() float64 { return t.body.Interest }
///////////////////////////////////////////////////////////////////////////////////////////////////
// FEE
// FeeEvent represents an event of type FEE
type FeeEvent struct {
evtHeader
body *evtBody
}
func (t *FeeEvent) Amount() float64 { return t.body.Amount }
func (t *FeeEvent) AccountBalance() float64 { return t.body.AccountBalance }
func (t *FeeEvent) Reason() string { return t.body.Reason }
type (
MinId Id
)
// EventsArg is an optional argument for method PollEvents.
type EventsArg interface {
applyEventsArg(url.Values)
}
func (mi MaxId) applyEventsArg(v url.Values) {
optionalArgs(v).SetId("maxId", Id(mi))
}
func (mi MinId) applyEventsArg(v url.Values) {
optionalArgs(v).SetId("minId", Id(mi))
}
func (c Count) applyEventsArg(v url.Values) {
optionalArgs(v).SetInt("count", int(c))
}
func (i Instrument) applyEventsArg(v url.Values) {
v.Set("instrument", string(i))
}
func (ids Ids) applyEventsArg(v url.Values) {
optionalArgs(v).SetIdArray("ids", ids)
}
// PollEvents returns an array of events. Optional arguments are MaxId, MinId, Count,
// Instrument and Ids.
//
// See http://developer.oanda.com/docs/v1/transactions/#get-transaction-history for further
// information.
func (c *Client) PollEvents(args ...EventsArg) ([]Event, error) {
urlStr := fmt.Sprintf("/v1/accounts/%d/transactions", c.accountId)
u, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
data := u.Query()
for _, arg := range args {
arg.applyEventsArg(data)
}
u.RawQuery = data.Encode()
urlStr = u.String()
s := struct {
Events []struct {
*evtHeaderContent
*evtBody
} `json:"transactions"`
}{}
if err = getAndDecode(c, urlStr, &s); err != nil {
return nil, err
}
events := []Event{}
for _, rawEvent := range s.Events {
evt, err := asEvent(rawEvent.evtHeaderContent, rawEvent.evtBody)
if err != nil {
return nil, err
}
events = append(events, evt)
}
return events, nil
}
// PollEvent returns data for a single event.
func (c *Client) PollEvent(tranId Id) (Event, error) {
evtData := struct {
evtHeaderContent
evtBody
}{}
urlStr := fmt.Sprintf("/v1/accounts/%d/transactions/%d", c.accountId, tranId)
if err := getAndDecode(c, urlStr, &evtData); err != nil {
return nil, err
}
return asEvent(&evtData.evtHeaderContent, &evtData.evtBody)
}
func asEvent(header *evtHeaderContent, body *evtBody) (Event, error) {
switch header.Type {
case "CREATE":
return &AccountCreateEvent{evtHeader{header}, body}, nil
case "MARKET_ORDER_CREATE":
return &TradeCreateEvent{evtHeader{header}, body}, nil
case "LIMIT_ORDER_CREATE", "STOP_ORDER_CREATE", "MARKET_IF_TOUCHED_ORDER_CREATE":
return &OrderCreateEvent{evtHeader{header}, body}, nil
case "ORDER_UPDATE":
return &OrderUpdateEvent{evtHeader{header}, body}, nil
case "ORDER_CANCEL":
return &OrderCancelEvent{evtHeader{header}, body}, nil
case "ORDER_FILLED":
return &OrderFilledEvent{evtHeader{header}, body}, nil
case "TRADE_UPDATE":
return &TradeUpdateEvent{evtHeader{header}, body}, nil
case "TRADE_CLOSE", "MIGRATE_TRADE_CLOSE", "TAKE_PROFIT_FILLED", "STOP_LOSS_FILLED", "TRAILING_STOP_FILLED",
"MARGIN_CLOSEOUT":
return &TradeCloseEvent{evtHeader{header}, body}, nil
case "MIGRATE_TRADE_OPEN":
return &MigrateTradeOpenEvent{evtHeader{header}, body}, nil
case "SET_MARGIN_RATE":
return &SetMarginRateEvent{evtHeader{header}, body}, nil
case "TRANSFER_FUNDS":
return &TransferFundsEvent{evtHeader{header}, body}, nil
case "DAILY_INTEREST":
return &DailyInterestEvent{evtHeader{header}, body}, nil
case "FEE":
return &FeeEvent{evtHeader{header}, body}, nil
}
return nil, fmt.Errorf("Unexpected event type %s", header.Type)
}
// FullEventHistory returns a url from which a file containing the full transaction history
// for the account can be downloaded.
func (c *Client) FullEventHistory() (*url.URL, error) {
urlStr := fmt.Sprintf("/v1/accounts/%d/alltransactions", c.accountId)
req, err := c.NewRequest("GET", urlStr, nil)
if err != nil {
return nil, err
}
rsp, err := c.Do(req)
if err != nil {
return nil, err
}
// FIXME: Return the io.ReadCloser to the data instead of the location URL. Might want to
// wrap that in a streamServer wrapper so that the request can be interrupted?
tranUrl, err := rsp.Location()
if err != nil {
return nil, err
}
return tranUrl, nil
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// EventServer
// An EventServer receives events (aka transactions) for one or more accountId(s).
type EventServer struct {
// If HeartbeatFunc is not nil it is invoked once for every heartbeat message that the
// EventServer receives.
HeartbeatFunc HeartbeatHandlerFunc
chanMap *eventChans
srv *messageServer
}
type (
EventHandlerFunc func(Id, Event)
)
// NewEventServer returns an server instance for receiving events for the specified accountId(s).
// If no accountId is specified events for all accountIds are received.
//
// See http://developer.oanda.com/docs/v1/stream/#events-streaming for further information.
func (c *Client) NewEventServer(accountId ...Id) (*EventServer, error) {
req, err := c.NewRequest("GET", "/v1/events", nil)
if err != nil {
return nil, err
}
useStreamHost(req)
q := req.URL.Query()
optionalArgs(q).SetIdArray("accountIds", accountId)
req.URL.RawQuery = q.Encode()
es := &EventServer{
chanMap: newEventChans(accountId),
}
streamSrv := StreamServer{
handleMessagesFn: es.handleMessages,
handleHeartbeatsFn: es.handleHeartbeats,
}
if s, err := c.newMessageServer(req, streamSrv, time.Second*20); err != nil {
return nil, err
} else {
es.srv = s
}
return es, nil
}
// ConnectAndDispatch starts the event server and blocks until Stop() is called. Function handleFn
// is called for each event that is received.
//
// See http://developer.oanda.com/docs/v1/stream/ and http://developer.oanda.com/docs/v1/transactions/
// for further information.
func (es *EventServer) ConnectAndHandle(handleFn EventHandlerFunc) (err error) {
es.initServer(handleFn)
return es.srv.ConnectAndDispatch()
}
// Stop terminates the events server and causes ConnectAndHandle() to return.
func (es *EventServer) Stop() {
es.srv.Stop()
}
func (es *EventServer) initServer(handleFn EventHandlerFunc) {
for _, accId := range es.chanMap.AccountIds() {
evtC := make(chan Event, defaultBufferSize)
es.chanMap.Set(accId, evtC)
go func(lclC <-chan Event) {
for evt := range lclC {
handleFn(evt.AccountId(), evt)
}
}(evtC)
}
return
}
func (es *EventServer) handleHeartbeats(hbC <-chan Time) {
for hb := range hbC {
if es.HeartbeatFunc != nil {
es.HeartbeatFunc(hb)
}
}
}
func (es *EventServer) handleMessages(msgC <-chan StreamMessage) {
for msg := range msgC {
evt, err := EventFromJSON(msg.RawMessage)
if err != nil {
// FIXME: Log error
return
}
evtC, ok := es.chanMap.Get(evt.AccountId())
if !ok {
// FIXME: log error "unexpected accountId"
} else if evtC != nil {
evtC <- evt
} else {
// FiXME: log "event after server closed"
}
}
for _, accId := range es.chanMap.AccountIds() {
evtC, _ := es.chanMap.Get(accId)
es.chanMap.Set(accId, nil)
if evtC != nil {
close(evtC)
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// private
type eventChans struct {
mtx sync.RWMutex
m map[Id]chan Event
}
func (ec *eventChans) AccountIds() Ids {
ec.mtx.RLock()
defer ec.mtx.RUnlock()
accIds := make(Ids, len(ec.m))
for accId := range ec.m {
accIds = append(accIds, accId)
}
return accIds
}
func (ec *eventChans) Set(accountId Id, ch chan Event) {
ec.mtx.Lock()
defer ec.mtx.Unlock()
ec.m[accountId] = ch
}
func (ec *eventChans) Get(accountId Id) (chan Event, bool) {
ec.mtx.RLock()
defer ec.mtx.RUnlock()
ch, ok := ec.m[accountId]
return ch, ok
}
func newEventChans(accountIds Ids) *eventChans {
m := make(map[Id]chan Event, len(accountIds))
for _, accId := range accountIds {
m[accId] = nil
}
return &eventChans{
m: m,
}
}