1
This commit is contained in:
parent
e018baeb18
commit
6e17bc5321
19
server/backtask/mt/BcCurrency.go
Normal file
19
server/backtask/mt/BcCurrency.go
Normal file
@ -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() {
|
||||
}
|
@ -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"
|
||||
})
|
||||
|
||||
})
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
|
90
server/backtask/service/bc_currency.go
Normal file
90
server/backtask/service/bc_currency.go
Normal file
@ -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 ""
|
||||
}
|
7
server/backtask/service/export.go
Normal file
7
server/backtask/service/export.go
Normal file
@ -0,0 +1,7 @@
|
||||
package service
|
||||
|
||||
var BcCurrency = new(bcCurrency)
|
||||
|
||||
func init() {
|
||||
BcCurrency.init()
|
||||
}
|
@ -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"
|
||||
)
|
||||
|
2
third_party/f5
vendored
2
third_party/f5
vendored
@ -1 +1 @@
|
||||
Subproject commit 1333e005d36e591fe24defb314be362f6ad8463f
|
||||
Subproject commit 717e9616ae51e87afbffb3d947a0cf81919c70d3
|
Loading…
x
Reference in New Issue
Block a user