github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/ammswap/types/swap.go (about) 1 package types 2 3 import ( 4 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 5 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/supply" 6 token "github.com/fibonacci-chain/fbc/x/token/types" 7 8 "fmt" 9 "strings" 10 ) 11 12 // PoolTokenPrefix defines pool token prefix name 13 const PoolTokenPrefix = "ammswap_" 14 15 // SwapTokenPair defines token pair exchange 16 type SwapTokenPair struct { 17 QuotePooledCoin sdk.SysCoin `json:"quote_pooled_coin"` // The volume of quote token in the token pair exchange pool 18 BasePooledCoin sdk.SysCoin `json:"base_pooled_coin"` // The volume of base token in the token pair exchange pool 19 PoolTokenName string `json:"pool_token_name"` // The name of pool token 20 } 21 22 func NewSwapPair(token0, token1 string) SwapTokenPair { 23 base, quote := GetBaseQuoteTokenName(token0, token1) 24 25 swapTokenPair := SwapTokenPair{ 26 sdk.NewDecCoinFromDec(quote, sdk.ZeroDec()), 27 sdk.NewDecCoinFromDec(base, sdk.ZeroDec()), 28 GetPoolTokenName(token0, token1), 29 } 30 return swapTokenPair 31 } 32 33 // NewSwapTokenPair is a constructor function for SwapTokenPair 34 func NewSwapTokenPair(quotePooledCoin sdk.SysCoin, basePooledCoin sdk.SysCoin, poolTokenName string) *SwapTokenPair { 35 swapTokenPair := &SwapTokenPair{ 36 QuotePooledCoin: quotePooledCoin, 37 BasePooledCoin: basePooledCoin, 38 PoolTokenName: poolTokenName, 39 } 40 return swapTokenPair 41 } 42 43 // String implement fmt.Stringer 44 func (s SwapTokenPair) String() string { 45 return strings.TrimSpace(fmt.Sprintf(`QuotePooledCoin: %s 46 BasePooledCoin: %s 47 PoolTokenName: %s`, s.QuotePooledCoin.String(), s.BasePooledCoin.String(), s.PoolTokenName)) 48 } 49 50 // TokenPairName defines token pair 51 func (s SwapTokenPair) TokenPairName() string { 52 return s.BasePooledCoin.Denom + "_" + s.QuotePooledCoin.Denom 53 } 54 55 // InitPoolToken default pool token 56 func InitPoolToken(poolTokenName string) token.Token { 57 return token.Token{ 58 Description: poolTokenName, 59 Symbol: poolTokenName, 60 OriginalSymbol: poolTokenName, 61 WholeName: poolTokenName, 62 OriginalTotalSupply: sdk.NewDec(0), 63 Owner: supply.NewModuleAddress(ModuleName), 64 Type: GenerateTokenType, 65 Mintable: true, 66 } 67 } 68 69 func GetSwapTokenPairName(token0, token1 string) string { 70 baseTokenName, quoteTokenName := GetBaseQuoteTokenName(token0, token1) 71 return baseTokenName + "_" + quoteTokenName 72 } 73 74 func GetBaseQuoteTokenName(token0, token1 string) (string, string) { 75 if token0 < token1 { 76 return token0, token1 77 } 78 return token1, token0 79 } 80 81 func ValidateBaseAndQuoteAmount(baseAmountName, quoteAmountName string) error { 82 if baseAmountName > quoteAmountName { 83 return ErrBaseAmountNameBiggerThanQuoteAmountName() 84 } else if baseAmountName == quoteAmountName { 85 return ErrBaseNameEqualQuoteName() 86 } 87 if err := ValidateSwapAmountName(baseAmountName); err != nil { 88 return err 89 } 90 91 if err := ValidateSwapAmountName(quoteAmountName); err != nil { 92 return err 93 } 94 return nil 95 } 96 97 func ValidateSwapAmountName(amountName string) error { 98 if sdk.ValidateDenom(amountName) != nil { 99 return ErrValidateDenom(amountName) 100 } 101 if token.NotAllowedOriginSymbol(amountName) { 102 return ErrNotAllowedOriginSymbol() 103 } 104 return nil 105 } 106 107 func GetPoolTokenName(token1, token2 string) string { 108 return PoolTokenPrefix + GetSwapTokenPairName(token1, token2) 109 } 110 111 func IsPoolToken(symbol string) bool { 112 return token.NotAllowedOriginSymbol(symbol) 113 } 114 115 func SplitPoolToken(symbol string) (token0, token1 string) { 116 splits := strings.Split(symbol, "_")[1:] 117 token0 = splits[0] 118 token1 = splits[1] 119 return 120 }