safe-global / safe-core-sdk

The Safe{Core} SDK allows builders to add account abstraction functionality into their apps.
https://docs.safe.global/sdk/overview
MIT License
243 stars 178 forks source link

Cannot send transactions after the first due to nonce = 0 in `Safe4337Pack.createTransaction` #819

Closed Qumeric closed 3 weeks ago

Qumeric commented 1 month ago

Description

Despite being able to create a new Safe (and execute the first tx at the same time) with 4337 relay kit, I can't sign next userOps because nonce is invalid (set to 0).

Environment

Create safe4337pack

  let newSafe = await Safe4337Pack.init({
    ethersAdapter,
    rpcUrl: baseSepoliaRpc,
    bundlerUrl:
      "https://api.pimlico.io/v2/84532/rpc?apikey=API_KEY",
    options: {
      owners: [address],
      threshold: 1,
    },
  });

Then safe.createTransaction, sign it (signSafeOperation) and execute. Very similar to the guide: https://docs.safe.global/sdk/relay-kit/guides/4337-safe-sdk. Everything is fine and I got this userOp: https://base-sepolia.blockscout.com/op/0xad3099202bf0de8ff4241a020b0ff66dbe0d422aa4645e0f3b736dec2a23fcdc

Now I am doing the same but with connecting to the deployed safe,

   let existingSafe = await Safe4337Pack.init({
      ethersAdapter,
      rpcUrl: baseSepoliaRpc,
      bundlerUrl:
        "https://api.pimlico.io/v2/84532/rpc?apikey=API_KEY",
      options: {
        safeAddress,
      },
    });
  }

I cannot even create transaction:

  const transactionData: SafeTransactionDataPartial = {
    to: addr,
    value: ethers.parseUnits("0.000001", "ether").toString(),
    data: "0x",
  };

  const safeOperation: SafeOperation =
    await existingSafe.createTransaction({
      transactions: [transactionData],
    });

I get this error:

Error: could not coalesce error (error={ "code": -32602, "message": "UserOperation reverted during simulation with reason: AA25 invalid account nonce" }, payload={ "id": 4, "jsonrpc": "2.0", "method": "eth_estimateUserOperationGas", "params": [ { "callData": "0x7bb374280000000000000000000000002255d3021187d22146b0e08c5a1683070fa32b7a000000000000000000000000000000000000000000000000000000e8d4a51000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "callGasLimit": "0x01", "initCode": "0x", "maxFeePerGas": "0x01", "maxPriorityFeePerGas": "0x01", "nonce": "0x00", "paymasterAndData": "0x", "preVerificationGas": "0x01", "sender": "0x81761C213EBc14f7A93F81Bde5f8389983891f2A", "signature": "0x000000000000000000000000", "verificationGasLimit": "0x01" }, "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789" ] }, code=UNKNOWN_ERROR, version=6.12.1)
    at makeError (.../ethers/src.ts/utils/errors.ts:694:21)
    at JsonRpcProvider.getRpcError (.../node_modules/ethers/src.ts/providers/provider-jsonrpc.ts:1049:25)
    at .../provider-jsonrpc.ts:563:45
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'UNKNOWN_ERROR',
  error: {
    message: 'UserOperation reverted during simulation with reason: AA25 invalid account nonce',
    code: -32602
  },
  payload: {
    method: 'eth_estimateUserOperationGas',
    params: [ [Object], '0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789' ],
    id: 4,
    jsonrpc: '2.0'
  },
  shortMessage: 'could not coalesce error'
}

Expected result

Gas shall be successfully estimated and user op created. Nonce shall be 1 I think (instead of 0).

yagopv commented 1 month ago

Hey @Qumeric, is it posible to share the whole snippet or some playground to reproduce it?

I'm looking forward about how you reinitialize the SDK or where the safeAddress come from

yagopv commented 1 month ago

Hey @Qumeric I created my own script to test this and seems to be working on base sepolia. Take a look please and let me know if this helps https://gist.github.com/yagopv/60367ed1343ccc73f91e8feb6547b0e3

yagopv commented 3 weeks ago

Hey @Qumeric could you fix this issue?