Skip to content

Commit

Permalink
Native to Transaction Receipt Jobject converter (#165)
Browse files Browse the repository at this point in the history
* Native to Transaction Receipt Jobject converter

* Included Obsolete tag to jobject native receipt

* Fix for Json converter

Fixed bad conversion from native response Jobject to transaction receipt

* Constructor fix

Added null check for NativeReceipt JObject before conversion to TransactionReceipt

* Still parse nativeReceipt

---------

Co-authored-by: Quinn Purdy <[email protected]>
  • Loading branch information
caballoninja and BellringerQuinn authored Oct 2, 2024
1 parent 837b9f8 commit bee7dfe
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Collections.Generic;
using System;

namespace Sequence.EmbeddedWallet
{
Expand All @@ -11,7 +14,10 @@ public class SuccessfulTransactionReturn : TransactionReturn
public string metaTxHash { get; private set; }
public IntentPayload request { get; private set; }
public MetaTxnReceipt receipt { get; private set; }

[Obsolete("nativeReceipt is deprecated. Please use nativeTransactionReceipt instead.")]
public JObject nativeReceipt { get; private set; }
public TransactionReceipt nativeTransactionReceipt { get; private set; }
public SimulateResult[] simulations { get; private set; }

public SuccessfulTransactionReturn() { }
Expand All @@ -24,6 +30,7 @@ public SuccessfulTransactionReturn(string txHash, string metaTxHash, IntentPaylo
this.request = request;
this.receipt = receipt;
this.nativeReceipt = nativeReceipt;
this.nativeTransactionReceipt = nativeReceipt?.ToObject<TransactionReceipt>(new JsonSerializer { Converters = { new TransactionReceiptConverter() } });
this.simulations = simulations;
}
}
Expand All @@ -38,4 +45,79 @@ public SuccessfulBatchTransactionReturn(SuccessfulTransactionReturn[] successful
}
}

}
public class TransactionReceiptConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(TransactionReceipt);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jsonObject;
try
{
jsonObject = JObject.Load(reader);
}
catch (Exception ex)
{
UnityEngine.Debug.LogError($"Failed to load JSON: {ex.Message}");
throw;
}

TransactionReceipt receipt = new TransactionReceipt();

try
{
receipt.transactionHash = jsonObject["transactionHash"]?.ToString();
receipt.transactionIndex = jsonObject["transactionIndex"]?.ToString();
receipt.blockHash = jsonObject["blockHash"]?.ToString();
receipt.blockNumber = jsonObject["blockNumber"]?.ToString();
receipt.from = jsonObject["from"]?.ToString();
receipt.to = jsonObject["to"]?.ToString();
receipt.cumulativeGasUsed = jsonObject["cumulativeGasUsed"]?.ToString();
receipt.effectiveGasPrice = jsonObject["effectiveGasPrice"]?.ToString();
receipt.gasUsed = jsonObject["gasUsed"]?.ToString();
receipt.contractAddress = jsonObject["contractAddress"]?.ToString();
receipt.logsBloom = jsonObject["logsBloom"]?.ToString();
receipt.type = jsonObject["type"]?.ToString();
receipt.root = jsonObject["root"]?.ToString();
receipt.status = jsonObject["status"]?.ToString();
receipt.logs = jsonObject["logs"]?.ToObject<List<Log>>(serializer);
}
catch (Exception ex)
{
UnityEngine.Debug.LogError($"Error during JSON conversion: {ex.Message}");
throw;
}

return receipt;
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
TransactionReceipt receipt = (TransactionReceipt)value;

JObject receiptObject = new JObject
{
{ "transactionHash", JToken.FromObject(receipt.transactionHash) },
{ "transactionIndex", JToken.FromObject(receipt.transactionIndex) },
{ "blockHash", JToken.FromObject(receipt.blockHash) },
{ "blockNumber", JToken.FromObject(receipt.blockNumber) },
{ "from", JToken.FromObject(receipt.from) },
{ "to", JToken.FromObject(receipt.to) },
{ "cumulativeGasUsed", JToken.FromObject(receipt.cumulativeGasUsed) },
{ "effectiveGasPrice", JToken.FromObject(receipt.effectiveGasPrice) },
{ "gasUsed", JToken.FromObject(receipt.gasUsed) },
{ "contractAddress", JToken.FromObject(receipt.contractAddress) },
{ "logsBloom", JToken.FromObject(receipt.logsBloom) },
{ "type", JToken.FromObject(receipt.type) },
{ "root", JToken.FromObject(receipt.root) },
{ "status", JToken.FromObject(receipt.status) },
{ "logs", JToken.FromObject(receipt.logs, serializer) }
};

receiptObject.WriteTo(writer);
}
}
}
2 changes: 1 addition & 1 deletion Packages/Sequence-Unity/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xyz.0xsequence.waas-unity",
"version": "3.8.1",
"version": "3.8.2",
"displayName": "Sequence Embedded Wallet SDK",
"description": "A Unity SDK for the Sequence WaaS API",
"unity": "2021.3",
Expand Down

0 comments on commit bee7dfe

Please sign in to comment.