Skip to content

Commit

Permalink
add balance sum to /addreses endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
grkamil committed Aug 28, 2019
1 parent 631bfb8 commit 9639fa1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
26 changes: 19 additions & 7 deletions api/v1/addresses/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type GetAddressRequestQuery struct {

type GetAddressesRequest struct {
Addresses []string `form:"addresses[]" binding:"required,minterAddress,max=50"`
WithSum bool `form:"withSum"`
}

// TODO: replace string to int
Expand Down Expand Up @@ -77,6 +78,18 @@ func GetAddresses(c *gin.Context) {
}
}

// calculate overall address balance in base coin and fiat
if request.WithSum {
c.JSON(http.StatusOK, gin.H{
"data": resource.TransformCollectionWithCallback(addresses, address.Resource{}, func(model interface{}) resource.ParamsInterface {
baseCoin, usd := explorer.BalanceService.GetSumByAddress(model.(models.Address))
return resource.ParamsInterface{baseCoin, usd}
}),
})

return
}

c.JSON(http.StatusOK, gin.H{
"data": resource.TransformCollection(addresses, address.Resource{}),
})
Expand All @@ -102,18 +115,17 @@ func GetAddress(c *gin.Context) {
// fetch address
model := explorer.AddressRepository.GetByAddress(*minterAddress)

// calculate overall address balance in base coin and fiat
var balanceSumInBaseCoin, balanceSumInUSD *big.Float
if request.WithSum {
balanceSumInBaseCoin = explorer.BalanceService.GetBalanceSumByAddress(model)
balanceSumInUSD = explorer.BalanceService.GetBalanceSumInUSDByBaseCoin(balanceSumInBaseCoin)
}

// if no models found
if model == nil {
model = makeEmptyAddressModel(*minterAddress, explorer.Environment.BaseCoin)
}

// calculate overall address balance in base coin and fiat
var balanceSumInBaseCoin, balanceSumInUSD *big.Float
if request.WithSum {
balanceSumInBaseCoin, balanceSumInUSD = explorer.BalanceService.GetSumByAddress(*model)
}

c.JSON(http.StatusOK, gin.H{
"data": new(address.Resource).Transform(*model, balanceSumInBaseCoin, balanceSumInUSD),
})
Expand Down
11 changes: 4 additions & 7 deletions balance/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ func NewService(baseCoin string, marketService *market.Service) *Service {
return &Service{baseCoin, marketService}
}

func (s *Service) GetBalanceSumByAddress(address *models.Address) *big.Float {
if address == nil {
return big.NewFloat(0)
}

func (s *Service) GetSumByAddress(address models.Address) (*big.Float, *big.Float) {
sum := big.NewInt(0)
for _, balance := range address.Balances {
if balance.Coin.Symbol == s.baseCoin {
Expand All @@ -37,9 +33,10 @@ func (s *Service) GetBalanceSumByAddress(address *models.Address) *big.Float {
))
}

return new(big.Float).SetInt(sum)
sumInBaseCoin := new(big.Float).SetInt(sum)
return sumInBaseCoin, s.getBalanceSumInUSDByBaseCoin(sumInBaseCoin)
}

func (s *Service) GetBalanceSumInUSDByBaseCoin(sumInBasecoin *big.Float) *big.Float {
func (s *Service) getBalanceSumInUSDByBaseCoin(sumInBasecoin *big.Float) *big.Float {
return new(big.Float).Mul(sumInBasecoin, big.NewFloat(s.marketService.PriceChange.Price))
}
29 changes: 23 additions & 6 deletions resource/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,33 @@ import (

type ItemInterface interface{}

type ParamsInterface []interface{}

type Interface interface {
Transform(model ItemInterface, params ...interface{}) Interface
}

func TransformCollection(collection interface{}, resource Interface) []Interface {
models := makeItemsFromModelsCollection(collection)
result := make([]Interface, len(models))
for i := range models {
result[i] = resource.Transform(models[i])
}

return result
}

func TransformCollectionWithCallback(collection interface{}, resource Interface, callbackFunc func(values interface{}) ParamsInterface) []Interface {
models := makeItemsFromModelsCollection(collection)
result := make([]Interface, len(models))
for i := range models {
result[i] = resource.Transform(models[i], callbackFunc(models[i])...)
}

return result
}

func makeItemsFromModelsCollection(collection interface{}) []ItemInterface {
val := reflect.ValueOf(collection)

models := make([]ItemInterface, val.Len())
Expand All @@ -22,10 +44,5 @@ func TransformCollection(collection interface{}, resource Interface) []Interface
}
}

result := make([]Interface, len(models))
for i := range models {
result[i] = resource.Transform(models[i])
}

return result
return models
}

0 comments on commit 9639fa1

Please sign in to comment.