forked from stellar/go
/
actions_offer.go
106 lines (94 loc) · 2.87 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
package horizon
import (
"errors"
"fmt"
"github.com/stellar/go/protocols/horizon"
"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:
// 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() {
action.Do(
action.loadParams,
action.loadRecords,
action.loadLedgers,
action.loadPage,
func() {
hal.Render(action.W, action.Page)
},
)
}
// SSE is a method for actions.SSE
func (action *OffersByAccountAction) SSE(stream sse.Stream) {
action.Do(
action.loadParams,
action.loadRecords,
action.loadLedgers,
func() {
stream.SetLimit(int(action.PageQuery.Limit))
for _, record := range action.Records[stream.SentCount():] {
ledger, found := action.Ledgers.Records[record.Lastmodified]
if !found {
msg := fmt.Sprintf("could not find ledger data for sequence %d", record.Lastmodified)
stream.Err(errors.New(msg))
return
}
var res horizon.Offer
resourceadapter.PopulateOffer(action.R.Context(), &res, record, ledger)
stream.Send(sse.Event{ID: res.PagingToken(), Data: res})
}
},
)
}
func (action *OffersByAccountAction) loadParams() {
action.PageQuery = action.GetPageQuery()
action.Address = action.GetString("account_id")
}
// loadLedgers populates the ledger cache for this action
func (action *OffersByAccountAction) loadLedgers() {
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]
if !found {
msg := fmt.Sprintf("could not find ledger data for sequence %d", record.Lastmodified)
action.Err = errors.New(msg)
return
}
var res horizon.Offer
resourceadapter.PopulateOffer(action.R.Context(), &res, record, ledger)
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()
}