2022-09-13 07:10:59 +00:00
|
|
|
package token
|
2021-09-09 14:28:54 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-14 17:33:05 +00:00
|
|
|
"reflect"
|
2021-09-09 14:28:54 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/appdatabase"
|
2023-02-06 17:05:58 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2021-09-09 14:28:54 +00:00
|
|
|
)
|
|
|
|
|
2022-10-25 14:50:32 +00:00
|
|
|
func setupTestTokenDB(t *testing.T) (*Manager, func()) {
|
2023-02-06 17:05:58 +00:00
|
|
|
db, err := appdatabase.InitializeDB(":memory:", "wallet-token-tests", 1)
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
2023-06-14 13:16:15 +00:00
|
|
|
return &Manager{db, nil, nil, nil, nil, nil, false}, func() {
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, db.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustoms(t *testing.T) {
|
|
|
|
manager, stop := setupTestTokenDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
rst, err := manager.GetCustoms()
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, rst)
|
|
|
|
|
|
|
|
token := Token{
|
|
|
|
Address: common.Address{1},
|
|
|
|
Name: "Zilliqa",
|
|
|
|
Symbol: "ZIL",
|
|
|
|
Decimals: 12,
|
|
|
|
Color: "#fa6565",
|
|
|
|
ChainID: 777,
|
|
|
|
}
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
err = manager.UpsertCustom(token)
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
rst, err = manager.GetCustoms()
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(rst))
|
|
|
|
require.Equal(t, token, *rst[0])
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
err = manager.DeleteCustom(777, token.Address)
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
rst, err = manager.GetCustoms()
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(rst))
|
|
|
|
}
|
2023-02-06 17:05:58 +00:00
|
|
|
|
|
|
|
func TestTokenOverride(t *testing.T) {
|
|
|
|
networks := []params.Network{
|
|
|
|
{
|
|
|
|
ChainID: 1,
|
|
|
|
ChainName: "TestChain1",
|
|
|
|
TokenOverrides: []params.TokenOverride{
|
|
|
|
{
|
|
|
|
Symbol: "SNT",
|
|
|
|
Address: common.Address{11},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
ChainID: 2,
|
|
|
|
ChainName: "TestChain2",
|
|
|
|
TokenOverrides: []params.TokenOverride{
|
|
|
|
{
|
|
|
|
Symbol: "STT",
|
|
|
|
Address: common.Address{33},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-03-14 17:33:05 +00:00
|
|
|
|
|
|
|
tokenList := []*Token{
|
|
|
|
&Token{
|
|
|
|
Address: common.Address{1},
|
|
|
|
Symbol: "SNT",
|
|
|
|
ChainID: 1,
|
2023-02-06 17:05:58 +00:00
|
|
|
},
|
2023-03-14 17:33:05 +00:00
|
|
|
&Token{
|
|
|
|
Address: common.Address{2},
|
|
|
|
Symbol: "TNT",
|
|
|
|
ChainID: 1,
|
|
|
|
},
|
|
|
|
&Token{
|
|
|
|
Address: common.Address{3},
|
|
|
|
Symbol: "STT",
|
|
|
|
ChainID: 2,
|
|
|
|
},
|
|
|
|
&Token{
|
|
|
|
Address: common.Address{4},
|
|
|
|
Symbol: "TTT",
|
|
|
|
ChainID: 2,
|
2023-02-06 17:05:58 +00:00
|
|
|
},
|
|
|
|
}
|
2023-03-14 17:33:05 +00:00
|
|
|
testStore := &DefaultStore{
|
|
|
|
tokenList,
|
|
|
|
}
|
|
|
|
|
|
|
|
overrideTokensInPlace(networks, testStore.tokenList)
|
2023-06-14 13:16:15 +00:00
|
|
|
tokens := testStore.GetTokens()
|
2023-03-14 17:33:05 +00:00
|
|
|
tokenMap := toTokenMap(tokens)
|
|
|
|
_, found := tokenMap[1][common.Address{1}]
|
2023-02-06 17:05:58 +00:00
|
|
|
require.False(t, found)
|
2023-03-14 17:33:05 +00:00
|
|
|
require.Equal(t, common.Address{11}, tokenMap[1][common.Address{11}].Address)
|
|
|
|
require.Equal(t, common.Address{2}, tokenMap[1][common.Address{2}].Address)
|
|
|
|
_, found = tokenMap[2][common.Address{3}]
|
2023-02-06 17:05:58 +00:00
|
|
|
require.False(t, found)
|
2023-03-14 17:33:05 +00:00
|
|
|
require.Equal(t, common.Address{33}, tokenMap[2][common.Address{33}].Address)
|
|
|
|
require.Equal(t, common.Address{4}, tokenMap[2][common.Address{4}].Address)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeTokenLists(t *testing.T) {
|
|
|
|
tokenList1 := []*Token{
|
|
|
|
&Token{
|
|
|
|
Address: common.Address{1},
|
|
|
|
Symbol: "SNT",
|
|
|
|
ChainID: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tokenList1Copy := []*Token{
|
|
|
|
&Token{
|
|
|
|
Address: common.Address{1},
|
|
|
|
Symbol: "SNT",
|
|
|
|
ChainID: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tokenList2 := []*Token{
|
|
|
|
&Token{
|
|
|
|
Address: common.Address{3},
|
|
|
|
Symbol: "STT",
|
|
|
|
ChainID: 2,
|
|
|
|
},
|
|
|
|
&Token{
|
|
|
|
Address: common.Address{4},
|
|
|
|
Symbol: "TTT",
|
|
|
|
ChainID: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tokenList1Plus2 := []*Token{
|
|
|
|
&Token{
|
|
|
|
Address: common.Address{1},
|
|
|
|
Symbol: "SNT",
|
|
|
|
ChainID: 1,
|
|
|
|
},
|
|
|
|
&Token{
|
|
|
|
Address: common.Address{3},
|
|
|
|
Symbol: "STT",
|
|
|
|
ChainID: 2,
|
|
|
|
},
|
|
|
|
&Token{
|
|
|
|
Address: common.Address{4},
|
|
|
|
Symbol: "TTT",
|
|
|
|
ChainID: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tokenListEmpty := []*Token{}
|
|
|
|
|
|
|
|
mergedList := mergeTokenLists([][]*Token{tokenListEmpty, tokenListEmpty})
|
|
|
|
require.Equal(t, 0, len(mergedList))
|
|
|
|
|
|
|
|
mergedList = mergeTokenLists([][]*Token{tokenListEmpty, tokenList1})
|
|
|
|
require.True(t, reflect.DeepEqual(mergedList, tokenList1))
|
|
|
|
|
|
|
|
mergedList = mergeTokenLists([][]*Token{tokenList1, tokenList1Copy})
|
|
|
|
require.True(t, reflect.DeepEqual(mergedList, tokenList1))
|
|
|
|
|
|
|
|
mergedList = mergeTokenLists([][]*Token{tokenList1, tokenList2})
|
|
|
|
require.True(t, reflect.DeepEqual(mergedList, tokenList1Plus2))
|
2023-02-06 17:05:58 +00:00
|
|
|
}
|