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