|
1 | 1 | using System.Numerics;
|
| 2 | +using Nethereum.ABI.FunctionEncoding; |
| 3 | +using Nethereum.ABI.Model; |
2 | 4 | using Nethereum.Contracts;
|
3 | 5 | using Nethereum.Hex.HexTypes;
|
| 6 | +using Newtonsoft.Json; |
4 | 7 |
|
5 | 8 | namespace Thirdweb;
|
6 | 9 |
|
@@ -119,7 +122,39 @@ public static async Task<T> Read<T>(ThirdwebContract contract, string method, pa
|
119 | 122 | var data = function.GetData(parameters);
|
120 | 123 | var resultData = await rpc.SendRequestAsync<string>("eth_call", new { to = contract.Address, data }, "latest").ConfigureAwait(false);
|
121 | 124 |
|
122 |
| - return function.DecodeTypeOutput<T>(resultData); |
| 125 | + if ((typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(List<>)) || typeof(T).IsArray) |
| 126 | + { |
| 127 | + var functionAbi = contractRaw.ContractBuilder.ContractABI.FindFunctionABIFromInputData(data); |
| 128 | + var decoder = new FunctionCallDecoder(); |
| 129 | + var outputList = new FunctionCallDecoder().DecodeDefaultData(resultData.HexToBytes(), functionAbi.OutputParameters); |
| 130 | + var resultList = outputList.Select(x => x.Result).ToList(); |
| 131 | + |
| 132 | + if (typeof(T) == typeof(List<object>)) |
| 133 | + { |
| 134 | + return (T)(object)resultList; |
| 135 | + } |
| 136 | + |
| 137 | + if (typeof(T) == typeof(object[])) |
| 138 | + { |
| 139 | + return (T)(object)resultList.ToArray(); |
| 140 | + } |
| 141 | + |
| 142 | + try |
| 143 | + { |
| 144 | + var json = JsonConvert.SerializeObject(resultList); |
| 145 | + return JsonConvert.DeserializeObject<T>(json); |
| 146 | + } |
| 147 | + catch (Exception) |
| 148 | + { |
| 149 | + var dict = outputList.ConvertToObjectDictionary(); |
| 150 | + var ser = JsonConvert.SerializeObject(dict.First().Value); |
| 151 | + return JsonConvert.DeserializeObject<T>(ser); |
| 152 | + } |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + return function.DecodeTypeOutput<T>(resultData); |
| 157 | + } |
123 | 158 | }
|
124 | 159 |
|
125 | 160 | /// <summary>
|
|
0 commit comments