Skip to content

Commit

Permalink
fix: extract correct account sequence for injective
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronCQL committed Dec 20, 2023
1 parent a2817ca commit a41cfa8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## `v0.0.46`

### Fixes

- Fixed `ConnectedWallet.estimateFee` to properly extract account sequences from errors thrown by Injective RPCs

## `v0.0.45`

### Improvements
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cosmes",
"version": "0.0.45",
"version": "0.0.46",
"private": false,
"packageManager": "[email protected]",
"sideEffects": false,
Expand Down
25 changes: 25 additions & 0 deletions src/wallet/utils/sequence.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, it } from "vitest";

import { extractExpectedAccountSequence } from "./sequence";

describe("extractExpectedAccountSequence", () => {
it("should extract expected account sequence numbers from valid errors", () => {
// Older cosmos sdk chains
const err1 = new Error(
`account sequence mismatch, expected 10, got 11: incorrect account sequence: invalid request`
);
expect(extractExpectedAccountSequence(err1)).toBe(10n);

// Newer cosmos sdk chains (0.45+)
const err2 = new Error(
`rpc error: code = Unknown desc = account sequence mismatch, expected 10, got 11: ......`
);
expect(extractExpectedAccountSequence(err2)).toBe(10n);

// Injective
const err3 = new Error(
`[reason]:"incorrect account sequence" metadata:{key:"ABCICode" value:"32"} ...... rpc error: code = Unknown desc = account sequence mismatch, expected 10, got 11: ......`
);
expect(extractExpectedAccountSequence(err3)).toBe(10n);
});
});
14 changes: 14 additions & 0 deletions src/wallet/utils/sequence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Extracts and returns the expected account sequence number from an error. If
* the error is not related to an account sequence mismatch, `null` is returned.
*/
export function extractExpectedAccountSequence(err: Error): bigint | null {
const matches = err.message.match(
// This regex is intentionally kept as strict as possible
/account sequence mismatch, expected (\d+), got (\d+):/
);
if (!matches || matches.length < 3) {
return null;
}
return BigInt(matches[1]);
}
20 changes: 8 additions & 12 deletions src/wallet/wallets/ConnectedWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {

import type { WalletName } from "../constants/WalletName";
import type { WalletType } from "../constants/WalletType";
import { extractExpectedAccountSequence } from "../utils/sequence";

export type UnsignedTx = {
msgs: Adapter[];
Expand Down Expand Up @@ -125,22 +126,17 @@ export abstract class ConnectedWallet {
try {
return await estimate();
} catch (err) {
if (
!(err instanceof Error) ||
!err.message.includes("account sequence mismatch")
) {
// Rethrow if the error is not related to account sequence
if (!(err instanceof Error)) {
// Rethrow non-errors
throw err;
}
// Possible messages:
// "account sequence mismatch, expected 10, got 11: incorrect account sequence: invalid request"
// "rpc error: code = Unknown desc = account sequence mismatch, expected 10, got 11: ..."
const matches = err.message.match(/(\d+)/g);
if (!matches || matches.length < 2) {
throw new Error("Failed to parse account sequence");
const expectedSequence = extractExpectedAccountSequence(err);
if (!expectedSequence) {
// Rethrow errors not related to account sequence mismatch
throw err;
}
// Set the cached sequence to the one from the error message
this.sequence = BigInt(matches[0]);
this.sequence = expectedSequence;
return estimate();
}
}
Expand Down

0 comments on commit a41cfa8

Please sign in to comment.