Custom ERC1155

MetaKeep NFT and Coin APIs provide limitless possibilities to support your blockchain use case. If you have a special requirement not supported by MetaKeep APIs, you can write your own ERC1155 contract and run it on MetaKeep's Lambda infrastructure. ERC1155 is a multi-token standard that may include any combination of fungible tokens, non-fungible tokens, or other configurations (e.g. semi-fungible tokens).

Your ERC1155 contract will run as a first-class citizen on MetaKeep, receiving the same privileges our first-party contracts do(NFT and Coin contracts), in terms of the ability to scale (to ~65K peak TPS), resiliency (AI-powered transaction failure recovery, Probabilistic gas price management) and without gas fee (for you, your customers, and their users)- i.e., deferred billing.

In this tutorial, we will discuss deploying your ERC1155 smart contract on MetaKeep Lambda Infrastructure.
You can find an end-to-end working demo here.

Getting started with the ERC1155 Contract

The first step is to get an API key. To do so, sign up here.

Step 1: Inherit MetaKeep Lambda Base Contracts to your ERC1155 contract

MetaKeep Lambda base contracts handle all the logic related to gasless transactions, access control, and a lot more. Follow the steps below to integrate it with your ERC1155 contract:

  • Install MetaKeep Lambda base contracts and inherit MetaKeepLambda.sol.

    npm install metakeep-lambda

  • Update Constructor: The constructor expectes 2 required parameters, lambdaOwner and lambdaName. LambdaOwner is the developer's address that you can get from getDeveloperWallet API.

  • Add _msgSender() function. This function ensures that we read the correct sender for gasless transactions.

  • Replace msg.sender with _msgSender().

  • Use the onlyMetaKeepLambdaOwner modifier to guard the access to functions that only the contract owner should be able to call. For e.g. you will use this for mint and burn calls.

Below is a small example of how the ERC1155 contract was modified to achieve this. After these minor updates, your custom ERC1155 contract would run as first-class citizens on the MetaKeep, receiving the same privileges our first party smart-contracts do(NFT and Coins contracts).

Step 2: Get the ABI, bytecode, and the Constructor Arguments of your smart contract.

Once you have compiled the ERC1155 smart contract in the lambda/custom-erc1155/smart-contracts directory, a new folder, artifacts`, will be created from which you can easily fetch the ABI and Bytecode of your ERC1155 contract.

Step 3: Create a POST request to the app/lambda/create API.

Let's construct the request body using all the information collected in the previous step, as shown below.

{
    "constructor" : { args : [lambdaOwner, lambdaName] },
    "abi" : < ABI >,
    "bytecode" : <Bytecode>
}

Step 4: Call the app/lambda/create API to deploy your ERC1155 contract.

To deploy, make a POST request to the API. You need to use the API KEY and the Idempotency-Key. Here is what the request will look like:

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

const url = 'https://api.metakeep.xyz/v2/app/lambda/create';
const request_body = {
    "constructor" : { args : [lambdaName,..., lambdaOwner]},
    "abi" : < ABI >,
    "bytecode" : <Bytecode>

}
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));

This is what you should see as the response from create API

{
  status: 'QUEUED',
  transactionId: '1f424819-4ddc-4bcb-9054-ca75b2719f9b',
  transactionHash: '0x9b304e368e0d5d6398c6dd6ad5e216bfc80fefebb99897e8238633ef1d957b7a',
  transactionChainScanUrl: 'https://mumbai.polygonscan.com/tx/0x9b304e368e0d5d6398c6dd6ad5e216bfc80fefebb99897e8238633ef1d957b7a',
  lambda: '0x17192E38Db788a15432149C93101C6cB040dE7C2'
}

📘

Note the lambda field in the response

The lambda field gives you the address of your ERC1155 contract. Keep the address recorded because it will be used for lambda invocation (You can always get the list of your deployed lambdas using the list lambdas API).

Running the Demo

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

Step 1: Compile the contract

Navigate to the lambda/custom-erc1155/smart-contracts directory. Then, run the command:

npm install
npx hardhat compile

To run contract unit tests, run the following command:

npx hardhat test

Step 2: Update .env file

Update the API key in the .env file.

Step 3: Deploy the contract

Navigate to the lambda/custom-erc1155/scripts directory. Then, run the command:

npm install
npm run create

The script will deploy the lambda, and wait for the transaction mining. You will see the created lambda address in the output of the script.

Step 4: Mint tokens

Update the .env file with the Lambda address from the previous step, and the user email to who you want to issue tokens. Then, run the command:

npm run invoke

The script will mint different types of tokens to the user.

Next Steps

Voila! You have deployed an ERC1155 contract with a few REST API calls 🎉🎉. Your ERC1155 contract now supports zero-friction and gasless blockchain transactions for you and your end users.