Skip to content

Commit

Permalink
feat(accounts): add combined interfaces for Account and Signer
Browse files Browse the repository at this point in the history
this is useful for abstracting an account that can sign transactions
  • Loading branch information
jasonboukheir committed Dec 21, 2022
1 parent 5c0ee3e commit 6cfee78
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace Algorand.Unity.WalletConnect
{
public interface IWalletConnectAccount
: IAsyncSigner
, IAccount
: IAsyncAccountSigner
{
/// <summary>
/// The status of the current wallet connected.
Expand Down
15 changes: 15 additions & 0 deletions Runtime/Algorand.Unity/Accounts/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ public interface IAccount
Address Address { get; }
}

public interface IAccountSigner : IAccount, ISigner
{
}

public interface IAsyncAccountSigner : IAccount, IAsyncSigner
{
}

public interface IAsyncAccountSignerWithProgress : IAccount, IAsyncSignerWithProgress
{
}

/// <summary>
/// A local, in-memory account.
/// </summary>
Expand All @@ -22,6 +34,9 @@ public interface IAccount
, ISigner
, IAsyncSigner
, IAsyncSignerWithProgress
, IAccountSigner
, IAsyncAccountSigner
, IAsyncAccountSignerWithProgress
{
private readonly PrivateKey privateKey;

Expand Down
3 changes: 1 addition & 2 deletions Runtime/Algorand.Unity/NodeServices/Kmd/KmdAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ namespace Algorand.Unity
{
[Serializable]
public struct KmdAccount
: IAccount
, IAsyncSigner
: IAsyncAccountSigner
{
[SerializeField] private KmdClient client;

Expand Down
41 changes: 21 additions & 20 deletions Samples~/CallingSmartContractAbi/Runtime/CallingSmartContractAbi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class CallingSmartContractAbi : MonoBehaviour

public SmartContractUI smartContractUI;

private KmdAccount account;
private IAsyncAccountSigner account;

private AlgodClient algod;

Expand All @@ -41,19 +41,31 @@ private void Awake()
indexer = new IndexerClient("http://localhost:8980");
}

private async UniTaskVoid Start()
private void Start()
{
StartAsync().Forget();
}

private async UniTaskVoid StartAsync()
{
smartContractUI.enabled = false;
account = await GetKmdAccount();
await GetOrCreateContract();
smartContractUI.account = account;
smartContractUI.algod = algod;
smartContractUI.contractIndex = contractIndex;
smartContractUI.contract = contract;
try
{
account = await GetAccount();
await GetOrCreateContract();
smartContractUI.account = account;
smartContractUI.algod = algod;
smartContractUI.contractIndex = contractIndex;
smartContractUI.contract = contract;
}
catch (Exception ex)
{
smartContractUI.error = ex.Message;
}
smartContractUI.enabled = true;
}

private async UniTask<KmdAccount> GetKmdAccount()
private async UniTask<IAsyncAccountSigner> GetAccount()
{
var (listWalletsErr, listWalletsResponse) = await kmd.ListWallets();
listWalletsErr.ThrowIfError();
Expand Down Expand Up @@ -128,15 +140,4 @@ private async UniTask<string> SendTransaction(byte[] signedTxn)
error.ThrowIfError();
return response.TxId;
}

public struct PaymentTxnArgs : IEquatable<PaymentTxnArgs>
{
public Address Receiver;
public MicroAlgos Amount;

public bool Equals(PaymentTxnArgs other)
{
return Receiver.Equals(other.Receiver) && Amount.Equals(other.Amount);
}
}
}
16 changes: 14 additions & 2 deletions Samples~/CallingSmartContractAbi/Runtime/SmartContractUI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Linq;
using Algorand.Unity;
using Algorand.Unity.Experimental.Abi;
Expand All @@ -9,10 +8,11 @@
[RequireComponent(typeof(UIDocument))]
public class SmartContractUI : MonoBehaviour
{
public KmdAccount account { get; set; }
public IAsyncAccountSigner account { get; set; }
public AlgodClient algod { get; set; }
public AppIndex contractIndex { get; set; }
public Contract contract { get; set; }
public string error { get; set; }

public UIDocument document { get; protected set; }

Expand All @@ -30,6 +30,18 @@ private void OnEnable()
return;
}

if (!string.IsNullOrEmpty(error))
{
var parent = new VisualElement();
var title = new Label("An error occurred trying to connect to your Algorand sandbox at localhost.");
var message = new Label(error);

parent.Add(title);
parent.Add(message);
document.rootVisualElement.Add(parent);
return;
}

if (contractField == null)
{
contractField = new ContractField(contract, CallContractAsync);
Expand Down

0 comments on commit 6cfee78

Please sign in to comment.