0g-chain/chaincfg/coin_helper.go

58 lines
1.2 KiB
Go
Raw Normal View History

2024-05-09 11:35:16 +00:00
package chaincfg
import (
"fmt"
"math/big"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/shopspring/decimal"
)
func toBigInt(amount any) *big.Int {
if amount == nil {
return big.NewInt(0)
}
var val *big.Int
switch amount.(type) {
case int:
val = big.NewInt(int64(amount.(int)))
case int32:
val = big.NewInt(int64(amount.(int32)))
case int64:
val = big.NewInt(amount.(int64))
case string:
var ok bool
val, ok = new(big.Int).SetString(amount.(string), 0)
if !ok {
panic(fmt.Sprintf("invalid amount string: %s", amount.(string)))
}
case math.Int:
val = amount.(math.Int).BigInt()
case *big.Int:
val = amount.(*big.Int)
case float64:
val = decimal.NewFromFloat(amount.(float64)).BigInt()
default:
panic(fmt.Sprintf("invalid amount type: %T", amount))
}
return val
}
func MakeCoinForStandardDenom(amount any) sdk.Coin {
return makeCoin(StandardDenom, toBigInt(amount))
}
2024-05-10 06:31:49 +00:00
func MakeCoinForGasDenom(amount any) sdk.Coin {
return makeCoin(GasDenom, toBigInt(amount))
2024-05-09 11:35:16 +00:00
}
2024-05-10 06:31:49 +00:00
func MakeCoinForEvmDenom(amount any) sdk.Coin {
return makeCoin(EvmDenom, toBigInt(amount))
2024-05-09 11:35:16 +00:00
}
func makeCoin(denom string, amount *big.Int) sdk.Coin {
return sdk.NewCoin(denom, math.NewIntFromBigInt(amount))
}