This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathsubscribe.go
87 lines (76 loc) · 3.15 KB
/
subscribe.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
package websockets
import (
"encoding/json"
"github.com/rubblelabs/ripple/data"
)
// Fields from subscribed ledger stream messages
type LedgerStreamMsg struct {
FeeBase uint64 `json:"fee_base"`
FeeRef uint64 `json:"fee_ref"`
LedgerSequence uint32 `json:"ledger_index"`
LedgerHash data.Hash256 `json:"ledger_hash"`
LedgerTime data.RippleTime `json:"ledger_time"`
ReserveBase uint64 `json:"reserve_base"`
ReserveIncrement uint64 `json:"reserve_inc"`
ValidatedLedgers string `json:"validated_ledgers"`
TxnCount uint32 `json:"txn_count"` // Only streamed, not in the subscribe result.
}
// Fields from subscribed transaction stream messages
type TransactionStreamMsg struct {
Transaction data.TransactionWithMetaData `json:"transaction"`
EngineResult data.TransactionResult `json:"engine_result"`
EngineResultCode int `json:"engine_result_code"`
EngineResultMessage string `json:"engine_result_message"`
LedgerHash data.Hash256 `json:"ledger_hash"`
LedgerSequence uint32 `json:"ledger_index"`
Status string
Validated bool
}
// Fields from subscribed server status stream messages
type ServerStreamMsg struct {
Status string `json:"server_status"`
BaseFee uint64 `json:"base_fee"`
LoadBase uint64 `json:"load_base"`
LoadFactor uint64 `json:"load_factor"`
LoadFactorFeeEscalation uint64 `json:"load_factor_fee_escalation"`
LoadFactorFeeQueue uint64 `json:"load_factor_fee_queue"`
LoadFactorFeeReference uint64 `json:"load_factor_fee_reference"`
LoadFactorServer uint64 `json:"load_factor_server"`
HostID string `json:"hostid"`
}
func (s *ServerStreamMsg) TransactionCost() uint64 {
return (s.BaseFee * s.LoadFactor) / s.LoadBase
}
// Map message types to the appropriate data structure
var streamMessageFactory = map[string]func() interface{}{
"ledgerClosed": func() interface{} { return &LedgerStreamMsg{} },
"transaction": func() interface{} { return &TransactionStreamMsg{} },
"serverStatus": func() interface{} { return &ServerStreamMsg{} },
}
type SubscribeCommand struct {
*Command
Streams []string `json:"streams"`
Books []OrderBookSubscription `json:"books,omitempty"`
Result *SubscribeResult `json:"result,omitempty"`
}
type SubscribeResult struct {
// Contains one or both of these, depending what streams were subscribed
*LedgerStreamMsg
*ServerStreamMsg
// Contains "bids" and "asks" when "both" is true.
Asks []data.OrderBookOffer
Bids []data.OrderBookOffer
// Contains "offers" when "both" is false.
Offers []data.OrderBookOffer
}
// Wrapper to stop recursive unmarshalling
type txStreamJSON TransactionStreamMsg
func (msg *TransactionStreamMsg) UnmarshalJSON(b []byte) error {
var extract struct {
*txStreamJSON
MetaData *data.MetaData `json:"meta"`
}
extract.txStreamJSON = (*txStreamJSON)(msg)
extract.MetaData = &msg.Transaction.MetaData
return json.Unmarshal(b, &extract)
}