-
Notifications
You must be signed in to change notification settings - Fork 496
/
liquidity_pools_transaction_processor.go
160 lines (137 loc) · 4.18 KB
/
liquidity_pools_transaction_processor.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
package processors
import (
"context"
"sort"
"github.com/stellar/go/ingest"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/support/db"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
)
type LiquidityPoolsTransactionProcessor struct {
lpLoader *history.LiquidityPoolLoader
txBatch history.TransactionLiquidityPoolBatchInsertBuilder
opBatch history.OperationLiquidityPoolBatchInsertBuilder
}
func NewLiquidityPoolsTransactionProcessor(
lpLoader *history.LiquidityPoolLoader,
txBatch history.TransactionLiquidityPoolBatchInsertBuilder,
opBatch history.OperationLiquidityPoolBatchInsertBuilder,
) *LiquidityPoolsTransactionProcessor {
return &LiquidityPoolsTransactionProcessor{
lpLoader: lpLoader,
txBatch: txBatch,
opBatch: opBatch,
}
}
func (p *LiquidityPoolsTransactionProcessor) Name() string {
return "processors.LiquidityPoolsTransactionProcessor"
}
func (p *LiquidityPoolsTransactionProcessor) ProcessTransaction(lcm xdr.LedgerCloseMeta, transaction ingest.LedgerTransaction) error {
err := p.addTransactionLiquidityPools(lcm.LedgerSequence(), transaction)
if err != nil {
return err
}
err = p.addOperationLiquidityPools(lcm.LedgerSequence(), transaction)
if err != nil {
return err
}
return nil
}
func (p *LiquidityPoolsTransactionProcessor) addTransactionLiquidityPools(sequence uint32, transaction ingest.LedgerTransaction) error {
transactionID := toid.New(int32(sequence), int32(transaction.Index), 0).ToInt64()
lps, err := liquidityPoolsForTransaction(transaction)
if err != nil {
return errors.Wrap(err, "Could not determine liquidity pools for transaction")
}
for _, lp := range dedupeStrings(lps) {
if err = p.txBatch.Add(transactionID, p.lpLoader.GetFuture(lp)); err != nil {
return err
}
}
return nil
}
func liquidityPoolsForTransaction(transaction ingest.LedgerTransaction) ([]string, error) {
changes, err := transaction.GetChanges()
if err != nil {
return nil, err
}
lps, err := liquidityPoolsForChanges(changes)
if err != nil {
return nil, errors.Wrapf(err, "reading transaction %v liquidity pools", transaction.Index)
}
return lps, nil
}
func dedupeStrings(in []string) []string {
if len(in) <= 1 {
return in
}
sort.Strings(in)
insert := 1
for cur := 1; cur < len(in); cur++ {
if in[cur] == in[cur-1] {
continue
}
if insert != cur {
in[insert] = in[cur]
}
insert++
}
return in[:insert]
}
func liquidityPoolsForChanges(
changes []ingest.Change,
) ([]string, error) {
var lps []string
for _, c := range changes {
if c.Type != xdr.LedgerEntryTypeLiquidityPool {
continue
}
if c.Pre == nil && c.Post == nil {
return nil, errors.New("Invalid io.Change: change.Pre == nil && change.Post == nil")
}
if c.Pre != nil {
poolID := c.Pre.Data.MustLiquidityPool().LiquidityPoolId
lps = append(lps, PoolIDToString(poolID))
}
if c.Post != nil {
poolID := c.Post.Data.MustLiquidityPool().LiquidityPoolId
lps = append(lps, PoolIDToString(poolID))
}
}
return lps, nil
}
func (p *LiquidityPoolsTransactionProcessor) addOperationLiquidityPools(sequence uint32, transaction ingest.LedgerTransaction) error {
for opi, op := range transaction.Envelope.Operations() {
operation := transactionOperationWrapper{
index: uint32(opi),
transaction: transaction,
operation: op,
ledgerSequence: sequence,
}
changes, err := transaction.GetOperationChanges(uint32(opi))
if err != nil {
return err
}
lps, err := liquidityPoolsForChanges(changes)
if err != nil {
return errors.Wrapf(err, "reading operation %v liquidity pools", operation.ID())
}
for _, lp := range dedupeStrings(lps) {
if err := p.opBatch.Add(operation.ID(), p.lpLoader.GetFuture(lp)); err != nil {
return err
}
}
}
return nil
}
func (p *LiquidityPoolsTransactionProcessor) Flush(ctx context.Context, session db.SessionInterface) error {
if err := p.txBatch.Exec(ctx, session); err != nil {
return errors.Wrap(err, "Could not flush transaction liquidity pools to db")
}
if err := p.opBatch.Exec(ctx, session); err != nil {
return errors.Wrap(err, "Could not flush operation liquidity pools to db")
}
return nil
}