2024-08-03 14:37:54 +08:00

130 lines
3.2 KiB
Go

package service
import (
"q5"
"f5"
"time"
"jccommon"
"math/big"
"main/mt"
"strings"
)
const DECIMALS = 10000
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]
lastRefreshOkTime int64
}
func (this *bcCurrency) init() () {
this.nameHash = new(q5.ConcurrentMap[string, int64])
this.addressHash = new(q5.ConcurrentMap[string, int64])
mt.Table.BcCurrency.Traverse(
func (e *mt.BcCurrency) bool {
this.nameHash.Store(e.GetName(), int64(e.GetCurrentPrice() * DECIMALS))
if e.GetContractAddress() != "" {
this.addressHash.Store(strings.ToLower(e.GetContractAddress()),
int64(e.GetCurrentPrice() * DECIMALS))
}
return true
})
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"
}
//bnDecimal := big.NewInt(DECIMALS)
bnDecimalEx := big.NewInt(100000000000000000)
{
usdcMeta := mt.Table.BcCurrency.GetByName(jccommon.BC_CURRENCY_NAME_USDC)
if usdcMeta != nil && strings.ToLower(usdcMeta.GetContractAddress()) == contractAddress {
bnDecimalEx = big.NewInt(100000)
}
}
//if itemType == jccommon.BC_CURRENCY_NAME_USDC {
//bnDecimalEx = big.NewInt(100000)
//}
bnPrice := big.NewInt(0)
bnRate := big.NewInt(rate)
bnPrice.Mul(bnAmount, bnRate)
//bnPrice.Mul(bnPrice, bnDecimal)
bnPrice.Div(bnPrice, bnDecimalEx)
return rate, bnPrice.String()
}
func (this *bcCurrency) refreshExchangeRate() () {
for true {
f5.GetSysLog().Info("bcCurrency refreshPrice1")
rspObj := []vsCurrency{}
f5.GetHttpCliMgr().SendGoStyleRequest(
jccommon.BC_CURRENCY_VS_URL,
map[string]string{},
func (rsp f5.HttpCliResponse) {
if rsp.GetErr() != nil {
f5.GetSysLog().Info("bcCurrency refreshPrice error%s", rsp.GetErr())
return
}
if q5.DecodeJson(rsp.GetRawData(), &rspObj) == nil {
this.updatePrice(rspObj)
}
})
f5.GetSysLog().Info("bcCurrency refreshPrice2")
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 * DECIMALS))
address := this.getAddressByName(val.Name)
if address != "" {
this.addressHash.Store(address, int64(val.CurrentPrice * DECIMALS))
}
}
if len(l) > 0 {
this.lastRefreshOkTime = f5.GetApp().GetRealSeconds()
}
f5.GetSysLog().Info("bcCurrency updatePrice %s", q5.EncodeJson(l))
}
func (this *bcCurrency) getAddressByName(name string) string {
meta := mt.Table.BcCurrency.GetByName(name)
if meta != nil {
return strings.ToLower(meta.GetContractAddress())
} else {
return ""
}
}
func (this *bcCurrency) GetLastRefreshOkTime() int64 {
if f5.IsOnlineEnv() {
return this.lastRefreshOkTime
} else {
return f5.GetApp().GetRealSeconds()
}
}