GithubHelp home page GithubHelp logo

stackup-wallet / erc-4337-examples Goto Github PK

View Code? Open in Web Editor NEW
201.0 5.0 137.0 132 KB

A collection of example scripts for working with ERC-4337

Home Page: https://docs.stackup.sh/docs/erc-4337-examples

License: MIT License

TypeScript 74.47% JavaScript 25.53%
account-abstraction blockchain erc4337 ethereum solidity

erc-4337-examples's Introduction

Getting started

A collection of example scripts for working with ERC-4337. For details on how to use this repository, check out the docs here.

The implementation for all commands are located in the scripts directory. All scripts are built with the following open source packages:

🚀 Looking for access to hosted infrastructure to build your Smart Accounts? Check out stackup.sh!

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Feel free to direct any technical related questions to the dev-hub channel in the Stackup Discord.

erc-4337-examples's People

Contributors

hazim-j avatar rewnad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

erc-4337-examples's Issues

error Command failed with exit code 1.

Hey, i would like to transfer some eth from my userAccount to my wallet account, but i got an error : reason: 'missing revert data in call exception; Transaction reverted without a reason string', code: 'CALL_EXCEPTION', data: '0x', transaction: { to: '0x0576a174D229E3cFA37253523E645A78A0C91B57', data: '0x35567e1a000000000000000000000000703022bf7725b36a9057eb86986544e3596c2f520000000000000000000000000000000000000000000000000000000000000000', accessList: null },
here is my command : yarn run simpleAccount transfer --to 0x274a4E4F93082738F1c142a9040C3DFAa632245e --amount 0.01 and i have 0.5 Goerli ETH on my userAccount.

Not able to transfer

