Skip to main content

EIP-7702 Delegation

This guide provides an educational framework to implementing EIP-7702 delegaton. It is not currently intended for production use.

EIP-7702 enhances Externally Owned Accounts (EOAs) by allowing them to perform smart contract-like operations. In this example, we will create a delegation authorization and send a raw transaction on the Mekong chain.

Prerequisites

Before you begin, ensure you have the following:

  • Node.js installed on your machine.
  • An Ethereum wallet with a private key (stored in an environment variable).
  • A fork of abstractionkit at the 7702 branch
git clone git@github.com:candidelabs/abstractionkit.git
cd abstractionkit
git checkout experimental7702
npm install
npm build
npm link

Then in a seperate project, you can use the local build version like the following:

npm link abstractionkit

Implementation

Step 1: Get current nonce for the EOA

We retrieve the current nonce for the EOA using the eth_getTransactionCount JSON-RPC method. The nonce is essential for ensuring that transactions are processed in the correct order.

import { sendJsonRpcRequest } from "abstractionkit";
import { Wallet } from "ethers";

const chainId = 7078815900; // Mekong Chain ID
const nodeRpc = "https://rpc.mekong.ethpandaops.io";
const eoaWallet = new Wallet(process.env.PRIVATE_KEY); // Load your wallet using the private key

const nonce = await sendJsonRpcRequest(
nodeRpc,
"eth_getTransactionCount",
[eoaWallet.address, "latest"]
);

Step 2: Create a delegation authorization

We create a delegation authorization using the createAndSignEip7702DelegationAuthorization function. This allows the specified address to act on behalf of the EOA.

import { createAndSignEip7702DelegationAuthorization } from "abstractionkit";

const delegation = createAndSignEip7702DelegationAuthorization(
chainId,
"0xB52D62510cdcEBfedEd46aF5E99dC50DD352F25F", // Delegation destination address
BigInt(nonce) + 1n, // If the delegator will be the transaction sender, increase the authorization nonce by one
eoaWallet.privateKey
);

Step 3: Create and sign 7702 raw transaction

We create a raw transaction using the createAndSignEip7702RawTransaction function. This includes details such as the nonce, gas fees, destination address, and the delegation authorization.

import {  createAndSignEip7702RawTransaction } from "abstractionkit";

const tx = createAndSignEip7702RawTransaction(
chainId,
nonce,
0xf078996n, // Max priority fee per gas
0xf078996n, // Max fee per gas
0x210000n, // Max priority
"0x0000000000000000000000000000000000000000", // Destination address
0n, // Value to send
"0x", // Data payload
[], // Access list
[delegation], // Authorization list
eoaWallet.privateKey // Use the EOA's private key
);

Step 4: Send the 7702 raw transaction

Finally, we send the raw transaction using the eth_sendRawTransaction method and log the response.

const res = await sendJsonRpcRequest(
nodeRpc,
"eth_sendRawTransaction",
[tx]
);

console.log("Transaction Response:", res);

In this guide, you have successfully upgraded an Externally Owned Account (EOA) by delegating its authorization to a designated smart contract address using EIP-7702. In the upcoming guides, we will demonstrate how to delegate authorization to a real smart account, enabling you to fully leverage the capabilities of smart wallet features.