forked from stellar/go
/
actions_offer.go
118 lines (103 loc) · 3.22 KB
/
actions_offer.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
package horizon
import (
"github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/services/horizon/internal/actions"
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/db2/core"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/render/sse"
"github.com/stellar/go/services/horizon/internal/resourceadapter"
"github.com/stellar/go/support/render/hal"
)
// This file contains the actions:
// Interface verifications
var _ actions.JSONer = (*OffersByAccountAction)(nil)
var _ actions.EventStreamer = (*OffersByAccountAction)(nil)
// OffersByAccountAction renders a page of offer resources, for a given
// account. These offers are present in the ledger as of the latest validated
// ledger.
type OffersByAccountAction struct {
Action
Address string
PageQuery db2.PageQuery
Records []core.Offer
Ledgers *history.LedgerCache
Page hal.Page
}
// JSON is a method for actions.JSON
func (action *OffersByAccountAction) JSON() error {
action.Do(
action.loadParams,
action.loadRecords,
action.loadLedgers,
action.loadPage,
func() { hal.Render(action.W, action.Page) },
)
return action.Err
}
// SSE is a method for actions.SSE
func (action *OffersByAccountAction) SSE(stream *sse.Stream) error {
// Load the page query params the first time SSE() is called. We update
// the pagination cursor below before sending each event to the stream.
if action.PageQuery.Cursor == "" {
action.loadParams()
if action.Err != nil {
return action.Err
}
}
action.Do(
action.loadRecords,
action.loadLedgers,
func() {
stream.SetLimit(int(action.PageQuery.Limit))
for _, record := range action.Records {
ledger, found := action.Ledgers.Records[record.Lastmodified]
ledgerPtr := &ledger
if !found {
ledgerPtr = nil
}
var res horizon.Offer
resourceadapter.PopulateOffer(action.R.Context(), &res, record, ledgerPtr)
action.PageQuery.Cursor = res.PagingToken()
stream.Send(sse.Event{ID: res.PagingToken(), Data: res})
}
},
)
return action.Err
}
func (action *OffersByAccountAction) loadParams() {
action.PageQuery = action.GetPageQuery()
action.Address = action.GetAddress("account_id")
}
// loadLedgers populates the ledger cache for this action
func (action *OffersByAccountAction) loadLedgers() {
action.Ledgers = &history.LedgerCache{}
for _, offer := range action.Records {
action.Ledgers.Queue(offer.Lastmodified)
}
action.Err = action.Ledgers.Load(action.HistoryQ())
}
func (action *OffersByAccountAction) loadRecords() {
action.Err = action.CoreQ().OffersByAddress(
&action.Records,
action.Address,
action.PageQuery,
)
}
func (action *OffersByAccountAction) loadPage() {
for _, record := range action.Records {
ledger, found := action.Ledgers.Records[record.Lastmodified]
ledgerPtr := &ledger
if !found {
ledgerPtr = nil
}
var res horizon.Offer
resourceadapter.PopulateOffer(action.R.Context(), &res, record, ledgerPtr)
action.Page.Add(res)
}
action.Page.FullURL = action.FullURL()
action.Page.Limit = action.PageQuery.Limit
action.Page.Cursor = action.PageQuery.Cursor
action.Page.Order = action.PageQuery.Order
action.Page.PopulateLinks()
}