Tried to create wallet using following command
$yarn run simpleAccount address
Later fund some eth to generated address then tried the following
$ yarn run simpleAccount transfer --to 0x17462eA99F7D4502347d629Dbd6f866Ee54E38E1 --amount 0.008
yarn run v1.22.19
$ ts-node scripts/simpleAccount/index.ts transfer --to 0x17462eA99F7D4502347d629Dbd6f866Ee54E38E1 --amount 0.008
Balance: 33844250040317952
Signed UserOperation: {
"sender": "0xd99D99a8d0bca1119403f1789721549ccCa9AE72",
"nonce": "0x0",
"initCode": "0x71d63edcda95c61d6235552b5bc74e32d8e2527b5fbfb9cf00000000000000000000000047b76a2dad532a791f2484ca47e0a25615384e3c0000000000000000000000000000000000000000000000000000000000000000",
"callData": "0xb61d27f600000000000000000000000017462ea99f7d4502347d629dbd6f866ee54e38e1000000000000000000000000000000000000000000000000001c6bf52634000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000",
"callGasLimit": "0x5580",
"verificationGasLimit": "0x65139",
"maxFeePerGas": "0x5a7e9",
"maxPriorityFeePerGas": "0x5a776",
"paymasterAndData": "0x",
"preVerificationGas": "0xaf84",
"signature": "0x1700fb614ad4ffde2c8664ae46462bab3a813d3c8bd6a5f9fa13e8f20bf387e2540ce6baedfdcb8291e3a9f3fa688d785178f12fe17270d7588b72b0a50728771c"
}
C:\Users\Omkar Sai Sunku\Documents\Micro1\indexx\erc-4337-examples\node_modules@ethersproject\logger\src.ts\index.ts:269
const error: any = new Error(message);
^
Error: processing response error (body="{"error":{"code":-32602,"data":"preVerificationGas: below expected gas of 48140","message":"preVerificationGas: below expected gas of 48140"},"id":43,"jsonrpc":"2.0"}", error={"code":-32602,"data":"preVerificationGas: below expected gas of 48140"}, requestBody="{"method":"eth_sendUserOperation","params":[{"sender":"0xd99D99a8d0bca1119403f1789721549ccCa9AE72","nonce":"0x0","initCode":"0x71d63edcda95c61d6235552b5bc74e32d8e2527b5fbfb9cf00000000000000000000000047b76a2dad532a791f2484ca47e0a25615384e3c0000000000000000000000000000000000000000000000000000000000000000","callData":"0xb61d27f600000000000000000000000017462ea99f7d4502347d629dbd6f866ee54e38e1000000000000000000000000000000000000000000000000001c6bf52634000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000","callGasLimit":"0x5580","verificationGasLimit":"0x65139","maxFeePerGas":"0x5a7e9","maxPriorityFeePerGas":"0x5a776","paymasterAndData":"0x","preVerificationGas":"0xaf84","signature":"0x1700fb614ad4ffde2c8664ae46462bab3a813d3c8bd6a5f9fa13e8f20bf387e2540ce6baedfdcb8291e3a9f3fa688d785178f12fe17270d7588b72b0a50728771c"},"0x0576a174D229E3cFA37253523E645A78A0C91B57"],"id":43,"jsonrpc":"2.0"}", requestMethod="POST", url="https://api.blocknative.com/v1/goerli/bundler", code=SERVER_ERROR, version=web/5.7.1)
at Logger.makeError (C:\Users\Omkar Sai Sunku\Documents\Micro1\indexx\erc-4337-examples\node_modules@ethersproject\logger\src.ts\index.ts:269:28)
at Logger.throwError (C:\Users\Omkar Sai Sunku\Documents\Micro1\indexx\erc-4337-examples\node_modules@ethersproject\logger\src.ts\index.ts:281:20)
at C:\Users\Omkar Sai Sunku\Documents\Micro1\indexx\erc-4337-examples\node_modules@ethersproject\web\src.ts\index.ts:341:28
at step (C:\Users\Omkar Sai Sunku\Documents\Micro1\indexx\erc-4337-examples\node_modules@ethersproject\web\lib\index.js:33:23)
at Object.next (C:\Users\Omkar Sai Sunku\Documents\Micro1\indexx\erc-4337-examples\node_modules@ethersproject\web\lib\index.js:14:53)
at fulfilled (C:\Users\Omkar Sai Sunku\Documents\Micro1\indexx\erc-4337-examples\node_modules@ethersproject\web\lib\index.js:5:58)
at processTicksAndRejections (node:internal/process/task_queues:95:5) {
reason: 'processing response error',
code: 'SERVER_ERROR',
body: '{"error":{"code":-32602,"data":"preVerificationGas: below expected gas of 48140","message":"preVerificationGas: below expected gas of 48140"},"id":43,"jsonrpc":"2.0"}',
error: Error: preVerificationGas: below expected gas of 48140
at getResult (C:\Users\Omkar Sai Sunku\Documents\Micro1\indexx\erc-4337-examples\node_modules@ethersproject\providers\src.ts\json-rpc-provider.ts:142:28)
at processJsonFunc (C:\Users\Omkar Sai Sunku\Documents\Micro1\indexx\erc-4337-examples\node_modules@ethersproject\web\src.ts\index.ts:383:22)
at C:\Users\Omkar Sai Sunku\Documents\Micro1\indexx\erc-4337-examples\node_modules@ethersproject\web\src.ts\index.ts:320:42
at step (C:\Users\Omkar Sai Sunku\Documents\Micro1\indexx\erc-4337-examples\node_modules@ethersproject\web\lib\index.js:33:23)
at Object.next (C:\Users\Omkar Sai Sunku\Documents\Micro1\indexx\erc-4337-examples\node_modules@ethersproject\web\lib\index.js:14:53)
at fulfilled (C:\Users\Omkar Sai Sunku\Documents\Micro1\indexx\erc-4337-examples\node_modules@ethersproject\web\lib\index.js:5:58)
at processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: -32602,
data: 'preVerificationGas: below expected gas of 48140'
},
requestBody: '{"method":"eth_sendUserOperation","params":[{"sender":"0xd99D99a8d0bca1119403f1789721549ccCa9AE72","nonce":"0x0","initCode":"0x71d63edcda95c61d6235552b5bc74e32d8e2527b5fbfb9cf00000000000000000000000047b76a2dad532a791f2484ca47e0a25615384e3c0000000000000000000000000000000000000000000000000000000000000000","callData":"0xb61d27f600000000000000000000000017462ea99f7d4502347d629dbd6f866ee54e38e1000000000000000000000000000000000000000000000000001c6bf52634000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000","callGasLimit":"0x5580","verificationGasLimit":"0x65139","maxFeePerGas":"0x5a7e9","maxPriorityFeePerGas":"0x5a776","paymasterAndData":"0x","preVerificationGas":"0xaf84","signature":"0x1700fb614ad4ffde2c8664ae46462bab3a813d3c8bd6a5f9fa13e8f20bf387e2540ce6baedfdcb8291e3a9f3fa688d785178f12fe17270d7588b72b0a50728771c"},"0x0576a174D229E3cFA37253523E645A78A0C91B57"],"id":43,"jsonrpc":"2.0"}',
requestMethod: 'POST',
url: 'https://api.blocknative.com/v1/goerli/bundler'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

What is the usecase? I can do the same with a normal wallet where I even own the private-key.

I don't get and I'm curiouss, why to use a stackup-bundler if I can have the same functionality and nothing more with plain RPC-calls and a wallet where I own the private key.

Let's discuss the examples:

yarn run simpleAccount address
this will always return the same address and I can do nothing with it besides transfering tokens and ETH/Matic, that I own on this address:

yarn run simpleAccount transfer --to <address> --amount <eth>

