2022-09-13 07:10:59 +00:00
|
|
|
package token
|
2021-09-09 14:28:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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)
|
2022-10-25 14:50:32 +00:00
|
|
|
return &Manager{db, nil, nil}, 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},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testTokenStore := map[uint64]map[common.Address]*Token{
|
|
|
|
1: {
|
|
|
|
common.Address{1}: {
|
|
|
|
Address: common.Address{1},
|
|
|
|
Symbol: "SNT",
|
|
|
|
},
|
|
|
|
common.Address{2}: {
|
|
|
|
Address: common.Address{2},
|
|
|
|
Symbol: "TNT",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
2: {
|
|
|
|
common.Address{3}: {
|
|
|
|
Address: common.Address{3},
|
|
|
|
Symbol: "STT",
|
|
|
|
},
|
|
|
|
common.Address{4}: {
|
|
|
|
Address: common.Address{4},
|
|
|
|
Symbol: "TTT",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
overrideTokensInPlace(networks, testTokenStore)
|
|
|
|
_, found := testTokenStore[1][common.Address{1}]
|
|
|
|
require.False(t, found)
|
|
|
|
require.Equal(t, common.Address{11}, testTokenStore[1][common.Address{11}].Address)
|
|
|
|
require.Equal(t, common.Address{2}, testTokenStore[1][common.Address{2}].Address)
|
|
|
|
_, found = testTokenStore[2][common.Address{3}]
|
|
|
|
require.False(t, found)
|
|
|
|
require.Equal(t, common.Address{33}, testTokenStore[2][common.Address{33}].Address)
|
|
|
|
require.Equal(t, common.Address{4}, testTokenStore[2][common.Address{4}].Address)
|
|
|
|
}
|