Skip to content

Commit

Permalink
Merge pull request #307 from bcnmy/fixes/build-userop-opt
Browse files Browse the repository at this point in the history
Latency Optimisations for buildUserOp
  • Loading branch information
livingrockrises authored Sep 28, 2023
2 parents 5053831 + 2a3e8c3 commit c1a28c8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 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"
}
}
10 changes: 10 additions & 0 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 } from '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 @@ -270,10 +275,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

0 comments on commit c1a28c8

Please sign in to comment.