Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggle chain not working with Metamask in WebGL #1257 #1261

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
rob1997 marked this conversation as resolved.
Show resolved Hide resolved
}

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