---
updatedAt: 2025-06-09T22:51:27.000Z
agentTools:
  projectIndex: https://docs.metakeep.xyz/llms.txt
---

# Upgradeable/Proxy Lambda

Are you looking to deploy an upgradeable smart contract for your blockchain application? With MetaKeep's Lambda Infrastructure, you can deploy custom upgradeable smart contracts with ease.

Your custom upgradeable 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'll guide you through the process of deploying an upgradeable smart contract on MetaKeep's Lambda infrastructure. The custom upgradeable contract is an ERC721 contract that utilizes the UUPS upgradeability Pattern. The contract only allows minting to whitelisted users and will only mint at most one token to a user. Let's dive in!

You can find an end-to-end working demo [here](https://github.com/PassbirdCo/MetaKeepCodeSamples/tree/main/lambda/custom-erc721-upgradeable).

## Getting started with deploying upgradeable contracts using MetaKeep Lambda

The first step is to get an API key. To do so, sign up [here](https://console.metakeep.xyz).

### Step 1: Inherit MetaKeep Lambda Base Contracts to your custom upgradeable 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 custom upgradeable contract:

* Install MetaKeep Lambda base contracts and inherit `MetaKeepLambdaUpgradeable.sol`

  `npm install metakeep-lambda@2.0.2`
* **Add initializer**: Make sure to include an `initialize` method in your contract. Within this method, you'll need to call `_MetaKeepLambda_init` to initialize the MetaKeep Lambda contracts. The `initialize` method expectes 2 required parameters, ***lambdaOwner*** and ***lambdaName***. LambdaOwner is the developer's address that you can get from [getDeveloperWallet](https://docs.metakeep.xyz/reference/v3getdeveloperwallet-1) API.
* **\[Optional]**: We recommend that you add a constructor that calls the initialize method.  Initializing the contract in the constructor is not required for upgradeable contracts. But, it's a good safety practice to prevent any potentially harmful operation from happening on the contract (e.g. a self-destruct function if there's any!).
* Add `_msgSender()` function. This function ensures that we read the correct sender for gasless transactions.
* Replace `msg.sender` with `_msgSender()`.
* Finally, use the `onlyMetaKeepLambdaOwner` modifier to guard access to functions that only the contract owner should be able to call. This modifier ensures that only the Lambda owner can execute these functions, providing an additional layer of security for your contract.

By following these steps, you can ensure that your upgradeable smart contract is initialized properly and operates securely on MetaKeep's Lambda infrastructure.

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

> 📘 Click the Image below to see the actual code on the Github.

<HTMLBlock>{`
<a href="https://github.com/PassbirdCo/MetaKeepCodeSamples/commit/0382670903fdc826128ac3c8bf627ccf60457eb8#diff-6106f75dec72ba39d99ac2616a56628df4c32859c0c93f4c6c755ac90ca4966e" target="_blank">
&nbsp; &nbsp; <span aria-label="2184" class="img" role="button" tabindex="0">
&nbsp; &nbsp; &nbsp; &nbsp; 
  <img src="https://files.readme.io/14c193d-6efae11-ImplementationContractDiff.png" alt="2184" title="Custom ERC721 diff" class="" align="" caption="" height="auto" width="auto" loading="lazy">
&nbsp; &nbsp; </span>
</a>
`}</HTMLBlock>

### Step 2: Update the Proxy contract

It is recommended that the proxy contract constructor include the two parameters - `lambdaOwner` and `lambdaName` as shown below. Although these parameters are not being used by the contract, they are necessary for deployment on MetaKeep Lambda infrastructure.

Also, make sure to initialize the upgradeable ERC721 contract by sending the initialization `data` in the proxy constructor.

<HTMLBlock>{`
<a href="https://github.com/PassbirdCo/MetaKeepCodeSamples/blob/main/lambda/custom-erc721-upgradeable/smart-contracts/contracts/CustomERC721Proxy.sol" target="_blank">
&nbsp; &nbsp; <span aria-label="2184" class="img" role="button" tabindex="0">
&nbsp; &nbsp; &nbsp; &nbsp; 
  <img src="https://files.readme.io/228dbc0-Screenshot_2023-04-07_at_18-37-17_MetaKeepCodeSamples_CustomERC721Proxy.sol_at_main__PassbirdCo_MetaKeepCodeSamples.png" alt="2184" title="Custom ERC721 diff" class="" align="" caption="" height="auto" width="auto" loading="lazy">
&nbsp; &nbsp; </span>
</a>
`}</HTMLBlock>

### Step 3:  Deploy custom ERC721 upgradeable contract

Once you have compiled both contracts, a new folder, `artifacts`, will be created from where you can easily fetch the ABI and the bytecode of your custom ERC721 upgradeable contract.

Once you have the data, make an API call to the`/v2/app/lambda/create` API to deploy the contract.

### Step 4: Deploy the proxy contract

> 🚧 Merge Proxy contract ABI with the ERC721 upgradeable Contract ABI
>
> To effectively use the MetaKeep Lambda API to invoke methods later, it is essential to merge the ABI of your Proxy contract with the ERC721 upgradeable Contract ABI(excluding the constructor) before deployment. This is a crucial step to ensure that the Proxy Contract is aware of all the methods present in the ERC721 contract, allowing it to execute gasless transactions seamlessly.

Then, make an API call to the`/v2/app/lambda/create` API with the merged ABI and bytecode to deploy the proxy contract.

## Running the Demo

You can find the end-to-end working demo [here](https://github.com/PassbirdCo/MetaKeepCodeSamples/tree/main/lambda/custom-erc721-upgradeable). Follow these steps to run the demo:

### Step1: Compile both contracts

Navigate to the `lambda/custom-erc721-upgradeable/smart-contracts` directory. Then, run the command

```shell
npm install
npx hardhat compile
```

### Step 2: Update the .env file

Update the API Key in the [.env](https://github.com/PassbirdCo/MetaKeepCodeSamples/blob/main/lambda/custom-erc721-upgradeable/scripts/.env) file.

### Step 3: Deploy the upgradeable ERC721 contract

Navigate to the `lambda/custom-erc721-upgradeable/scripts` directory. Then, run the command:

```shell
npm install
npm run deployCustomERC721Upgradeable
```

The script will deploy the lambda, and wait for the transaction mining. After the successful call, you will get the custom ERC721 lambda address in the output. Paste it in the `.env`file against `CUSTOM_ERC721_UPGRADEABLE_CONTRACT_ADDRESS`

### Step 4: Deploy proxy Contract

Run the command below to deploy the proxy contract:

```shell
npm run deployProxy
```

After the proxy is successfully deployed, you will get the proxy contract in the output. Paste it in the .env file against the`CUSTOM_ERC721_PROXY_ADDRESS`

### Step 4: Invoke Methods(Whitelist and Mint) through Proxy.

Update `USER_EMAIL` in the [.env](https://github.com/PassbirdCo/MetaKeepCodeSamples/blob/main/lambda/custom-erc721-upgradeable/scripts/.env) file with the email of the user who will be getting the minted NFT.

Then, run the following command to whitelist the user and mint an NFT:

```shell
npm run invoke
```

### Step 5: Upgrade to a new contract

Run the command below to upgrade the contract to a new ERC721 contract:

```shell
npm run upgradeToCustomERC721UpgradeableV2
```

The proxy will now use the new contract.

## Next Steps

Voila! You have deployed your upgradeable smart contract and invoked methods with a few REST API calls 🎉🎉. Your custom upgradeable ERC721 contract now supports zero-friction and gasless blockchain transactions for you and your end users.