Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning Struct Array in solidity

This is my contract code. Here I'm trying to store the coordinates of a particular trip. While storing the information contract executing fine. But When I retrieve the data, It should give the array of coordinates. But it is throwing an error.

reason: 'insufficient data for uint256 type'

contract TripHistory {
       struct Trip {
           string lat;
           string lon;
       }
        mapping(string => Trip[]) trips;

        function getTrip(string _trip_id) public view returns (Trip[]) {
            return trips[_trip_id];
        }
        function storeTrip(string _trip_id, string _lat, string _lon) public  {
           trips[_trip_id].push(Trip(_lat, _lon));
        }

}

What I'm missing here. Is there any other way to achieve what I'm trying here?

P.S: I'm new to solidity.

like image 553
Vivek Aasaithambi Avatar asked May 18 '26 13:05

Vivek Aasaithambi


2 Answers

First of returning structs is not supported in Solidity directly. Instead you need to return every individual element in the struct as below.

Function xyz(uint256 _value) returns(uint256 User.x, uint256 User.y)
public {}

But then there’s an experimental feature that will help you with returning struct. All that you need to do is add the following after your first pragma line

pragma experimental ABIEncoderV2;

then continue with your code. That should work with no changes to your code.

An example of abiencoderv2 returning struct can be found at this link

like image 111
Khaja Mohammed Avatar answered May 20 '26 12:05

Khaja Mohammed


It is not possible in solidity to return struct array.

like image 28
Sidharth Srivastava Avatar answered May 20 '26 13:05

Sidharth Srivastava