Biconomy Gasless SDK (EOA)
DashboardMediumWebsite
  • 🚀Getting Started
  • Introduction
    • 🤘Why Biconomy?
    • 🙂How we simplify transactions
  • Products
    • 💸Gasless Transactions
      • Choose An Approach to Enable Gasless
        • Standard EIP 2771 Approach
          • 1. Register Artifacts on the Dashboard
          • 2. Code Changes
            • Using SDK
            • Using API
        • Custom Implementation Approach
          • 1. Register Artifacts on the Dashboard
          • 2. Code Changes
            • Using SDK
            • Using API
        • Smart Contract Wallet Approach
          • Gnosis
        • Network Agnostic Transactions
          • EIP-2771 Approach
          • Custom Approach
      • Conditional Whitelisting
      • Gasless SDK (EOA) 3
      • Networks Supported
    • ↔️Hyphen - Instant Cross-Chain Transfers
      • SDK
        • DepositManager
        • TransferManager
        • TokenManager
      • APIs
      • Helper Methods
      • Migrating from Hyphen V1
      • Contract Addresses
      • Hyphen Widget
  • Guides
    • 💻Dashboard
      • DApp Statistics
    • ⛽Gas Tank Deposits
      • Via Dashboard
      • Via Smart Contract
  • api
    • 🔧Native Meta Transaction
      • Get Retried Hashes
    • 🌎Dashboard APIs
    • ⚪Whitelist API
      • Whitelist Destination Address
      • Whitelist Proxy Contracts
    • 〰️ Check Limits
    • 💿Biconomy Data API
      • 👨‍🚀Unique User Data
      • 🧑‍🔧Per User Limits Data
      • ⛽Gas Tank Balance Data
  • SDK
    • 📙Gasless SDK (EOA)
      • Configuration
  • Tutorials
    • 🔰Native Meta Transactions
      • How To Build Your First DApp
        • Write Your First Smart Contract
        • Initialize Web3 on Client Side
        • Executing First Blockchain Transaction
      • Enable Native Meta Transactions
        • Smart Contract
          • Describe Your Structs
          • Declare Your Variables
          • Modify Respective Function
        • Client Side
          • Design Your JSON structure
          • Design Your Domain Separator
          • Design Data Types
          • Define Data To Sign
          • Generate Signatures
      • Integrate Biconomy
        • Register On Dashboard
        • Integrate Gasless SDK (EOA)
      • Summary
  • BICO Staking
    • 🪁Safety Module
  • Get in touch
    • 👥Contact Us
  • Misc
    • 🧩Projects on Biconomy
    • 🌐Supported Networks
    • 📫Contract Addresses
    • ✔︎ Smart Contracts Audit
    • ❓FAQs
Powered by GitBook
On this page

Was this helpful?

  1. Tutorials
  2. Native Meta Transactions
  3. Enable Native Meta Transactions
  4. Smart Contract

Modify Respective Function

Make a method which supports native meta transactions

Now using all the information from previous sections, let's make a new method that sets the quote from the user but this time we do it with native meta transaction support.

Create a new function to include the following params

  • userAddress

  • newQuote

  • sigR

  • sigS

  • sigV

function setQuoteMeta(address userAddress,string memory newQuote, bytes32 sigR, bytes32 sigS, uint8 sigV) public {
 }

Add an instance of the struct

  // Make an instance of the predefined struct
  MetaTransaction memory metaTx = MetaTransaction({
    nonce: nonces[userAddress],
    from: userAddress
   });

Hash message in EIP712 compatible form

   // Hash the mandatory fields of EIP712
   bytes32 digest = keccak256(
    abi.encodePacked(
            "\x19\x01",
            DOMAIN_SEPARATOR,
            keccak256(abi.encode(META_TRANSACTION_TYPEHASH, metaTx.nonce, metaTx.from))
        )
    );

"\x19" makes the encoding deterministic

"\x01" is the version byte to make it compatible to EIP-191

Verify the Signatures On Chain

// Verify the userAddress is not address zero 
require(userAddress != address(0), "invalid-address-0");

// Verify the userAddress with the address recovered from the signatures
require(userAddress == ecrecover(digest, v, r, s), "invalid-signatures");

ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address) recover the address associated with the public key from elliptic curve signature

Add the Necessary Logic

quote = newQuote;
owner = userAddress;

Increment Nonce for Replay Protection

nonces[userAddress]++;

Complete Code Snippet

function setQuoteMeta(address userAddress,string memory newQuote, bytes32 r, bytes32 s, uint8 v) public {
    
 MetaTransaction memory metaTx = MetaTransaction({
    nonce: nonces[userAddress],
    from: userAddress
   });
    
 bytes32 digest = keccak256(
    abi.encodePacked(
            "\x19\x01",
            DOMAIN_SEPARATOR,
            keccak256(abi.encode(META_TRANSACTION_TYPEHASH, metaTx.nonce, metaTx.from))
        )
    );

   require(userAddress != address(0), "invalid-address-0");
   require(userAddress == ecrecover(digest, v, r, s), "invalid-signatures");
	
   quote = newQuote;
   owner = userAddress;
   nonces[userAddress]++;
 } 

Congratulations! You have successfully integrated meta transactions in your Smart Contracts

In the next section, Let's see what changes we need to do on the Client side.

PreviousDeclare Your VariablesNextClient Side

Last updated 5 years ago

Was this helpful?

Checkout the full working code here

🔰
https://github.com/bcnmy/dapp-demo