Как получить массив объектов из смарт-контракта с помощью Nethereum
У меня есть функция в смарт-контракте:
struct DrugBox {
uint256 weight; // weight is accumulated by delegation
uint256 creationDate;
address producer;
string drugName;
uint256 id;
}
function getAllBoxes() public view returns (DrugBox[] memory box1) {
return boxes;
}
И у меня есть код на C#. Я хочу вернуть список коробок с лекарствами из смарт-контракта
[FunctionOutput]
public class DrugBoxesDTO
{
[Parameter("tuple[]", "box1", 1)]
public List<DrugBoxDTO> Boxes { get; set; }
}
public class DrugBoxDTO
{
public string DrugName { get; set; }
public string Producer { get; set; }
public int Weight { get; set; }
public int Id { get; set; }
}
Task<DrugBoxesDTO> qwe = drugStoreContract.GetFunction("getAllBoxes").CallAsync<DrugBoxesDTO>();
Но получаю ошибку:
System.AggregateException
HResult=0x80131500
Message=One or more errors occurred. (Arrays containing Dynamic Types are not supported)
Inner Exception 1:
NotSupportedException: Arrays containing Dynamic Types are not supported
Как правильно десериализовать список объектов?
1 ответ
Для полей в классе DrugBoxDTO вам также необходимо добавить атрибут Parameter . А для полей Id и Weight используйте BigInteger вместо int, так как вы используете uint256 в контракте.
public class DrugBoxDTO
{
[Parameter("uint256", "weight", 1)]
public BigInteger Weight { get; set; }
[Parameter("uint256", "creationDate", 2)]
public BigInteger CreationDate { get; set; }
[Parameter("address", "producer", 3)]
public string Producer { get; set; }
[Parameter("string", "drugName", 4)]
public string DrugName { get; set; }
[Parameter("uint256", "token", 5)]
public BigInteger Id { get; set; }
}
Предлагаю использовать инструменты генерации кода nethereum для генерации кода C#:https://docs.nethereum.com/en/latest/nethereum-code-generation/