Skip to content

Commit ef2ff72

Browse files
authored
Restful api. (#16)
1 parent 5448b0b commit ef2ff72

File tree

12 files changed

+2465
-204
lines changed

12 files changed

+2465
-204
lines changed

api/api.go

Lines changed: 162 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,28 @@ func MustInit(client *web3go.Client, store *store.MysqlStore) {
2727

2828
var charge struct {
2929
Erc20TokenAddress string
30+
Symbol string
31+
Decimals uint8
3032
}
3133
viperUtil.MustUnmarshalKey("charge", &charge)
3234

33-
name, symbol, decimals, err := nhContract.TokenInfo(client, charge.Erc20TokenAddress)
34-
if err != nil {
35-
logrus.WithError(err).Fatal("Get erc20 token info")
36-
}
37-
38-
chargeToken = &TokenInfo{
39-
Address: charge.Erc20TokenAddress,
40-
Name: name,
41-
Symbol: symbol,
42-
Decimals: decimals,
35+
if charge.Erc20TokenAddress != "" {
36+
name, symbol, decimals, err := nhContract.TokenInfo(client, charge.Erc20TokenAddress)
37+
if err != nil {
38+
logrus.WithError(err).Fatal("Get erc20 token info")
39+
}
40+
chargeToken = &TokenInfo{
41+
Address: charge.Erc20TokenAddress,
42+
Name: name,
43+
Symbol: symbol,
44+
Decimals: decimals,
45+
}
46+
} else {
47+
chargeToken = &TokenInfo{
48+
Symbol: charge.Symbol,
49+
Decimals: charge.Decimals,
50+
}
51+
chargeToken.Native = true
4352
}
4453

4554
var flow struct {
@@ -49,7 +58,7 @@ func MustInit(client *web3go.Client, store *store.MysqlStore) {
4958
viperUtil.MustUnmarshalKey("flow", &flow)
5059
}
5160

52-
// @title 0G Storage Scan API
61+
// @title 0G Storage Scan API
5362
// @version 1.0
5463
// @description Use any http client to fetch data from the 0G Storage Scan.
5564
func init() {
@@ -60,7 +69,7 @@ func init() {
6069
//
6170
// @Summary Statistics dashboard
6271
// @Description Query statistics dashboard includes `storage fee` and `log sync height`
63-
// @Tags statistic
72+
// @Tags (deprecated)statistic
6473
// @Produce json
6574
// @Success 200 {object} api.BusinessError{Data=Dashboard}
6675
// @Failure 600 {object} api.BusinessError
@@ -73,7 +82,7 @@ func dashboardHandler(c *gin.Context) {
7382
//
7483
// @Summary Transaction statistics
7584
// @Description Query transaction statistics, including incremental and full data, and support querying at hourly or daily time intervals
76-
// @Tags statistic
85+
// @Tags (deprecated)statistic
7786
// @Accept json
7887
// @Produce json
7988
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
@@ -93,7 +102,7 @@ func listTxStatHandler(c *gin.Context) {
93102
//
94103
// @Summary Data storage statistics
95104
// @Description Query data storage statistics, including incremental and full data, and support querying at hourly or daily time intervals
96-
// @Tags statistic
105+
// @Tags (deprecated)statistic
97106
// @Accept json
98107
// @Produce json
99108
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
@@ -113,7 +122,7 @@ func listDataStatHandler(c *gin.Context) {
113122
//
114123
// @Summary fee statistics
115124
// @Description Query fee statistics, including incremental and full data, and support querying at hourly or daily time intervals
116-
// @Tags statistic
125+
// @Tags (deprecated)statistic
117126
// @Accept json
118127
// @Produce json
119128
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
@@ -133,7 +142,7 @@ func listFeeStatHandler(c *gin.Context) {
133142
//
134143
// @Summary Layer2 transaction list
135144
// @Description Query layer2 transactions, support address and root hash filter
136-
// @Tags transaction
145+
// @Tags (deprecated)transaction
137146
// @Accept json
138147
// @Produce json
139148
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
@@ -151,7 +160,7 @@ func listTxHandler(c *gin.Context) {
151160
//
152161
// @Summary Layer2 transaction overview
153162
// @Description Query layer2 transaction overview by txSeq
154-
// @Tags transaction
163+
// @Tags (deprecated)transaction
155164
// @Accept json
156165
// @Produce json
157166
// @Param txSeq query string true "Lay2 transaction sequence number"
@@ -166,7 +175,7 @@ func getTxBriefHandler(c *gin.Context) {
166175
//
167176
// @Summary Layer2 transaction advanced info
168177
// @Description Query layer2 transaction advanced info by txSeq
169-
// @Tags transaction
178+
// @Tags (deprecated)transaction
170179
// @Accept json
171180
// @Produce json
172181
// @Param txSeq query string true "Lay2 transaction sequence number"
@@ -191,5 +200,140 @@ func RegisterRouter(router *gin.Engine) {
191200
txRoute.GET("brief", getTxBriefHandler)
192201
txRoute.GET("detail", getTxDetailHandler)
193202

203+
statsRoute := apiRoute.Group("/stats")
204+
statsRoute.GET("summary", summaryHandler)
205+
statsRoute.GET("layer1-tx", listTxStatsHandler)
206+
statsRoute.GET("storage", listDataStatsHandler)
207+
statsRoute.GET("fee", listFeeStatsHandler)
208+
209+
txsRoute := apiRoute.Group("/txs")
210+
txsRoute.GET("", listTxsHandler)
211+
txsRoute.GET(":txSeq", getTxHandler)
212+
213+
accountsRoute := apiRoute.Group("/accounts")
214+
accountsRoute.GET(":address/txs", listAddressTxsHandler)
215+
194216
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
195217
}
218+
219+
// summaryHandler godoc
220+
//
221+
// @Summary Statistics summary
222+
// @Description Query statistics summary includes `storage fee` and `log sync height`
223+
// @Tags statistic
224+
// @Produce json
225+
// @Success 200 {object} api.BusinessError{Data=Summary}
226+
// @Failure 600 {object} api.BusinessError
227+
// @Router /stats/summary [get]
228+
func summaryHandler(c *gin.Context) {
229+
api.Wrap(summary)(c)
230+
}
231+
232+
// listTxStatsHandler godoc
233+
//
234+
// @Summary Layer1 transaction statistics
235+
// @Description Query transaction statistics, including incremental and full data, and support querying at hourly or daily time intervals
236+
// @Tags statistic
237+
// @Accept json
238+
// @Produce json
239+
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
240+
// @Param limit query int false "The number of records displayed on the page" minimum(1) maximum(2000) default(10)
241+
// @Param minTimestamp query int false "Timestamp in seconds"
242+
// @Param maxTimestamp query int false "Timestamp in seconds"
243+
// @Param intervalType query string false "Statistics interval" Enums(hour, day) default(day)
244+
// @Param sort query string false "Sort by timestamp" Enums(asc, desc) default(desc)
245+
// @Success 200 {object} api.BusinessError{Data=TxStatList}
246+
// @Failure 600 {object} api.BusinessError
247+
// @Router /stats/layer1-tx [get]
248+
func listTxStatsHandler(c *gin.Context) {
249+
api.Wrap(listTxStat)(c)
250+
}
251+
252+
// listDataStatsHandler godoc
253+
//
254+
// @Summary Data storage statistics
255+
// @Description Query data storage statistics, including incremental and full data, and support querying at hourly or daily time intervals
256+
// @Tags statistic
257+
// @Accept json
258+
// @Produce json
259+
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
260+
// @Param limit query int false "The number of records displayed on the page" minimum(1) maximum(2000) default(10)
261+
// @Param minTimestamp query int false "Timestamp in seconds"
262+
// @Param maxTimestamp query int false "Timestamp in seconds"
263+
// @Param intervalType query string false "Statistics interval" Enums(hour, day) default(day)
264+
// @Param sort query string false "Sort by timestamp" Enums(asc, desc) default(desc)
265+
// @Success 200 {object} api.BusinessError{Data=DataStatList}
266+
// @Failure 600 {object} api.BusinessError
267+
// @Router /stats/storage [get]
268+
func listDataStatsHandler(c *gin.Context) {
269+
api.Wrap(listDataStat)(c)
270+
}
271+
272+
// listFeeStatsHandler godoc
273+
//
274+
// @Summary Storage fee statistics
275+
// @Description Query fee statistics, including incremental and full data, and support querying at hourly or daily time intervals
276+
// @Tags statistic
277+
// @Accept json
278+
// @Produce json
279+
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
280+
// @Param limit query int false "The number of records displayed on the page" minimum(1) maximum(2000) default(10)
281+
// @Param minTimestamp query int false "Timestamp in seconds"
282+
// @Param maxTimestamp query int false "Timestamp in seconds"
283+
// @Param intervalType query string false "Statistics interval" Enums(hour, day) default(day)
284+
// @Param sort query string false "Sort by timestamp" Enums(asc, desc) default(desc)
285+
// @Success 200 {object} api.BusinessError{Data=FeeStatList}
286+
// @Failure 600 {object} api.BusinessError
287+
// @Router /stats/fee [get]
288+
func listFeeStatsHandler(c *gin.Context) {
289+
api.Wrap(listFeeStat)(c)
290+
}
291+
292+
// listTxsHandler godoc
293+
//
294+
// @Summary Storage transaction list
295+
// @Description Query storage transactions, support address and root hash filter
296+
// @Tags transaction
297+
// @Accept json
298+
// @Produce json
299+
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
300+
// @Param limit query int false "The number of records displayed on the page" minimum(1) maximum(100) default(10)
301+
// @Success 200 {object} api.BusinessError{Data=StorageTxList}
302+
// @Failure 600 {object} api.BusinessError
303+
// @Router /txs [get]
304+
func listTxsHandler(c *gin.Context) {
305+
api.Wrap(listStorageTx)(c)
306+
}
307+
308+
// getTxHandler godoc
309+
//
310+
// @Summary Storage transaction information
311+
// @Description Query storage transaction by txSeq
312+
// @Tags transaction
313+
// @Accept json
314+
// @Produce json
315+
// @Param txSeq path string true "storage transaction sequence number"
316+
// @Success 200 {object} api.BusinessError{Data=StorageTxDetail}
317+
// @Failure 600 {object} api.BusinessError
318+
// @Router /txs/{txSeq} [get]
319+
func getTxHandler(c *gin.Context) {
320+
api.Wrap(getStorageTx)(c)
321+
}
322+
323+
// listAddressTxsHandler godoc
324+
//
325+
// @Summary Account's storage transaction list
326+
// @Description Query storage transactions for specified account, support root hash filter
327+
// @Tags account
328+
// @Accept json
329+
// @Produce json
330+
// @Param address path string false "The submitter address of the uploaded file"
331+
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
332+
// @Param limit query int false "The number of records displayed on the page" minimum(1) maximum(100) default(10)
333+
// @Param rootHash query string false "The merkle root hash of the uploaded file"
334+
// @Success 200 {object} api.BusinessError{Data=StorageTxList}
335+
// @Failure 600 {object} api.BusinessError
336+
// @Router /accounts/{address}/txs [get]
337+
func listAddressTxsHandler(c *gin.Context) {
338+
api.Wrap(listAddressStorageTx)(c)
339+
}

api/stat_api.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ func getSubmitStatByType(c *gin.Context, t Type) (interface{}, error) {
105105
list := make([]FeeStat, 0)
106106
for _, r := range records {
107107
list = append(list, FeeStat{
108-
StatTime: r.StatTime,
109-
BaseFee: r.BaseFee,
110-
BaseFeeTotal: r.BaseFeeTotal,
108+
StatTime: r.StatTime,
109+
StorageFee: r.BaseFee,
110+
StorageFeeTotal: r.BaseFeeTotal,
111111
})
112112
}
113113
result["list"] = list
@@ -117,3 +117,37 @@ func getSubmitStatByType(c *gin.Context, t Type) (interface{}, error) {
117117

118118
return result, nil
119119
}
120+
121+
func summary(_ *gin.Context) (interface{}, error) {
122+
value, exist, err := db.ConfigStore.Get(store.KeyLogSyncInfo)
123+
if err != nil {
124+
return nil, commonApi.ErrInternal(err)
125+
}
126+
if !exist {
127+
return nil, ErrConfigNotFound
128+
}
129+
130+
var logSyncInfo stat.LogSyncInfo
131+
if err := json.Unmarshal([]byte(value), &logSyncInfo); err != nil {
132+
return nil, commonApi.ErrInternal(err)
133+
}
134+
135+
submitStat, err := db.SubmitStatStore.LastByType(store.Day)
136+
if err != nil {
137+
return nil, commonApi.ErrInternal(err)
138+
}
139+
if submitStat == nil {
140+
return nil, ErrStorageBaseFeeNotStat
141+
}
142+
143+
storageFee := StorageFeeStat{
144+
TokenInfo: *chargeToken,
145+
StorageFeeTotal: submitStat.BaseFeeTotal,
146+
}
147+
result := Summary{
148+
StorageFeeStat: storageFee,
149+
LogSyncInfo: logSyncInfo,
150+
}
151+
152+
return result, nil
153+
}

0 commit comments

Comments
 (0)