Invoke Lambda method as the developer (Zero-Friction and Gasless)

In the previous tutorial, we discussed how to deploy your own smart contract and support gas-less transactions by leveraging MetaKeep's Lambda Infrastructure. Coming to the next step, we will look at how to invoke a method in the deployed smart contract without worrying at all about the transaction fees, blockchain nodes, nonce, etc. with a simple API call.

How to invoke a Smart Contract’s Method?

In this tutorial, like earlier, we will start with the basic Voting Smart contract from our GitHub repo.

You can find an end-to-end working demo here.

Step 1: Copy the address of the Lambda that you want to invoke.

You should have gotten the Lambda address as the output of the previous tutorial.
If you have lost the address, you can make a request to List Lambdas API to get the list of all deployed Lambdas. Else, you can always deploy a new contract by following the previous tutorial.

Step 2: Create A POST request to the Invoke API.

{
    "function": {
        "name": < Name of the function to invoke >,
        //keep it empty in case of no args
        "args": []
    },
    "lambda": < lambda_address >,
    "reason": <Reason for Invocation>
}

You need to provide the three mandatory fields mentioned above.

Step 3: Call invoke API to invoke the contract function.

To invoke a method in the deployed contract, make a POST request to the API. Ensure that you use the same API key used for the lambda/create. Below is the code snippet that you can use to make the API call.

const fetch = require('node-fetch');

const url = 'https://api.metakeep.xyz/v2/app/lambda/invoke’;
const request_body = {
    "function" : {
      "name" < Name of the function >, 
      //keep it empty in case of no args
      "args" : [] 
		},
    "lambda" : < lambda_address >,
    "reason" : <Reason for Invocation>
}
const options = {
  method: 'POST',
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
    'x-api-key': <API_KEY>,
    'Idempotency-Key': <IDEMPOTENCY_KEY>
  },
  body: JSON.stringify(request_body)
};

fetch(url, options)
  .then(res => res.json())
  .then(json => console.log(json))
  .catch(err => console.error('error:' + err));

The following should be the ideal outcome that you should get after making a POST call to API.

{
   "status": "QUEUED",
   "transactionId": "4773da73-d9ae-41fe-a64f-290c63bae105",
   "transactionHash": "0xc4fb69222df5a13e4c411575af4611491c5cf13a983cbbbcb238ecb9317f4501",
   "transactionChainScanUrl": "https://mumbai.polygonscan.com/tx/0xc4fb69222df5a13e4c411575af4611491c5cf13a983cbbbcb238ecb9317f4501"
}

Running the Demo

You can find the end-to-end working demo here. Follow these steps to run the demo:

Step 1: Prerequisite

Before proceeding, you need to finish the first part of the tutorial Create your first Lambda. Once you have the Lambda address, store it somewhere because we would need that in the next step.

Step 2: Update .env file

Update the .env file with the API key, Lambda address from the previous step, and the candidate email who you want to register.

Step 3: Run the script

Navigate to the lambda/voting/scripts directory. Then, run the command:

npm install
npm run invoke

The demo will first register the new candidate and vote for the candidate using your developer account, and wait for the transaction mining.

Next steps

Voila! You have invoked the function from your first smart contract deployed using MetaKeep Lambda with one REST API call 🎉🎉.