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.
Getting started with deploying upgradeable contracts using MetaKeep Lambda
The first step is to get an API key. To do so, sign up here.
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 [email protected]
-
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. Theinitialize
method expectes 2 required parameters, lambdaOwner and lambdaName. LambdaOwner is the developer's address that you can get from getDeveloperWallet 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.
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.
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. 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
npm install
npx hardhat compile
Step 2: Update the .env file
Update the API Key in the .env file.
Step 3: Deploy the upgradeable ERC721 contract
Navigate to the lambda/custom-erc721-upgradeable/scripts
directory. Then, run the command:
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:
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 theCUSTOM_ERC721_PROXY_ADDRESS
Step 4: Invoke Methods(Whitelist and Mint) through Proxy.
Update USER_EMAIL
in the .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:
npm run invoke
Step 5: Upgrade to a new contract
Run the command below to upgrade the contract to a new ERC721 contract:
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.
Updated over 1 year ago