Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Thirdweb.Tests/Thirdweb.Contracts/Thirdweb.Contracts.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ public async Task PrepareTest_NoSig()
Assert.Contains("Method signature not found in contract ABI.", exception.Message);
}

[Fact(Timeout = 120000)]
public async Task ReadTest_TupleIntoArray()
{
var contract = await ThirdwebContract.Create(this.Client, "0xEb4AAB0253a50918a2Cbb7ADBaab78Ad19C07Bb1", 421614);
var result = await contract.Read<List<object>>("getReserves");
Assert.NotNull(result);
Assert.NotEmpty(result);
Assert.True(result.Count == 3);
}

[Fact(Timeout = 120000)]
public async Task ReadTest_TupleIntoList()
{
var contract = await ThirdwebContract.Create(this.Client, "0xEb4AAB0253a50918a2Cbb7ADBaab78Ad19C07Bb1", 421614);
var result = await contract.Read<List<object>>("getReserves");
Assert.NotNull(result);
Assert.NotEmpty(result);
Assert.True(result.Count == 3);
}

private sealed class AllowlistProof
{
public List<byte[]> Proof { get; set; } = new List<byte[]>();
Expand Down
37 changes: 36 additions & 1 deletion Thirdweb/Thirdweb.Contracts/ThirdwebContract.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Numerics;
using Nethereum.ABI.FunctionEncoding;
using Nethereum.ABI.Model;
using Nethereum.Contracts;
using Nethereum.Hex.HexTypes;
using Newtonsoft.Json;

namespace Thirdweb;

Expand Down Expand Up @@ -119,7 +122,39 @@ public static async Task<T> Read<T>(ThirdwebContract contract, string method, pa
var data = function.GetData(parameters);
var resultData = await rpc.SendRequestAsync<string>("eth_call", new { to = contract.Address, data }, "latest").ConfigureAwait(false);

return function.DecodeTypeOutput<T>(resultData);
if ((typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(List<>)) || typeof(T).IsArray)
{
var functionAbi = contractRaw.ContractBuilder.ContractABI.FindFunctionABIFromInputData(data);
var decoder = new FunctionCallDecoder();
var outputList = new FunctionCallDecoder().DecodeDefaultData(resultData.HexToBytes(), functionAbi.OutputParameters);
var resultList = outputList.Select(x => x.Result).ToList();

if (typeof(T) == typeof(List<object>))
{
return (T)(object)resultList;
}

if (typeof(T) == typeof(object[]))
{
return (T)(object)resultList.ToArray();
}

try
{
var json = JsonConvert.SerializeObject(resultList);
return JsonConvert.DeserializeObject<T>(json);
}
catch (Exception)
{
var dict = outputList.ConvertToObjectDictionary();
var ser = JsonConvert.SerializeObject(dict.First().Value);
return JsonConvert.DeserializeObject<T>(ser);
}
}
else
{
return function.DecodeTypeOutput<T>(resultData);
}
}

/// <summary>
Expand Down
Loading