github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/types/denom_ibc.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // DefaultIbcWei for ibc transfer default fibo min unit
     8  const DefaultIbcWei = "wei"
     9  const DefaultDecInt = 1000000000000000000
    10  const DefaultDecStr = "1000000000000000000"
    11  
    12  // baseDenom is the denom of smallest unit registered
    13  var baseDenom string
    14  
    15  // GetBaseDenom returns the denom of smallest unit registered
    16  func GetBaseDenom() (string, error) {
    17  	if baseDenom == "" {
    18  		return "", fmt.Errorf("no denom is registered")
    19  	}
    20  	return baseDenom, nil
    21  }
    22  
    23  // ConvertDecCoin attempts to convert a decimal coin to a given denomination. If the given
    24  // denomination is invalid or if neither denomination is registered, an error
    25  // is returned.
    26  func ConvertDecCoin(coin DecCoin, denom string) (DecCoin, error) {
    27  	if err := ValidateDenom(denom); err != nil {
    28  		return DecCoin{}, err
    29  	}
    30  
    31  	srcUnit, ok := GetDenomUnit(coin.Denom)
    32  	if !ok {
    33  		return DecCoin{}, fmt.Errorf("source denom not registered: %s", coin.Denom)
    34  	}
    35  
    36  	dstUnit, ok := GetDenomUnit(denom)
    37  	if !ok {
    38  		return DecCoin{}, fmt.Errorf("destination denom not registered: %s", denom)
    39  	}
    40  
    41  	if srcUnit.Equal(dstUnit) {
    42  		return NewDecCoinFromDec(denom, coin.Amount), nil
    43  	}
    44  
    45  	return NewDecCoinFromDec(denom, coin.Amount.Mul(srcUnit).Quo(dstUnit)), nil
    46  }
    47  
    48  // NormalizeCoin try to convert a coin to the smallest unit registered,
    49  // returns original one if failed.
    50  func NormalizeCoin(coin Coin) Coin {
    51  	base, err := GetBaseDenom()
    52  	if err != nil {
    53  		return coin
    54  	}
    55  	newCoin, err := ConvertCoin(coin, base)
    56  	if err != nil {
    57  		return coin
    58  	}
    59  	return newCoin
    60  }
    61  
    62  // NormalizeDecCoin try to convert a decimal coin to the smallest unit registered,
    63  // returns original one if failed.
    64  func NormalizeDecCoin(coin DecCoin) DecCoin {
    65  	base, err := GetBaseDenom()
    66  	if err != nil {
    67  		return coin
    68  	}
    69  	newCoin, err := ConvertDecCoin(coin, base)
    70  	if err != nil {
    71  		return coin
    72  	}
    73  	return newCoin
    74  }
    75  
    76  // NormalizeCoins normalize and truncate a list of decimal coins
    77  func NormalizeCoins(coins []DecCoin) Coins {
    78  	if coins == nil {
    79  		return nil
    80  	}
    81  	result := make([]Coin, 0, len(coins))
    82  
    83  	for _, coin := range coins {
    84  		newCoin, _ := NormalizeDecCoin(coin).TruncateDecimal()
    85  		result = append(result, newCoin)
    86  	}
    87  
    88  	return result
    89  }