forked from stellar/go
/
actions_assets.go
87 lines (76 loc) · 2.05 KB
/
actions_assets.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
package horizon
import (
"fmt"
"github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/db2/assets"
"github.com/stellar/go/services/horizon/internal/resourceadapter"
"github.com/stellar/go/support/render/hal"
)
// This file contains the actions:
//
// AssetsAction: pages of assets
// AssetsAction renders a page of Assets
type AssetsAction struct {
Action
AssetCode string
AssetIssuer string
PagingParams db2.PageQuery
Records []assets.AssetStatsR
Page hal.Page
}
const maxAssetCodeLength = 12
// JSON is a method for actions.JSON
func (action *AssetsAction) JSON() {
action.Do(
action.loadParams,
action.loadRecords,
action.loadPage,
func() {
hal.Render(action.W, action.Page)
},
)
}
func (action *AssetsAction) loadParams() {
action.AssetCode = action.GetString("asset_code")
if len(action.AssetCode) > maxAssetCodeLength {
action.SetInvalidField("asset_code", fmt.Errorf("max length is: %d", maxAssetCodeLength))
return
}
if len(action.GetString("asset_issuer")) > 0 {
issuerAccount := action.GetAccountID("asset_issuer")
if action.Err != nil {
return
}
action.AssetIssuer = issuerAccount.Address()
}
action.PagingParams = action.GetPageQuery()
}
func (action *AssetsAction) loadRecords() {
sql, err := assets.AssetStatsQ{
AssetCode: &action.AssetCode,
AssetIssuer: &action.AssetIssuer,
PageQuery: &action.PagingParams,
}.GetSQL()
if err != nil {
action.Err = err
return
}
action.Err = action.HistoryQ().Select(&action.Records, sql)
}
func (action *AssetsAction) loadPage() {
for _, record := range action.Records {
var res horizon.AssetStat
err := resourceadapter.PopulateAssetStat(action.R.Context(), &res, record)
if err != nil {
action.Err = err
return
}
action.Page.Add(res)
}
action.Page.FullURL = action.FullURL()
action.Page.Limit = action.PagingParams.Limit
action.Page.Cursor = action.PagingParams.Cursor
action.Page.Order = action.PagingParams.Order
action.Page.PopulateLinks()
}