diff --git a/src/ChainSafe.Gaming/Web3/Evm/Wallet/IWalletProvider.cs b/src/ChainSafe.Gaming/Web3/Evm/Wallet/IWalletProvider.cs index cbb30c95b..463c4ffb6 100644 --- a/src/ChainSafe.Gaming/Web3/Evm/Wallet/IWalletProvider.cs +++ b/src/ChainSafe.Gaming/Web3/Evm/Wallet/IWalletProvider.cs @@ -28,6 +28,6 @@ public interface IWalletProvider : IRpcProvider /// /// Throwing exception if we fail to switch to the network. /// Nothing. - Task AddNetworkIfNotExistInWallet(IChainConfig chainConfig = null); + Task SwitchChainAddIfMissing(IChainConfig chainConfig = null); } } \ No newline at end of file diff --git a/src/ChainSafe.Gaming/Web3/Evm/Wallet/WalletProvider.cs b/src/ChainSafe.Gaming/Web3/Evm/Wallet/WalletProvider.cs index b2f03a1e4..245f5f774 100644 --- a/src/ChainSafe.Gaming/Web3/Evm/Wallet/WalletProvider.cs +++ b/src/ChainSafe.Gaming/Web3/Evm/Wallet/WalletProvider.cs @@ -52,7 +52,7 @@ public async Task HandleChainSwitching() private async Task SwitchChain(IChainConfig chainId) { - await AddNetworkIfNotExistInWallet(chainId); + await SwitchChainAddIfMissing(chainId); } private async Task GetWalletChainId() @@ -65,7 +65,7 @@ private async Task GetWalletChainId() return number.ToString(CultureInfo.InvariantCulture); } - public async Task AddNetworkIfNotExistInWallet(IChainConfig config = null) + public async Task SwitchChainAddIfMissing(IChainConfig config = null) { var chainId = await GetWalletChainId(); var chainConfig = config ?? this.chainConfig; @@ -76,40 +76,33 @@ public async Task AddNetworkIfNotExistInWallet(IChainConfig config = null) return; } - var addChainParameters = new[] + var addChainParameter = new { - new + chainId = "0x" + ulong.Parse(chainConfig.ChainId).ToString("X"), + chainName = chainConfig.Chain, + nativeCurrency = new { - chainId = "0x" + ulong.Parse(chainConfig.ChainId).ToString("X"), - chainName = chainConfig.Chain, - nativeCurrency = new - { - name = chainConfig.NativeCurrency.Name, - symbol = chainConfig.NativeCurrency.Symbol, - decimals = chainConfig.NativeCurrency.Decimals, - }, - rpcUrls = new[] { chainConfig.Rpc }, - blockExplorerUrls = new[] { chainConfig.BlockExplorerUrl }, + name = chainConfig.NativeCurrency.Name, + symbol = chainConfig.NativeCurrency.Symbol, + decimals = chainConfig.NativeCurrency.Decimals, }, + rpcUrls = new[] { chainConfig.Rpc }, + blockExplorerUrls = new[] { chainConfig.BlockExplorerUrl }, }; - using (operationTracker.TrackOperation($"Adding or Switching the network to: {chainConfig.Chain}...")) + using (operationTracker.TrackOperation($"Switching the network to: {chainConfig.Chain}...\n(The network will be added if it's missing)")) { try { - await AddChain(addChainParameters); + // this will switch to the network and add it to the wallet if it's missing + await Request("wallet_addEthereumChain", addChainParameter); } catch (Exception e) { - logWriter.LogError($"Failed to add and switch to the network: {e.Message}"); + logWriter.LogError($"Failed to add or switch to the network: {e.Message}"); throw new InvalidOperationException("Failed to add or switch to the network.", e); } } } - - protected async Task AddChain(object addChainParameters) - { - await Request("wallet_addEthereumChain", addChainParameters); - } } } \ No newline at end of file diff --git a/src/ChainSafe.Gaming/Web3/Evm/Wallet/WalletSigner.cs b/src/ChainSafe.Gaming/Web3/Evm/Wallet/WalletSigner.cs index af9f11551..bc700f711 100644 --- a/src/ChainSafe.Gaming/Web3/Evm/Wallet/WalletSigner.cs +++ b/src/ChainSafe.Gaming/Web3/Evm/Wallet/WalletSigner.cs @@ -30,7 +30,7 @@ public virtual async ValueTask WillStartAsync() PublicAddress = address.AssertIsPublicAddress(); - await walletProvider.AddNetworkIfNotExistInWallet(); + await walletProvider.SwitchChainAddIfMissing(); } public virtual async Task SignMessage(string message)