github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/acm/balance/balance.go (about)

     1  package balance
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"sort"
     7  )
     8  
     9  var (
    10  	ethInWei = new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
    11  )
    12  
    13  type Balances []Balance
    14  
    15  func (b Balance) String() string {
    16  	return fmt.Sprintf("{%v: %d}", b.Type, b.Amount)
    17  }
    18  
    19  func New() Balances {
    20  	return []Balance{}
    21  }
    22  
    23  func (bs Balances) Sort() Balances {
    24  	sort.Stable(bs)
    25  	return bs
    26  }
    27  
    28  func (bs Balances) Len() int {
    29  	return len(bs)
    30  }
    31  
    32  func (bs Balances) Less(i, j int) bool {
    33  	if bs[i].Type < bs[j].Type {
    34  		return true
    35  	}
    36  	return bs[i].Type == bs[j].Type && bs[i].Amount < bs[j].Amount
    37  }
    38  
    39  func (bs Balances) Swap(i, j int) {
    40  	bs[i], bs[j] = bs[j], bs[i]
    41  }
    42  
    43  func (bs Balances) Add(ty Type, amount uint64) Balances {
    44  	return append(bs, Balance{
    45  		Type:   ty,
    46  		Amount: amount,
    47  	})
    48  }
    49  
    50  func (bs Balances) Native(amount uint64) Balances {
    51  	return bs.Add(TypeNative, amount)
    52  }
    53  
    54  func (bs Balances) Power(amount uint64) Balances {
    55  	return bs.Add(TypePower, amount)
    56  }
    57  
    58  func (bs Balances) Sum(bss ...Balances) Balances {
    59  	return Sum(append(bss, bs)...)
    60  }
    61  
    62  func Sum(bss ...Balances) Balances {
    63  	sum := New()
    64  	sumMap := make(map[Type]uint64)
    65  	for _, bs := range bss {
    66  		for _, b := range bs {
    67  			sumMap[b.Type] += b.Amount
    68  		}
    69  	}
    70  	for k, v := range sumMap {
    71  		sum = sum.Add(k, v)
    72  	}
    73  	sort.Stable(sum)
    74  	return sum
    75  }
    76  
    77  func Native(native uint64) Balance {
    78  	return Balance{
    79  		Type:   TypeNative,
    80  		Amount: native,
    81  	}
    82  }
    83  
    84  func Power(power uint64) Balance {
    85  	return Balance{
    86  		Type:   TypePower,
    87  		Amount: power,
    88  	}
    89  }
    90  
    91  func (bs Balances) Has(ty Type) bool {
    92  	for _, b := range bs {
    93  		if b.Type == ty {
    94  			return true
    95  		}
    96  	}
    97  	return false
    98  }
    99  
   100  func (bs Balances) Get(ty Type) *uint64 {
   101  	for _, b := range bs {
   102  		if b.Type == ty {
   103  			return &b.Amount
   104  		}
   105  	}
   106  	return nil
   107  }
   108  
   109  func (bs Balances) GetFallback(ty Type, fallback uint64) uint64 {
   110  	for _, b := range bs {
   111  		if b.Type == ty {
   112  			return b.Amount
   113  		}
   114  	}
   115  	return fallback
   116  }
   117  
   118  func (bs Balances) GetNative(fallback uint64) uint64 {
   119  	return bs.GetFallback(TypeNative, fallback)
   120  }
   121  
   122  func (bs Balances) GetPower(fallback uint64) uint64 {
   123  	return bs.GetFallback(TypePower, fallback)
   124  }
   125  
   126  func (bs Balances) HasNative() bool {
   127  	return bs.Has(TypeNative)
   128  }
   129  
   130  func (bs Balances) HasPower() bool {
   131  	return bs.Has(TypePower)
   132  }
   133  
   134  func NativeToWei(n uint64) *big.Int {
   135  	// 1 native unit to 1 ether (wei)
   136  	x := new(big.Int).SetUint64(n)
   137  	return new(big.Int).Mul(x, ethInWei)
   138  }
   139  
   140  func WeiToNative(x *big.Int) *big.Int {
   141  	return new(big.Int).Div(x, ethInWei)
   142  }