osmosis-labs / osmojs

OsmosJS makes it easy to compose and broadcast Osmosis and Cosmos messages
https://cosmology.zone/products/osmojs
Apache License 2.0
62 stars 32 forks source link

Random "signature verification failed" errors on WithdrawPosition txs #85

Open Moreno-dev opened 5 months ago

Moreno-dev commented 5 months ago

Title:

Random "signature verification failed" errors on WithdrawPosition txs

Description:

When executing transactions to withdraw a position, I occasionally encounter a failure in the transaction simulation and broadcasting process. The error returned is "Failed to simulate and broadcast after 3 attempts: Error: Broadcasting transaction failed with code 4 (codespace: sdk). Log: signature verification failed; please verify account number (12345), sequence (123) and chain-id (osmosis-1): unauthorized." This issue arises sporadically and does not consistently reproduce with the same conditions.

Environment Details:

Implementation

// all imports for the signing client

async init() {
  this.signer = await Secp256k1HdWallet.fromMnemonic(
    this.accountMnemonic,
    {
      prefix: "osmo",
      hdPaths: [makeCosmoshubPath(this.accountIndex)]
    }
  );

  this.address = (await this.signer.getAccounts())[0].address

  const protoRegistry: ReadonlyArray<[string, GeneratedType]> = [
    ...cosmosProtoRegistry,
    ...cosmwasmProtoRegistry,
    ...ibcProtoRegistry,
    ...osmosisProtoRegistry
  ];

  const aminoConverters = {
    ...cosmosAminoConverters,
    ...cosmwasmAminoConverters,
    ...ibcAminoConverters,
    ...osmosisAminoConverters
  };

  const registry = new Registry(protoRegistry);
  const aminoTypes = new AminoTypes(aminoConverters);

  this.client = await SigningStargateClient.connectWithSigner(this.rpcEndpoint, this.signer, {
    registry,
    aminoTypes
  });
}

import {MsgWithdrawPosition} from "osmojs/dist/codegen/osmosis/concentratedliquidity/v1beta1/tx";
import {Position} from "osmojs/dist/codegen/osmosis/concentratedliquidity/v1beta1/position";

async withdrawPosition(position: Position): Promise<DeliverTxResponse> {
    let value: MsgWithdrawPosition = {
        positionId: position.positionId,
        sender: this.address,
        liquidityAmount: position.liquidity,
    }

    return await this.submitTx([{
        typeUrl: "/osmosis.concentratedliquidity.v1beta1.MsgWithdrawPosition",
        value
    }])
}

async submitTx() {
    const gasWanted: number = await this.client.simulate(this.address, [messages[i]])
    const fee = await this.calculateFee(gasWanted);
    const response = await this.client.signAndBroadcast(this.address, [messages[i]], fee);
}

private async calculateFee(gasWanted: number): Promise<StdFee> {
    const gas = Math.ceil(gasWanted * 1.3);
    const amount = String(Math.ceil((0.0025 * 1) * gas)); // TODO Implement EIP1559
    return {
        amount: [{denom: "uosmo", amount}],
        gas: gas.toString(),
    }
}

Actual Behavior:

The transaction intermittently fails with a signature verification error, despite extensive testing across 5 different public RPCs and accounts on the mainnet. These tests, conducted simultaneously to rule out time-related factors, yielded inconsistent results—successes mixed with failures—without any discernible pattern. The issue's sporadic nature, occurring in about half of the attempts, and the straightforward code complicate pinpointing the cause, as failures seem random and without a clear failure pattern.

Error Messages:

Failed to simulate and broadcast after 3 attempts: Error: Broadcasting transaction failed with code 4 (codespace: sdk). Log: signature verification failed; please verify account number (12345), sequence (123) and chain-id (osmosis-1): unauthorized.

Additional Context:

I suspect this could be related to how transaction simulation and sequence numbers are handled or possibly an issue with the chain-id validation process. Any insights or suggestions on how to troubleshoot or resolve this issue would be greatly appreciated.