This project implements a meta-transaction-enabled NFT contract using ERC-2771. It uses a Trusted Forwarder to enable gasless transactions for end users. The project utilizes Hardhat Ignition for deploying and managing smart contracts.
- ERC-2771 Meta Transactions: Enables gasless transactions using a forwarder contract.
- NFT Contract: Supports minting and transferring NFTs.
- EIP-712 Domain: Provides structured data signing for secure meta-transactions.
- TrustedForwarder: A contract that validates the meta-transaction requests.
- MetaTxNFT: The main NFT contract that integrates with the Trusted Forwarder and inherits ERC2771Context.
-
Deployment Modules:
forwarder.js
: Deploys the TrustedForwarder contract.nft.js
: Deploys the MetaTxNFT contract.
-
EIP-712 Utilities:
eip712.js
: Provides utilities to generate domain types and hash typed data.eip712-types.js
: Defines schemas for EIP-712 structured data.
-
Helper Functions:
iterate.js
: Provides utility functions for handling arrays and objects.
ERC-2771 is a standard for supporting meta-transactions on Ethereum. Meta-transactions allow a third party (known as a relayer) to pay for the gas fees on behalf of the end user. This approach is especially useful for enhancing user experience, as it enables gasless interactions with blockchain applications.
-
Trusted Forwarder:
- Acts as a middleman to validate and forward transactions on behalf of users.
- Ensures that the transaction originates from a trusted relayer.
-
MetaTxNFT Contract:
- Integrates with the Trusted Forwarder to process meta-transactions.
- Validates the meta-transaction signature using the EIP-712 standard.
- Improved User Experience: End users are not required to hold ETH for gas fees.
- Seamless Integration: Developers can integrate gasless transactions without significant changes to the existing contract logic.
- Security: Relayers are authenticated using signatures and domain separation, reducing the risk of malicious activities.
- Node.js (>= 14.x)
- Hardhat (latest version)
- NPM or Yarn
Install dependencies by running:
npm install
This project uses Hardhat Ignition for deploying contracts.
-
Compile Contracts:
npx hardhat compile
-
Deploy TrustedForwarder: Use the following command to deploy the TrustedForwarder contract:
npx hardhat ignition deploy ./ignition/modules/forwarder.js --network polygonAmoy
-
Deploy MetaTxNFT: Update the
nft.js
module with the Trusted Forwarder's deployed address, then deploy the NFT contract:npx hardhat ignition deploy ./ignition/modules/nft.js --network polygonAmoy
-
Run Meta-Transaction Script: After deployment, use the following command to execute the ERC-2771 meta-transaction script:
npx hardhat run scripts/ERC2771MetaTransaction.ts
Update the constructor arguments in nft.js
to include:
- FORWARDER_CONTRACT_ADDRESS: Address of the deployed Trusted Forwarder.
- MYNft: Name of your NFT.
- NFT_CONTRACT_OWNER: Address of the owner.
Generate the domain details for a contract:
node eip712.js
npx hardhat compile
npx hardhat ignition deploy --module <MODULE_FILE> --network <NETWORK_NAME>
npx hardhat run scripts/ERC2771MetaTransaction.ts
- ethers.js: For interacting with Ethereum contracts.
- Hardhat Ignition: For managing complex deployment processes.
- Ensure you configure the
hardhat.config.js
file to include your network details. - Double-check the deployment addresses in your
nft.js
andforwarder.js
files.
This project is licensed under the MIT License.