Skip to content

Commit

Permalink
Merge pull request #46 from reown-com/feat/webgl-custom-chain
Browse files Browse the repository at this point in the history
feat: custom chains in webgl
  • Loading branch information
skibitsky authored Jan 22, 2025
2 parents 964c6a4 + 6a2cd86 commit ff8853e
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 43 deletions.
14 changes: 7 additions & 7 deletions src/Reown.AppKit.Unity/Plugins/AppKit.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,30 @@ mergeInto(LibraryManager.library, {

const projectId = parameters.projectId;
const metadata = parameters.metadata;
const chains = parameters.chains;

const chains = parameters.supportedChains;
const enableEmail = parameters.enableEmail;
const enableOnramp = parameters.enableOnramp;
const enableAnalytics = parameters.enableAnalytics;

// Load the scripts and initialize the configuration
import("https://cdn.jsdelivr.net/npm/@reown/[email protected].0/dist/appkit.js").then(AppKit => {
import("https://cdn.jsdelivr.net/npm/@reown/[email protected].4/dist/appkit.js").then(AppKit => {
const WagmiCore = AppKit['WagmiCore'];
const WagmiAdapter = AppKit['WagmiAdapter'];
const Chains = AppKit['networks'];
const reconnect = WagmiCore['reconnect'];
const createAppKit = AppKit['createAppKit'];

const chainsArr = chains.map(chainName => Chains[chainName]);

const networks = chains.map(c => Chains.defineChain(c));
const wagmiAdapter = new WagmiAdapter({
networks: chainsArr,
networks: networks,
projectId
})

const modal = createAppKit({
adapters: [wagmiAdapter],
networks: chainsArr,
networks: networks,
metadata: metadata,
projectId,
features: {
Expand Down
25 changes: 8 additions & 17 deletions src/Reown.AppKit.Unity/Runtime/Chain.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Reown.AppKit.Unity
{
Expand All @@ -9,6 +10,7 @@ public class Chain
public virtual string Name { get; }

// https://github.com/wevm/viem/blob/main/src/chains/index.ts
[Obsolete("The ViemName property will be removed")]
public virtual string ViemName { get; }
public virtual Currency NativeCurrency { get; }
public virtual BlockExplorer BlockExplorer { get; }
Expand Down Expand Up @@ -46,6 +48,11 @@ public Chain(
IsTestnet = isTestnet;
ImageUrl = imageUrl;
ViemName = viemName;

if (!string.IsNullOrWhiteSpace(viemName))
{
Debug.LogWarning($"The ViemName property is deprecated and will be removed in the future. You don't need to set <i>{viemName}</i> for the chain <b>{name}</b> in the `Chain` constructor.");
}
}
}

Expand Down Expand Up @@ -105,7 +112,6 @@ public static class References
public const string Arbitrum = "42161";
public const string Celo = "42220";
public const string CeloAlfajores = "44787";
public const string Solana = "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp";
public const string Polygon = "137";
public const string Avalanche = "43114";
}
Expand Down Expand Up @@ -137,8 +143,6 @@ public static class References
{ References.Polygon, "41d04d42-da3b-4453-8506-668cc0727900" },
// Avalanche
{ References.Avalanche, "30c46e53-e989-45fb-4549-be3bd4eb3b00" },
// Solana
{ References.Solana, "a1b58899-f671-4276-6a5e-56ca5bd59700" }
};

public static class Chains
Expand Down Expand Up @@ -287,18 +291,6 @@ public static class Chains
"avalanche"
);

public static readonly Chain Solana = new(
Namespaces.Solana,
References.Solana,
"Solana",
new Currency("Sol", "SOL", 9),
new BlockExplorer("Solana Explorer", "https://explorer.solana.com"),
"https://api.mainnet-beta.solana.com",
false,
$"{ChainImageUrl}/{ImageIds[References.Solana]}",
"solana"
);

public static readonly IReadOnlyCollection<Chain> All = new HashSet<Chain>
{
Ethereum,
Expand All @@ -312,8 +304,7 @@ public static class Chains
Base,
BaseGoerli,
Polygon,
Avalanche,
Solana
Avalanche
};
}
}
Expand Down
25 changes: 6 additions & 19 deletions src/Reown.AppKit.Unity/Runtime/Connectors/WebGl/WebGlConnector.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using AOT;
using Newtonsoft.Json;
using Reown.AppKit.Unity.WebGl.Modal;
using Reown.AppKit.Unity.WebGl.Wagmi;
using Reown.Sign.Models;
using Reown.Sign.Nethereum.Model;
using Reown.Sign.Unity;

namespace Reown.AppKit.Unity
Expand All @@ -28,19 +30,19 @@ public WebGlConnector()

protected override async Task InitializeAsyncCore(AppKitConfig appKitConfig, SignClientUnity _)
{
var viemChainNames = appKitConfig.supportedChains
.Where(c => !string.IsNullOrWhiteSpace(c.ViemName))
.Select(c => c.ViemName)
var supportedChains = appKitConfig.supportedChains
.Select(c => new WebGlChain(c))
.ToArray();

var parameters = new WebGlInitializeParameters
{
projectId = appKitConfig.projectId,
metadata = appKitConfig.metadata,
chains = viemChainNames,
supportedChains = supportedChains,
includeWalletIds = appKitConfig.includedWalletIds ?? Array.Empty<string>(),
excludeWalletIds = appKitConfig.excludedWalletIds ?? Array.Empty<string>(),


enableEmail = appKitConfig.enableEmail,
enableOnramp = appKitConfig.enableOnramp,
enableAnalytics = appKitConfig.enableAnalytics,
Expand Down Expand Up @@ -169,20 +171,5 @@ public static void InitializationCallback()
_initializationTaskCompletionSource.SetResult(true);
}
}

[Serializable]
internal class WebGlInitializeParameters
{
public string projectId;
public Core.Metadata metadata;
public string[] chains;
public string[] includeWalletIds;
public string[] excludeWalletIds;

public bool enableEmail;
public bool enableOnramp;
public bool enableAnalytics;
public bool enableCoinbaseWallet;
}
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using Newtonsoft.Json;

namespace Reown.AppKit.Unity
{
#if UNITY_WEBGL
[Serializable]
internal class WebGlInitializeParameters
{
public string projectId;
public Core.Metadata metadata;
public WebGlChain[] supportedChains;
public string[] includeWalletIds;
public string[] excludeWalletIds;

public bool enableEmail;
public bool enableOnramp;
public bool enableAnalytics;
public bool enableCoinbaseWallet;
}

[Serializable]
internal class WebGlChain
{
[JsonProperty("id")]
public long Id { get; }

[JsonProperty("caipNetworkId")]
public string CaipNetworkId { get; }

[JsonProperty("chainNamespace")]
public string ChainNamespace { get; }

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

[JsonProperty("nativeCurrency")]
public Currency NativeCurrency { get; }

[JsonProperty("rpcUrls")]
public GenericDefault<RpcUrls> RpcUrls { get; }

[JsonProperty("blockExplorers")]
public GenericDefault<BlockExplorer> BlockExplorers { get; }


public WebGlChain(Chain chain)
{
Id = long.Parse(chain.ChainReference);
CaipNetworkId = chain.ChainId;
ChainNamespace = chain.ChainNamespace;
Name = chain.Name;
NativeCurrency = chain.NativeCurrency;
RpcUrls = new GenericDefault<RpcUrls>
{
@default = new RpcUrls
{
http = new[]
{
chain.RpcUrl
}
}
};
BlockExplorers = new GenericDefault<BlockExplorer>
{
@default = chain.BlockExplorer
};
}
}

[Serializable]
internal class GenericDefault<T>
{
[JsonProperty("default")]
public T @default;
}

[Serializable]
internal class RpcUrls
{
[JsonProperty("http")]
public string[] http;
}
#endif
}

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

0 comments on commit ff8853e

Please sign in to comment.