2024-07-18 15:30:10 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2024-08-15 11:49:29 +00:00
|
|
|
"encoding/json"
|
2024-07-26 20:00:12 +00:00
|
|
|
"fmt"
|
2024-07-18 15:30:10 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
2024-08-15 11:49:29 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
2024-07-18 15:30:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFailToSwitchEthereumChainWithMissingDAppFields(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_SwitchEthereumChain)
|
|
|
|
t.Cleanup(close)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
// Missing DApp fields
|
|
|
|
request, err := ConstructRPCRequest("wallet_switchEthereumChain", []interface{}{}, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
result, err := state.cmd.Execute(state.ctx, request)
|
2024-07-18 15:30:10 +00:00
|
|
|
assert.Equal(t, ErrRequestMissingDAppData, err)
|
|
|
|
assert.Empty(t, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailToSwitchEthereumChainWithNoChainId(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_SwitchEthereumChain)
|
|
|
|
t.Cleanup(close)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
request, err := ConstructRPCRequest("wallet_switchEthereumChain", []interface{}{}, &testDAppData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
_, err = state.cmd.Execute(state.ctx, request)
|
2024-07-18 15:30:10 +00:00
|
|
|
assert.Equal(t, ErrEmptyRPCParams, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailToSwitchEthereumChainWithUnsupportedChainId(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_SwitchEthereumChain)
|
|
|
|
t.Cleanup(close)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
params := make([]interface{}, 1)
|
2024-07-26 20:00:12 +00:00
|
|
|
params[0] = map[string]interface{}{
|
|
|
|
"chainId": "0x1a343",
|
|
|
|
} // some unrecoginzed chain id
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
request, err := ConstructRPCRequest("wallet_switchEthereumChain", params, &testDAppData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
_, err = state.cmd.Execute(state.ctx, request)
|
2024-07-18 15:30:10 +00:00
|
|
|
assert.Equal(t, ErrUnsupportedNetwork, err)
|
|
|
|
}
|
|
|
|
|
2024-08-15 11:49:29 +00:00
|
|
|
func TestSwitchEthereumChainSuccess(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_SwitchEthereumChain)
|
|
|
|
t.Cleanup(close)
|
2024-07-18 15:30:10 +00:00
|
|
|
|
2024-08-15 11:49:29 +00:00
|
|
|
chainId := fmt.Sprintf(`0x%s`, walletCommon.ChainID(walletCommon.EthereumMainnet).String())
|
|
|
|
chainIdSwitched := false
|
|
|
|
|
|
|
|
signal.SetMobileSignalHandler(signal.MobileSignalHandler(func(s []byte) {
|
|
|
|
var evt EventType
|
|
|
|
err := json.Unmarshal(s, &evt)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
switch evt.Type {
|
|
|
|
case signal.EventConnectorDAppChainIdSwitched:
|
|
|
|
var ev signal.ConnectorDAppChainIdSwitchedSignal
|
|
|
|
err := json.Unmarshal(evt.Event, &ev)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, chainId, ev.ChainId)
|
|
|
|
assert.Equal(t, testDAppData.URL, ev.URL)
|
|
|
|
chainIdSwitched = true
|
|
|
|
}
|
|
|
|
}))
|
2024-09-03 08:41:56 +00:00
|
|
|
t.Cleanup(signal.ResetMobileSignalHandler)
|
2024-08-15 11:49:29 +00:00
|
|
|
|
2024-07-18 15:30:10 +00:00
|
|
|
params := make([]interface{}, 1)
|
2024-07-26 20:00:12 +00:00
|
|
|
params[0] = map[string]interface{}{
|
|
|
|
"chainId": "0x1",
|
|
|
|
}
|
2024-07-18 15:30:10 +00:00
|
|
|
|
|
|
|
request, err := ConstructRPCRequest("wallet_switchEthereumChain", params, &testDAppData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
err = PersistDAppData(state.walletDb, testDAppData, types.HexToAddress("0x6d0aa2a774b74bb1d36f97700315adf962c69fcg"), walletCommon.EthereumMainnet)
|
2024-07-18 15:30:10 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
response, err := state.cmd.Execute(state.ctx, request)
|
2024-07-18 15:30:10 +00:00
|
|
|
assert.NoError(t, err)
|
2024-07-26 20:00:12 +00:00
|
|
|
assert.Equal(t, chainId, response)
|
2024-08-15 11:49:29 +00:00
|
|
|
assert.True(t, chainIdSwitched)
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|