Skip to content

Commit 5211446

Browse files
authored
Cache create2 factory addresses (#4922)
1 parent d4c423c commit 5211446

File tree

1 file changed

+70
-57
lines changed

1 file changed

+70
-57
lines changed

packages/thirdweb/src/contract/deployment/utils/create-2-factory.ts

Lines changed: 70 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { prepareTransaction } from "../../../transaction/prepare-transaction.js"
99
import { isEIP155Enforced } from "../../../utils/any-evm/is-eip155-enforced.js";
1010
import { getKeylessTransaction } from "../../../utils/any-evm/keyless-transaction.js";
1111
import { isContractDeployed } from "../../../utils/bytecode/is-contract-deployed.js";
12+
import { withCache } from "../../../utils/promise/withCache.js";
1213
import type {
1314
ClientAndChain,
1415
ClientAndChainAndAccount,
@@ -42,73 +43,85 @@ export async function computeCreate2FactoryAddress(
4243
): Promise<string> {
4344
const chainId = options.chain.id;
4445

45-
// special handling for chains with hardcoded gasPrice and gasLimit
46-
if (CUSTOM_GAS_FOR_CHAIN[chainId]) {
47-
const enforceEip155 = await isEIP155Enforced(options);
48-
const eipChain = enforceEip155 ? chainId : 0;
49-
const gasPrice = CUSTOM_GAS_FOR_CHAIN[chainId.toString()]?.gasPrice;
50-
const gasLimit = CUSTOM_GAS_FOR_CHAIN[chainId.toString()]?.gasLimit;
46+
return withCache(
47+
async () => {
48+
// special handling for chains with hardcoded gasPrice and gasLimit
49+
if (CUSTOM_GAS_FOR_CHAIN[chainId]) {
50+
const enforceEip155 = await isEIP155Enforced(options);
51+
const eipChain = enforceEip155 ? chainId : 0;
52+
const gasPrice = CUSTOM_GAS_FOR_CHAIN[chainId.toString()]?.gasPrice;
53+
const gasLimit = CUSTOM_GAS_FOR_CHAIN[chainId.toString()]?.gasLimit;
5154

52-
const deploymentInfo = await _getCreate2FactoryDeploymentInfo(eipChain, {
53-
gasPrice,
54-
gasLimit,
55-
});
55+
const deploymentInfo = await _getCreate2FactoryDeploymentInfo(
56+
eipChain,
57+
{
58+
gasPrice,
59+
gasLimit,
60+
},
61+
);
5662

57-
return deploymentInfo.predictedAddress;
58-
}
63+
return deploymentInfo.predictedAddress;
64+
}
5965

60-
// default flow
61-
const allBinsInfo = await Promise.all([
62-
// to generate EIP-155 transaction
63-
...CUSTOM_GAS_BINS.map((b) => {
64-
return _getCreate2FactoryDeploymentInfo(chainId, { gasPrice: b });
65-
}),
66+
// default flow
67+
const allBinsInfo = await Promise.all([
68+
// to generate EIP-155 transaction
69+
...CUSTOM_GAS_BINS.map((b) => {
70+
return _getCreate2FactoryDeploymentInfo(chainId, { gasPrice: b });
71+
}),
6672

67-
// to generate pre EIP-155 transaction, hence chainId 0
68-
...CUSTOM_GAS_BINS.map((b) => {
69-
return _getCreate2FactoryDeploymentInfo(0, { gasPrice: b });
70-
}),
71-
]);
73+
// to generate pre EIP-155 transaction, hence chainId 0
74+
...CUSTOM_GAS_BINS.map((b) => {
75+
return _getCreate2FactoryDeploymentInfo(0, { gasPrice: b });
76+
}),
77+
]);
7278

73-
const allFactories = await Promise.all(
74-
allBinsInfo.map((b) => {
75-
const tempFactory = getContract({
76-
...options,
77-
address: b.predictedAddress,
78-
});
79-
return isContractDeployed(tempFactory);
80-
}),
81-
);
79+
const allFactories = await Promise.all(
80+
allBinsInfo.map((b) => {
81+
const tempFactory = getContract({
82+
...options,
83+
address: b.predictedAddress,
84+
});
85+
return isContractDeployed(tempFactory);
86+
}),
87+
);
8288

83-
const indexOfCommonFactory = allBinsInfo.findIndex(
84-
(b) => b.predictedAddress === COMMON_FACTORY_ADDRESS,
85-
);
86-
if (indexOfCommonFactory && allFactories[indexOfCommonFactory]) {
87-
return COMMON_FACTORY_ADDRESS;
88-
}
89+
const indexOfCommonFactory = allBinsInfo.findIndex(
90+
(b) => b.predictedAddress === COMMON_FACTORY_ADDRESS,
91+
);
92+
if (indexOfCommonFactory && allFactories[indexOfCommonFactory]) {
93+
return COMMON_FACTORY_ADDRESS;
94+
}
8995

90-
const indexOfExistingDeployment = allFactories.findIndex((b) => b);
91-
if (
92-
indexOfExistingDeployment &&
93-
allBinsInfo &&
94-
allBinsInfo[indexOfExistingDeployment]?.predictedAddress
95-
) {
96-
// TODO: cleanup
97-
return allBinsInfo[indexOfExistingDeployment]?.predictedAddress as string;
98-
}
96+
const indexOfExistingDeployment = allFactories.findIndex((b) => b);
97+
if (
98+
indexOfExistingDeployment &&
99+
allBinsInfo &&
100+
allBinsInfo[indexOfExistingDeployment]?.predictedAddress
101+
) {
102+
// TODO: cleanup
103+
return allBinsInfo[indexOfExistingDeployment]
104+
?.predictedAddress as string;
105+
}
99106

100-
const [enforceEip155, gasPriceFetched] = await Promise.all([
101-
isEIP155Enforced(options),
102-
getGasPrice(options),
103-
]);
104-
const eipChain = enforceEip155 ? chainId : 0;
105-
const bin = _getNearestGasPriceBin(gasPriceFetched);
107+
const [enforceEip155, gasPriceFetched] = await Promise.all([
108+
isEIP155Enforced(options),
109+
getGasPrice(options),
110+
]);
111+
const eipChain = enforceEip155 ? chainId : 0;
112+
const bin = _getNearestGasPriceBin(gasPriceFetched);
106113

107-
const deploymentInfo = await _getCreate2FactoryDeploymentInfo(eipChain, {
108-
gasPrice: bin,
109-
});
114+
const deploymentInfo = await _getCreate2FactoryDeploymentInfo(eipChain, {
115+
gasPrice: bin,
116+
});
110117

111-
return deploymentInfo.predictedAddress;
118+
return deploymentInfo.predictedAddress;
119+
},
120+
{
121+
cacheKey: `create2factory:${chainId}`,
122+
cacheTime: 24 * 60 * 60 * 1000, // 1 day
123+
},
124+
);
112125
}
113126

114127
/**

0 commit comments

Comments
 (0)