Skip to content

Commit

Permalink
Cache create2 factory addresses (#4922)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaryash90 authored Oct 4, 2024
1 parent d4c423c commit 5211446
Showing 1 changed file with 70 additions and 57 deletions.
127 changes: 70 additions & 57 deletions packages/thirdweb/src/contract/deployment/utils/create-2-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -42,73 +43,85 @@ export async function computeCreate2FactoryAddress(
): Promise<string> {
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
},
);
}

/**
Expand Down

0 comments on commit 5211446

Please sign in to comment.