Skip to content

Commit

Permalink
Toggle chain not working with Metamask in WebGL #1257 (#1261)
Browse files Browse the repository at this point in the history
  • Loading branch information
creeppak authored Dec 16, 2024
1 parent f050d60 commit 7ff030d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/ChainSafe.Gaming/Web3/Evm/Wallet/IWalletProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public interface IWalletProvider : IRpcProvider
/// </summary>
/// <exception cref="InvalidOperationException">Throwing exception if we fail to switch to the network.</exception>
/// <returns>Nothing.</returns>
Task AddNetworkIfNotExistInWallet(IChainConfig chainConfig = null);
Task SwitchChainAddIfMissing(IChainConfig chainConfig = null);
}
}
37 changes: 15 additions & 22 deletions src/ChainSafe.Gaming/Web3/Evm/Wallet/WalletProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task HandleChainSwitching()

private async Task SwitchChain(IChainConfig chainId)
{
await AddNetworkIfNotExistInWallet(chainId);
await SwitchChainAddIfMissing(chainId);
}

private async Task<string> GetWalletChainId()
Expand All @@ -65,7 +65,7 @@ private async Task<string> 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;
Expand All @@ -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<object[]>("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<object[]>("wallet_addEthereumChain", addChainParameters);
}
}
}
2 changes: 1 addition & 1 deletion src/ChainSafe.Gaming/Web3/Evm/Wallet/WalletSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public virtual async ValueTask WillStartAsync()

PublicAddress = address.AssertIsPublicAddress();

await walletProvider.AddNetworkIfNotExistInWallet();
await walletProvider.SwitchChainAddIfMissing();
}

public virtual async Task<string> SignMessage(string message)
Expand Down

0 comments on commit 7ff030d

Please sign in to comment.