2021-09-09 14:28:54 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/appdatabase"
|
2022-01-12 20:04:43 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2023-08-11 11:28:45 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
2021-09-09 14:28:54 +00:00
|
|
|
)
|
|
|
|
|
2022-01-12 20:04:43 +00:00
|
|
|
var initNetworks = []params.Network{
|
2021-09-09 14:28:54 +00:00
|
|
|
{
|
|
|
|
ChainID: 1,
|
|
|
|
ChainName: "Ethereum Mainnet",
|
|
|
|
RPCURL: "https://mainnet.infura.io/nKmXgiFgc2KqtoQ8BCGJ",
|
|
|
|
BlockExplorerURL: "https://etherscan.io/",
|
|
|
|
IconURL: "",
|
|
|
|
NativeCurrencyName: "Ether",
|
|
|
|
NativeCurrencySymbol: "ETH",
|
|
|
|
NativeCurrencyDecimals: 18,
|
|
|
|
IsTest: false,
|
|
|
|
Layer: 1,
|
|
|
|
Enabled: true,
|
2024-02-20 09:30:47 +00:00
|
|
|
RelatedChainID: 11155111,
|
2021-09-09 14:28:54 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-20 09:30:47 +00:00
|
|
|
ChainID: 11155111,
|
2024-10-15 07:29:21 +00:00
|
|
|
ChainName: "Sepolia",
|
|
|
|
RPCURL: "https://sepolia.infura.io/v3/",
|
|
|
|
BlockExplorerURL: "https://sepolia.etherscan.io/",
|
2021-09-09 14:28:54 +00:00
|
|
|
IconURL: "",
|
|
|
|
NativeCurrencyName: "Ether",
|
|
|
|
NativeCurrencySymbol: "ETH",
|
|
|
|
NativeCurrencyDecimals: 18,
|
|
|
|
IsTest: true,
|
|
|
|
Layer: 1,
|
|
|
|
Enabled: false,
|
2023-07-13 14:03:49 +00:00
|
|
|
RelatedChainID: 1,
|
2021-09-09 14:28:54 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ChainID: 10,
|
|
|
|
ChainName: "Optimistic Ethereum",
|
|
|
|
RPCURL: "https://mainnet.infura.io/nKmXgiFgc2KqtoQ8BCGJ",
|
|
|
|
BlockExplorerURL: "https://optimistic.etherscan.io",
|
|
|
|
IconURL: "",
|
|
|
|
NativeCurrencyName: "Ether",
|
|
|
|
NativeCurrencySymbol: "ETH",
|
|
|
|
NativeCurrencyDecimals: 18,
|
|
|
|
IsTest: false,
|
|
|
|
Layer: 2,
|
|
|
|
Enabled: true,
|
2024-10-15 07:29:21 +00:00
|
|
|
RelatedChainID: 11155420,
|
2021-09-09 14:28:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupTestNetworkDB(t *testing.T) (*sql.DB, func()) {
|
2023-08-11 11:28:45 +00:00
|
|
|
db, cleanup, err := helpers.SetupTestSQLDB(appdatabase.DbInitializer{}, "wallet-network-tests")
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
2023-08-11 11:28:45 +00:00
|
|
|
return db, func() { require.NoError(t, cleanup()) }
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitNetwork(t *testing.T) {
|
|
|
|
db, stop := setupTestNetworkDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
2023-10-03 13:27:42 +00:00
|
|
|
nm := NewManager(db)
|
2021-09-09 14:28:54 +00:00
|
|
|
err := nm.Init(initNetworks)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
network := nm.Find(1)
|
|
|
|
require.NotNil(t, network)
|
|
|
|
require.Equal(t, (uint64)(1), network.ChainID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
db, stop := setupTestNetworkDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
2023-10-03 13:27:42 +00:00
|
|
|
nm := NewManager(db)
|
2021-09-09 14:28:54 +00:00
|
|
|
err := nm.Init(initNetworks)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
networks, err := nm.Get(true)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, 2, len(networks))
|
|
|
|
}
|
|
|
|
|
2023-07-13 14:03:49 +00:00
|
|
|
func TestGetCombinedNetworks(t *testing.T) {
|
|
|
|
db, stop := setupTestNetworkDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
2023-10-03 13:27:42 +00:00
|
|
|
nm := NewManager(db)
|
2023-07-13 14:03:49 +00:00
|
|
|
err := nm.Init(initNetworks)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
combinedNetworks, err := nm.GetCombinedNetworks()
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, 2, len(combinedNetworks))
|
|
|
|
require.Equal(t, uint64(1), combinedNetworks[0].Prod.ChainID)
|
2024-02-20 09:30:47 +00:00
|
|
|
require.Equal(t, uint64(11155111), combinedNetworks[0].Test.ChainID)
|
2023-07-13 14:03:49 +00:00
|
|
|
require.Equal(t, uint64(10), combinedNetworks[1].Prod.ChainID)
|
|
|
|
require.Nil(t, combinedNetworks[1].Test)
|
|
|
|
}
|
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
func TestDelete(t *testing.T) {
|
|
|
|
db, stop := setupTestNetworkDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
2023-10-03 13:27:42 +00:00
|
|
|
nm := NewManager(db)
|
2021-09-09 14:28:54 +00:00
|
|
|
err := nm.Init(initNetworks)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = nm.Delete(1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
networks, err := nm.Get(true)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, 1, len(networks))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpsert(t *testing.T) {
|
|
|
|
db, stop := setupTestNetworkDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
2023-10-03 13:27:42 +00:00
|
|
|
nm := NewManager(db)
|
2021-09-09 14:28:54 +00:00
|
|
|
err := nm.Init(initNetworks)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
network := nm.Find(1)
|
|
|
|
require.NotNil(t, network)
|
|
|
|
|
|
|
|
newName := "New Chain Name"
|
|
|
|
network.ChainName = newName
|
|
|
|
err = nm.Upsert(network)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
network = nm.Find(1)
|
|
|
|
require.NotNil(t, network)
|
|
|
|
require.Equal(t, newName, network.ChainName)
|
|
|
|
}
|