Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #331 from coticom/openapi
Browse files Browse the repository at this point in the history
return "" replace "0"
  • Loading branch information
kaifei Hu authored Feb 20, 2023
2 parents 3418525 + 4090d01 commit eb1a2fa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
13 changes: 9 additions & 4 deletions internal/app/model/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,24 @@ func CaculateRelayerTotalValue(denomPriceMap map[string]CoinItem, relayerTxsData
return totalValue
}

func CaculateChainTotalValue(denomPriceMap map[string]CoinItem, flowStatistics []*FlowStatisticsDTO) decimal.Decimal {
func CaculateChainTotalValue(denomPriceMap map[string]CoinItem, flowStatistics []*FlowStatisticsDTO) (decimal.Decimal, bool) {
isPriceZero := false
totalValue := decimal.NewFromFloat(0)
for _, inflowInfo := range flowStatistics {
baseDenomValue := decimal.NewFromFloat(0)
decAmt := decimal.NewFromFloat(inflowInfo.DenomAmount)
if coin, ok := denomPriceMap[fmt.Sprintf("%s%s", inflowInfo.BaseDenom, inflowInfo.BaseDenomChain)]; ok {
if coin.Scale > 0 {
baseDenomValue = decAmt.Div(decimal.NewFromFloat(math.Pow10(coin.Scale))).Mul(decimal.NewFromFloat(coin.Price))
if coin.Price == 0 {
isPriceZero = true
}
baseDenomValue = decAmt.Div(decimal.NewFromFloat(math.Pow10(coin.Scale))).Mul(decimal.NewFromFloat(coin.Price))
}
totalValue = totalValue.Add(baseDenomValue)
}
return totalValue
if totalValue.IsZero() && !isPriceZero {
return totalValue, true
}
return totalValue, false
}

type BaseDenomAmountDTO struct {
Expand Down
23 changes: 19 additions & 4 deletions internal/app/service/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ func (svc *TxService) FlowInfoStatistics(chain string, startTime, endTime int64)
totalInflowTxsNum int64
totalOutflowTxsNum int64
totalTxsUSDValue string
inflowTxsUSDValue string
outflowTxsUSDValue string
inflowStatistics []*dto.FlowStatisticsDTO
outflowStatistics []*dto.FlowStatisticsDTO
)
Expand Down Expand Up @@ -517,9 +519,22 @@ func (svc *TxService) FlowInfoStatistics(chain string, startTime, endTime int64)

denomPriceMap := cache.TokenPriceMap()

inflowTotalValue := dto.CaculateChainTotalValue(denomPriceMap, inflowStatistics)
outflowTotalValue := dto.CaculateChainTotalValue(denomPriceMap, outflowStatistics)
inflowTotalValue, isNullIn := dto.CaculateChainTotalValue(denomPriceMap, inflowStatistics)
outflowTotalValue, isNullOut := dto.CaculateChainTotalValue(denomPriceMap, outflowStatistics)
totalTxsUSDValue = inflowTotalValue.Add(outflowTotalValue).String()
if inflowTotalValue.IsZero() && isNullIn {
inflowTxsUSDValue = ""
} else {
inflowTxsUSDValue = inflowTotalValue.String()
}
if outflowTotalValue.IsZero() && isNullOut {
outflowTxsUSDValue = ""
} else {
outflowTxsUSDValue = outflowTotalValue.String()
}
if inflowTxsUSDValue == "" && outflowTxsUSDValue == "" {
totalTxsUSDValue = ""
}

for _, inflowInfo := range inflowStatistics {
totalInflowTxsNum += inflowInfo.TxsCount
Expand All @@ -533,8 +548,8 @@ func (svc *TxService) FlowInfoStatistics(chain string, startTime, endTime int64)
TransferTxsNumber: totalTxsNum,
TransferTxsUSDValue: totalTxsUSDValue,
InflowTxsNumber: totalInflowTxsNum,
InflowTxsUSDValue: inflowTotalValue.String(),
InflowTxsUSDValue: inflowTxsUSDValue,
OutflowTxsNumber: totalOutflowTxsNum,
OutflowTxsUSDValue: outflowTotalValue.String(),
OutflowTxsUSDValue: outflowTxsUSDValue,
}, nil
}

0 comments on commit eb1a2fa

Please sign in to comment.