github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/token/types/token_test.go (about) 1 package types 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/fibonacci-chain/fbc/x/common" 8 9 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 10 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto/secp256k1" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestAccountResponse(t *testing.T) { 15 accResp := AccountResponse{ 16 Address: "address", 17 Currencies: []CoinInfo{}, 18 } 19 accResp1 := NewAccountResponse("address") 20 require.EqualValues(t, accResp, accResp1) 21 } 22 23 func TestCoinInfo(t *testing.T) { 24 coinInfo := CoinInfo{ 25 Symbol: "btc", 26 Available: "1000001", 27 Locked: "8888", 28 } 29 30 coinInfo1 := NewCoinInfo("btc", "1000001", "8888") 31 require.EqualValues(t, coinInfo, *coinInfo1) 32 } 33 34 func TestCurrency(t *testing.T) { 35 testCase := []struct { 36 currency Currency 37 expectedStr string 38 }{ 39 {Currency{ 40 Description: "my currency", 41 Symbol: common.NativeToken, 42 TotalSupply: sdk.NewDec(10000000), 43 }, `{"description":"my currency","symbol":"` + common.NativeToken + `","total_supply":"10000000.000000000000000000"}`}, 44 {Currency{ 45 Description: common.NativeToken, 46 Symbol: common.NativeToken, 47 TotalSupply: sdk.NewDec(10000), 48 }, `{"description":"` + common.NativeToken + `","symbol":"` + common.NativeToken + `","total_supply":"10000.000000000000000000"}`}, 49 } 50 for _, currencyCase := range testCase { 51 b, err := json.Marshal(currencyCase.currency) 52 require.Nil(t, err) 53 require.EqualValues(t, string(b), currencyCase.currency.String()) 54 require.EqualValues(t, currencyCase.expectedStr, currencyCase.currency.String()) 55 } 56 } 57 58 func TestToken(t *testing.T) { 59 60 common.InitConfig() 61 addr, err := sdk.AccAddressFromBech32("fb18rrc500xu2haw7vyksqlj2lfp9xex2hczv3jkx") 62 require.Nil(t, err) 63 64 testCase := []struct { 65 token Token 66 expectedStr string 67 }{ 68 {Token{ 69 Description: "my token", 70 Symbol: common.NativeToken, 71 OriginalSymbol: common.NativeToken, 72 WholeName: "btc", 73 OriginalTotalSupply: sdk.NewDec(1000000), 74 Type: 0, 75 Owner: nil, 76 Mintable: false, 77 }, `{"description":"my token","symbol":"` + common.NativeToken + `","original_symbol":"` + common.NativeToken + `","whole_name":"btc","original_total_supply":"1000000.000000000000000000","type":0,"owner":"","mintable":false}`}, 78 {Token{ 79 Description: "fibolockchain coin", 80 Symbol: common.NativeToken, 81 OriginalSymbol: common.NativeToken, 82 WholeName: "fb coin", 83 OriginalTotalSupply: sdk.NewDec(1000000000), 84 Type: 0, 85 Owner: addr, 86 Mintable: true, 87 }, `{"description":"fibolockchain coin","symbol":"` + common.NativeToken + `","original_symbol":"` + common.NativeToken + `","whole_name":"fb coin","original_total_supply":"1000000000.000000000000000000","type":0,"owner":"fb18rrc500xu2haw7vyksqlj2lfp9xex2hczv3jkx","mintable":true}`}, 88 } 89 for _, tokenCase := range testCase { 90 b, err := json.Marshal(tokenCase.token) 91 require.Nil(t, err) 92 require.EqualValues(t, string(b), tokenCase.token.String()) 93 require.EqualValues(t, tokenCase.expectedStr, tokenCase.token.String()) 94 } 95 } 96 97 func TestKeys(t *testing.T) { 98 symbol := common.NativeToken 99 b := GetTokenAddress(symbol) 100 require.EqualValues(t, b, append(TokenKey, []byte(symbol)...)) 101 102 privKey := secp256k1.GenPrivKey() 103 pubKey := privKey.PubKey() 104 addr := sdk.AccAddress(pubKey.Address()) 105 106 b = GetLockAddress(addr) 107 require.EqualValues(t, b, append(LockKey, addr.Bytes()...)) 108 }