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 #346 from bianjieai/develop
Browse files Browse the repository at this point in the history
v0.15.1-20230523
  • Loading branch information
kaifei Hu authored May 23, 2023
2 parents d355b60 + 0164010 commit 64ce5c9
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 16 deletions.
1 change: 1 addition & 0 deletions configs/cfg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ db = 0

[spi]
coingecko_price_url = "https://api.coingecko.com/api/v3/simple/price"
ccdata_price_url = "https://min-api.cryptocompare.com/data/pricemulti?api_key=f58cdd6f5d9cf0f6c0f44a1036924e2b17391ef3aea75e2be5f6133ff4c1ed84"

[task]
cron_time_statistic_task = 5
Expand Down
1 change: 1 addition & 0 deletions internal/app/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type Task struct {

type Spi struct {
CoingeckoPriceUrl string `mapstructure:"coingecko_price_url"`
CcDataPriceUrl string `mapstructure:"ccdata_price_url"`
}

type ChainConfig struct {
Expand Down
3 changes: 2 additions & 1 deletion internal/app/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ const (
SECP256K1 = "secp256k1"
ICS20 = "ics20"

ChainFlowTrendDays = 365
ChainFlowTrendDays = 365
ChainFlowVolumeDays = 10000

ExportTxsNum = 1000
)
Expand Down
21 changes: 21 additions & 0 deletions internal/app/task/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package task

import (
"encoding/json"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -247,6 +248,26 @@ func parseRecvPacketTxEvents(msgIndex int, tx *entity.Tx) (dcConnection, packetA
}
}
}

if evt.Type == "uptick.erc20.v1.EventIBCERC20" {
var message string
var status string
for _, attr := range evt.Attributes {
switch attr.Key {
case "status":
status = attr.Value
existPacketAck = true
case "message":
message = attr.Value
default:
}
}
if status == "\"STATUS_SUCCESS\"" {
packetAck = status
} else {
packetAck = fmt.Sprintf("error:\"%s\" ", message)
}
}
}
}

