pact-lang

How do you call non-write functions from a module without needing to pay gas, using pact-lang-api?


I have a few simple functions that get data from the blockchain, which normally does not require sending a transaction when using Chainweaver.

(defun get-price (price-key:decimal)
    (at "price" (read price-table price-key ["price"]
)

This function does not change any data, so it shouldn't require gas. How can I use something like the x-wallet browser plugin to call this function and just get the data?


Solution

  • You can just call the local API via HTTP request.

    I.e.

    localRes = await fetch(
       `${networkUrl}/api/v1/local`, 
      {
        headers: {
          "Content-Type": "application/json",
        },
        method: "POST",
        body: JSON.stringify(cmd),
      };
    );
    

    Note: For the cmd - you need to make a valid pact-lang-api command which specifies the smart contract and method to call