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

     1  package types
     2  
     3  func CoinAdapterToCoin(adapter CoinAdapter) Coin {
     4  	return Coin{
     5  		Denom:  adapter.Denom,
     6  		Amount: NewDecFromBigIntWithPrec(adapter.Amount.BigInt(), Precision),
     7  	}
     8  }
     9  
    10  func CoinToCoinAdapter(adapter Coin) CoinAdapter {
    11  	return CoinAdapter{
    12  		Denom:  adapter.Denom,
    13  		Amount: NewIntFromBigInt(adapter.Amount.BigInt()),
    14  	}
    15  }
    16  
    17  func CoinAdaptersToCoins(adapters CoinAdapters) Coins {
    18  	var coins Coins = make([]Coin, 0, len(adapters))
    19  	for i, _ := range adapters {
    20  		coins = append(coins, CoinAdapterToCoin(adapters[i]))
    21  	}
    22  	return coins
    23  }
    24  
    25  func CoinsToCoinAdapters(coins Coins) CoinAdapters {
    26  	//Note:
    27  	// `var adapters CoinAdapters = make([]CoinAdapter, 0)`
    28  	// The code above if invalid.
    29  	// []CoinAdapter{} and nil are different in json format which can make different signBytes.
    30  	var adapters CoinAdapters
    31  	for i, _ := range coins {
    32  		adapters = append(adapters, CoinToCoinAdapter(coins[i]))
    33  	}
    34  	return adapters
    35  }
    36  
    37  func removeZeroCoinAdapters(coins CoinAdapters) CoinAdapters {
    38  	for i := 0; i < len(coins); i++ {
    39  		if coins[i].IsZero() {
    40  			break
    41  		} else if i == len(coins)-1 {
    42  			return coins
    43  		}
    44  	}
    45  	var result []CoinAdapter
    46  	if len(coins) > 0 {
    47  		result = make([]CoinAdapter, 0, len(coins)-1)
    48  	}
    49  
    50  	for _, coin := range coins {
    51  		if !coin.IsZero() {
    52  			result = append(result, coin)
    53  		}
    54  	}
    55  	return result
    56  }
    57  
    58  func (coins CoinAdapters) IsZero() bool {
    59  	for _, coin := range coins {
    60  		if !coin.IsZero() {
    61  			return false
    62  		}
    63  	}
    64  	return true
    65  }