@@ -9,6 +9,7 @@ import { prepareTransaction } from "../../../transaction/prepare-transaction.js"
9
9
import { isEIP155Enforced } from "../../../utils/any-evm/is-eip155-enforced.js" ;
10
10
import { getKeylessTransaction } from "../../../utils/any-evm/keyless-transaction.js" ;
11
11
import { isContractDeployed } from "../../../utils/bytecode/is-contract-deployed.js" ;
12
+ import { withCache } from "../../../utils/promise/withCache.js" ;
12
13
import type {
13
14
ClientAndChain ,
14
15
ClientAndChainAndAccount ,
@@ -42,73 +43,85 @@ export async function computeCreate2FactoryAddress(
42
43
) : Promise < string > {
43
44
const chainId = options . chain . id ;
44
45
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 ;
51
54
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
+ ) ;
56
62
57
- return deploymentInfo . predictedAddress ;
58
- }
63
+ return deploymentInfo . predictedAddress ;
64
+ }
59
65
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
+ } ) ,
66
72
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
+ ] ) ;
72
78
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
+ ) ;
82
88
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
+ }
89
95
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
+ }
99
106
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 ) ;
106
113
107
- const deploymentInfo = await _getCreate2FactoryDeploymentInfo ( eipChain , {
108
- gasPrice : bin ,
109
- } ) ;
114
+ const deploymentInfo = await _getCreate2FactoryDeploymentInfo ( eipChain , {
115
+ gasPrice : bin ,
116
+ } ) ;
110
117
111
- return deploymentInfo . predictedAddress ;
118
+ return deploymentInfo . predictedAddress ;
119
+ } ,
120
+ {
121
+ cacheKey : `create2factory:${ chainId } ` ,
122
+ cacheTime : 24 * 60 * 60 * 1000 , // 1 day
123
+ } ,
124
+ ) ;
112
125
}
113
126
114
127
/**
0 commit comments