Skip to content

Commit

Permalink
Aggregated rewards; fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
grkamil committed Oct 1, 2019
1 parent 3262603 commit b81b23c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 23 deletions.
6 changes: 3 additions & 3 deletions address/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type Resource struct {
Balances []resource.Interface `json:"balances"`
TotalBalanceSum *string `json:"total_balance_sum,omitempty"`
TotalBalanceSumUSD *string `json:"total_balance_sum_usd,omitempty"`
AvailableBalanceSum *string `json:"available_balance_sum,omitempty"` // TODO: remove field
AvailableBalanceSum *string `json:"available_balance_sum,omitempty"` // TODO: remove field
AvailableBalanceSumUSD *string `json:"available_balance_sum_usd,omitempty"` // TODO: remove field
}

type Params struct {
TotalBalanceSum *big.Float
TotalBalanceSumUSD *big.Float
TotalBalanceSum *big.Float
TotalBalanceSumUSD *big.Float
}

func (r Resource) Transform(model resource.ItemInterface, resourceParams ...resource.ParamInterface) resource.Interface {
Expand Down
14 changes: 7 additions & 7 deletions aggregated_reward/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package aggregated_reward
import "github.com/go-pg/pg/orm"

type SelectFilter struct {
Address string
StartBlock *string
EndBlock *string
Address string
StartTime *string
EndTime *string
}

func (f SelectFilter) Filter(q *orm.Query) (*orm.Query, error) {
if f.StartBlock != nil {
q = q.Where("from_block_id >= ?", f.StartBlock)
if f.StartTime != nil {
q = q.Where("time_id >= ?", f.StartTime)
}

if f.EndBlock != nil {
q = q.Where("to_block_id <= ?", f.EndBlock)
if f.EndTime != nil {
q = q.Where("time_id <= ?", f.EndTime)
}

return q.Where("address.address = ?", f.Address), nil
Expand Down
7 changes: 3 additions & 4 deletions aggregated_reward/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"github.com/MinterTeam/minter-explorer-api/resource"
validatorMeta "github.com/MinterTeam/minter-explorer-api/validator/meta"
"github.com/MinterTeam/minter-explorer-tools/models"
"time"
)

type Resource struct {
FromBlockID uint64 `json:"from_block_id"`
ToBlockID uint64 `json:"to_block_id"`
TimeID string `json:"time_id"`
Role string `json:"role"`
Amount string `json:"amount"`
Address string `json:"address"`
Expand All @@ -21,8 +21,7 @@ func (Resource) Transform(model resource.ItemInterface, params ...resource.Param
reward := model.(models.AggregatedReward)

return Resource{
FromBlockID: reward.FromBlockID,
ToBlockID: reward.ToBlockID,
TimeID: reward.TimeID.Format(time.RFC3339),
Role: reward.Role,
Amount: helpers.PipStr2Bip(reward.Amount),
Address: reward.Address.GetAddress(),
Expand Down
17 changes: 11 additions & 6 deletions api/v1/addresses/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ type StatisticsQueryRequest struct {
EndTime *string `form:"endTime" binding:"omitempty,timestamp"`
}

type AggregatedRewardsQueryRequest struct {
StartTime *string `form:"startTime" binding:"omitempty,timestamp"`
EndTime *string `form:"endTime" binding:"omitempty,timestamp"`
}

// Get list of addresses
func GetAddresses(c *gin.Context) {
explorer := c.MustGet("explorer").(*core.Explorer)
Expand Down Expand Up @@ -116,8 +121,8 @@ func GetAddress(c *gin.Context) {

c.JSON(http.StatusOK, gin.H{
"data": new(address.Resource).Transform(*model, address.Params{
TotalBalanceSum: totalBalanceSum,
TotalBalanceSumUSD: totalBalanceSumUSD,
TotalBalanceSum: totalBalanceSum,
TotalBalanceSumUSD: totalBalanceSumUSD,
}),
})

Expand Down Expand Up @@ -182,7 +187,7 @@ func GetAggregatedRewards(c *gin.Context) {
return
}

var requestQuery FilterQueryRequest
var requestQuery AggregatedRewardsQueryRequest
if err := c.ShouldBindQuery(&requestQuery); err != nil {
errors.SetValidationErrorResponse(err, c)
return
Expand All @@ -191,9 +196,9 @@ func GetAggregatedRewards(c *gin.Context) {
// fetch data
pagination := tools.NewPagination(c.Request)
rewards := explorer.RewardRepository.GetPaginatedAggregatedByAddress(aggregated_reward.SelectFilter{
Address: *minterAddress,
StartBlock: requestQuery.StartBlock,
EndBlock: requestQuery.EndBlock,
Address: *minterAddress,
StartTime: requestQuery.StartTime,
EndTime: requestQuery.EndTime,
}, &pagination)

c.JSON(http.StatusOK, resource.TransformPaginatedCollection(rewards, aggregated_reward.Resource{}, pagination))
Expand Down
2 changes: 1 addition & 1 deletion api/v1/addresses/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ func ApplyRoutes(r *gin.RouterGroup) {
addresses.GET("/:address/events/slashes", GetSlashes)
addresses.GET("/:address/delegations", GetDelegations)
addresses.GET("/:address/statistics/rewards", GetRewardsStatistics)
//addresses.GET("/:address/events/rewards/aggregated", GetAggregatedRewards)
addresses.GET("/:address/events/rewards/aggregated", GetAggregatedRewards)
}
}
2 changes: 1 addition & 1 deletion balance/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ func (s *Service) GetTotalBalance(address *models.Address) *big.Float {

func (s *Service) GetTotalBalanceInUSD(sumInBasecoin *big.Float) *big.Float {
return new(big.Float).Mul(sumInBasecoin, big.NewFloat(s.marketService.PriceChange.Price))
}
}
2 changes: 1 addition & 1 deletion reward/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (repository Repository) GetPaginatedAggregatedByAddress(filter aggregated_r
Column("Address.address", "Validator").
Apply(filter.Filter).
Apply(pagination.Filter).
Order("from_block_id DESC").
Order("time_id DESC").
Order("amount").
SelectAndCount()

Expand Down

0 comments on commit b81b23c

Please sign in to comment.