Skip to content

Commit

Permalink
feat(algod): add utility method AlgodClient.WaitForConfirmation to …
Browse files Browse the repository at this point in the history
…wait for Txn confirmation
  • Loading branch information
jasonboukheir committed Jun 12, 2022
1 parent fcc1b1c commit 1e60a2d
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions Runtime/CareBoo.AlgoSdk/Services/Algod/AlgodClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Threading;
using AlgoSdk.Algod;
using AlgoSdk.LowLevel;
using Cysharp.Threading.Tasks;
using Unity.Collections;
using UnityEngine;

Expand Down Expand Up @@ -55,6 +57,38 @@ public AlgodClient(string address, params Header[] headers) : this(address, null

public Header[] Headers => headers;

public AlgoApiRequest.Sent<PostTransactionsResponse> SendTransaction<T>(SignedTxn<T> txn)
where T : struct, ITransaction, IEquatable<T>
{
using var data = AlgoApiSerializer.SerializeMessagePack(txn, Allocator.Persistent);
return RawTransaction(data.ToArray());
}

/// <summary>
/// Utility method to wait for a transaction to be confirmed given a transaction id.
/// </summary>
/// <param name="txid">The transaction id to wait for.</param>
/// <param name="pollInterval">An optional <see cref="TimeSpan"/> to control how often this method polls the algod service.</param>
/// <param name="cancellationToken">An optional token for cancelling this task early.</param>
/// <returns>The algod response that either caused an error or showed a confirmed round.</returns>
public async UniTask<AlgoApiResponse<PendingTransactionResponse>> WaitForConfirmation(
string txid,
Optional<TimeSpan> pollInterval = default,
CancellationToken cancellationToken = default
)
{
await UniTask.Delay(TimeSpan.FromSeconds(4), cancellationToken: cancellationToken);
var pollInt = pollInterval.Else(TimeSpan.FromMilliseconds(100));
var response = await PendingTransactionInformation(txid)
.WithCancellation(cancellationToken);
while (!response.Error && response.Payload.ConfirmedRound == default)
{
await UniTask.Delay(pollInt, cancellationToken: cancellationToken);
response = await PendingTransactionInformation(txid)
.WithCancellation(cancellationToken);
}
return response;
}

[Obsolete("Call AlgodClient.GetGenesis instead.")]
public AlgoApiRequest.Sent<AlgoApiObject> GetGenesisInformation()
Expand Down Expand Up @@ -155,14 +189,6 @@ public AlgoApiRequest.Sent<NodeStatusResponse> GetStatusAfterWaitingForRound(ulo
return WaitForBlock(round);
}

[Obsolete("Call RawTransaction instead.")]
public AlgoApiRequest.Sent<PostTransactionsResponse> SendTransaction<T>(SignedTxn<T> txn)
where T : struct, ITransaction, IEquatable<T>
{
using var data = AlgoApiSerializer.SerializeMessagePack(txn, Allocator.Persistent);
return RawTransaction(data.ToArray());
}

[Obsolete("Call RawTransaction instead.")]
public AlgoApiRequest.Sent<PostTransactionsResponse> SendTransaction(byte[] txn)
{
Expand Down

0 comments on commit 1e60a2d

Please sign in to comment.