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

     1  package types
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"regexp"
     7  	"sort"
     8  	"strings"
     9  
    10  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth"
    12  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto"
    13  )
    14  
    15  var (
    16  	notAllowedPrefix       = "ammswap"
    17  	notAllowedOriginSymbol = regexp.MustCompile(fmt.Sprintf("^%s.*?", notAllowedPrefix))
    18  	regOriginalSymbol      = regexp.MustCompile("^[a-z][a-z0-9]{0,5}$")
    19  	reWholeName            = `[a-zA-Z0-9[:space:]]{1,30}`
    20  	reWhole                = regexp.MustCompile(fmt.Sprintf(`^%s$`, reWholeName))
    21  )
    22  
    23  func WholeNameCheck(wholeName string) (newName string, isValid bool) {
    24  	wholeName = strings.TrimSpace(wholeName)
    25  	strs := strings.Fields(wholeName)
    26  	wholeName = strings.Join(strs, " ")
    27  	if !reWhole.MatchString(wholeName) {
    28  		return wholeName, false
    29  	}
    30  	return wholeName, true
    31  }
    32  
    33  func wholeNameValid(wholeName string) bool {
    34  	return reWhole.MatchString(wholeName)
    35  }
    36  
    37  type BaseAccount struct {
    38  	Address       sdk.AccAddress `json:"address"`
    39  	Coins         sdk.Coins      `json:"coins"`
    40  	PubKey        crypto.PubKey  `json:"public_key"`
    41  	AccountNumber uint64         `json:"account_number"`
    42  	Sequence      uint64         `json:"sequence"`
    43  }
    44  
    45  type DecAccount struct {
    46  	Address       sdk.AccAddress `json:"address"`
    47  	Coins         sdk.SysCoins   `json:"coins"`
    48  	PubKey        crypto.PubKey  `json:"public_key"`
    49  	AccountNumber uint64         `json:"account_number"`
    50  	Sequence      uint64         `json:"sequence"`
    51  }
    52  
    53  // String implements fmt.Stringer
    54  func (acc DecAccount) String() string {
    55  	var pubkey string
    56  
    57  	if acc.PubKey != nil {
    58  		pubkey = sdk.MustBech32ifyAccPub(acc.PubKey)
    59  	}
    60  
    61  	return fmt.Sprintf(`Account:
    62   Address:       %s
    63   Pubkey:        %s
    64   Coins:         %v
    65   AccountNumber: %d
    66   Sequence:      %d`,
    67  		acc.Address, pubkey, acc.Coins, acc.AccountNumber, acc.Sequence,
    68  	)
    69  }
    70  
    71  func NotAllowedOriginSymbol(name string) bool {
    72  	return notAllowedOriginSymbol.MatchString(name)
    73  }
    74  
    75  func ValidOriginalSymbol(name string) bool {
    76  	return !NotAllowedOriginSymbol(name) && regOriginalSymbol.MatchString(name)
    77  }
    78  
    79  // Convert a formatted json string into a TransferUnit array
    80  // e.g.) [{"to": "addr", "amount": "1BNB,2BTC"}, ...]
    81  func StrToTransfers(str string) (transfers []TransferUnit, err error) {
    82  	var transfer []Transfer
    83  	err = json.Unmarshal([]byte(str), &transfer)
    84  	if err != nil {
    85  		return transfers, err
    86  	}
    87  
    88  	for _, trans := range transfer {
    89  		var t TransferUnit
    90  		to, err := sdk.AccAddressFromBech32(trans.To)
    91  		if err != nil {
    92  			return transfers, fmt.Errorf("invalid address:%s", trans.To)
    93  		}
    94  		t.To = to
    95  		t.Coins, err = sdk.ParseDecCoins(trans.Amount)
    96  		if err != nil {
    97  			return transfers, err
    98  		}
    99  		transfers = append(transfers, t)
   100  	}
   101  	return transfers, nil
   102  }
   103  
   104  func BaseAccountToDecAccount(account auth.BaseAccount) DecAccount {
   105  	var decCoins sdk.SysCoins
   106  	for _, coin := range account.Coins {
   107  		dec := coin.Amount
   108  		decCoin := sdk.NewDecCoinFromDec(coin.Denom, dec)
   109  		decCoins = append(decCoins, decCoin)
   110  	}
   111  	decAccount := DecAccount{
   112  		Address:       account.Address,
   113  		PubKey:        account.PubKey,
   114  		Coins:         decCoins,
   115  		AccountNumber: account.AccountNumber,
   116  		Sequence:      account.Sequence,
   117  	}
   118  	return decAccount
   119  }
   120  
   121  func (acc *DecAccount) ToBaseAccount() *auth.BaseAccount {
   122  	decAccount := auth.BaseAccount{
   123  		Address:       acc.Address,
   124  		PubKey:        acc.PubKey,
   125  		Coins:         acc.Coins,
   126  		AccountNumber: acc.AccountNumber,
   127  		Sequence:      acc.Sequence,
   128  	}
   129  	return &decAccount
   130  }
   131  
   132  func DecAccountArrToBaseAccountArr(decAccounts []DecAccount) (baseAccountArr []auth.Account) {
   133  	for _, decAccount := range decAccounts {
   134  		baseAccountArr = append(baseAccountArr, decAccount.ToBaseAccount())
   135  	}
   136  	return baseAccountArr
   137  }
   138  
   139  func MergeCoinInfo(availableCoins, lockedCoins sdk.SysCoins) (coinsInfo CoinsInfo) {
   140  	m := make(map[string]CoinInfo)
   141  
   142  	for _, availableCoin := range availableCoins {
   143  		coinInfo, ok := m[availableCoin.Denom]
   144  		if !ok {
   145  			coinInfo.Symbol = availableCoin.Denom
   146  
   147  			coinInfo.Available = availableCoin.Amount.String()
   148  			coinInfo.Locked = "0"
   149  			m[availableCoin.Denom] = coinInfo
   150  		}
   151  	}
   152  
   153  	for _, lockedCoin := range lockedCoins {
   154  		coinInfo, ok := m[lockedCoin.Denom]
   155  		if ok {
   156  			coinInfo.Locked = lockedCoin.Amount.String()
   157  			m[lockedCoin.Denom] = coinInfo
   158  		} else {
   159  			coinInfo.Symbol = lockedCoin.Denom
   160  			coinInfo.Available = "0"
   161  			coinInfo.Locked = lockedCoin.Amount.String()
   162  
   163  			m[lockedCoin.Denom] = coinInfo
   164  		}
   165  	}
   166  
   167  	for _, coinInfo := range m {
   168  		coinsInfo = append(coinsInfo, coinInfo)
   169  	}
   170  	sort.Sort(coinsInfo)
   171  	return coinsInfo
   172  }
   173  
   174  func GenTokenResp(token Token) TokenResp {
   175  	return TokenResp{
   176  		Description:         token.Description,
   177  		Symbol:              token.Symbol,
   178  		OriginalSymbol:      token.OriginalSymbol,
   179  		WholeName:           token.WholeName,
   180  		OriginalTotalSupply: token.OriginalTotalSupply,
   181  		Owner:               token.Owner,
   182  		Type:                token.Type,
   183  		Mintable:            token.Mintable,
   184  	}
   185  }