Skip to content

Commit

Permalink
feat(assets): ✨ add AlgorandStandardAsset for creating tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonboukheir committed Oct 24, 2023
1 parent dde5ee0 commit af702e0
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 71 deletions.

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,17 +1,17 @@
using UnityEditor;
using UnityEngine;

namespace Algorand.Unity.Samples.CreatingAsas.Editor
namespace Algorand.Unity.Editor
{
[CustomEditor(typeof(AssetObject))]
public class AssetObjectEditor : UnityEditor.Editor
[CustomEditor(typeof(AlgorandStandardAsset))]
public class AlgorandStandardAssetEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();

var assetObject = (AssetObject)serializedObject.targetObject;
if (assetObject.Index == 0)
var assetObject = (AlgorandStandardAsset)serializedObject.targetObject;
if (assetObject.index == 0)
{
EditorGUILayout.Space();
if (GUILayout.Button("Create Asset"))
Expand All @@ -21,4 +21,4 @@ public override void OnInspectorGUI()
}
}
}
}
}

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
Expand Up @@ -5,17 +5,17 @@
using UnityEngine;
using UnityEngine.UIElements;

namespace Algorand.Unity.Samples.CreatingAsas.Editor
namespace Algorand.Unity.Editor
{
public class AssetCreateWindow : EditorWindow
{
[SerializeField] private AssetObject asset;
public AlgorandStandardAsset asset;

[SerializeField] private AccountObject creatorAccount;
public AccountAsset creatorAccount;

[SerializeField] private AlgodClientObject algod;
public AlgodClientAsset algod;

public static void Show(AssetObject asset)
public static void Show(AlgorandStandardAsset asset)
{
var window = GetWindow<AssetCreateWindow>("Create ASA");
window.asset = asset;
Expand Down Expand Up @@ -50,39 +50,40 @@ private async UniTaskVoid CreateAsaAsync()
throw new ArgumentNullException(nameof(algod));

// check algod health
var healthResponse = await algod.Client.HealthCheck();
var healthResponse = await algod.client.HealthCheck();
if (healthResponse.Error)
throw new Exception($"Algod health check failed: {healthResponse.Error}");

// get txn params
var (txnParamsErr, txnParams) = await algod.Client.TransactionParams();
var (txnParamsErr, txnParams) = await algod.client.TransactionParams();
if (txnParamsErr)
throw new Exception(txnParamsErr);

// construct and sign the transaction
var txn = Transaction.AssetCreate(
sender: creatorAccount.Address,
txnParams: txnParams,
assetParams: asset.Params
assetParams: asset.assetParams
);
var signedTxn = creatorAccount.SignTxn(txn);
var txns = new AssetConfigTxn[] { txn };
var signedTxns = await creatorAccount.SignTxnsAsync(txns, TxnIndices.All);

// send the transaction
var (submitTxnErr, txnId) = await algod.Client.SendTransaction(signedTxn);
var (submitTxnErr, txnId) = await algod.client.SendTransaction(signedTxns[0]);
if (submitTxnErr)
throw new Exception(submitTxnErr);
Debug.Log($"Submitted txn with id: {txnId.TxId}");

// wait for confirmation
var (txnConfirmErr, confirmedTxn) = await algod.Client.WaitForConfirmation(txnId.TxId);
var (txnConfirmErr, confirmedTxn) = await algod.client.WaitForConfirmation(txnId.TxId);
if (txnConfirmErr)
throw new Exception(txnConfirmErr);

// Apply index and network to the ASA
var serializedObject = new SerializedObject(asset);
serializedObject.Update();
asset.Index = confirmedTxn.AssetIndex.Value;
asset.Network = algod.Network;
asset.index = confirmedTxn.AssetIndex.Value;
asset.network = algod.network;
serializedObject.ApplyModifiedPropertiesWithoutUndo();
Debug.Log("Asset Created!");

Expand Down

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
@@ -0,0 +1,13 @@
using UnityEngine;

namespace Algorand.Unity
{
[HelpURL(DocUrl.Api + "Algorand.Unity.AlgorandStandardAsset.html")]
[CreateAssetMenu(menuName = "Algorand/ASA")]
public class AlgorandStandardAsset : ScriptableObject
{
public AssetIndex index;
public AlgorandNetwork network;
public AssetParams assetParams;
}
}

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

11 changes: 0 additions & 11 deletions Samples~/CreatingAsas/Editor/AssetObjectEditor.cs.meta

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Algorand.Unity.Samples.CreatingAsas.Editor
{
[CustomEditor(typeof(AccountObject))]
[CustomEditor(typeof(UnsafeAccountAsset))]
public class AccountObjectEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
Expand All @@ -12,7 +12,7 @@ public override void OnInspectorGUI()
"This asset should only be used for sample purposes. Storing private keys in an asset is not safe for production.",
MessageType.Warning);
DrawDefaultInspector();
var accountObject = (AccountObject)serializedObject.targetObject;
var accountObject = (UnsafeAccountAsset)serializedObject.targetObject;
using (new GUILayout.HorizontalScope())
{
if (GUILayout.Button("Generate new Account"))
Expand Down
12 changes: 0 additions & 12 deletions Samples~/CreatingAsas/Runtime/AlgodClientObject.cs

This file was deleted.

12 changes: 0 additions & 12 deletions Samples~/CreatingAsas/Runtime/AssetObject.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
using System;
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;

namespace Algorand.Unity.Samples.CreatingAsas
{
[CreateAssetMenu]
public class AccountObject
: ScriptableObject
, IAccount
, ISigner
, ISerializationCallbackReceiver
public class UnsafeAccountAsset
: AccountAsset
, ISigner
, ISerializationCallbackReceiver
{
[SerializeField] private Mnemonic mnemonic;

[SerializeField] private Address address;

private Account account;

public Address Address => account.Address;
public override Address Address => account.Address;

public SignedTxn<T> SignTxn<T>(T txn)
where T : ITransaction, IEquatable<T> => account.SignTxn(txn);

public SignedTxn<T>[] SignTxns<T>(T[] txns, TxnIndices txnsToSign)
where T : ITransaction, IEquatable<T> => account.SignTxns(txns, txnsToSign);

public override UniTask<SignedTxn<T>[]> SignTxnsAsync<T>(T[] txns, TxnIndices txnsToSign, CancellationToken cancellationToken = default)
{
return account.SignTxnsAsync(txns, txnsToSign, cancellationToken);
}

[System.Diagnostics.Conditional("UNITY_EDITOR")]
[ContextMenu(nameof(GenerateNewAccount))]
public void GenerateNewAccount()
Expand Down
3 changes: 0 additions & 3 deletions Samples~/CreatingAsas/TestNetAlgod.asset

This file was deleted.

0 comments on commit af702e0

Please sign in to comment.