Skip to content

Commit

Permalink
Pass enableAnalytics flag into AppKit Wagmi
Browse files Browse the repository at this point in the history
  • Loading branch information
skibitsky committed Aug 23, 2024
1 parent ac24fe6 commit 6898066
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 106 deletions.
4 changes: 3 additions & 1 deletion Packages/com.walletconnect.web3modal/Plugins/Web3Modal.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ mergeInto(LibraryManager.library, {
const chains = parameters.chains;

const enableOnramp = parameters.enableOnramp;
const enableAnalytics = parameters.enableAnalytics;

// Load the scripts and initialize the configuration
import("https://cdn.jsdelivr.net/npm/@web3modal/cdn@5.0.11/dist/wagmi.js").then(CDNW3M => {
import("https://cdn.jsdelivr.net/npm/@web3modal/cdn@5.1.2/dist/wagmi.js").then(CDNW3M => {
const WagmiCore = CDNW3M['WagmiCore'];
const Chains = CDNW3M['Chains'];
const Web3modal = CDNW3M['Web3modal'];
Expand Down Expand Up @@ -107,6 +108,7 @@ mergeInto(LibraryManager.library, {
wagmiConfig: config,
projectId,
enableOnramp: enableOnramp,
enableAnalytics: enableAnalytics,
disableAppend: true,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ namespace WalletConnect.Web3Modal
#if UNITY_WEBGL
public class WebGlConnector : Connector
{

[DllImport("__Internal")]
private static extern void Initialize(string parameters, Action callback);

private static TaskCompletionSource<bool> _initializationTaskCompletionSource;

private string _lastAccountStatus;

public WebGlConnector()
Expand All @@ -36,28 +35,29 @@ protected override async Task InitializeAsyncCore(Web3ModalConfig web3ModalConfi
.Where(c => !string.IsNullOrWhiteSpace(c.ViemName))
.Select(c => c.ViemName)
.ToArray();

var parameters = new WebGlInitializeParameters
{
projectId = walletConnectConfig.Id,
metadata = walletConnectConfig.Metadata,
chains = viemChainNames,
enableOnramp = web3ModalConfig.enableOnramp,
enableAnalytics = web3ModalConfig.enableAnalytics
};

var parametersJson = JsonConvert.SerializeObject(parameters);

#pragma warning disable S2696
_initializationTaskCompletionSource = new TaskCompletionSource<bool>();
#pragma warning restore S2696

Initialize(parametersJson, InitializationCallback);

await _initializationTaskCompletionSource.Task;

WagmiInterop.InitializeEvents();
ModalInterop.InitializeEvents();

WagmiInterop.WatchAccountTriggered += WatchAccountTriggeredHandler;
WagmiInterop.WatchChainIdTriggered += WatchChainIdTriggeredHandler;
}
Expand All @@ -79,9 +79,9 @@ protected override async Task<bool> TryResumeSessionAsyncCore()
if (getAccountResult.isConnecting)
{
var tcs = new TaskCompletionSource<bool>();

WagmiInterop.WatchAccountTriggered += WagmiInteropOnWatchAccountTriggered;

void WagmiInteropOnWatchAccountTriggered(GetAccountReturnType arg)
{
if (arg.isConnecting)
Expand All @@ -91,6 +91,7 @@ void WagmiInteropOnWatchAccountTriggered(GetAccountReturnType arg)

WagmiInterop.WatchAccountTriggered -= WagmiInteropOnWatchAccountTriggered;
}

var result = await tcs.Task;

return result;
Expand Down Expand Up @@ -125,12 +126,12 @@ protected override async Task<Account[]> GetAccountsCore()
.Select(addr => new Account(addr, chainId))
.ToArray();
}

private void WatchAccountTriggeredHandler(GetAccountReturnType arg)
{
var previousLastAccountStatus = _lastAccountStatus;
_lastAccountStatus = arg.status;

var account = new Account(arg.address, $"eip155:{arg.chainId}");

if (_lastAccountStatus == "connected" && previousLastAccountStatus != "connected")
Expand All @@ -155,11 +156,11 @@ private void WatchChainIdTriggeredHandler(int ethChainId)
{
if (ethChainId == default)
return;

var chainId = $"eip155:{ethChainId}";
OnChainChanged(new ChainChangedEventArgs(chainId));
}

[MonoPInvokeCallback(typeof(Action))]
public static void InitializationCallback()
{
Expand All @@ -173,8 +174,9 @@ internal class WebGlInitializeParameters
public string projectId;
public Metadata metadata;
public string[] chains;

public bool enableOnramp;
public bool enableAnalytics;
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class EventsController
public async Task InitializeAsync(Web3ModalConfig config, ApiController apiController)
{
#if UNITY_WEBGL && !UNITY_EDITOR
_state = AnalyticsState.Disabled;
return;
#endif

Expand Down Expand Up @@ -68,7 +69,7 @@ public async void SendEvent(Event @event)

var requestJson = JsonConvert.SerializeObject(request);

Debug.Log($"[EventsController] Sending event: {@event.name}");
Debug.Log($"[EventsController] Sending event: {@event.name}.\n\nRequest payload:\n {requestJson}");

await _httpClient.PostAsync("e", requestJson);
}
Expand Down Expand Up @@ -104,7 +105,7 @@ public struct Event
[JsonProperty("event")]
public string name;

[JsonProperty("properties")]
[JsonProperty("properties", NullValueHandling = NullValueHandling.Ignore)]
public IDictionary<string, object> properties;
}
}

This file was deleted.

This file was deleted.

0 comments on commit 6898066

Please sign in to comment.