Skip to content

Commit

Permalink
Merge pull request #37 from bcnmy/feat/add-suggestions
Browse files Browse the repository at this point in the history
Add suggestions to decoded error
  • Loading branch information
tomarsachin2271 authored Jan 12, 2024
2 parents 6e0e17a + ccfc384 commit 17885a2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,33 @@ import { networkConfig } from "../../../../../config/network";
import { EntryPointFactory } from "../../../../../repository/entryPoint/factory";
import { DecodedError, ErrorSource } from "../../../../../types";
import { ErrorDecoderParams, IErrorDecoder } from "../../../interface/IErrorDecoder";
import { trim } from "../../../../../utils";

export class AA21Decoder implements IErrorDecoder {

async decodeError(param: ErrorDecoderParams): Promise<DecodedError> {
// TODO: Extract more information that is related to this error eg., the amount of native balance the smart account has
// and how much is needed to pay for the gas.
let epService = EntryPointFactory.getEntryPointService(param.entryPointAddress);
let requiredFunds;
let symbol, suggestedActions: string[] = [];
if(epService) {
let maxTransactionFee = await epService.getRequiredPreFund(param.userOp);
let symbol = networkConfig[param.networkId].nativeSymbol;
symbol = networkConfig[param.networkId].nativeSymbol;
requiredFunds = `${formatEther(maxTransactionFee)} ${symbol}`;
}
if(requiredFunds && symbol) {
suggestedActions = [
`Please add at least ${trim(requiredFunds, 5)} ${symbol} to the Smart Account.`,
`You can use a paymaster to either sponsor the gas fee or use ERC20 tokens to pay for the gas.`
];
}

return {
message: `Smart Account is supposed to pay for this userOp but it does not have enough native balance to pay for the gas.
Max ${requiredFunds} is required to pay for the gas.}`,
errorSource: ErrorSource.SMART_ACCOUNT
Max ${requiredFunds} is required to pay for the gas.`,
errorSource: ErrorSource.SMART_ACCOUNT,
suggestions: suggestedActions
}
}

Expand Down
1 change: 1 addition & 0 deletions src/types/userOp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface Error {
export interface DecodedError {
message: string;
errorSource: ErrorSource;
suggestions: string[];
}

export enum ErrorSource {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './userOpUtils';
export * from './date';
export * from './date';
export * from './math';
5 changes: 5 additions & 0 deletions src/utils/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function trim(numberString: string, decimalPlaces: number): string {
const regex = new RegExp(`^-?\\d+(?:\\.\\d{0,${decimalPlaces}})?`);
const match = numberString.match(regex);
return match ? match[0] : numberString;
}

0 comments on commit 17885a2

Please sign in to comment.