-
Notifications
You must be signed in to change notification settings - Fork 1
/
swap.go
120 lines (102 loc) · 3.44 KB
/
swap.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package types
import (
sdk "github.com/FiboChain/fbc/libs/cosmos-sdk/types"
"github.com/FiboChain/fbc/libs/cosmos-sdk/x/supply"
token "github.com/FiboChain/fbc/x/token/types"
"fmt"
"strings"
)
// PoolTokenPrefix defines pool token prefix name
const PoolTokenPrefix = "ammswap_"
// SwapTokenPair defines token pair exchange
type SwapTokenPair struct {
QuotePooledCoin sdk.SysCoin `json:"quote_pooled_coin"` // The volume of quote token in the token pair exchange pool
BasePooledCoin sdk.SysCoin `json:"base_pooled_coin"` // The volume of base token in the token pair exchange pool
PoolTokenName string `json:"pool_token_name"` // The name of pool token
}
func NewSwapPair(token0, token1 string) SwapTokenPair {
base, quote := GetBaseQuoteTokenName(token0, token1)
swapTokenPair := SwapTokenPair{
sdk.NewDecCoinFromDec(quote, sdk.ZeroDec()),
sdk.NewDecCoinFromDec(base, sdk.ZeroDec()),
GetPoolTokenName(token0, token1),
}
return swapTokenPair
}
// NewSwapTokenPair is a constructor function for SwapTokenPair
func NewSwapTokenPair(quotePooledCoin sdk.SysCoin, basePooledCoin sdk.SysCoin, poolTokenName string) *SwapTokenPair {
swapTokenPair := &SwapTokenPair{
QuotePooledCoin: quotePooledCoin,
BasePooledCoin: basePooledCoin,
PoolTokenName: poolTokenName,
}
return swapTokenPair
}
// String implement fmt.Stringer
func (s SwapTokenPair) String() string {
return strings.TrimSpace(fmt.Sprintf(`QuotePooledCoin: %s
BasePooledCoin: %s
PoolTokenName: %s`, s.QuotePooledCoin.String(), s.BasePooledCoin.String(), s.PoolTokenName))
}
// TokenPairName defines token pair
func (s SwapTokenPair) TokenPairName() string {
return s.BasePooledCoin.Denom + "_" + s.QuotePooledCoin.Denom
}
// InitPoolToken default pool token
func InitPoolToken(poolTokenName string) token.Token {
return token.Token{
Description: poolTokenName,
Symbol: poolTokenName,
OriginalSymbol: poolTokenName,
WholeName: poolTokenName,
OriginalTotalSupply: sdk.NewDec(0),
Owner: supply.NewModuleAddress(ModuleName),
Type: GenerateTokenType,
Mintable: true,
}
}
func GetSwapTokenPairName(token0, token1 string) string {
baseTokenName, quoteTokenName := GetBaseQuoteTokenName(token0, token1)
return baseTokenName + "_" + quoteTokenName
}
func GetBaseQuoteTokenName(token0, token1 string) (string, string) {
if token0 < token1 {
return token0, token1
}
return token1, token0
}
func ValidateBaseAndQuoteAmount(baseAmountName, quoteAmountName string) error {
if baseAmountName > quoteAmountName {
return ErrBaseAmountNameBiggerThanQuoteAmountName()
} else if baseAmountName == quoteAmountName {
return ErrBaseNameEqualQuoteName()
}
if err := ValidateSwapAmountName(baseAmountName); err != nil {
return err
}
if err := ValidateSwapAmountName(quoteAmountName); err != nil {
return err
}
return nil
}
func ValidateSwapAmountName(amountName string) error {
if sdk.ValidateDenom(amountName) != nil {
return ErrValidateDenom(amountName)
}
if token.NotAllowedOriginSymbol(amountName) {
return ErrNotAllowedOriginSymbol()
}
return nil
}
func GetPoolTokenName(token1, token2 string) string {
return PoolTokenPrefix + GetSwapTokenPairName(token1, token2)
}
func IsPoolToken(symbol string) bool {
return token.NotAllowedOriginSymbol(symbol)
}
func SplitPoolToken(symbol string) (token0, token1 string) {
splits := strings.Split(symbol, "_")[1:]
token0 = splits[0]
token1 = splits[1]
return
}