mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 10:05:18 +00:00
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
|
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))
|
||
|
}
|
||
|
|
||
|
func MakeCoinForAuxiliaryDenom(amount any) sdk.Coin {
|
||
|
return makeCoin(AuxiliaryDenom, toBigInt(amount))
|
||
|
}
|
||
|
|
||
|
func MakeCoinForBaseDenom(amount any) sdk.Coin {
|
||
|
return makeCoin(BaseDenom, toBigInt(amount))
|
||
|
}
|
||
|
|
||
|
func makeCoin(denom string, amount *big.Int) sdk.Coin {
|
||
|
return sdk.NewCoin(denom, math.NewIntFromBigInt(amount))
|
||
|
}
|