generated from CareBoo/UPMTemplate-2020
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(abi): add
Decode
methods to ABI
also includes new AbiTypeCode
- Loading branch information
1 parent
2623f43
commit a83f1fe
Showing
30 changed files
with
636 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
namespace AlgoSdk.Experimental.Abi | ||
{ | ||
/// <summary> | ||
/// The untagged part of the tagged <see cref="AbiType"/> union. | ||
/// </summary> | ||
public enum AbiTypeCode | ||
{ | ||
/// <summary> | ||
/// Default code | ||
/// </summary> | ||
None, | ||
|
||
/// <summary> | ||
/// A reference to an account, asset, or application represented as a uint8. | ||
/// </summary> | ||
Reference, | ||
|
||
/// <summary> | ||
/// A transaction type that represents a previous transaction in the group. | ||
/// </summary> | ||
Transaction, | ||
|
||
/// <summary> | ||
/// An account address | ||
/// </summary> | ||
Address, | ||
|
||
/// <summary> | ||
/// A boolean value, true or false | ||
/// </summary> | ||
Bool, | ||
|
||
/// <summary> | ||
/// Alias of uint8. | ||
/// </summary> | ||
Byte, | ||
|
||
/// <summary> | ||
/// Fixed sized array of any other type, T[N]. | ||
/// </summary> | ||
FixedArray, | ||
|
||
/// <summary> | ||
/// Variable sized array of any other type, T[]. | ||
/// </summary> | ||
VariableArray, | ||
|
||
/// <summary> | ||
/// An alias of byte[] (variable sized array of bytes). | ||
/// </summary> | ||
String, | ||
|
||
/// <summary> | ||
/// A group of ordered abi types, (T1, T2, T3, ..., TN) | ||
/// </summary> | ||
Tuple, | ||
|
||
/// <summary> | ||
/// A fixed-length, decimal precision number, ufixedNxM. | ||
/// </summary> | ||
UFixed, | ||
|
||
/// <summary> | ||
/// A fixed-length integer, uintN. | ||
/// </summary> | ||
UInt, | ||
|
||
/// <summary> | ||
/// Used to find the count of AbiTypes - 1 (because this enum's starting index is 1). | ||
/// </summary> | ||
AbiTypeCount | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Runtime/CareBoo.AlgoSdk/Experimental/Abi/AbiTypeCode.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
Runtime/CareBoo.AlgoSdk/Experimental/Abi/MethodCallResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using AlgoSdk.Algod; | ||
|
||
namespace AlgoSdk.Experimental.Abi | ||
{ | ||
public struct MethodCallResult | ||
{ | ||
private static readonly byte[] ReturnHash = new byte[] { 0x15, 0x1f, 0x7c, 0x75 }; | ||
public string TxnId { get; set; } | ||
|
||
public byte[] RawValue { get; set; } | ||
|
||
public IAbiValue ReturnValue { get; set; } | ||
|
||
public string DecodeError { get; set; } | ||
|
||
public PendingTransactionResponse TxnInfo { get; set; } | ||
|
||
public Method Method { get; set; } | ||
|
||
public MethodCallResult( | ||
string txnId, | ||
PendingTransactionResponse txnInfo, | ||
Method method | ||
) | ||
{ | ||
TxnId = txnId; | ||
Method = method; | ||
TxnInfo = txnInfo; | ||
(DecodeError, RawValue, ReturnValue) = ParseResult(txnInfo, method); | ||
} | ||
|
||
private static (string, byte[], IAbiValue) ParseResult( | ||
PendingTransactionResponse txnInfo, | ||
Method method | ||
) | ||
{ | ||
if (method.Returns.Type == null) | ||
{ | ||
return (null, null, null); | ||
} | ||
|
||
if (txnInfo.Logs == null || txnInfo.Logs.Length == 0) | ||
{ | ||
return ("app call transaction did not log a return value", null, null); | ||
} | ||
|
||
var result = txnInfo.Logs[txnInfo.Logs.Length - 1]; | ||
if (!CheckReturnHash(result)) | ||
{ | ||
return ("app call transaction did not log a return value", null, null); | ||
} | ||
|
||
var rawValue = new byte[result.Length - ReturnHash.Length]; | ||
Array.Copy(result, ReturnHash.Length, rawValue, 0, rawValue.Length); | ||
|
||
try | ||
{ | ||
var (decodeError, returnValue) = method.Returns.Type.Decode(rawValue); | ||
return (decodeError, rawValue, returnValue); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return (ex.Message, rawValue, null); | ||
} | ||
} | ||
|
||
private static bool CheckReturnHash(byte[] result) | ||
{ | ||
if (result == null || result.Length < ReturnHash.Length) | ||
{ | ||
return false; | ||
} | ||
|
||
for (var i = 0; i < ReturnHash.Length; i++) | ||
{ | ||
if (result[i] != ReturnHash[i]) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Runtime/CareBoo.AlgoSdk/Experimental/Abi/MethodCallResult.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
Runtime/CareBoo.AlgoSdk/Experimental/Abi/PackedBooleans.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
namespace AlgoSdk.Experimental.Abi | ||
{ | ||
/// <summary> | ||
/// Utility struct to handle booleans packed in a single byte. | ||
/// </summary> | ||
public struct PackedBooleans | ||
{ | ||
byte b; | ||
|
||
public bool this[int index] | ||
{ | ||
get | ||
{ | ||
if (index < 0 || index > 7) | ||
{ | ||
throw new System.ArgumentOutOfRangeException(nameof(index)); | ||
} | ||
|
||
return (b & (1 << index)) > 0 ? true : false; | ||
} | ||
set | ||
{ | ||
if (index < 0 || index > 7) | ||
{ | ||
throw new System.ArgumentOutOfRangeException(nameof(index)); | ||
} | ||
|
||
if (value) | ||
{ | ||
b = (byte)(b & ~(1 << index)); | ||
} | ||
else | ||
{ | ||
b = (byte)(b | (1 << index)); | ||
} | ||
} | ||
} | ||
|
||
public PackedBooleans(byte b) | ||
{ | ||
this.b = b; | ||
} | ||
|
||
public static explicit operator byte(PackedBooleans packedBooleans) | ||
{ | ||
return packedBooleans.b; | ||
} | ||
|
||
public static explicit operator PackedBooleans(byte b) | ||
{ | ||
return new PackedBooleans(b); | ||
} | ||
} | ||
} |
Oops, something went wrong.