Skip to content

Commit

Permalink
fix: optimistic implementation for getNonce() and cache for isAccount…
Browse files Browse the repository at this point in the history
…Deployed
  • Loading branch information
ankurdubey521 committed Sep 28, 2023
1 parent 5053831 commit 5b1d4bf
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
4 changes: 3 additions & 1 deletion packages/account/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
"@biconomy/node-client": "^3.1.0",
"@biconomy/paymaster": "^3.1.0",
"@ethersproject/providers": "^5.7.2",
"ethers": "^5.7.0"
"ethers": "^5.7.0",
"loglevel": "^1.8.1",
"lru-cache": "^10.0.1"
}
}
15 changes: 13 additions & 2 deletions packages/account/src/BaseSmartAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { BaseSmartAccountConfig, Overrides, TransactionDetailsForUserOp } from "
import { GasOverheads } from "./utils/Preverificaiton";
import { EntryPoint, EntryPoint__factory } from "@account-abstraction/contracts";
import { DEFAULT_ENTRYPOINT_ADDRESS } from "./utils/Constants";
import LRUCache = require("lru-cache");

type UserOperationKey = keyof UserOperation;

Expand All @@ -35,6 +36,10 @@ export abstract class BaseSmartAccount implements IBaseSmartAccount {
// entryPoint connected to "zero" address. allowed to make static calls (e.g. to getSenderAddress)
private readonly entryPoint!: EntryPoint;

private isContractDeployedCache = new LRUCache({
max: 500,
});

constructor(_smartAccountConfig: BaseSmartAccountConfig) {
this.index = _smartAccountConfig.index ?? 0;
this.overheads = _smartAccountConfig.overheads;
Expand Down Expand Up @@ -242,8 +247,9 @@ export abstract class BaseSmartAccount implements IBaseSmartAccount {
delete userOp.maxFeePerGas;
delete userOp.maxPriorityFeePerGas;
// Making call to bundler to get gas estimations for userOp
const { callGasLimit, verificationGasLimit, preVerificationGas, maxFeePerGas, maxPriorityFeePerGas } =
await this.bundler.estimateUserOpGas(userOp);
const { callGasLimit, verificationGasLimit, preVerificationGas, maxFeePerGas, maxPriorityFeePerGas } = await this.bundler.estimateUserOpGas(

Check failure on line 250 in packages/account/src/BaseSmartAccount.ts

View workflow job for this annotation

GitHub Actions / Lint sources (18.x)

Replace `·await·this.bundler.estimateUserOpGas(⏎········userOp,⏎······` with `⏎········await·this.bundler.estimateUserOpGas(userOp`
userOp,
);
// if neither user sent gas fee nor the bundler, estimate gas from provider
if (!userOp.maxFeePerGas && !userOp.maxPriorityFeePerGas && (!maxFeePerGas || !maxPriorityFeePerGas)) {
const feeData = await this.provider.getFeeData();
Expand All @@ -270,10 +276,15 @@ export abstract class BaseSmartAccount implements IBaseSmartAccount {
}

async isAccountDeployed(address: string): Promise<boolean> {
if (this.isContractDeployedCache.get(address)) {
return true;
}

this.isProviderDefined();
let isDeployed = false;
const contractCode = await this.provider.getCode(address);
if (contractCode.length > 2) {
this.isContractDeployedCache.set(address, true);
isDeployed = true;
} else {
isDeployed = false;
Expand Down
10 changes: 7 additions & 3 deletions packages/account/src/BiconomySmartAccountV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
DEFAULT_FALLBACK_HANDLER_ADDRESS,
PROXY_CREATION_CODE,
} from "./utils/Constants";
import log from "loglevel";

type UserOperationKey = keyof UserOperation;
export class BiconomySmartAccountV2 extends BaseSmartAccount {
Expand Down Expand Up @@ -130,11 +131,14 @@ export class BiconomySmartAccountV2 extends BaseSmartAccount {
// Could call it nonce space
async getNonce(nonceKey?: number): Promise<BigNumber> {
const nonceSpace = nonceKey ?? 0;
if (await this.isAccountDeployed(await this.getAccountAddress())) {
try {
const accountContract = await this._getAccountContract();
return accountContract.nonce(nonceSpace);
const nonce = await accountContract.nonce(nonceSpace);
return nonce;
} catch (e) {
log.debug("Failed to get nonce from deployed account. Returning 0 as nonce");
return BigNumber.from(0);
}
return BigNumber.from(0);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/account/src/SmartAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ export abstract class SmartAccount implements ISmartAccount {
delete userOp.maxFeePerGas;
delete userOp.maxPriorityFeePerGas;
// Making call to bundler to get gas estimations for userOp
const { callGasLimit, verificationGasLimit, preVerificationGas, maxFeePerGas, maxPriorityFeePerGas } =
await this.bundler.estimateUserOpGas(userOp);
const { callGasLimit, verificationGasLimit, preVerificationGas, maxFeePerGas, maxPriorityFeePerGas } = await this.bundler.estimateUserOpGas(

Check failure on line 127 in packages/account/src/SmartAccount.ts

View workflow job for this annotation

GitHub Actions / Lint sources (18.x)

Replace `·await·this.bundler.estimateUserOpGas(⏎········userOp,⏎······` with `⏎········await·this.bundler.estimateUserOpGas(userOp`
userOp,
);
if (!userOp.maxFeePerGas && !userOp.maxPriorityFeePerGas && (!maxFeePerGas || !maxPriorityFeePerGas)) {
const feeData = await this.provider.getFeeData();
finalUserOp.maxFeePerGas = feeData.maxFeePerGas ?? feeData.gasPrice ?? (await this.provider.getGasPrice());
Expand Down
5 changes: 3 additions & 2 deletions packages/web3-auth-native/src/SocialLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ class SocialLogin {
}

async whitelistUrl(origin: string): Promise<string> {
const whiteListUrlResponse: WhiteListSignatureResponse =
await this.nodeClient.whitelistUrl(origin)
const whiteListUrlResponse: WhiteListSignatureResponse = await this.nodeClient.whitelistUrl(

Check failure on line 34 in packages/web3-auth-native/src/SocialLogin.ts

View workflow job for this annotation

GitHub Actions / Lint sources (18.x)

Replace `·await·this.nodeClient.whitelistUrl(⏎······origin⏎····` with `⏎······await·this.nodeClient.whitelistUrl(origin`
origin
)
return whiteListUrlResponse.data
}

Expand Down

0 comments on commit 5b1d4bf

Please sign in to comment.