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

Now the default behaviour for switching the chain is actually to try to add it, if it already exists, we're getting a prompt to switch to it. #1250

Merged
merged 8 commits into from
Dec 3, 2024
Merged
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private async void SetTokens()
customTokenDisplay.SetActive(false);
}
// Set native token
nativeTokenSymbolText.text = Web3Unity.Web3.ChainConfig.Symbol.ToUpper();
nativeTokenSymbolText.text = Web3Unity.Web3.ChainConfig.NativeCurrency.Symbol.ToUpper();
var hexBalance = await Web3Unity.Web3.RpcProvider.GetBalance(Web3Unity.Web3.Signer.PublicAddress);
var weiBalance = BigInteger.Parse(hexBalance.ToString());
decimal ethBalance = (decimal)weiBalance / (decimal)Math.Pow(10, 18);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public override async Task Initialize(bool rememberSession)

//1155 is a decimal number, we need to convert it to an integer
InitWeb3Auth(clientId, new HexBigInteger(BigInteger.Parse(chainConfig.ChainId)).HexValue,
chainConfig.Rpc, chainConfig.Network, "", chainConfig.Symbol, "", network.ToString().ToLower(), Initialized, InitializeError);
chainConfig.Rpc, chainConfig.Network, "", chainConfig.NativeCurrency.Symbol, "", network.ToString().ToLower(), Initialized, InitializeError);

await _initializeTcs.Task;
}
Expand Down
22 changes: 22 additions & 0 deletions Packages/io.chainsafe.web3-unity/Editor/OpenPersistenDataPath.cs
kantagara marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using UnityEditor;
using UnityEngine;
using System.IO;

