1

I'm trying to interact with the smart contract that I have created. At the moment I deploy the contract on GANACHE. Adding new items into contract works.

This is the part that doesn't work, and it looks like this:

function getCarEvents(
        string memory vin
    ) external view returns (CarEvent[] memory) {
        require(
            bytes(allCars[vin].id).length > 0,
            "Car with this VIN does not exist"
        );

        return carEvents[vin];
    }

struct CarEvent {
  string id; //equal to VIN
  bool isAdditionalEntry;
  uint256 milage;
  uint256 eventCode;
  uint256 dealershipId;
}

and my code in .NET

    public class CarEvent
    {
       public bool IsAdditionalEntry { get; set; }
       public BigInteger Milage { get; set; }
       public BigInteger EventCode { get; set; }
       public BigInteger DealershipId { get; set; }
    }
    public async Task<Car> GetCarAsync(string vin)
    {
      
        ...
          var getCarFunction = contract.GetFunction("getCar");
          var queryResult = await getCarFunction.CallAsync<CarEvent[]>(vin);
 
      ...
    }

EDIT: I left only the important part I have found the issue...at the moment I'm trying to get CarEvent[] from the smart contract function...but the Nethereum library has difficulties with getting an array and deserializing it to C# objects...

I'm not sure how to solve this, any suggestions are more than welcome.

1 Answer 1

0

If you want to interact with your smart contract, you need to change some code - I haven't found a better solution

  1. return data from smart contract as a list of variable not as a object, i.e.
#bad
return carEvents[vin]

#good
return (carEvents[vin].id, carEvents[vin].nameOfDifferentProperty, carEvents[vin].nProperty)
  1. If you want to return list of object, you nedd to query this function as many times as you have objetcs on this list, but with different 'index'

If somebody know how to do thjis in more proper way, feel free to point it out :)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.