Skip to content

Commit

Permalink
fix using coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
fraidev committed Oct 22, 2023
1 parent 3121688 commit 18498de
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 19 deletions.
6 changes: 3 additions & 3 deletions Runtime/Scripts/Beacon/BeaconConnectorDotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Beacon.Sdk;
using Beacon.Sdk.Beacon;
using Beacon.Sdk.Beacon.Operation;
Expand Down Expand Up @@ -32,7 +33,7 @@ public class BeaconConnectorDotNet : IBeaconConnector, IDisposable

#region IBeaconConnector

public async void ConnectAccount()
public async Task ConnectAccount()
{
if (BeaconDappClient != null) return;

Expand Down Expand Up @@ -102,8 +103,7 @@ public void SetWalletMessageReceiver(WalletMessageReceiver messageReceiver)
{
_walletMessageReceiver = messageReceiver;
}

public async Awaitable RequestTezosPermission(string networkName = "", string networkRPC = "")
public async Task RequestTezosPermission(string networkName = "", string networkRPC = "")
{
if (!Enum.TryParse(networkName, out NetworkType networkType))
networkType = TezosConfig.Instance.Network;
Expand Down
5 changes: 3 additions & 2 deletions Runtime/Scripts/Beacon/IBeaconConnector.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using Beacon.Sdk.Beacon.Sign;
using TezosSDK.Tezos.Wallet;
using UnityEngine;
Expand Down Expand Up @@ -27,7 +28,7 @@ public interface IBeaconConnector
/// Starts the connection between Beacon SDK and a wallet to connect to
/// an account
/// </summary>
void ConnectAccount();
Task ConnectAccount();

/// <summary>
/// Checks if there is an active account paired.
Expand All @@ -40,7 +41,7 @@ public interface IBeaconConnector
/// </summary>
/// <param name="networkName">The name of the desired network.</param>
/// <param name="networkRPC">The RPC to the desired network</param>
public Awaitable RequestTezosPermission(string networkName = "", string networkRPC = "");
public Task RequestTezosPermission(string networkName = "", string networkRPC = "");

/// <summary>
/// Allows requesting a new operation such as sending tezos or initiating a smart contract.
Expand Down
15 changes: 15 additions & 0 deletions Runtime/Scripts/Helpers/TaskExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;

namespace TezosSDK.Helpers
{
public static class TaskExtension
{
public static IEnumerator ToCoroutine(this Task task)
{
var t = Task.Run(async () => await task);
yield return new WaitUntil(() => t.IsCompleted);
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Scripts/Helpers/TaskExtension.cs.meta

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

2 changes: 1 addition & 1 deletion Runtime/Scripts/Tezos/TezosConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TezosConfig

public NetworkType Network { get; set; } = NetworkType.ghostnet;

public string RpcBaseUrl => $"https://rpc.{Network}.teztnets.xyz";
public string RpcBaseUrl => $"https://{Network}.tezos.marigold.dev";
public int DefaultTimeoutSeconds => 45;
}

Expand Down
6 changes: 4 additions & 2 deletions Runtime/Scripts/Tezos/Wallet/IWalletProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Beacon.Sdk.Beacon.Sign;
using System.Collections;
using System.Threading.Tasks;
using Beacon.Sdk.Beacon.Sign;
using TezosSDK.Beacon;
using UnityEngine;

Expand All @@ -20,7 +22,7 @@ public interface IWalletProvider
/// Makes a call to connect with a wallet
/// <param name="withRedirectToWallet">Should we open wallet app on mobiles after connect?</param>
/// </summary>
Awaitable Connect(WalletProviderType walletProvider, bool withRedirectToWallet = true);
IEnumerator Connect(WalletProviderType walletProvider, bool withRedirectToWallet = true);

/// <summary>
/// Unpair with wallet and disconnect
Expand Down
29 changes: 20 additions & 9 deletions Runtime/Scripts/Tezos/Wallet/WalletProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public class WalletProvider : IWalletProvider, IDisposable

public WalletProvider()
{
InitBeaconConnector();
CoroutineRunner.Instance.StartWrappedCoroutine(InitBeaconConnector());
}

private async void InitBeaconConnector()
private IEnumerator InitBeaconConnector()
{
// Create or get a WalletMessageReceiver Game object to receive callback messages
var unityBeacon = GameObject.Find("UnityBeacon");
Expand All @@ -39,7 +39,7 @@ private async void InitBeaconConnector()
#else
_beaconConnector = new BeaconConnectorDotNet();
(_beaconConnector as BeaconConnectorDotNet)?.SetWalletMessageReceiver(MessageReceiver);
await Connect(WalletProviderType.beacon, withRedirectToWallet: false);
yield return Connect(WalletProviderType.beacon, withRedirectToWallet: false);

// todo: maybe call RequestTezosPermission from _beaconConnector?
MessageReceiver.PairingCompleted += _ =>
Expand All @@ -49,7 +49,11 @@ private async void InitBeaconConnector()
networkRPC: TezosConfig.Instance.RpcBaseUrl);
};
#endif
MessageReceiver.HandshakeReceived += handshake => { _handshake = handshake; };
MessageReceiver.HandshakeReceived += handshake =>
{
Debug.Log($"Handshake Received: ${handshake}");
_handshake = handshake;
};
MessageReceiver.AccountConnected += account =>
{
var json = JsonSerializer.Deserialize<JsonElement>(account);
Expand Down Expand Up @@ -79,25 +83,32 @@ public void OnReady()
_beaconConnector.OnReady();
}

public async Awaitable Connect(WalletProviderType walletProvider, bool withRedirectToWallet)
public IEnumerator Connect(WalletProviderType walletProvider, bool withRedirectToWallet)
{
Debug.Log("InitWalletProvider");
_beaconConnector.InitWalletProvider(
network: TezosConfig.Instance.Network.ToString(),
rpc: TezosConfig.Instance.RpcBaseUrl,
walletProviderType: walletProvider);
_beaconConnector.ConnectAccount();
Debug.Log("ConnectAccount");
var connectAccountCoroutine = _beaconConnector.ConnectAccount().ToCoroutine();
yield return connectAccountCoroutine;

#if UNITY_ANDROID || UNITY_IOS
if (withRedirectToWallet){
await _beaconConnector.RequestTezosPermission(
Debug.Log("RequestTezosPermission");
yield return _beaconConnector.RequestTezosPermission(
networkName: TezosConfig.Instance.Network.ToString(),
networkRPC: TezosConfig.Instance.RpcBaseUrl);
networkRPC: TezosConfig.Instance.RpcBaseUrl).ToCoroutine();
if (string.IsNullOrEmpty(_handshake))
{
//No handshake, Waiting for handshake...
await Awaitable.WaitForSecondsAsync(2.5f);
Debug.Log("No handshake, Waiting for handshake...");
// yield return new WaitUntil(() => string.IsNullOrEmpty(_handshake));
yield return new WaitForSecondsRealtime(2.5f);
}
if (!string.IsNullOrEmpty(_handshake)){
Debug.Log("tezos://?type=tzip10&data=" + _handshake);
Application.OpenURL($"tezos://?type=tzip10&data={_handshake}");
}
}
Expand Down
4 changes: 2 additions & 2 deletions Runtime/View/AuthenticationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public void DisconnectWallet()
_tezos.Wallet.Disconnect();
}

public async void ConnectByDeeplink()
public void ConnectByDeeplink()
{
await _tezos.Wallet.Connect(WalletProviderType.beacon);
_tezos.Wallet.Connect(WalletProviderType.beacon);
}

void EnableUI(bool isAuthenticated)
Expand Down

0 comments on commit 18498de

Please sign in to comment.