This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
liquidity_pool.go
170 lines (145 loc) · 4.55 KB
/
liquidity_pool.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
package actions
import (
"context"
"net/http"
"strings"
protocol "github.com/xdbfoundation/go/protocols/frontier"
frontierContext "github.com/xdbfoundation/go/services/frontier/internal/context"
"github.com/xdbfoundation/go/services/frontier/internal/db2/history"
"github.com/xdbfoundation/go/services/frontier/internal/ledger"
"github.com/xdbfoundation/go/services/frontier/internal/resourceadapter"
"github.com/xdbfoundation/go/support/errors"
"github.com/xdbfoundation/go/support/render/hal"
"github.com/xdbfoundation/go/support/render/problem"
"github.com/xdbfoundation/go/xdr"
)
// GetLiquidityPoolByIDHandler is the action handler for all end-points returning a liquidity pool.
type GetLiquidityPoolByIDHandler struct{}
// LiquidityPoolQuery query struct for liquidity_pools/id endpoint
type LiquidityPoolQuery struct {
ID string `schema:"liquidity_pool_id" valid:"sha256"`
}
// GetResource returns an claimable balance page.
func (handler GetLiquidityPoolByIDHandler) GetResource(w HeaderWriter, r *http.Request) (interface{}, error) {
ctx := r.Context()
qp := LiquidityPoolQuery{}
err := getParams(&qp, r)
if err != nil {
return nil, err
}
historyQ, err := frontierContext.HistoryQFromRequest(r)
if err != nil {
return nil, err
}
cb, err := historyQ.FindLiquidityPoolByID(ctx, qp.ID)
if err != nil {
return nil, err
}
ledger := &history.Ledger{}
err = historyQ.LedgerBySequence(ctx, ledger, int32(cb.LastModifiedLedger))
if historyQ.NoRows(err) {
ledger = nil
} else if err != nil {
return nil, errors.Wrap(err, "LedgerBySequence error")
}
var resource protocol.LiquidityPool
err = resourceadapter.PopulateLiquidityPool(ctx, &resource, cb, ledger)
if err != nil {
return nil, err
}
return resource, nil
}
// LiquidityPoolsQuery query struct for liquidity_pools end-point
type LiquidityPoolsQuery struct {
Reserves string `schema:"reserves" valid:"optional"`
Account string `schema:"account" valid:"optional"`
reserves []xdr.Asset
}
// URITemplate returns a rfc6570 URI template the query struct
func (q LiquidityPoolsQuery) URITemplate() string {
return getURITemplate(&q, "liquidity_pools", true)
}
// Validate validates and parses the query
func (q *LiquidityPoolsQuery) Validate() error {
assets := []xdr.Asset{}
reserves := strings.Split(q.Reserves, ",")
reservesErr := problem.MakeInvalidFieldProblem(
"reserves",
errors.New("Invalid reserves, should be comma-separated list of assets in canonical form"),
)
for _, reserve := range reserves {
if reserve == "" {
continue
}
switch reserve {
case "native":
assets = append(assets, xdr.MustNewNativeAsset())
default:
parts := strings.Split(reserve, ":")
if len(parts) != 2 {
return reservesErr
}
asset, err := xdr.NewCreditAsset(parts[0], parts[1])
if err != nil {
return reservesErr
}
assets = append(assets, asset)
}
}
q.reserves = assets
return nil
}
type GetLiquidityPoolsHandler struct {
LedgerState *ledger.State
}
// GetResourcePage returns a page of liquidity pools.
func (handler GetLiquidityPoolsHandler) GetResourcePage(w HeaderWriter, r *http.Request) ([]hal.Pageable, error) {
ctx := r.Context()
qp := LiquidityPoolsQuery{}
err := getParams(&qp, r)
if err != nil {
return nil, err
}
pq, err := GetPageQuery(handler.LedgerState, r, DisableCursorValidation)
if err != nil {
return nil, err
}
query := history.LiquidityPoolsQuery{
PageQuery: pq,
Account: qp.Account,
Assets: qp.reserves,
}
historyQ, err := frontierContext.HistoryQFromRequest(r)
if err != nil {
return nil, err
}
liquidityPools, err := handler.getLiquidityPoolsPage(ctx, historyQ, query)
if err != nil {
return nil, err
}
return liquidityPools, nil
}
func (handler GetLiquidityPoolsHandler) getLiquidityPoolsPage(ctx context.Context, historyQ *history.Q, query history.LiquidityPoolsQuery) ([]hal.Pageable, error) {
records, err := historyQ.GetLiquidityPools(ctx, query)
if err != nil {
return nil, err
}
ledgerCache := history.LedgerCache{}
for _, record := range records {
ledgerCache.Queue(int32(record.LastModifiedLedger))
}
if err := ledgerCache.Load(ctx, historyQ); err != nil {
return nil, errors.Wrap(err, "failed to load ledger batch")
}
var liquidityPools []hal.Pageable
for _, record := range records {
var response protocol.LiquidityPool
var ledger *history.Ledger
if l, ok := ledgerCache.Records[int32(record.LastModifiedLedger)]; ok {
ledger = &l
}
resourceadapter.PopulateLiquidityPool(ctx, &response, record, ledger)
liquidityPools = append(liquidityPools, response)
}
return liquidityPools, nil
}