public class OpenPersistentDataPath
{
[MenuItem("Edit/Open Persistent Data Path")]
private static void OpenPersistentDataPathFolder()
{
string path = Application.persistentDataPath;

// Check if the directory exists
if (!Directory.Exists(path))
kantagara marked this conversation as resolved.
Show resolved Hide resolved
{
Debug.LogWarning("Persistent Data Path directory does not exist.");
return;
kantagara marked this conversation as resolved.
Show resolved Hide resolved
}

// Open the folder in the file explorer
EditorUtility.RevealInFinder(path);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Linq;
using ChainSafe.Gaming;
using ChainSafe.Gaming.Web3;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using NativeCurrency = ChainSafe.Gaming.Web3.NativeCurrency;

namespace ChainSafe.GamingSdk.Editor
{
Expand Down Expand Up @@ -85,7 +87,13 @@ public void OnGUI()

chainConfig.Network = EditorGUILayout.TextField("Network", chainConfig.Network);
chainConfig.ChainId = EditorGUILayout.TextField("Chain ID", chainConfig.ChainId);
chainConfig.Symbol = EditorGUILayout.TextField("Symbol", chainConfig.Symbol);
EditorGUILayout.LabelField("Native Currency", EditorStyles.boldLabel);
EditorGUI.indentLevel++;
// Draw fields for NativeCurrency
chainConfig.NativeCurrency.Name = EditorGUILayout.TextField("Name", chainConfig.NativeCurrency.Name);
chainConfig.NativeCurrency.Symbol = EditorGUILayout.TextField("Symbol", chainConfig.NativeCurrency.Symbol);
chainConfig.NativeCurrency.Decimals = EditorGUILayout.IntField("Decimals", chainConfig.NativeCurrency.Decimals);
EditorGUI.indentLevel--;
chainConfig.BlockExplorerUrl = EditorGUILayout.TextField("Block Explorer", chainConfig.BlockExplorerUrl);

GUI.enabled = true;
Expand Down Expand Up @@ -195,7 +203,12 @@ private void UpdateServerMenuInfo(bool chainSwitched = false)
{
chainConfig.Network = chainPrototype.chain;
chainConfig.ChainId = chainPrototype.chainId.ToString();
chainConfig.Symbol = chainPrototype.nativeCurrency.symbol;
chainConfig.NativeCurrency = new NativeCurrency()
{
Name = chainPrototype.nativeCurrency.Name,
Symbol = chainPrototype.nativeCurrency.Symbol,
Decimals = chainPrototype.nativeCurrency.Decimals
};
if (chainPrototype.explorers != null)
{
chainConfig.BlockExplorerUrl = chainPrototype.explorers[0].url;
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using ChainSafe.Gaming.Web3;
using ChainSafe.Gaming.Web3.Core.Chains;
using UnityEngine;

namespace ChainSafe.Gaming
Expand All @@ -10,13 +11,32 @@ public class ChainConfigEntry : IChainConfig
private const string ChainIdDefault = "11155111";
private const string ChainDefault = "Sepolia";
private const string NetworkDefault = "Sepolia";
private const string SymbolDefault = "Seth";

private static readonly NativeCurrencyUnityWrapper DefaultNativeCurrency = new()
{
Name = "Sepolia Ether",
Symbol = "ETH",
Decimals = 18
};

private const string RpcDefault = "https://rpc.sepolia.org";
private const string BlockExplorerUrlDefault = "https://sepolia.etherscan.io";
private const string WsDefault = "";

[field: SerializeField] public string ChainId { get; set; }
[field: SerializeField] public string Symbol { get; set; }

public INativeCurrency NativeCurrency
{
get => nativeCurrency;
set => nativeCurrency = new NativeCurrencyUnityWrapper()
{
Name = value.Name,
Symbol = value.Symbol,
Decimals = value.Decimals
};
}

[SerializeField] private NativeCurrencyUnityWrapper nativeCurrency ;
[field: SerializeField] public string Chain { get; set; }
[field: SerializeField] public string Network { get; set; }
[field: SerializeField] public string Rpc { get; set; }
Expand All @@ -26,7 +46,7 @@ public class ChainConfigEntry : IChainConfig
public static ChainConfigEntry Default => new()
{
ChainId = ChainIdDefault,
Symbol = SymbolDefault,
NativeCurrency = DefaultNativeCurrency,
Chain = ChainDefault,
Network = NetworkDefault,
Rpc = RpcDefault,
Expand All @@ -37,12 +57,23 @@ public class ChainConfigEntry : IChainConfig
public static ChainConfigEntry Empty => new()
{
ChainId = string.Empty,
Symbol = string.Empty,
NativeCurrency = new NativeCurrencyUnityWrapper(),
Chain = "Custom",
Network = string.Empty,
Rpc = string.Empty,
BlockExplorerUrl = string.Empty,
Ws = string.Empty
};
}

/// <summary>
/// This is basically a wrapper for the INativeCurrency interface to allow for Unity serialization.
/// </summary>
[Serializable]
public class NativeCurrencyUnityWrapper : INativeCurrency
{
[field: SerializeField] public string Name { get; set; } = "Ether";
[field: SerializeField] public string Symbol { get; set; } = "ETH";
[field: SerializeField] public int Decimals { get; set; } = 18;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace ChainSafe.Gaming.UnityPackage.Model
{
public struct NativeCurrency
{
public string symbol { get; set; }
[JsonProperty(PropertyName = "symbol")]
public string Symbol { get; set; }

[JsonProperty(PropertyName = "decimals")]
public int Decimals { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
}

public struct Explorer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using ChainSafe.Gaming.Web3;
using ChainSafe.Gaming.Web3.Core.Chains;
using UnityEngine;

namespace ChainSafe.Gaming.UnityPackage
Expand Down Expand Up @@ -39,7 +40,12 @@ public static Web3ConfigAsset Create(string projectId, string chainId, string ch
ChainId = chainId,
Chain = chain,
Network = network,
Symbol = symbol,
NativeCurrency = new NativeCurrencyUnityWrapper()
{
Name = symbol,
Symbol = symbol,
Decimals = 18
},
Rpc = rpc,
Ws = ws,
BlockExplorerUrl = blockExplorerUrl,
Expand Down Expand Up @@ -91,7 +97,7 @@ public LocalhostChainConfig(string chainId, string symbol, string chain, string
var localhostEndPoint = $"127.0.0.1:{port}";

ChainId = chainId;
Symbol = symbol;
NativeCurrency = new NativeCurrencyUnityWrapper(){ Symbol = symbol, Name = symbol, Decimals = 18 };
Chain = chain;
Network = network;
Rpc = $"http://{localhostEndPoint}";
Expand All @@ -100,7 +106,7 @@ public LocalhostChainConfig(string chainId, string symbol, string chain, string
}

public string ChainId { get; }
public string Symbol { get; }
public INativeCurrency NativeCurrency { get; }
public string Chain { get; }
public string Network { get; }
public string Rpc { get; }
Expand Down
5 changes: 0 additions & 5 deletions scripts/lint.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,5 @@ cd ..
:: Run format command in project root
dotnet format --verbosity=d --severity=warn ChainSafe.Gaming.sln --exclude /submodules

:: Navigate to the UnitySampleProject within src and run the format command
pushd .\src\UnitySampleProject
kantagara marked this conversation as resolved.
Show resolved Hide resolved
dotnet format --verbosity=d --severity=warn .\UnitySampleProject.sln
popd

:: Restore the original directory
popd
21 changes: 21 additions & 0 deletions src/ChainSafe.Gaming.Reown/Methods/WalletAddEthereumChain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using ChainSafe.Gaming.Evm.Network;
using Reown.Core.Common.Utils;
using Reown.Core.Network.Models;

namespace ChainSafe.Gaming.Reown.Methods
{
[RpcMethod("wallet_addEthereumChain")]
[RpcRequestOptions(Clock.ONE_MINUTE, 99993)]
public class WalletAddEthereumChain : List<object>
{
public WalletAddEthereumChain(object[] chains)
: base(chains)
{
}

public WalletAddEthereumChain()
{
}
}
}
53 changes: 11 additions & 42 deletions src/ChainSafe.Gaming.Reown/ReownProvider.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;
using ChainSafe.Gaming.Evm.Network;
using ChainSafe.Gaming.Evm.Providers;
using ChainSafe.Gaming.Reown.Connection;
using ChainSafe.Gaming.Reown.Methods;
using ChainSafe.Gaming.Reown.Models;
Expand All @@ -16,6 +20,7 @@
using ChainSafe.Gaming.Web3.Core.Operations;
using ChainSafe.Gaming.Web3.Environment;
using ChainSafe.Gaming.Web3.Evm.Wallet;
using Nethereum.Hex.HexTypes;
using Nethereum.JsonRpc.Client.RpcMessages;
using Newtonsoft.Json;
using Reown.Core.Common.Logging;
Expand Down Expand Up @@ -48,7 +53,7 @@ public class ReownProvider : WalletProvider, ILifecycleParticipant, IConnectionH
private readonly Web3Environment environment;
private readonly IChainConfigSet chainConfigSet;
private readonly IOperationTracker operationTracker;
private readonly IMainThreadRunner mainThreadRunner;
private readonly IRpcProvider rpcProvider;

private SessionStruct session;
private bool connected;
Expand All @@ -66,11 +71,11 @@ public ReownProvider(
Web3Environment environment,
ReownHttpClient reownHttpClient,
IOperationTracker operationTracker,
IMainThreadRunner mainThreadRunner)
IRpcProvider rpcProvider)
: base(environment, chainConfig, operationTracker)
{
this.operationTracker = operationTracker;
this.mainThreadRunner = mainThreadRunner;
this.rpcProvider = rpcProvider;
this.chainConfigSet = chainConfigSet;
this.environment = environment;
analyticsClient = environment.AnalyticsClient;
Expand Down Expand Up @@ -172,6 +177,7 @@ private async Task Initialize()
"eth_sendTransaction",
"eth_getTransactionByHash",
"wallet_switchEthereumChain",
"wallet_addEthereumChain",
"eth_blockNumber",
},
Events = new[]
Expand Down Expand Up @@ -234,8 +240,6 @@ public override async Task<string> Connect()

connected = true;

await CheckAndSwitchNetwork();

return address;
}
catch (Exception e)
Expand All @@ -247,43 +251,6 @@ public override async Task<string> Connect()
}
}

private async Task CheckAndSwitchNetwork()
{
var chainId = ExtractChainIdFromAddress();
if (chainId == $"{EvmNamespace}:{chainConfig.ChainId}")
{
return;
}

const int maxSwitchAttempts = 3;

for (var i = 0; ;)
{
var messageToUser = i == 0
? "Switching wallet network..."
: $"Switching wallet network (attempt {i + 1})...";

using (operationTracker.TrackOperation(messageToUser))
{
try
{
await SwitchChain(chainConfig.ChainId);
UpdateSessionChainId();
return; // success, exit loop
}
catch (ReownNetworkException)
{
if (++i >= maxSwitchAttempts)
{
throw;
}

logWriter.Log("Attempted to switch the network, but was rejected. Trying again...");
}
}
}
}

private void UpdateSessionChainId()
{
var defaultChain = session.Namespaces.Keys.FirstOrDefault();
Expand Down Expand Up @@ -589,6 +556,8 @@ async Task<T> MakeRequest<TRequest>(bool sendChainId = true)
return await MakeRequest<EthSendTransaction>();
case "wallet_switchEthereumChain":
return await MakeRequest<WalletSwitchEthereumChain>(false);
case "wallet_addEthereumChain":
return await MakeRequest<WalletAddEthereumChain>(false);
default:
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public NativeCurrency(string symbol)
Decimals = 18;
kantagara marked this conversation as resolved.
Show resolved Hide resolved
}

public NativeCurrency(string name, string symbol, int decimals)
{
Name = name;
Symbol = symbol;
Decimals = decimals;
}

[JsonProperty("name")]
public string Name { get; set; }

Expand Down
Loading