Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@defi-wonderland/hardhat-account-abstraction

Package Overview
Dependencies
Maintainers
7
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@defi-wonderland/hardhat-account-abstraction

Hardhat TypeScript plugin for Account Abstraction

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
40
increased by166.67%
Maintainers
7
Weekly downloads
 
Created
Source

Hardhat Account Abstraction

A plugin to send sponsored transactions utilizing account abstraction!

What

This plugin sponsors any transaction the user sends through the power of account abstraction. Through seemless integration after the configuration is set just submit any transaction, and you can see it get mined on the testnets without costing the signer any gas!

Installation

yarn install @defi-wonderland/hardhat-account-abstraction

Import the plugin in your hardhat.config.js:

require("@defi-wonderland/hardhat-account-abstraction");

Or if you are using TypeScript, in your hardhat.config.ts:

import "@defi-wonderland/hardhat-account-abstraction";

Required plugins

NOTE: Only one of these packages is needed depending on what you are writing your scripts in


  • @nomicfoundation/hardhat-ethers
  • @nomicfoundation/hardhat-viem

Tasks

This plugin creates no additional tasks.

Environment extensions

This plugin does not extend the hardhat runtime environment

Configuration

NOTE: Currently the plugin will only use the first private key in accounts

This plugin requires 2 new fields inside an accountAbstraction object which will be nested inside each hardhat network that is set in the config

This is an example of how to set it:

const config: HardhatUserConfig = {
  solidity: '0.8.19',
  defaultNetwork: 'goerli',
  networks: {
    goerli: {
      url: process.env.GOERLI_RPC_URL as string,
      accounts: [process.env.PRIVATE_KEY as string],
      accountAbstraction: {
        bundlerUrl: 'https://example.com',
        paymasterUrl: 'https://example.com',
      }
    }
  }
};

Options

OptionDescriptionRequiredDefault
bundlerUrlThe bundler that the UserOperations will be sent toYesNo default
paymasterUrlThe paymaster API that will be used for sponsoring transactionsYesNo default
simpleAccountFactoryAddressThe simple account factory address you want to useNo0x9406cc6185a346906296840746125a0e44976454
smartAccountAddress of a smart account to use in your scriptsNoWill deploy one for you
policyIdThe policy id to use if your paymaster has oneNoNo default

Supported Paymaster Types

The list of paymasters we currently support

  1. Pimlico
  2. Stackup
  3. Alchemy
  4. Base

If you would like to add support for a new paymaster check out the contributors guide

Supported Chains

For a chain to be supported the conditions are:
  1. The SimpleAccount Factory is deployed to the address 0x9406cc6185a346906296840746125a0e44976454 or alternatively entered as an optional parameter in the config
  2. The CreateXFactory needs to be deployed to its standard address
Currently the list of supported chains is, but not limited to the following:
  1. Ethereum Sepolia
  2. Ethereum Goerli
  3. Polygon Mumbai
  4. Base Goerli
  5. Optimism Goerli
  6. Arbitrum Goerli

Usage

⚠ WARNING: Any non-zero msg.value call will not work as intended as paymaster's dont sponsor this value, in order to use native transfers or interact with payable functions you will need the native token of your chain in the smart account wallet beforehand

After you have setup the configuration for the accountAbstraction and you are using a network that has them enable you are good to go, you can right a simple script below and your transactions will be mined on the testnet that you have configured!

const signer = await ethers.provider.getSigner();
const testToken = new ethers.Contract('0x16F63C5036d3F48A239358656a8f123eCE85789C', TEST_TOKEN_ABI, signer);
const amountToMint = ethers.parseEther('6.9');
await testToken.mint(amountToMint);

Deploying Contracts

Deploying contracts works just as any other transaction would, however due to the nature of account abstraction we deploy all contracts through the CreateXFactory, The CreateXFactory is deployed on most EVM compatible chains, if you would like to learn more about it or deploy it to your new chain you can do so by checking out their repo

What are the differences?

  • Ownable contracts

    We do support ownable contracts and the default owner of your contract will be the smart account you deployed with, the only condition is you must have a transferOwnership(address) function to make this work, if you have a custom implementation of Ownable and dont instantiate owner as msg.sender in the constructor it will also work.

  • Contract addresses in scripts

    Scripting with deployed contracts works pretty much out of the box with one caveat, the address that ethers provides for your address is wrong. This is because ethers predicts the deployment address by the default CREATE opcode standards which takes the transaction's from and nonce values, these values do not match that of our middlewares deployment so we expose a custom method to get this address. This issue is only present with libraries that hardcode the predicted address such as ethers. Other libraries use the receipt to retrieve the contract address such as viem, for those libraries the address returned will be correct as we modify the receipts in our middleware.


    Scripting will work as expected even with this incorrect address param, this is because our middleware overwrites any transaction being sent to the incorrect predicted address and routes it to the address we deployed. As you can see from this example below.

    const lock = await ethers.getContractFactory('Lock');
    
    const lockContract = await lock.deploy();
    
    const originalOwner = await lockContract.owner();
    
    console.log('Original owner set to: ', originalOwner); // Logs the smart account address
    
    await lockContract.transferOwnership("0xEB7cFd33CfEfFf98EF067F501B81D31C9a7077C3");
    
    const newOwner = await lockContract.owner();
    
    console.log('New owner set to: ', newOwner); // Logs the 0xEB7... address
    

    However it is very important to remember that lockContract has the wrong address, however using the address the contract computes will work as a parameter as you can see below


    const lock = await ethers.getContractFactory('Lock');
    const lockContract = await lock.deploy();
    
    console.log(lockContract.target); // Wrong address
    
    const lockContractAddress = await network.provider.request({
      method: 'aa_getDeploymentFor',
      params: [lockContract.target]
    });
    
    console.log(lockContractAddress) // Correct address
    
    lockContract.randomFunctionWithAddressAsParam(lockContract.target) // Will use the correct address
    

Custom JSON API methods

This plugin adds additional JSON-RPC methods to be able to interact and get data from our custom provider middleware.

aa_getSmartAccountAddress

Description: Returns the address for the smart account that is being used by the provider.

  • Example:
const smartAccountAddress = await network.provider.request({
    method: 'aa_getSmartAccountAddress',
    params: [],
  });
  console.log(`Smart account address: ${smartAccountAddress}`);

aa_getDeploymentFor:

Description: Returns the address of which a contract was deployed through our middleware, to learn more about why this is needed click here

  • Parameters:
    • contract: 0x${string}: The contract address that you want to check the deployment for
  • Example:
const lock = await ethers.getContractFactory('Lock');
const lockContract = await lock.deploy();

const lockContractAddress = await network.provider.request({
  method: 'aa_getDeploymentFor',
  params: [lockContract.target] 
});

Contributors

If you want to learn how to add support for your own paymaster implementation checkout our guide here to learn how to add it to the plugin!


Hardhat Account Abstraction was built with ❤️ by Wonderland.

Wonderland the largest core development group in web3. Our commit ment is to a financial future that's open, decentralized, and accessible to all.

DeFi sucks, but Wonderland is here to make it better.

Licensing

The primary license for Hardhat Account Abstraction is MIT, see LICENSE.

Keywords

FAQs

Package last updated on 08 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc