Read Lambda Data

After the lambda is created from the /v2/app/lambda/create or imported using /v2/app/lambda/import API call, use this API to read data from the read-only or getter function(also known as the view-only function in solidity). Calling a non-read function will result in an error.

JSON response data field contains the return value of the read-only function. The data field format follows specification here. Here are some examples:

// Read smart contract data: tokenURI(uint256 tokenId)
{
  "status": "SUCCESS",
  "data": "https://api.dev.metakeep.xyz/v2/nft/metadata?chain=80002&collection=0x5059221e0971b1761f71a9619932c2b395c266d2&token=70247059"
}
// Read smart contract data: ownerOf(uint256 tokenId)
{
  "status": "SUCCESS",
  "data": "0x6B40cA2814198A37BA5798B0C5EE1A2E029b997f"
}
// Read from a function that returns an array
{
  "status": "SUCCESS",
  "data": [
    "0x6B40cA2814198A37BA5798B0C5EE1A2E029b997f",
    "123"
  ]
}

If the contract function throws an error, the response will contain the error message and the status will be FUNCTION_FAILURE. Here's an example

/*  This function gets balance of the user.

    function balanceOf(address of) public view virtual {
        require(to != address(0), "ERC721: balance query for the zero address");
        return _balance[of];
    }
*/

// Call balanceOf with zero address.
// The response will have the error message.
{
  "status": "FUNCTION_FAILURE",
  "failureReason": "ERC721: balance query for the zero address"
}

šŸ‘

This API doesn't consume any gas.

You don't need to pay any gas fee when using this API. It's okay to call this API several times without having to worry about paying for blockchain transactions.

Data Reading Limits

The API has limits on the amount of data(gas < 200M) that can be returned in a single call and the time it takes to execute the function call. Trying to read too much data can result in failure and cause a poor user experience due to higher latency. This also prevents reading from functions that can go in an infinite loop.

For best results, we recommend the following:

  • Only read the data you need. If you need to read a lot of data, consider breaking it up into multiple calls.
  • Use Pagination. If you need to read all data, design your function so that instead of returning all the data, it returns a page of data. You can then use the pagination parameters to retrieve the next page of data.
  • Store data in database. Consider storing the large data in your own database instead (SQL, S3, etc).

Note that if you have already deployed the lambda contract, you can update the contract to add pagination parameters if the contract is upgradable or deploy a new contract with pagination parameters which will read the data from your existing contract.

Error Status

šŸš§

Api Error Status

You can find error status returned by the api here.

Ā© Copyright 2024, Passbird Research Inc.

Language
Authorization
Header
Click Try It! to start a request and see the response here!