Repository navigation
Expand file tree
/
Copy pathbatch.go
More file actions
177 lines (164 loc) · 4.83 KB
/
Copy pathbatch.go
File metadata and controls
177 lines (164 loc) · 4.83 KB
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
171
172
173
174
175
176
177
package api
import (
"github.com/gin-gonic/gin"
"github.com/trustwallet/blockatlas/coin"
"github.com/trustwallet/blockatlas/pkg/blockatlas"
"github.com/trustwallet/blockatlas/pkg/ginutils"
"github.com/trustwallet/blockatlas/platform"
"strconv"
)
type AddressBatchRequest struct {
Address string `json:"address"`
CoinBatchRequest
}
type CoinBatchRequest struct {
Coin uint `json:"coin"`
}
type ENSBatchRequest struct {
Coins []uint64 `json:"coins"`
Name string `json:"name"`
}
type AddressesRequest []AddressBatchRequest
type CoinsRequest []CoinBatchRequest
// @Summary Get Multiple Stake Delegations
// @ID batch_delegations
// @Description Get Stake Delegations for multiple coins
// @Accept json
// @Produce json
// @Tags platform,staking
// @Param delegations body api.AddressesRequest true "Validators addresses and coins"
// @Success 200 {object} blockatlas.DelegationsBatchPage
// @Router /v2/staking/delegations [post]
func makeStakingDelegationsBatchRoute(router gin.IRouter) {
router.POST("/staking/delegations", func(c *gin.Context) {
var reqs AddressesRequest
if err := c.BindJSON(&reqs); err != nil {
ginutils.ErrorResponse(c).Message(err.Error()).Render()
return
}
batch := make(blockatlas.DelegationsBatchPage, 0)
for _, r := range reqs {
c, ok := coin.Coins[r.Coin]
if !ok {
continue
}
p, ok := platform.StakeAPIs[c.Handle]
if !ok {
continue
}
delegation, err := getDelegationResponse(p, r.Address)
if err != nil {
continue
}
batch = append(batch, delegation)
}
ginutils.RenderSuccess(c, blockatlas.DocsResponse{Docs: batch})
})
}
// @Summary Get Multiple Stake Delegations
// @ID batch_delegations
// @Description Get Stake Delegations for multiple coins
// @Accept json
// @Produce json
// @Tags platform,staking
// @Param delegations body api.AddressesRequest true "Validators addresses and coins"
// @Success 200 {object} blockatlas.DelegationsBatchPage
// @Router /v2/staking/list [post]
func makeStakingDelegationsSimpleBatchRoute(router gin.IRouter) {
router.POST("/staking/list", func(c *gin.Context) {
var reqs CoinsRequest
if err := c.BindJSON(&reqs); err != nil {
ginutils.ErrorResponse(c).Message(err.Error()).Render()
return
}
batch := make(blockatlas.StakingBatchPage, 0)
for _, r := range reqs {
c, ok := coin.Coins[r.Coin]
if !ok {
continue
}
p, ok := platform.StakeAPIs[c.Handle]
if !ok {
continue
}
staking := getStakingResponse(p)
batch = append(batch, staking)
}
ginutils.RenderSuccess(c, blockatlas.DocsResponse{Docs: batch})
})
}
// @Description Get collection categories
// @ID collection_categories_v2
// @Summary Get list of collections from a specific coin and addresses
// @Accept json
// @Produce json
// @Tags Collectibles
// @Param data body string true "Payload" default({"60": ["0xb3624367b1ab37daef42e1a3a2ced012359659b0"]})
// @Success 200 {object} blockatlas.DocsResponse
// @Router /v2/collectibles/categories [post]
//TODO: remove once most of the clients will be updated (deadline: March 17th)
func oldMakeCategoriesBatchRoute(router gin.IRouter) {
router.POST("/collectibles/categories", func(c *gin.Context) {
var reqs map[string][]string
if err := c.BindJSON(&reqs); err != nil {
ginutils.ErrorResponse(c).Message(err.Error()).Render()
return
}
batch := make(blockatlas.CollectionPage, 0)
for key, addresses := range reqs {
coinId, err := strconv.Atoi(key)
if err != nil {
continue
}
p, ok := platform.CollectionAPIs[uint(coinId)]
if !ok {
continue
}
for _, address := range addresses {
collections, err := p.OldGetCollections(address)
if err != nil {
continue
}
batch = append(batch, collections...)
}
}
ginutils.RenderSuccess(c, batch)
})
}
// @Description Get collection categories
// @ID collection_categories_v3
// @Summary Get list of collections from a specific coin and addresses
// @Accept json
// @Produce json
// @Tags Collectibles
// @Param data body string true "Payload" default({"60": ["0xb3624367b1ab37daef42e1a3a2ced012359659b0"]})
// @Success 200 {object} blockatlas.DocsResponse
// @Router /v3/collectibles/categories [post]
func makeCategoriesBatchRoute(router gin.IRouter) {
router.POST("/collectibles/categories", func(c *gin.Context) {
var reqs map[string][]string
if err := c.BindJSON(&reqs); err != nil {
ginutils.ErrorResponse(c).Message(err.Error()).Render()
return
}
batch := make(blockatlas.CollectionPage, 0)
for key, addresses := range reqs {
coinId, err := strconv.Atoi(key)
if err != nil {
continue
}
p, ok := platform.CollectionAPIs[uint(coinId)]
if !ok {
continue
}
for _, address := range addresses {
collections, err := p.GetCollections(address)
if err != nil {
continue
}
batch = append(batch, collections...)
}
}
ginutils.RenderSuccess(c, batch)
})
}