Expand Down
86 changes: 85 additions & 1 deletion internal/app/task/denom_heatmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (t *DenomHeatmapTask) coinPriceHandler() (map[string]float64, error) {
bz, err := utils.HttpGet(url)
if err != nil {
logrus.Errorf("task %s get coin price error, %v", t.Name(), err)
return nil, err
return t.coinPriceHandlerBackup()
}

var priceResp map[string]map[string]float64
Expand Down Expand Up @@ -148,6 +148,90 @@ func (t *DenomHeatmapTask) coinPriceHandler() (map[string]float64, error) {
return priceFloatMap, nil
}

func (t *DenomHeatmapTask) coinPriceHandlerBackup() (map[string]float64, error) {
var symbols []string
coinIdSymbolMap := make(map[string]string)
for _, v := range t.authDenomList {
if v.CoinId != "" {
symbols = append(symbols, v.Symbol)
coinIdSymbolMap[v.CoinId] = v.Symbol
}
}

if len(symbols) == 0 {
return nil, fmt.Errorf("no symbol")
}

getPriceFunc := func(fsyms string) (map[string]map[string]float64, error) {
url := fmt.Sprintf("%s&fsyms=%s&tsyms=USD", global.Config.Spi.CcDataPriceUrl, fsyms)
bz, err := utils.HttpGet(url)
if err != nil {
logrus.Errorf("task %s get coin price from ccdata error, %v", t.Name(), err)
return nil, err
}
var symbolPriceResp map[string]map[string]float64
err = json.Unmarshal(bz, &symbolPriceResp)
if err != nil {
logrus.Errorf("task %s get coin ccdata price error, %v", t.Name(), err)
return nil, err
}
return symbolPriceResp, nil
}
// fsyms param maxlength is 300, so we need request in two steps
segment1Resp, err := getPriceFunc(strings.Join(symbols[:len(symbols)/2], ","))
if err != nil {
return nil, err
}
segment2Resp, err := getPriceFunc(strings.Join(symbols[len(symbols)/2:], ","))
if err != nil {
return nil, err
}
symbolPriceResp := make(map[string]map[string]float64)
for k, v := range segment1Resp {
symbolPriceResp[k] = v
}
for k, v := range segment2Resp {
symbolPriceResp[k] = v
}

cachePriceMap, err := tokenPriceRepo.GetAll()
if err != nil {
logrus.Errorf("task %s get coin price from cache error, %v", t.Name(), err)
return nil, err
}
priceMap := make(map[string]string, len(cachePriceMap))
for k, v := range cachePriceMap {
if symbol, ok := coinIdSymbolMap[k]; ok {
if price, exists := symbolPriceResp[symbol]; exists {
cachePriceMap[k] = price["USD"]

result := strconv.FormatFloat(price["USD"], 'f', 12, 64)
for strings.HasSuffix(result, "0") {
result = strings.TrimSuffix(result, "0")
}
if strings.HasSuffix(result, ".") {
result = strings.TrimSuffix(result, ".")
}
priceMap[k] = result
continue
}
}
result := strconv.FormatFloat(v, 'f', 12, 64)
for strings.HasSuffix(result, "0") {
result = strings.TrimSuffix(result, "0")
}
if strings.HasSuffix(result, ".") {
result = strings.TrimSuffix(result, ".")
}
priceMap[k] = result
}

if err = tokenPriceRepo.BatchSet(priceMap); err != nil {
logrus.Errorf("task %s set coin price cache error, %v", t.Name(), err)
}
return cachePriceMap, nil
}

// supplyHandler Get supply of denoms, then save supply info to cache
func (t *DenomHeatmapTask) supplyHandler() {
wg := sync.WaitGroup{}
Expand Down
26 changes: 19 additions & 7 deletions internal/app/task/ibc_chain_inflow_statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ func (t *ChainInflowStatisticsTask) setStatisticsDataCache() {
startTime, _ := lastNDaysZeroTimeUnix(days)
_, endTime := todayUnix()

allDays := constant.ChainFlowVolumeDays
allStartTime, _ := lastNDaysZeroTimeUnix(allDays)

chainInfosMap, err := getAllChainInfosMap()
if err != nil {
logrus.Errorf("task %s getAllChainInfosMap err, %v", t.Name(), err)
Expand All @@ -217,24 +220,28 @@ func (t *ChainInflowStatisticsTask) setStatisticsDataCache() {

priceMap := cache.TokenPriceMap()
for chain, _ := range chainInfosMap {
trendList, err := chainInflowStatisticsRepo.AggrTrend(chain, startTime, endTime)
trendList, err := chainInflowStatisticsRepo.AggrTrend(chain, allStartTime, endTime)
if err != nil {
logrus.Errorf("task %s AggrTrend %s err, %v", t.Name(), chain, err)
continue
}

volumeMap := make(map[string]decimal.Decimal, len(trendList))
totalDenomValue := decimal.Zero
allDaysTotalDenomValue := decimal.Zero
for _, v := range trendList {
denomAmount := decimal.NewFromFloat(v.DenomAmount)
denomValue := ibctool.CalculateDenomValue(priceMap, v.BaseDenom, v.BaseDenomChain, denomAmount)
dt := time.Unix(v.SegmentStartTime, 0).Format(constant.DateFormat)
if vol, ok := volumeMap[dt]; ok {
volumeMap[dt] = vol.Add(denomValue)
} else {
volumeMap[dt] = denomValue
if v.SegmentStartTime >= startTime {
dt := time.Unix(v.SegmentStartTime, 0).Format(constant.DateFormat)
if vol, ok := volumeMap[dt]; ok {
volumeMap[dt] = vol.Add(denomValue)
} else {
volumeMap[dt] = denomValue
}
totalDenomValue = totalDenomValue.Add(denomValue)
}
totalDenomValue = totalDenomValue.Add(denomValue)
allDaysTotalDenomValue = allDaysTotalDenomValue.Add(denomValue)
}

volumeItemList := make([]vo.VolumeItem, 0, len(volumeMap))
Expand All @@ -252,8 +259,13 @@ func (t *ChainInflowStatisticsTask) setStatisticsDataCache() {
if err = chainFlowCacheRepo.SetInflowVolume(days, chain, totalDenomValue.String()); err != nil {
logrus.Errorf("task %s SetInflowVolume %s err, %v", t.Name(), chain, err)
}

if err = chainFlowCacheRepo.SetInflowVolume(allDays, chain, allDaysTotalDenomValue.String()); err != nil {
logrus.Errorf("task %s SetInflowVolume all %s err, %v", t.Name(), chain, err)
}
}

chainFlowCacheRepo.ExpireInflowTrend(days, OneWeek*time.Second)
chainFlowCacheRepo.ExpireInflowVolume(days, OneWeek*time.Second)
chainFlowCacheRepo.ExpireInflowVolume(allDays, OneWeek*time.Second)
}
26 changes: 19 additions & 7 deletions internal/app/task/ibc_chain_outflow_statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ func (t *ChainOutflowStatisticsTask) setStatisticsDataCache() {
startTime, _ := lastNDaysZeroTimeUnix(days)
_, endTime := todayUnix()

allDays := constant.ChainFlowVolumeDays
allStartTime, _ := lastNDaysZeroTimeUnix(allDays)

chainInfosMap, err := getAllChainInfosMap()
if err != nil {
logrus.Errorf("task %s getAllChainInfosMap err, %v", t.Name(), err)
Expand All @@ -216,24 +219,28 @@ func (t *ChainOutflowStatisticsTask) setStatisticsDataCache() {

priceMap := cache.TokenPriceMap()
for chain, _ := range chainInfosMap {
trendList, err := chainOutflowStatisticsRepo.AggrTrend(chain, startTime, endTime)
trendList, err := chainOutflowStatisticsRepo.AggrTrend(chain, allStartTime, endTime)
if err != nil {
logrus.Errorf("task %s AggrTrend %s err, %v", t.Name(), chain, err)
continue
}

volumeMap := make(map[string]decimal.Decimal, len(trendList))
totalDenomValue := decimal.Zero
allDaysTotalDenomValue := decimal.Zero
for _, v := range trendList {
denomAmount := decimal.NewFromFloat(v.DenomAmount)
denomValue := ibctool.CalculateDenomValue(priceMap, v.BaseDenom, v.BaseDenomChain, denomAmount)
dt := time.Unix(v.SegmentStartTime, 0).Format(constant.DateFormat)
if vol, ok := volumeMap[dt]; ok {
volumeMap[dt] = vol.Add(denomValue)
} else {
volumeMap[dt] = denomValue
if v.SegmentStartTime >= startTime {
dt := time.Unix(v.SegmentStartTime, 0).Format(constant.DateFormat)
if vol, ok := volumeMap[dt]; ok {
volumeMap[dt] = vol.Add(denomValue)
} else {
volumeMap[dt] = denomValue
}
totalDenomValue = totalDenomValue.Add(denomValue)
}
totalDenomValue = totalDenomValue.Add(denomValue)
allDaysTotalDenomValue = allDaysTotalDenomValue.Add(denomValue)
}

volumeItemList := make([]vo.VolumeItem, 0, len(volumeMap))
Expand All @@ -251,8 +258,13 @@ func (t *ChainOutflowStatisticsTask) setStatisticsDataCache() {
if err = chainFlowCacheRepo.SetOutflowVolume(days, chain, totalDenomValue.String()); err != nil {
logrus.Errorf("task %s SetOutflowVolume %s err, %v", t.Name(), chain, err)
}

if err = chainFlowCacheRepo.SetOutflowVolume(allDays, chain, allDaysTotalDenomValue.String()); err != nil {
logrus.Errorf("task %s SetOutflowVolume all %s err, %v", t.Name(), chain, err)
}
}

chainFlowCacheRepo.ExpireOutflowTrend(days, OneWeek*time.Second)
chainFlowCacheRepo.ExpireOutflowVolume(days, OneWeek*time.Second)
chainFlowCacheRepo.ExpireOutflowVolume(allDays, OneWeek*time.Second)
}
13 changes: 13 additions & 0 deletions internal/app/task/ibc_tx_relate_task_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package task

import (
"fmt"
"github.com/bianjieai/iobscan-ibc-explorer-backend/internal/app/utils"
"testing"
)
Expand All @@ -26,3 +27,15 @@ func Test_HandlerIbcTxs(t *testing.T) {
rw.handlerIbcTxs(chain, ibcTxList, denomMap)
t.Log(utils.MustMarshalJsonToStr(ibcTxList))
}

func Test_parseRecvPacketTxEvents(t *testing.T) {
txs, err := txRepo.GetTxByHashes("uptick", []string{"C476E603D7A3329FCB8486897B465815F10E3A6B70F7BC89657986409CBC3FB6"})
if err != nil {
t.Log(err.Error())
}

dcConnection, packetAck, exists := parseRecvPacketTxEvents(1, txs[0])
fmt.Println(dcConnection)
fmt.Println(packetAck)
fmt.Println(exists)
}

0 comments on commit 64ce5c9

Please sign in to comment.