github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/dex/types/pair.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     8  )
     9  
    10  // DefaultTokenPairDeposit defines default deposit of token pair
    11  var DefaultTokenPairDeposit = sdk.NewDecCoin(sdk.DefaultBondDenom, sdk.NewInt(0))
    12  
    13  // TokenPair represents token pair object
    14  type TokenPair struct {
    15  	BaseAssetSymbol  string         `json:"base_asset_symbol"`
    16  	QuoteAssetSymbol string         `json:"quote_asset_symbol"`
    17  	InitPrice        sdk.Dec        `json:"price"`
    18  	MaxPriceDigit    int64          `json:"max_price_digit"`
    19  	MaxQuantityDigit int64          `json:"max_size_digit"`
    20  	MinQuantity      sdk.Dec        `json:"min_trade_size"`
    21  	ID               uint64         `json:"token_pair_id"`
    22  	Delisting        bool           `json:"delisting"`
    23  	Owner            sdk.AccAddress `json:"owner"`
    24  	Deposits         sdk.SysCoin    `json:"deposits"`
    25  	BlockHeight      int64          `json:"block_height"`
    26  }
    27  
    28  // Name returns name of token pair
    29  func (tp *TokenPair) Name() string {
    30  	return fmt.Sprintf("%s_%s", tp.BaseAssetSymbol, tp.QuoteAssetSymbol)
    31  }
    32  
    33  // IsGT returns true if the token pair is greater than the other one
    34  // 1. compare deposits
    35  // 2. compare block height
    36  // 3. compare name
    37  func (tp *TokenPair) IsGT(other *TokenPair) bool {
    38  	if tp.Deposits.IsLT(other.Deposits) {
    39  		return false
    40  	}
    41  
    42  	if !tp.Deposits.IsEqual(other.Deposits) {
    43  		return true
    44  	}
    45  
    46  	if tp.BlockHeight < other.BlockHeight {
    47  		return true
    48  	}
    49  
    50  	if tp.BlockHeight > other.BlockHeight {
    51  		return false
    52  	}
    53  
    54  	return strings.Compare(tp.BaseAssetSymbol, other.BaseAssetSymbol) < 0 || strings.Compare(tp.QuoteAssetSymbol, other.QuoteAssetSymbol) < 0
    55  }
    56  
    57  // TokenPairs represents token pair slice, support sorting
    58  type TokenPairs []*TokenPair
    59  
    60  // Len Implements Sort
    61  func (tp TokenPairs) Len() int { return len(tp) }
    62  
    63  // Less Implements Sort
    64  func (tp TokenPairs) Less(i, j int) bool { return tp[i].IsGT(tp[j]) }
    65  
    66  // Swap Implements Sort
    67  func (tp TokenPairs) Swap(i, j int) { tp[i], tp[j] = tp[j], tp[i] }