github.com/InjectiveLabs/sdk-go@v1.53.0/chain/exchange/types/deposit.go (about)

     1  package types
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"sort"
     7  
     8  	"cosmossdk.io/math"
     9  	"github.com/ethereum/go-ethereum/common"
    10  )
    11  
    12  func NewDeposit() *Deposit {
    13  	return &Deposit{
    14  		AvailableBalance: math.LegacyZeroDec(),
    15  		TotalBalance:     math.LegacyZeroDec(),
    16  	}
    17  }
    18  
    19  func (d *Deposit) IsEmpty() bool {
    20  	return d.AvailableBalance.IsZero() && d.TotalBalance.IsZero()
    21  }
    22  
    23  func (d *Deposit) Display() string {
    24  	return fmt.Sprintf("Deposit Available: %s, Total: %s", getReadableDec(d.AvailableBalance), getReadableDec(d.TotalBalance))
    25  }
    26  
    27  type DepositDelta struct {
    28  	AvailableBalanceDelta math.LegacyDec
    29  	TotalBalanceDelta     math.LegacyDec
    30  }
    31  
    32  func NewUniformDepositDelta(delta math.LegacyDec) *DepositDelta {
    33  	return &DepositDelta{
    34  		AvailableBalanceDelta: delta,
    35  		TotalBalanceDelta:     delta,
    36  	}
    37  }
    38  
    39  func NewDepositDelta() *DepositDelta {
    40  	return NewUniformDepositDelta(math.LegacyZeroDec())
    41  }
    42  
    43  func (d *DepositDelta) AddAvailableBalance(amount math.LegacyDec) {
    44  	d.AvailableBalanceDelta = d.AvailableBalanceDelta.Add(amount)
    45  }
    46  
    47  func (d *DepositDelta) IsEmpty() bool {
    48  	if d == nil {
    49  		return true
    50  	}
    51  	hasEmptyTotalBalanceDelta := d.TotalBalanceDelta.IsNil() || d.TotalBalanceDelta.IsZero()
    52  	hasEmptyAvailableBalanceDelta := d.AvailableBalanceDelta.IsNil() || d.AvailableBalanceDelta.IsZero()
    53  	return hasEmptyTotalBalanceDelta && hasEmptyAvailableBalanceDelta
    54  }
    55  
    56  type DepositDeltas map[common.Hash]*DepositDelta
    57  
    58  func NewDepositDeltas() DepositDeltas {
    59  	return make(DepositDeltas)
    60  }
    61  
    62  func (d *DepositDeltas) GetSortedSubaccountKeys() []common.Hash {
    63  	subaccountKeys := make([]common.Hash, 0)
    64  	for k := range *d {
    65  		subaccountKeys = append(subaccountKeys, k)
    66  	}
    67  	sort.SliceStable(subaccountKeys, func(i, j int) bool {
    68  		return bytes.Compare(subaccountKeys[i].Bytes(), subaccountKeys[j].Bytes()) < 0
    69  	})
    70  	return subaccountKeys
    71  }
    72  
    73  func (d *DepositDeltas) ApplyDepositDelta(subaccountID common.Hash, delta *DepositDelta) {
    74  	d.ApplyDelta(subaccountID, delta.TotalBalanceDelta, delta.AvailableBalanceDelta)
    75  }
    76  
    77  func (d *DepositDeltas) ApplyUniformDelta(subaccountID common.Hash, delta math.LegacyDec) {
    78  	d.ApplyDelta(subaccountID, delta, delta)
    79  }
    80  
    81  func (d *DepositDeltas) ApplyDelta(subaccountID common.Hash, totalBalanceDelta, availableBalanceDelta math.LegacyDec) {
    82  	delta := (*d)[subaccountID]
    83  	if delta == nil {
    84  		delta = NewDepositDelta()
    85  		(*d)[subaccountID] = delta
    86  	}
    87  	delta.AvailableBalanceDelta = delta.AvailableBalanceDelta.Add(availableBalanceDelta)
    88  	delta.TotalBalanceDelta = delta.TotalBalanceDelta.Add(totalBalanceDelta)
    89  }
    90  
    91  func (d *Deposit) HasTransientOrRestingVanillaLimitOrders() bool {
    92  	return d.AvailableBalance.LT(d.TotalBalance)
    93  }
    94  
    95  func GetSortedBalanceKeys(p map[string]*Deposit) []string {
    96  	denoms := make([]string, 0)
    97  	for k := range p {
    98  		denoms = append(denoms, k)
    99  	}
   100  	sort.SliceStable(denoms, func(i, j int) bool {
   101  		return denoms[i] < denoms[j]
   102  	})
   103  	return denoms
   104  }