Skip to content

Commit

Permalink
fix: decode result data error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyl-ivanchuk committed Oct 31, 2024
1 parent edcbd02 commit 1329bcf
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
29 changes: 28 additions & 1 deletion packages/data-fetcher/src/blockchain/retryableContract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe("RetryableContract", () => {
expect(result).toBe(functionResult);
});

describe("when throws a permanent call exception function error", () => {
describe("when throws a permanent execution reverted error", () => {
const callExceptionError = {
code: 3,
shortMessage: "execution reverted...",
Expand All @@ -125,6 +125,33 @@ describe("RetryableContract", () => {
});
});

describe("when throws a permanent could not decode result data error", () => {
const callExceptionError = {
code: "BAD_DATA",
shortMessage: "could not decode result data...",
};

beforeEach(() => {
(ethers.Contract as any as jest.Mock).mockReturnValue({
contractFn: async () => {
throw callExceptionError;
},
});

contract = new RetryableContract(tokenAddress, utils.IERC20, providerMock);
});

it("throws an error", async () => {
expect.assertions(1);

try {
await contract.contractFn();
} catch (e) {
expect(e).toBe(callExceptionError);
}
});
});

describe("when throws an invalid argument function error", () => {
const invalidArgumentError = {
code: "INVALID_ARGUMENT",
Expand Down
8 changes: 7 additions & 1 deletion packages/data-fetcher/src/blockchain/retryableContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ const PERMANENT_ERRORS: ErrorCode[] = [

const shouldRetry = (error: EthersError): boolean => {
const isPermanentErrorCode = PERMANENT_ERRORS.find((errorCode) => isError(error, errorCode));
return !isPermanentErrorCode && !(error.code === 3 && error.shortMessage?.startsWith("execution reverted"));
return (
!isPermanentErrorCode &&
// example block mainnet 47752810
!(error.code === 3 && error.shortMessage?.startsWith("execution reverted")) &&
// example block mainnet 47819836
!(error.code === "BAD_DATA" && error.shortMessage?.startsWith("could not decode result data"))
);
};

const retryableFunctionCall = async (
Expand Down
31 changes: 29 additions & 2 deletions packages/worker/src/blockchain/retryableContract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,37 @@ describe("RetryableContract", () => {
expect(result).toBe(functionResult);
});

describe("when throws a permanent call exception function error", () => {
describe("when throws a permanent execution reverted error", () => {
const callExceptionError = {
code: 3,
shortMessage: "execution reverted ....",
shortMessage: "execution reverted...",
};

beforeEach(() => {
(ethers.Contract as any as jest.Mock).mockReturnValue({
contractFn: async () => {
throw callExceptionError;
},
});

contract = new RetryableContract(tokenAddress, utils.IERC20, providerMock);
});

it("throws an error", async () => {
expect.assertions(1);

try {
await contract.contractFn();
} catch (e) {
expect(e).toBe(callExceptionError);
}
});
});

describe("when throws a permanent could not decode result data error", () => {
const callExceptionError = {
code: "BAD_DATA",
shortMessage: "could not decode result data...",
};

beforeEach(() => {
Expand Down
8 changes: 7 additions & 1 deletion packages/worker/src/blockchain/retryableContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ const PERMANENT_ERRORS: ErrorCode[] = [

const shouldRetry = (error: EthersError): boolean => {
const isPermanentErrorCode = PERMANENT_ERRORS.find((errorCode) => isError(error, errorCode));
return !isPermanentErrorCode && !(error.code === 3 && error.shortMessage?.startsWith("execution reverted"));
return (
!isPermanentErrorCode &&
// example block mainnet 47752810
!(error.code === 3 && error.shortMessage?.startsWith("execution reverted")) &&
// example block mainnet 47819836
!(error.code === "BAD_DATA" && error.shortMessage?.startsWith("could not decode result data"))
);
};

const retryableFunctionCall = async (
Expand Down

0 comments on commit 1329bcf

Please sign in to comment.