So it is basically nothing else than I could achieve with a "normal" wallet-address where I even own the Private key.

I would understand if the Bundler would allow me to act on someone else's behalf. At least this is how I understand account abstraction. But all examples only transfer "my" own assets from the address that has been created with yarn run simpleAddress address.

What am I not seeing here?

failed examples : `batchERC20Transfer` & `batchTransfer`

Thank you for providing various examples, which have been helpful in understanding Abstraction Account. I was able to reproduce other parts successfully, but I am having issues with the implementation of batchERC20Transfer and batchTransfer. When called, I receive the following error message:

{
....
"reason": "Error: VM Exception while processing transaction: reverted with reason string 'account: not Owner or EntryPoint'",
....
}

Upon further investigation, I found that the issue occurs when passing the encodeFunctionData(executeBatch) as a parameter to accountAPI.createSignedUserOp.

const op = await accountAPI.createSignedUserOp({
target: sender,
data: ac.interface.encodeFunctionData("executeBatch", [dest, data]),
...(await getGasFee(provider)),
});
console.log(`Signed UserOperation: ${await printOp(op)}`);

within the implementation of accountAPI, the encodeFunctionData(executeBatch) is re-encoded as execute, and this results in an error when execute is called again within executeBatch.

https://github.com/eth-infinitism/bundler/blob/22166ee14b7ea4d017a365fe36cf9d4a49520e6b/packages/sdk/src/BaseAccountAPI.ts#L169-L188

It seems that a modification to the bundler SDK of eth-infinitism is necessary to support executeBatch.

Uncaught ReferenceError: Buffer is not defined

Hi, I'm getting the following error when using the stackup-wallet/erc-4337-examples package from a React application project context:

Uncaught ReferenceError: Buffer is not defined
at ./node_modules/@account-abstraction/utils/dist/src/ERC4337Utils.js

Looks like it's coming from the @account-abstraction package. Apparently it's a deprecated one. Are there any fixes planned for this?

Everything works fine when calling these examples from this project or another non-React environment

How can I find the project ID?

I'm following Stackup guide and programmed an app.js by that guide. But when I run NodeJS this error throwed:
statusCode: 401, responseText: 'project id required in the url\n
So, how can I find the project ID and how to include it into the url?

Can't get ERC-20 Paymaster working // -32601 error — simulation: insufficient token payments

Overview of the problem

I am trying to implement a simple proof-of-concept wallet that would be able to make USDC transfers and pay for gas in USDC.

I have a smart account with 10 USDC on it's balance (basescan).

This is what i'm trying to do:

  • init a SimpleAccount using Presets.Builder.SimpleAccount preset with Paymaster of type erc20token.
  • build and send UserOperation for USDC transfer

However, when I run the code, I get an error message that says "processing response error" and "simulation: insufficient token payments". Here is the error message:

reason: 'processing response error',
  code: 'SERVER_ERROR',
  body: '{"error":{"code":-32601,"data":"simulation: insufficient token payments","message":"simulation: insufficient token payments"},"id":42,"jsonrpc":"2.0"}'

And here's the code I'm running:

import { ethers, Contract, Signer, Wallet } from "ethers";
import { Client, Presets } from "userop";
import { usdcAbi } from "./usdc-abi";

const entryPointAddress = "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789";
const usdcAddress = "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913";
const rpcUrl = "https://api.stackup.sh/v1/node/API_KEY";
const paymasterUrl = "https://api.stackup.sh/v1/paymaster/API_KEY";
const recipient = "RECEPIENT_ADDRESS";
const smartAccountOwnerPrivateKey = "smart account owner private key";
const lightAccountFactoryAddress = "0x00000055C0b4fA41dde26A74435ff03692292FBD";
// I'm using LightAccount from Alchemy. I've also tried SimpleAccount, results are the same
// const simpleAccountFactoryAddress = "0x15Ba39375ee2Ab563E8873C8390be6f2E2F50232";

async function sendTransferUserOp(
  signer: Signer,
  usdc: Contract,
  recipientAddress: string
) {
  const builder = await Presets.Builder.SimpleAccount.init(
    signer,
    rpcUrl,
    {
      factory: lightAccountFactoryAddress,
      paymasterMiddleware: Presets.Middleware.verifyingPaymaster(paymasterUrl, {
        type: "erc20token",
        token: usdcAddress,
      }),
    }
  );
  const address = builder.getSender();
  console.log(`Smart account address: ${address}`);
  console.log(`Balance: ${await usdc.balanceOf(address)}`); // 10000000

  const client = await Client.init(rpcUrl);

  const amount = ethers.utils.parseUnits("5", 6);
  const res = await client.sendUserOperation(
    builder.executeBatch(
      [usdcAddress, usdcAddress],
      [
        usdc.interface.encodeFunctionData("approve", [
          entryPointAddress,
          amount,
        ]),
        usdc.interface.encodeFunctionData("transfer", [
          recipientAddress,
          amount,
        ]),
      ]
    ),
    {
      onBuild: (op) => console.log("Signed UserOperation:", op),
    }
  );

  console.log(`UserOpHash: ${res.userOpHash}`);
  console.log("Waiting for transaction...");
  const ev = await res.wait();
  console.log(`Transaction hash: ${ev?.transactionHash ?? null}`);
}

