0

I have function in smart contract:

 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;
    }

And I have code in C#. I want to return list of drug boxes from smart contract

    [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>();

But I get an error:

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

How to deserialize list of object correctly?

1 Answer 1

1

For the fields in DrugBoxDTO class, you need to add Parameter attribute as well. And for Id & Weight fields, use BigInteger instead of int, since you use uint256 in contract.

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; }
}

Would suggest to use nethereum code generation tools to generate C# code: https://docs.nethereum.com/en/latest/nethereum-code-generation/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.