forked from stellar/go
/
actions_path.go
62 lines (53 loc) · 1.63 KB
/
actions_path.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
package horizon
import (
"github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/services/horizon/internal/actions"
"github.com/stellar/go/services/horizon/internal/paths"
"github.com/stellar/go/services/horizon/internal/resourceadapter"
"github.com/stellar/go/support/render/hal"
)
// Interface verification
var _ actions.JSONer = (*PathIndexAction)(nil)
// PathIndexAction provides path finding
type PathIndexAction struct {
Action
Query paths.Query
Records []paths.Path
Page hal.BasePage
}
// JSON implements actions.JSON
func (action *PathIndexAction) JSON() error {
action.Do(
action.loadQuery,
action.loadSourceAssets,
action.loadRecords,
action.loadPage,
func() { hal.Render(action.W, action.Page) },
)
return action.Err
}
func (action *PathIndexAction) loadQuery() {
action.Query.DestinationAmount = action.GetPositiveAmount("destination_amount")
action.Query.DestinationAddress = action.GetAddress("destination_account", actions.RequiredParam)
action.Query.DestinationAsset = action.GetAsset("destination_")
}
func (action *PathIndexAction) loadSourceAssets() {
action.Err = action.CoreQ().AssetsForAddress(
&action.Query.SourceAssets,
action.GetAddress("source_account"),
)
}
func (action *PathIndexAction) loadRecords() {
action.Records, action.Err = action.App.paths.Find(action.Query, action.App.config.MaxPathLength)
}
func (action *PathIndexAction) loadPage() {
action.Page.Init()
for _, p := range action.Records {
var res horizon.Path
action.Err = resourceadapter.PopulatePath(action.R.Context(), &res, action.Query, p)
if action.Err != nil {
return
}
action.Page.Add(res)
}
}