Skip to content

Commit

Permalink
feat(abi): add ArgsArray, making it easy to pass in IAbiValue par…
Browse files Browse the repository at this point in the history
…ams to AtomicTxn Builder
  • Loading branch information
jasonboukheir committed Jun 14, 2022
1 parent f765159 commit 523d765
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 5 deletions.
65 changes: 65 additions & 0 deletions Runtime/CareBoo.AlgoSdk/SmartContracts/Abi/Args/ArgsArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Unity.Collections;

namespace AlgoSdk.Abi
{
/// <summary>
/// Represents an array of <see cref="IAbiValue"/> args given via params to an ABI method.
/// </summary>
public readonly struct ArgsArray
: IArgEnumerator<ArgsArray>
{
readonly IAbiValue[] values;

readonly int current;

public int Count => values?.Length ?? 0;

public ArgsArray(IAbiValue[] values, int current)
{
this.values = values;
this.current = current;
}

public EncodedAbiArg EncodeCurrent(IAbiType type, AbiReferences references, Allocator allocator)
{
if (Count == 0)
throw new System.NotSupportedException("ArgsArray is empty");

return values[current].Encode(type, references, allocator);
}

public int LengthOfCurrent(IAbiType type)
{
if (Count == 0)
throw new System.NotSupportedException("ArgsArray is empty");

return values[current].Length(type);
}

public bool TryNext(out ArgsArray next)
{
var nextIndex = current + 1;
if (nextIndex >= Count)
{
next = this;
return false;
}

next = new ArgsArray(values, nextIndex);
return true;
}

public bool TryPrev(out ArgsArray prev)
{
var prevIndex = current - 1;
if (prevIndex < 0)
{
prev = this;
return false;
}

prev = new ArgsArray(values, prevIndex);
return true;
}
}
}
11 changes: 11 additions & 0 deletions Runtime/CareBoo.AlgoSdk/SmartContracts/Abi/Args/ArgsArray.cs.meta

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

73 changes: 68 additions & 5 deletions Runtime/CareBoo.AlgoSdk/Transactions/Atomic/AtomicTxn.Building.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using AlgoSdk.Abi;
using AlgoSdk.Crypto;
using AlgoSdk.MessagePack;
using Unity.Collections;
using Unity.Mathematics;

namespace AlgoSdk
{
Expand Down Expand Up @@ -76,17 +74,17 @@ public Building AddTxn<T>(T txn)
/// <param name="txnParams">See <see cref="TransactionParams"/></param>
/// <param name="applicationId">ID of the application being configured.</param>
/// <param name="method">The ABI method definition.</param>
/// <param name="methodArgs">The list of arguments to encode.</param>
/// <param name="onComplete">Defines what additional actions occur with the transaction.</param>
/// <param name="methodArgs">The list of arguments to encode.</param>
/// <typeparam name="T">The type of arg enumerator.</typeparam>
/// <returns>An Atomic Transaction in the Building state, ready to add more transactions or build.</returns>
public Building AddMethodCall<T>(
Address sender,
TransactionParams txnParams,
AppIndex applicationId,
OnCompletion onComplete,
Abi.Method method,
in T methodArgs,
OnCompletion onComplete = OnCompletion.NoOp
in T methodArgs
)
where T : struct, IArgEnumerator<T>
{
Expand All @@ -104,6 +102,71 @@ public Building AddTxn<T>(T txn)
return AddTxn(txn);
}

/// <summary>
/// Encode and apply ABI Method arguments to an <see cref="AppCallTxn"/> then add the transaction to this group.
/// </summary>
/// <param name="sender">The address of the account that pays the fee and amount.</param>
/// <param name="txnParams">See <see cref="TransactionParams"/></param>
/// <param name="applicationId">ID of the application being configured.</param>
/// <param name="method">The ABI method definition.</param>
/// <param name="methodArgs">The list of arguments to encode.</param>
/// <typeparam name="T">The type of arg enumerator.</typeparam>
/// <returns>An Atomic Transaction in the Building state, ready to add more transactions or build.</returns>
public Building AddMethodCall<T>(
Address sender,
TransactionParams txnParams,
AppIndex applicationId,
Abi.Method method,
in T methodArgs
)
where T : struct, IArgEnumerator<T>
{
return AddMethodCall(sender, txnParams, applicationId, OnCompletion.NoOp, method, in methodArgs);
}

/// <summary>
/// Encode and apply ABI Method arguments to an <see cref="AppCallTxn"/> then add the transaction to this group.
/// </summary>
/// <param name="sender">The address of the account that pays the fee and amount.</param>
/// <param name="txnParams">See <see cref="TransactionParams"/></param>
/// <param name="applicationId">ID of the application being configured.</param>
/// <param name="method">The ABI method definition.</param>
/// <param name="onComplete">Defines what additional actions occur with the transaction.</param>
/// <param name="methodArgs">The list of arguments to encode.</param>
/// <returns>An Atomic Transaction in the Building state, ready to add more transactions or build.</returns>
public Building AddMethodCall(
Address sender,
TransactionParams txnParams,
AppIndex applicationId,
OnCompletion onComplete,
Abi.Method method,
params IAbiValue[] methodArgsParams
)
{
var methodArgs = new ArgsArray(methodArgsParams, 0);
return AddMethodCall(sender, txnParams, applicationId, onComplete, method, methodArgs);
}

/// <summary>
/// Encode and apply ABI Method arguments to an <see cref="AppCallTxn"/> then add the transaction to this group.
/// </summary>
/// <param name="sender">The address of the account that pays the fee and amount.</param>
/// <param name="txnParams">See <see cref="TransactionParams"/></param>
/// <param name="applicationId">ID of the application being configured.</param>
/// <param name="method">The ABI method definition.</param>
/// <param name="methodArgs">The list of arguments to encode.</param>
/// <returns>An Atomic Transaction in the Building state, ready to add more transactions or build.</returns>
public Building AddMethodCall(
Address sender,
TransactionParams txnParams,
AppIndex applicationId,
Abi.Method method,
params IAbiValue[] methodArgsParams
)
{
return AddMethodCall(sender, txnParams, applicationId, OnCompletion.NoOp, method, methodArgsParams);
}

/// <summary>
/// Builds the current Atomic Transaction, generating a group ID and
/// assigning it to all transactions in this group.
Expand Down
6 changes: 6 additions & 0 deletions Runtime/CareBoo.AlgoSdk/Transactions/ITransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public interface ITransaction : ITransactionHeader

public static class TransactionExtensions
{
/// <summary>
/// Converts a transaction to bytes to prepare it for signing.
/// </summary>
/// <param name="txn">The transaction to convert.</param>
/// <typeparam name="T">The type of the transaction.</typeparam>
/// <returns>Bytes in the transaction that are used for signing.</returns>
public static byte[] ToSignatureMessage<T>(this T txn)
where T : ITransaction
{
Expand Down

0 comments on commit 523d765

Please sign in to comment.