From 5211446f2066f5aa8cb441d8e66477e34c0c9ace Mon Sep 17 00:00:00 2001 From: Yash <72552910+kumaryash90@users.noreply.github.com> Date: Sat, 5 Oct 2024 02:57:46 +0530 Subject: [PATCH] Cache create2 factory addresses (#4922) --- .../deployment/utils/create-2-factory.ts | 127 ++++++++++-------- 1 file changed, 70 insertions(+), 57 deletions(-) diff --git a/packages/thirdweb/src/contract/deployment/utils/create-2-factory.ts b/packages/thirdweb/src/contract/deployment/utils/create-2-factory.ts index 91d281dd78f..118d907d6a5 100644 --- a/packages/thirdweb/src/contract/deployment/utils/create-2-factory.ts +++ b/packages/thirdweb/src/contract/deployment/utils/create-2-factory.ts @@ -9,6 +9,7 @@ import { prepareTransaction } from "../../../transaction/prepare-transaction.js" import { isEIP155Enforced } from "../../../utils/any-evm/is-eip155-enforced.js"; import { getKeylessTransaction } from "../../../utils/any-evm/keyless-transaction.js"; import { isContractDeployed } from "../../../utils/bytecode/is-contract-deployed.js"; +import { withCache } from "../../../utils/promise/withCache.js"; import type { ClientAndChain, ClientAndChainAndAccount, @@ -42,73 +43,85 @@ export async function computeCreate2FactoryAddress( ): Promise { const chainId = options.chain.id; - // special handling for chains with hardcoded gasPrice and gasLimit - if (CUSTOM_GAS_FOR_CHAIN[chainId]) { - const enforceEip155 = await isEIP155Enforced(options); - const eipChain = enforceEip155 ? chainId : 0; - const gasPrice = CUSTOM_GAS_FOR_CHAIN[chainId.toString()]?.gasPrice; - const gasLimit = CUSTOM_GAS_FOR_CHAIN[chainId.toString()]?.gasLimit; + return withCache( + async () => { + // special handling for chains with hardcoded gasPrice and gasLimit + if (CUSTOM_GAS_FOR_CHAIN[chainId]) { + const enforceEip155 = await isEIP155Enforced(options); + const eipChain = enforceEip155 ? chainId : 0; + const gasPrice = CUSTOM_GAS_FOR_CHAIN[chainId.toString()]?.gasPrice; + const gasLimit = CUSTOM_GAS_FOR_CHAIN[chainId.toString()]?.gasLimit; - const deploymentInfo = await _getCreate2FactoryDeploymentInfo(eipChain, { - gasPrice, - gasLimit, - }); + const deploymentInfo = await _getCreate2FactoryDeploymentInfo( + eipChain, + { + gasPrice, + gasLimit, + }, + ); - return deploymentInfo.predictedAddress; - } + return deploymentInfo.predictedAddress; + } - // default flow - const allBinsInfo = await Promise.all([ - // to generate EIP-155 transaction - ...CUSTOM_GAS_BINS.map((b) => { - return _getCreate2FactoryDeploymentInfo(chainId, { gasPrice: b }); - }), + // default flow + const allBinsInfo = await Promise.all([ + // to generate EIP-155 transaction + ...CUSTOM_GAS_BINS.map((b) => { + return _getCreate2FactoryDeploymentInfo(chainId, { gasPrice: b }); + }), - // to generate pre EIP-155 transaction, hence chainId 0 - ...CUSTOM_GAS_BINS.map((b) => { - return _getCreate2FactoryDeploymentInfo(0, { gasPrice: b }); - }), - ]); + // to generate pre EIP-155 transaction, hence chainId 0 + ...CUSTOM_GAS_BINS.map((b) => { + return _getCreate2FactoryDeploymentInfo(0, { gasPrice: b }); + }), + ]); - const allFactories = await Promise.all( - allBinsInfo.map((b) => { - const tempFactory = getContract({ - ...options, - address: b.predictedAddress, - }); - return isContractDeployed(tempFactory); - }), - ); + const allFactories = await Promise.all( + allBinsInfo.map((b) => { + const tempFactory = getContract({ + ...options, + address: b.predictedAddress, + }); + return isContractDeployed(tempFactory); + }), + ); - const indexOfCommonFactory = allBinsInfo.findIndex( - (b) => b.predictedAddress === COMMON_FACTORY_ADDRESS, - ); - if (indexOfCommonFactory && allFactories[indexOfCommonFactory]) { - return COMMON_FACTORY_ADDRESS; - } + const indexOfCommonFactory = allBinsInfo.findIndex( + (b) => b.predictedAddress === COMMON_FACTORY_ADDRESS, + ); + if (indexOfCommonFactory && allFactories[indexOfCommonFactory]) { + return COMMON_FACTORY_ADDRESS; + } - const indexOfExistingDeployment = allFactories.findIndex((b) => b); - if ( - indexOfExistingDeployment && - allBinsInfo && - allBinsInfo[indexOfExistingDeployment]?.predictedAddress - ) { - // TODO: cleanup - return allBinsInfo[indexOfExistingDeployment]?.predictedAddress as string; - } + const indexOfExistingDeployment = allFactories.findIndex((b) => b); + if ( + indexOfExistingDeployment && + allBinsInfo && + allBinsInfo[indexOfExistingDeployment]?.predictedAddress + ) { + // TODO: cleanup + return allBinsInfo[indexOfExistingDeployment] + ?.predictedAddress as string; + } - const [enforceEip155, gasPriceFetched] = await Promise.all([ - isEIP155Enforced(options), - getGasPrice(options), - ]); - const eipChain = enforceEip155 ? chainId : 0; - const bin = _getNearestGasPriceBin(gasPriceFetched); + const [enforceEip155, gasPriceFetched] = await Promise.all([ + isEIP155Enforced(options), + getGasPrice(options), + ]); + const eipChain = enforceEip155 ? chainId : 0; + const bin = _getNearestGasPriceBin(gasPriceFetched); - const deploymentInfo = await _getCreate2FactoryDeploymentInfo(eipChain, { - gasPrice: bin, - }); + const deploymentInfo = await _getCreate2FactoryDeploymentInfo(eipChain, { + gasPrice: bin, + }); - return deploymentInfo.predictedAddress; + return deploymentInfo.predictedAddress; + }, + { + cacheKey: `create2factory:${chainId}`, + cacheTime: 24 * 60 * 60 * 1000, // 1 day + }, + ); } /**