async function main() {
  const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
  const signer = new Wallet(smartAccountOwnerPrivateKey, provider);
  console.log(`Wallet address: ${await signer.getAddress()}`);

  const contract = new Contract(usdcAddress, usdcAbi, signer);
  const call = await sendTransferUserOp(signer, contract, recipient);
}

main();

Also a question: why does the doc and examples say that ERC-20 approve call is needed? According to ERC-20 standard approve is required for transferFrom call and is not needed for direct transfer.

pending ops: replacement op must increase maxFeePerGas and MaxPriorityFeePerGas by >= 10%

I'm running my Geth node and stackup bundler on my machine. When I run yarn run simpleAccount transfer --to <address> --amount 0.01. I got the following error:

  error: Error: pending ops: replacement op must increase maxFeePerGas and MaxPriorityFeePerGas by >= 10%
      at getResult (/server2/weihan/erc-4337-examples/node_modules/@ethersproject/providers/src.ts/json-rpc-provider.ts:142:28)
      at processJsonFunc (/server2/weihan/erc-4337-examples/node_modules/@ethersproject/web/src.ts/index.ts:383:22)
      at /server2/weihan/erc-4337-examples/node_modules/@ethersproject/web/src.ts/index.ts:320:42
      at step (/server2/weihan/erc-4337-examples/node_modules/@ethersproject/web/lib/index.js:33:23)
      at Object.next (/server2/weihan/erc-4337-examples/node_modules/@ethersproject/web/lib/index.js:14:53)
      at fulfilled (/server2/weihan/erc-4337-examples/node_modules/@ethersproject/web/lib/index.js:5:58)
      at processTicksAndRejections (node:internal/process/task_queues:96:5) {
    code: -32602,
    data: 'pending ops: replacement op must increase maxFeePerGas and MaxPriorityFeePerGas by >= 10%'
  },
  requestBody: '{"method":"eth_sendUserOperation","params":[{"sender":"0xFC901229E04ca3fB830a5658A09807a7D5a012AF","nonce":"0x0","initCode":"0x71d63edcda95c61d6235552b5bc74e32d8e2527b5fbfb9cf0000000000000000000000009c85856d30cff8847939adbae5bddf3f7c549f960000000000000000000000000000000000000000000000000000000000000000","callData":"0xb61d27f6000000000000000000000000519eaeb4fd8986e2bc8a23741f2bf84fff3b2857000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000","callGasLimit":"0x5580","verificationGasLimit":"0x65139","maxFeePerGas":"0x51b5f0c4a","maxPriorityFeePerGas":"0x285c28","paymasterAndData":"0x","preVerificationGas":"0xaf9c","signature":"0xadc7c317397fba318fe075654586d4cb90ff1de3b92dd9ce271bca0f8403b571159dbc6924c3b50b40862369ff9426117d7930053669020dca1afec6dd500e7e1c"},"0x0576a174D229E3cFA37253523E645A78A0C91B57"],"id":43,"jsonrpc":"2.0"}',
  requestMethod: 'POST',
  url: 'http://localhost:4337'
}

Here's the Geth node command:

./go-ethereum/build/bin/geth --goerli --datadir /server2/weihan/ethereum/execution/datadir  --http --http.api="eth,web3,net,debug" --http.vhosts="*" --http.corsdomain "*" --authrpc.jwtsecret=/opt/jwtsecret --authrpc.vhosts="*"

May I ask if there's any solution to this issue?

Documentation not clear on need to install SimpleAccountFactory contract from account-abstraction

Set up:

With this setup, yarn run simpleAccount address produced SimpleAccount address: 0x0000000000000000000000000000000000000000 as the output, with the following logged in the local node:

WARN [03-10|13:33:30.709] Served eth_call  conn=127.0.0.1:52652 reqid=44 duration="581.499µs" err="execution reverted"                    
errdata=0x6ca7b8060000000000000000000000000000000000000000000000000000000000000000

Remedy

To remedy this, I installed the smart contract from the account-abstraction project by cloning that project and running the deploy script, e.g. yarn deploy --network localhost and updating config.json with the address of the freshly deployed SimpleAccountFactory address.

After deploying the account abstraction into the local environment, yarn run simpleAccount address produced an address as expected.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.