Skip to content

Commit

Permalink
Restful api. (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Argeric authored Mar 21, 2024
1 parent 5448b0b commit ef2ff72
Show file tree
Hide file tree
Showing 12 changed files with 2,465 additions and 204 deletions.
180 changes: 162 additions & 18 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,28 @@ func MustInit(client *web3go.Client, store *store.MysqlStore) {

var charge struct {
Erc20TokenAddress string
Symbol string
Decimals uint8
}
viperUtil.MustUnmarshalKey("charge", &charge)

name, symbol, decimals, err := nhContract.TokenInfo(client, charge.Erc20TokenAddress)
if err != nil {
logrus.WithError(err).Fatal("Get erc20 token info")
}

chargeToken = &TokenInfo{
Address: charge.Erc20TokenAddress,
Name: name,
Symbol: symbol,
Decimals: decimals,
if charge.Erc20TokenAddress != "" {
name, symbol, decimals, err := nhContract.TokenInfo(client, charge.Erc20TokenAddress)
if err != nil {
logrus.WithError(err).Fatal("Get erc20 token info")
}
chargeToken = &TokenInfo{
Address: charge.Erc20TokenAddress,
Name: name,
Symbol: symbol,
Decimals: decimals,
}
} else {
chargeToken = &TokenInfo{
Symbol: charge.Symbol,
Decimals: charge.Decimals,
}
chargeToken.Native = true
}

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

// @title 0G Storage Scan API
// @title 0G Storage Scan API
// @version 1.0
// @description Use any http client to fetch data from the 0G Storage Scan.
func init() {
Expand All @@ -60,7 +69,7 @@ func init() {
//
// @Summary Statistics dashboard
// @Description Query statistics dashboard includes `storage fee` and `log sync height`
// @Tags statistic
// @Tags (deprecated)statistic
// @Produce json
// @Success 200 {object} api.BusinessError{Data=Dashboard}
// @Failure 600 {object} api.BusinessError
Expand All @@ -73,7 +82,7 @@ func dashboardHandler(c *gin.Context) {
//
// @Summary Transaction statistics
// @Description Query transaction statistics, including incremental and full data, and support querying at hourly or daily time intervals
// @Tags statistic
// @Tags (deprecated)statistic
// @Accept json
// @Produce json
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
Expand All @@ -93,7 +102,7 @@ func listTxStatHandler(c *gin.Context) {
//
// @Summary Data storage statistics
// @Description Query data storage statistics, including incremental and full data, and support querying at hourly or daily time intervals
// @Tags statistic
// @Tags (deprecated)statistic
// @Accept json
// @Produce json
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
Expand All @@ -113,7 +122,7 @@ func listDataStatHandler(c *gin.Context) {
//
// @Summary fee statistics
// @Description Query fee statistics, including incremental and full data, and support querying at hourly or daily time intervals
// @Tags statistic
// @Tags (deprecated)statistic
// @Accept json
// @Produce json
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
Expand All @@ -133,7 +142,7 @@ func listFeeStatHandler(c *gin.Context) {
//
// @Summary Layer2 transaction list
// @Description Query layer2 transactions, support address and root hash filter
// @Tags transaction
// @Tags (deprecated)transaction
// @Accept json
// @Produce json
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
Expand All @@ -151,7 +160,7 @@ func listTxHandler(c *gin.Context) {
//
// @Summary Layer2 transaction overview
// @Description Query layer2 transaction overview by txSeq
// @Tags transaction
// @Tags (deprecated)transaction
// @Accept json
// @Produce json
// @Param txSeq query string true "Lay2 transaction sequence number"
Expand All @@ -166,7 +175,7 @@ func getTxBriefHandler(c *gin.Context) {
//
// @Summary Layer2 transaction advanced info
// @Description Query layer2 transaction advanced info by txSeq
// @Tags transaction
// @Tags (deprecated)transaction
// @Accept json
// @Produce json
// @Param txSeq query string true "Lay2 transaction sequence number"
Expand All @@ -191,5 +200,140 @@ func RegisterRouter(router *gin.Engine) {
txRoute.GET("brief", getTxBriefHandler)
txRoute.GET("detail", getTxDetailHandler)

statsRoute := apiRoute.Group("/stats")
statsRoute.GET("summary", summaryHandler)
statsRoute.GET("layer1-tx", listTxStatsHandler)
statsRoute.GET("storage", listDataStatsHandler)
statsRoute.GET("fee", listFeeStatsHandler)

txsRoute := apiRoute.Group("/txs")
txsRoute.GET("", listTxsHandler)
txsRoute.GET(":txSeq", getTxHandler)

accountsRoute := apiRoute.Group("/accounts")
accountsRoute.GET(":address/txs", listAddressTxsHandler)

router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}

// summaryHandler godoc
//
// @Summary Statistics summary
// @Description Query statistics summary includes `storage fee` and `log sync height`
// @Tags statistic
// @Produce json
// @Success 200 {object} api.BusinessError{Data=Summary}
// @Failure 600 {object} api.BusinessError
// @Router /stats/summary [get]
func summaryHandler(c *gin.Context) {
api.Wrap(summary)(c)
}

// listTxStatsHandler godoc
//
// @Summary Layer1 transaction statistics
// @Description Query transaction statistics, including incremental and full data, and support querying at hourly or daily time intervals
// @Tags statistic
// @Accept json
// @Produce json
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
// @Param limit query int false "The number of records displayed on the page" minimum(1) maximum(2000) default(10)
// @Param minTimestamp query int false "Timestamp in seconds"
// @Param maxTimestamp query int false "Timestamp in seconds"
// @Param intervalType query string false "Statistics interval" Enums(hour, day) default(day)
// @Param sort query string false "Sort by timestamp" Enums(asc, desc) default(desc)
// @Success 200 {object} api.BusinessError{Data=TxStatList}
// @Failure 600 {object} api.BusinessError
// @Router /stats/layer1-tx [get]
func listTxStatsHandler(c *gin.Context) {
api.Wrap(listTxStat)(c)
}

// listDataStatsHandler godoc
//
// @Summary Data storage statistics
// @Description Query data storage statistics, including incremental and full data, and support querying at hourly or daily time intervals
// @Tags statistic
// @Accept json
// @Produce json
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
// @Param limit query int false "The number of records displayed on the page" minimum(1) maximum(2000) default(10)
// @Param minTimestamp query int false "Timestamp in seconds"
// @Param maxTimestamp query int false "Timestamp in seconds"
// @Param intervalType query string false "Statistics interval" Enums(hour, day) default(day)
// @Param sort query string false "Sort by timestamp" Enums(asc, desc) default(desc)
// @Success 200 {object} api.BusinessError{Data=DataStatList}
// @Failure 600 {object} api.BusinessError
// @Router /stats/storage [get]
func listDataStatsHandler(c *gin.Context) {
api.Wrap(listDataStat)(c)
}

// listFeeStatsHandler godoc
//
// @Summary Storage fee statistics
// @Description Query fee statistics, including incremental and full data, and support querying at hourly or daily time intervals
// @Tags statistic
// @Accept json
// @Produce json
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
// @Param limit query int false "The number of records displayed on the page" minimum(1) maximum(2000) default(10)
// @Param minTimestamp query int false "Timestamp in seconds"
// @Param maxTimestamp query int false "Timestamp in seconds"
// @Param intervalType query string false "Statistics interval" Enums(hour, day) default(day)
// @Param sort query string false "Sort by timestamp" Enums(asc, desc) default(desc)
// @Success 200 {object} api.BusinessError{Data=FeeStatList}
// @Failure 600 {object} api.BusinessError
// @Router /stats/fee [get]
func listFeeStatsHandler(c *gin.Context) {
api.Wrap(listFeeStat)(c)
}

// listTxsHandler godoc
//
// @Summary Storage transaction list
// @Description Query storage transactions, support address and root hash filter
// @Tags transaction
// @Accept json
// @Produce json
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
// @Param limit query int false "The number of records displayed on the page" minimum(1) maximum(100) default(10)
// @Success 200 {object} api.BusinessError{Data=StorageTxList}
// @Failure 600 {object} api.BusinessError
// @Router /txs [get]
func listTxsHandler(c *gin.Context) {
api.Wrap(listStorageTx)(c)
}

// getTxHandler godoc
//
// @Summary Storage transaction information
// @Description Query storage transaction by txSeq
// @Tags transaction
// @Accept json
// @Produce json
// @Param txSeq path string true "storage transaction sequence number"
// @Success 200 {object} api.BusinessError{Data=StorageTxDetail}
// @Failure 600 {object} api.BusinessError
// @Router /txs/{txSeq} [get]
func getTxHandler(c *gin.Context) {
api.Wrap(getStorageTx)(c)
}

// listAddressTxsHandler godoc
//
// @Summary Account's storage transaction list
// @Description Query storage transactions for specified account, support root hash filter
// @Tags account
// @Accept json
// @Produce json
// @Param address path string false "The submitter address of the uploaded file"
// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0)
// @Param limit query int false "The number of records displayed on the page" minimum(1) maximum(100) default(10)
// @Param rootHash query string false "The merkle root hash of the uploaded file"
// @Success 200 {object} api.BusinessError{Data=StorageTxList}
// @Failure 600 {object} api.BusinessError
// @Router /accounts/{address}/txs [get]
func listAddressTxsHandler(c *gin.Context) {
api.Wrap(listAddressStorageTx)(c)
}
40 changes: 37 additions & 3 deletions api/stat_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func getSubmitStatByType(c *gin.Context, t Type) (interface{}, error) {
list := make([]FeeStat, 0)
for _, r := range records {
list = append(list, FeeStat{
StatTime: r.StatTime,
BaseFee: r.BaseFee,
BaseFeeTotal: r.BaseFeeTotal,
StatTime: r.StatTime,
StorageFee: r.BaseFee,
StorageFeeTotal: r.BaseFeeTotal,
})
}
result["list"] = list
Expand All @@ -117,3 +117,37 @@ func getSubmitStatByType(c *gin.Context, t Type) (interface{}, error) {

return result, nil
}

func summary(_ *gin.Context) (interface{}, error) {
value, exist, err := db.ConfigStore.Get(store.KeyLogSyncInfo)
if err != nil {
return nil, commonApi.ErrInternal(err)
}
if !exist {
return nil, ErrConfigNotFound
}

var logSyncInfo stat.LogSyncInfo
if err := json.Unmarshal([]byte(value), &logSyncInfo); err != nil {
return nil, commonApi.ErrInternal(err)
}

submitStat, err := db.SubmitStatStore.LastByType(store.Day)
if err != nil {
return nil, commonApi.ErrInternal(err)
}
if submitStat == nil {
return nil, ErrStorageBaseFeeNotStat
}

storageFee := StorageFeeStat{
TokenInfo: *chargeToken,
StorageFeeTotal: submitStat.BaseFeeTotal,
}
result := Summary{
StorageFeeStat: storageFee,
LogSyncInfo: logSyncInfo,
}

return result, nil
}
Loading

0 comments on commit ef2ff72

Please sign in to comment.