How to read solidity function with tuple data in ether-js

Viewed 34

When I call the getLandById function in remix it gives the desired result as you can see in the screenshot Screeshot of remix IDE to get landIdbyId

When I call the same function using ether js. it will gives the output like this :

[ '0.007062190', '-0.01878356', '\x00\x00', [Getter] ]

Instead of

[ '0.007062190', '-0.01878356', '-0.019048060716011,0.007015231577652,-0.018794627684582,0.007386060761845,-0.018423798497481,0.007132627732498,-0.018677231528875,0.006761798548127,-0.019048060716011,0.007015231577652']

I'm struggling to understand how to get the data. Any idea on what should I do to get the desired result? An example would be great.

Ether-js code:

let customHttpProvider = new ethers.providers.JsonRpcProvider(API_URL);
const contract = new ethers.Contract(
  Contract_Address,
  contractAbi,
  customHttpProvider
);

//Calling readOnly Method
async function getLand() {
  const getLandById = await contract.getLandById("502");

  console.log("Land-Info", getLandById);
}

getLand();

Contract ABIs:

{
        "inputs": [
            {
                "internalType": "uint256",
                "name": "landId",
                "type": "uint256"
            }
        ],
        "name": "getLandById",
        "outputs": [
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            },
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            },
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            },
            {
                "components": [
                    {
                        "internalType": "string",
                        "name": "longitude",
                        "type": "string"
                    },
                    {
                        "internalType": "string",
                        "name": "latitude",
                        "type": "string"
                    }
                ],
                "internalType": "struct LandContract.PolygonCoordinates[]",
                "name": "",
                "type": "tuple[]"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },

Solidity code:

function getLandById(uint landId)
        public
        view
        returns (
            string memory,
            string memory,
            PolygonCoordinates[] memory
        )
    {
        if (!_exists(landId)) {
            revert IdNotExist();
        }

        PolygonCoordinates[] memory coordinates = new PolygonCoordinates[](
            land[landId].polygonCoordinates.length
        );

        for (uint i = 0; i < land[landId].polygonCoordinates.length; ) {
            coordinates[i].longitude = land[landId]
                .polygonCoordinates[i]
                .longitude;
            coordinates[i].latitude = land[landId]
                .polygonCoordinates[i]
                .latitude;

            unchecked {
                i++;
            }
        }

        return (land[landId].longitude, land[landId].latitude, coordinates);
    }
1 Answers

The contract ABI defines 4 output values but your solidity code getLandById() returns only 3 values.

Related