Skip to content

Commit bee7dfe

Browse files
Native to Transaction Receipt Jobject converter (#165)
* 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]>
1 parent 837b9f8 commit bee7dfe

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/DataTypes/ReturnTypes/SuccessfulTransactionReturn.cs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using Newtonsoft.Json;
22
using Newtonsoft.Json.Linq;
3+
using Newtonsoft.Json;
4+
using System.Collections.Generic;
5+
using System;
36

47
namespace Sequence.EmbeddedWallet
58
{
@@ -11,7 +14,10 @@ public class SuccessfulTransactionReturn : TransactionReturn
1114
public string metaTxHash { get; private set; }
1215
public IntentPayload request { get; private set; }
1316
public MetaTxnReceipt receipt { get; private set; }
17+
18+
[Obsolete("nativeReceipt is deprecated. Please use nativeTransactionReceipt instead.")]
1419
public JObject nativeReceipt { get; private set; }
20+
public TransactionReceipt nativeTransactionReceipt { get; private set; }
1521
public SimulateResult[] simulations { get; private set; }
1622

1723
public SuccessfulTransactionReturn() { }
@@ -24,6 +30,7 @@ public SuccessfulTransactionReturn(string txHash, string metaTxHash, IntentPaylo
2430
this.request = request;
2531
this.receipt = receipt;
2632
this.nativeReceipt = nativeReceipt;
33+
this.nativeTransactionReceipt = nativeReceipt?.ToObject<TransactionReceipt>(new JsonSerializer { Converters = { new TransactionReceiptConverter() } });
2734
this.simulations = simulations;
2835
}
2936
}
@@ -38,4 +45,79 @@ public SuccessfulBatchTransactionReturn(SuccessfulTransactionReturn[] successful
3845
}
3946
}
4047

41-
}
48+
public class TransactionReceiptConverter : JsonConverter
49+
{
50+
public override bool CanConvert(Type objectType)
51+
{
52+
return objectType == typeof(TransactionReceipt);
53+
}
54+
55+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
56+
{
57+
JObject jsonObject;
58+
try
59+
{
60+
jsonObject = JObject.Load(reader);
61+
}
62+
catch (Exception ex)
63+
{
64+
UnityEngine.Debug.LogError($"Failed to load JSON: {ex.Message}");
65+
throw;
66+
}
67+
68+
TransactionReceipt receipt = new TransactionReceipt();
69+
70+
try
71+
{
72+
receipt.transactionHash = jsonObject["transactionHash"]?.ToString();
73+
receipt.transactionIndex = jsonObject["transactionIndex"]?.ToString();
74+
receipt.blockHash = jsonObject["blockHash"]?.ToString();
75+
receipt.blockNumber = jsonObject["blockNumber"]?.ToString();
76+
receipt.from = jsonObject["from"]?.ToString();
77+
receipt.to = jsonObject["to"]?.ToString();
78+
receipt.cumulativeGasUsed = jsonObject["cumulativeGasUsed"]?.ToString();
79+
receipt.effectiveGasPrice = jsonObject["effectiveGasPrice"]?.ToString();
80+
receipt.gasUsed = jsonObject["gasUsed"]?.ToString();
81+
receipt.contractAddress = jsonObject["contractAddress"]?.ToString();
82+
receipt.logsBloom = jsonObject["logsBloom"]?.ToString();
83+
receipt.type = jsonObject["type"]?.ToString();
84+
receipt.root = jsonObject["root"]?.ToString();
85+
receipt.status = jsonObject["status"]?.ToString();
86+
receipt.logs = jsonObject["logs"]?.ToObject<List<Log>>(serializer);
87+
}
88+
catch (Exception ex)
89+
{
90+
UnityEngine.Debug.LogError($"Error during JSON conversion: {ex.Message}");
91+
throw;
92+
}
93+
94+
return receipt;
95+
}
96+
97+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
98+
{
99+
TransactionReceipt receipt = (TransactionReceipt)value;
100+
101+
JObject receiptObject = new JObject
102+
{
103+
{ "transactionHash", JToken.FromObject(receipt.transactionHash) },
104+
{ "transactionIndex", JToken.FromObject(receipt.transactionIndex) },
105+
{ "blockHash", JToken.FromObject(receipt.blockHash) },
106+
{ "blockNumber", JToken.FromObject(receipt.blockNumber) },
107+
{ "from", JToken.FromObject(receipt.from) },
108+
{ "to", JToken.FromObject(receipt.to) },
109+
{ "cumulativeGasUsed", JToken.FromObject(receipt.cumulativeGasUsed) },
110+
{ "effectiveGasPrice", JToken.FromObject(receipt.effectiveGasPrice) },
111+
{ "gasUsed", JToken.FromObject(receipt.gasUsed) },
112+
{ "contractAddress", JToken.FromObject(receipt.contractAddress) },
113+
{ "logsBloom", JToken.FromObject(receipt.logsBloom) },
114+
{ "type", JToken.FromObject(receipt.type) },
115+
{ "root", JToken.FromObject(receipt.root) },
116+
{ "status", JToken.FromObject(receipt.status) },
117+
{ "logs", JToken.FromObject(receipt.logs, serializer) }
118+
};
119+
120+
receiptObject.WriteTo(writer);
121+
}
122+
}
123+
}

Packages/Sequence-Unity/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xyz.0xsequence.waas-unity",
3-
"version": "3.8.1",
3+
"version": "3.8.2",
44
"displayName": "Sequence Embedded Wallet SDK",
55
"description": "A Unity SDK for the Sequence WaaS API",
66
"unity": "2021.3",

0 commit comments

Comments
 (0)