2024-12-20 17:41:24 +08:00

165 lines
4.2 KiB
Go

package mt
import (
"f5"
"fmt"
"jccommon"
"main/constant"
"q5"
"strings"
)
type Currency struct {
currencyName string
contractName string
currencyDecimal int64
contract *Contract
}
type CurrencyTable struct {
f5.CustomMetaTable
netIdHash *q5.ConcurrentMap[int32, *q5.ConcurrentMap[string, *Currency]]
}
func (this *Currency) init(currencyName string, contractName string, currencyDecimal int64) {
this.currencyName = currencyName
this.contractName = contractName
this.currencyDecimal = currencyDecimal
}
func (this *Currency) GetCurrencyName() string {
return this.currencyName
}
func (this *Currency) GetContractName() string {
return this.contractName
}
func (this *Currency) GetCurrencyDecimal() int64 {
return this.currencyDecimal
}
func (this *Currency) GetContract() *Contract {
return this.contract
}
func (this *Currency) check(netId int32) {
if this.GetCurrencyDecimal() <= 0 {
panic("Currency currency_decimal <= 0")
return
}
if netId == 56 || netId == 97 {
if this.GetCurrencyDecimal() != constant.BNB_RECHARGE_CURRENCY_MAX_DECIMAL {
panic("Currency exchange_rate > uplimit")
return
}
} else {
if this.GetCurrencyDecimal() != constant.RECHARGE_CURRENCY_MAX_DECIMAL {
panic("Currency exchange_rate > uplimit")
return
}
}
}
func (this *CurrencyTable) GetByNetId(netId int32) *q5.ConcurrentMap[string, *Currency] {
if v, ok := this.netIdHash.Load(netId); ok {
return *v
} else {
return nil
}
}
func (this *CurrencyTable) GetByNetIdAddress(netId int32, currencyAddress string) *Currency {
currencysMeta := this.GetByNetId(netId)
if currencysMeta == nil {
return nil
}
var result *Currency
currencyAddress = strings.ToLower(currencyAddress)
currencysMeta.Range(
func(key string, meta *Currency) bool {
if meta.GetContract().GetAddress() == currencyAddress {
result = meta
return false
}
return true
})
return result
}
func (this *CurrencyTable) Load() {
}
func (this *CurrencyTable) PostInit1() {
}
func (this *CurrencyTable) verifyCurrencyAddress(netId int32, name string, address string) {
switch netId {
case jccommon.ETH_NET_ID:
{
if name == "BEUSDC" {
if strings.ToLower(address) != strings.ToLower(jccommon.ETH_USDC_ADDRESS) {
panic(fmt.Sprintf("verifyCurrencyAddress error net:%d name:%s address:%s",
netId, name, address))
}
} else if name == "BEUSDT" {
if strings.ToLower(address) != strings.ToLower(jccommon.ETH_USDT_ADDRESS) {
panic(fmt.Sprintf("verifyCurrencyAddress error net:%d name:%s address:%s",
netId, name, address))
}
} else {
panic(fmt.Sprintf("verifyCurrencyAddress error net:%d name:%s address:%s",
netId, name, address))
}
}
case jccommon.IMTBL_NET_ID:
{
if name == "BEUSDC" {
if strings.ToLower(address) != strings.ToLower(jccommon.IMTBL_USDC_ADDRESS) {
panic(fmt.Sprintf("verifyCurrencyAddress error net:%d name:%s address:%s",
netId, name, address))
}
} else {
panic(fmt.Sprintf("verifyCurrencyAddress error net:%d name:%s address:%s",
netId, name, address))
}
}
case jccommon.ARB_NET_ID:
{
if name == "BEUSDC" {
if strings.ToLower(address) != strings.ToLower(jccommon.ARB_USDC_ADDRESS) {
panic(fmt.Sprintf("verifyCurrencyAddress error net:%d name:%s address:%s",
netId, name, address))
}
} else if name == "BEUSDT" {
if strings.ToLower(address) != strings.ToLower(jccommon.ARB_USDT_ADDRESS) {
panic(fmt.Sprintf("verifyCurrencyAddress error net:%d name:%s address:%s",
netId, name, address))
}
} else {
panic(fmt.Sprintf("verifyCurrencyAddress error net:%d name:%s address:%s",
netId, name, address))
}
}
case jccommon.BNB_NET_ID:
{
if name == "BEUSDC" {
if strings.ToLower(address) != strings.ToLower(jccommon.BNB_USDC_ADDRESS) {
panic(fmt.Sprintf("verifyCurrencyAddress error net:%d name:%s address:%s",
netId, name, address))
}
} else if name == "BEUSDT" {
if strings.ToLower(address) != strings.ToLower(jccommon.BNB_USDT_ADDRESS) {
panic(fmt.Sprintf("verifyCurrencyAddress error net:%d name:%s address:%s",
netId, name, address))
}
} else {
panic(fmt.Sprintf("verifyCurrencyAddress error net:%d name:%s address:%s",
netId, name, address))
}
}
}
}