Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reward open api. #20

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ func RegisterRouter(router *gin.Engine) {
txsRoute.GET("", listTxsHandler)
txsRoute.GET(":txSeq", getTxHandler)

rewardsRoute := apiRoute.Group("/rewards")
rewardsRoute.GET("", listRewardsHandler)

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

router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}
Expand Down Expand Up @@ -160,7 +164,7 @@ func listFeeStatsHandler(c *gin.Context) {
// listTxsHandler godoc
//
// @Summary Storage transaction list
// @Description Query storage transactions, support address and root hash filter
// @Description Query storage transactions
// @Tags transaction
// @Accept json
// @Produce json
Expand All @@ -170,7 +174,7 @@ func listFeeStatsHandler(c *gin.Context) {
// @Failure 600 {object} api.BusinessError
// @Router /txs [get]
func listTxsHandler(c *gin.Context) {
api.Wrap(listStorageTx)(c)
api.Wrap(listStorageTxs)(c)
}

// getTxHandler godoc
Expand All @@ -188,6 +192,22 @@ func getTxHandler(c *gin.Context) {
api.Wrap(getStorageTx)(c)
}

// listRewardsHandler godoc
//
// @Summary Storage reward list
// @Description Query storage rewards
// @Tags reward
// @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=RewardList}
// @Failure 600 {object} api.BusinessError
// @Router /rewards [get]
func listRewardsHandler(c *gin.Context) {
api.Wrap(listStorageRewards)(c)
}

// listAddressTxsHandler godoc
//
// @Summary Account's storage transaction list
Expand All @@ -203,5 +223,22 @@ func getTxHandler(c *gin.Context) {
// @Failure 600 {object} api.BusinessError
// @Router /accounts/{address}/txs [get]
func listAddressTxsHandler(c *gin.Context) {
api.Wrap(listAddressStorageTx)(c)
api.Wrap(listAddressStorageTxs)(c)
}

// listAddressRewardsHandler godoc
//
// @Summary Account's storage reward list
// @Description Query storage rewards for specified account
// @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)
// @Success 200 {object} api.BusinessError{Data=RewardList}
// @Failure 600 {object} api.BusinessError
// @Router /accounts/{address}/rewards [get]
func listAddressRewardsHandler(c *gin.Context) {
api.Wrap(listAddressStorageRewards)(c)
}
104 changes: 104 additions & 0 deletions api/reward_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package api

import (
"github.com/0glabs/0g-storage-scan/store"
commonApi "github.com/Conflux-Chain/go-conflux-util/api"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

func listStorageRewards(c *gin.Context) (interface{}, error) {
var param PageParam
if err := c.ShouldBind(&param); err != nil {
return nil, err
}

total, rewards, err := listRewards(nil, param.isDesc(), param.Skip, param.Limit)
if err != nil {
return nil, err
}

return convertStorageRewards(total, rewards)
}
func listAddressStorageRewards(c *gin.Context) (interface{}, error) {
address := c.Param("address")
if address == "" {
logrus.Error("Failed to parse nil address")
return nil, errors.Errorf("Biz error, nil address %v", address)
}
addr, exist, err := db.AddressStore.Get(address)
if err != nil {
return nil, commonApi.ErrInternal(err)
}
if !exist {
return RewardList{}, nil
}
addrIDPtr := &addr.ID

var param PageParam
if err := c.ShouldBind(&param); err != nil {
return nil, err
}

total, rewards, err := listRewards(addrIDPtr, param.isDesc(), param.Skip, param.Limit)
if err != nil {
return nil, err
}

return convertStorageRewards(total, rewards)
}

func listRewards(addressID *uint64, idDesc bool, skip, limit int) (int64, []store.Reward, error) {
if addressID == nil {
return db.RewardStore.List(idDesc, skip, limit)
}

total, addrRewards, err := db.AddressRewardStore.List(addressID, idDesc, skip, limit)
if err != nil {
return 0, nil, err
}

rewards := make([]store.Reward, 0)
for _, ar := range addrRewards {
rewards = append(rewards, store.Reward{
PricingIndex: ar.PricingIndex,
MinerID: ar.MinerID,
Amount: ar.Amount,
BlockNumber: ar.BlockNumber,
BlockTime: ar.BlockTime,
TxHash: ar.TxHash,
})
}

return total, rewards, nil
}

func convertStorageRewards(total int64, rewards []store.Reward) (*RewardList, error) {
addrIDs := make([]uint64, 0)
for _, r := range rewards {
addrIDs = append(addrIDs, r.MinerID)
}
addrMap, err := db.BatchGetAddresses(addrIDs)
if err != nil {
return nil, err
}

storageRewards := make([]Reward, 0)
for _, r := range rewards {
storageReward := Reward{
RewardSeq: r.PricingIndex,
Miner: addrMap[r.MinerID].Address,
Amount: r.Amount,
BlockNumber: r.BlockNumber,
TxHash: r.TxHash,
Timestamp: r.BlockTime.Unix(),
}
storageRewards = append(storageRewards, storageReward)
}

return &RewardList{
Total: total,
List: storageRewards,
}, nil
}
Loading
Loading