Gasless transactions via relayer example
This is a basic example project showing how to implement gasless meta-transactions via a Biconomy relayer and ERC2771Context.
Project setup
- Clone the repo
git clone git@github.com:dabit3/gasless-transactions-example.git
- Install dependencies
cd gasless-transactions-example
npm install
# or
yarn
- Configure
PRIVATE_KEY
andINFURA_ENDPOINT
environment variables inhardhat.config.js
.
Also if you are deploying to a network other than Polygon, you can configure that information in hardhat.config.js
.
- Compile the project
npx hardhat compile
- Deploy the smart contract
npx hardhat run scripts/deploy.js --network polygon
- Update the
greeterAddress
inpages/index.js
Biconomy integration
-
Create an account on Biconomy
-
Create a new app. Choose the network you'd like to use (I deployed to Polygon).
-
Enter the smart contact address and ABI (ABI located at
artifacts/contracts/Greeter/Greeter.sol/Greeter.json
)
-
In Dapp APIs, add the
setGreeting
function -
Create a
.env.local
file in the Next.js project and add the Biconomy API Key:
NEXT_PUBLIC_BICONOMY_API_KEY=your-api-key
- Run the app
npm start
TLDR for implementing in existing app
- Update contract to inherit from ERC2771Context
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/metatx/ERC2771Context.sol";
contract Greeter is ERC2771Context {}
-
msg.sender -> _msgSender()
- (though this contract does not usemsg.sender
) -
Configure Biconomy app with contract address and methods you'd like to use
-
Update client-side code to use Biconomy SDKs