From 6e17bc532122633dc9e5b04d656a0ad1e195e51c Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 27 Jun 2024 15:22:22 +0800 Subject: [PATCH] 1 --- server/backtask/mt/BcCurrency.go | 19 ++++++ server/backtask/mt/export.go | 7 ++ server/backtask/mtb/mtb.auto_gen.go | 49 ++++++++++++++ server/backtask/proto/mt.proto | 7 ++ server/backtask/service/bc_currency.go | 90 ++++++++++++++++++++++++++ server/backtask/service/export.go | 7 ++ server/jccommon/constant.go | 9 +++ third_party/f5 | 2 +- 8 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 server/backtask/mt/BcCurrency.go create mode 100644 server/backtask/service/bc_currency.go create mode 100644 server/backtask/service/export.go diff --git a/server/backtask/mt/BcCurrency.go b/server/backtask/mt/BcCurrency.go new file mode 100644 index 00000000..c1219866 --- /dev/null +++ b/server/backtask/mt/BcCurrency.go @@ -0,0 +1,19 @@ +package mt + +import ( + "q5" + "f5" + "mtb" +) + +type BcCurrency struct { + mtb.BcCurrency +} + +type BcCurrencyTable struct { + f5.IdMetaTable[BcCurrency] + itemIdHash *q5.ConcurrentMap[int64, *BcCurrency] +} + +func (this *BcCurrencyTable) PostInit1() { +} diff --git a/server/backtask/mt/export.go b/server/backtask/mt/export.go index ee0fbf3a..1b0581b5 100644 --- a/server/backtask/mt/export.go +++ b/server/backtask/mt/export.go @@ -17,6 +17,7 @@ type table struct { RankSeason *RankSeasonTable HashRateCommon *HashRateCommonTable Contract *ContractTable + BcCurrency *BcCurrencyTable } var Table = f5.New(func(this *table) { @@ -73,4 +74,10 @@ var Table = f5.New(func(this *table) { }) this.Contract = new(ContractTable) + + this.BcCurrency= f5.New(func(this *BcCurrencyTable) { + this.FileName = "../config/bc_currency.json" + this.PrimKey = "name" + }) + }) diff --git a/server/backtask/mtb/mtb.auto_gen.go b/server/backtask/mtb/mtb.auto_gen.go index 0a139ba9..927c0ad3 100644 --- a/server/backtask/mtb/mtb.auto_gen.go +++ b/server/backtask/mtb/mtb.auto_gen.go @@ -93,6 +93,16 @@ type Language struct { _flags2_ uint64 } +type BcCurrency struct { + id string + symbol string + contract_address string + current_price float64 + + _flags1_ uint64 + _flags2_ uint64 +} + type RankSeason struct { id int32 name string @@ -441,6 +451,38 @@ func (this *Language) HasEn() bool { return (this._flags1_ & (uint64(1) << 2)) > 0 } +func (this *BcCurrency) GetId() string { + return this.id +} + +func (this *BcCurrency) HasId() bool { + return (this._flags1_ & (uint64(1) << 1)) > 0 +} + +func (this *BcCurrency) GetSymbol() string { + return this.symbol +} + +func (this *BcCurrency) HasSymbol() bool { + return (this._flags1_ & (uint64(1) << 2)) > 0 +} + +func (this *BcCurrency) GetContractAddress() string { + return this.contract_address +} + +func (this *BcCurrency) HasContractAddress() bool { + return (this._flags1_ & (uint64(1) << 3)) > 0 +} + +func (this *BcCurrency) GetCurrentPrice() float64 { + return this.current_price +} + +func (this *BcCurrency) HasCurrentPrice() bool { + return (this._flags1_ & (uint64(1) << 4)) > 0 +} + func (this *RankSeason) GetId() int32 { return this.id } @@ -571,6 +613,13 @@ func (this *Language) LoadFromKv(kv map[string]interface{}) { f5.ReadMetaTableField(&this.en, "en", &this._flags1_, 2, kv) } +func (this *BcCurrency) LoadFromKv(kv map[string]interface{}) { + f5.ReadMetaTableField(&this.id, "id", &this._flags1_, 1, kv) + f5.ReadMetaTableField(&this.symbol, "symbol", &this._flags1_, 2, kv) + f5.ReadMetaTableField(&this.contract_address, "contract_address", &this._flags1_, 3, kv) + f5.ReadMetaTableField(&this.current_price, "current_price", &this._flags1_, 4, kv) +} + func (this *RankSeason) LoadFromKv(kv map[string]interface{}) { f5.ReadMetaTableField(&this.id, "id", &this._flags1_, 1, kv) f5.ReadMetaTableField(&this.name, "name", &this._flags1_, 2, kv) diff --git a/server/backtask/proto/mt.proto b/server/backtask/proto/mt.proto index aa4d9030..6a2ffd0b 100644 --- a/server/backtask/proto/mt.proto +++ b/server/backtask/proto/mt.proto @@ -75,6 +75,13 @@ message Language optional string en = 2; } +message BcCurrency +{ + optional string name = 1; + optional string contract_address = 3; + optional double current_price = 4; +} + message RankSeason { optional int32 id = 1; diff --git a/server/backtask/service/bc_currency.go b/server/backtask/service/bc_currency.go new file mode 100644 index 00000000..f656e9c7 --- /dev/null +++ b/server/backtask/service/bc_currency.go @@ -0,0 +1,90 @@ +package service + +import ( + "q5" + "f5" + "time" + "jccommon" + "math/big" +) + +type vsCurrency struct { + Name string `json:"name"` + CurrentPrice float64 `json:"current_price"` +} + +type bcCurrency struct { + nameHash *q5.ConcurrentMap[string, int64] + addressHash *q5.ConcurrentMap[string, int64] +} + +func (this *bcCurrency) init() () { + go this.refreshExchangeRate() +} + +func (this *bcCurrency) ExchangeUSD(amount string, itemType string, contractAddress string) (int64, string) { + var rate int64 + if itemType == jccommon.BC_CURRENCY_NAME_NATIVE { + if val, ok := this.nameHash.Load(itemType); ok { + rate = *val + } + } else { + if val, ok := this.addressHash.Load(contractAddress); ok { + rate = *val + } + } + bnAmount, ok := new(big.Int).SetString(amount, 10) + if !ok { + return 0, "0" + } + bnPrice := big.NewInt(0) + bnRate := big.NewInt(rate) + bnPrice.Mul(bnAmount, bnRate) + return rate, bnPrice.String() +} + +func (this *bcCurrency) refreshExchangeRate() () { + for true { + rspObj := []vsCurrency{} + f5.GetHttpCliMgr().SendGoStyleRequest( + jccommon.BC_CURRENCY_VS_URL, + map[string]string{}, + func (rsp f5.HttpCliResponse) { + if rsp.GetErr() != nil { + return + } + if q5.DecodeJson(rsp.GetRawData(), &rspObj) == nil { + this.updatePrice(rspObj) + } + }) + if f5.IsOnlineEnv() { + time.Sleep(time.Second * 60 * 10) + } else { + time.Sleep(time.Second * 30) + } + } +} + +func (this *bcCurrency) updatePrice(l []vsCurrency) { + for _, val := range l { + this.nameHash.Store(val.Name, int64(val.CurrentPrice * 10000)) + address := this.getAddressByName(val.Name) + if address != "" { + this.addressHash.Store(address, int64(val.CurrentPrice * 10000)) + } + } + f5.GetSysLog().Info("bcCurrency updatePrice %s", q5.EncodeJson(l)) +} + +func (this *bcCurrency) getAddressByName(name string) string { + switch name { + case jccommon.BC_CURRENCY_NAME_ETHEREUM: { + } + case jccommon.BC_CURRENCY_NAME_USDC: { + } + case jccommon.BC_CURRENCY_NAME_IMMUTABLE, + jccommon.BC_CURRENCY_NAME_NATIVE: { + } + } + return "" +} diff --git a/server/backtask/service/export.go b/server/backtask/service/export.go new file mode 100644 index 00000000..de6030a5 --- /dev/null +++ b/server/backtask/service/export.go @@ -0,0 +1,7 @@ +package service + +var BcCurrency = new(bcCurrency) + +func init() { + BcCurrency.init() +} diff --git a/server/jccommon/constant.go b/server/jccommon/constant.go index 0d4ee765..2ebcfaea 100644 --- a/server/jccommon/constant.go +++ b/server/jccommon/constant.go @@ -86,3 +86,12 @@ const ( const ( BC_POLY_POLY_METAKASK = 13 ) + +const ( + BC_CURRENCY_NAME_ETHEREUM = "Ethereum" + BC_CURRENCY_NAME_USDC = "USDC" + BC_CURRENCY_NAME_IMMUTABLE = "Immutable" + BC_CURRENCY_NAME_NATIVE = "NATIVE" + + BC_CURRENCY_VS_URL = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=USD&ids=immutable-x,usd-coin,immutable-x,ethereum" +) diff --git a/third_party/f5 b/third_party/f5 index 1333e005..717e9616 160000 --- a/third_party/f5 +++ b/third_party/f5 @@ -1 +1 @@ -Subproject commit 1333e005d36e591fe24defb314be362f6ad8463f +Subproject commit 717e9616ae51e87afbffb3d947a0cf81919c70d3