This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
contractConnectors.ts
314 lines (283 loc) Β· 9.54 KB
/
contractConnectors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { BaseContract, ethers } from 'ethers';
import { merge } from 'merge-anything';
import {
TContractConnectorBase,
TContractConnector,
TContractConnectFunc,
TExternalContractsAddressMap,
TBasicContractDeployment,
TContractDeploymentMap,
TContractMapWithAbi,
TDeployedHardhatContractsJson,
deployedHardhatContractsJsonSchema,
TBasicContract,
TForgeDeploymentBroadcastCollection,
forgeDeploymentBroadcastCollectionSchema,
externalContractAddressMap,
TBaseContractExtended,
} from '~~/models';
/**
* #### Summary
* This function is used to extract the contract data from hardhat deployment json files
*
* @internal
* @category ContractAppContext
* @param configJson {@link TDeployedHardhatContractsJson}
* @returns
*/
const extractHardhatContracts = (configJson: TDeployedHardhatContractsJson): TContractMapWithAbi => {
const parse = deployedHardhatContractsJsonSchema.safeParse(configJson);
if (!parse.success) {
console.error('Invalid deployment hardhat_contracts.json TDeployedHardhatContractsJson', parse.error);
}
const contractData: TContractMapWithAbi = {};
for (const chainIdStr in configJson) {
const chainId = parseInt(chainIdStr);
if (chainId == null || isNaN(chainId)) continue;
const deployedDataByNetwork = Object.values(configJson[chainId]).filter(
(f) => parseInt(f?.chainId) === chainId
)?.[0];
if (deployedDataByNetwork?.chainId != null) {
for (const contractName in deployedDataByNetwork.contracts) {
const config: TBasicContractDeployment = {
[chainId]: { address: deployedDataByNetwork.contracts[contractName].address, chainId },
};
const abi = deployedDataByNetwork.contracts[contractName].abi;
if (abi && abi?.length > 0) {
contractData[contractName] = merge(contractData[contractName] ?? {}, { abi: abi });
}
contractData[contractName] = merge({ ...contractData[contractName] }, { config });
}
}
}
return contractData;
};
/**
* #### Summary
* Extract externalContracts from TExternalContractsAddressMap
* @param configJson
* @returns
*/
const extractExternalContracts = (configJson: TExternalContractsAddressMap): TContractDeploymentMap => {
const parse = externalContractAddressMap.safeParse(configJson);
if (!parse.success) {
console.error('Invalid TExternalContractsAddressMap', parse.error);
}
const contractData: TContractDeploymentMap = {};
for (const chainIdStr in configJson) {
const chainId = parseInt(chainIdStr);
if (chainId == null || isNaN(chainId)) continue;
for (const contractName in configJson[chainId]) {
const config: TBasicContractDeployment = {
[chainId]: { address: configJson[chainId][contractName], chainId: chainId },
};
contractData[contractName] = merge({ ...(contractData[contractName] ?? {}) }, { config });
}
}
return contractData;
};
/**
* #### Summary
* Extract forge contracts from forgeDeploymentBroadcasts
* @param configJson
* @returns
*/
const extractForgeBroadcastContracts = (configJson: TForgeDeploymentBroadcastCollection): TContractDeploymentMap => {
const parse = forgeDeploymentBroadcastCollectionSchema.safeParse(configJson);
if (!parse.success) {
console.error('Invalid forge boradcast json TForgeBroadcastJson', parse.error);
}
const contractData: TContractDeploymentMap = {};
for (const chainIdStr in configJson) {
const chainId = parseInt(chainIdStr);
if (chainId == null || isNaN(chainId)) continue;
const contractDeployments = configJson[chainId].transactions
.filter((f) => f.transactionType === 'CREATE')
.map((m) => {
const td: TBasicContract = {
contractName: m.contractName.replace('Deploy', '').replace('deploy', ''),
address: m.contractAddress,
};
return td;
});
const contractNameList = contractDeployments.map((m) => m.contractName);
contractNameList.forEach((contractName) => {
const data = contractDeployments.filter((f) => f.contractName === contractName);
if (data.length === 0) {
console.error('Contract deployments NOT found for contract', contractName, `chainId: ${chainId}`);
} else if (data.length > 1) {
console.error(
'Multiple contract deployments found for contract on the same chainId',
contractName,
`chainId: ${chainId}`
);
}
console.log('output data', data[0]);
const config: TBasicContractDeployment = {
[chainId]: { address: data?.[0]?.address, chainId: chainId },
};
contractData[contractName] = merge({ ...(contractData[contractName] ?? {}) }, { config });
});
}
return contractData;
};
/**
* ##### Summary
* Creates a connector for any of your hardhat contracts
*
* @category ContractAppContext
* @param contractName
* @param typechainFactory
* @param deployedHardhatContractJson
* @returns
*/
export const createConnectorForHardhatContract = <
GContractNames extends string,
GBaseContract extends TBaseContractExtended<GContractNames>
>(
contractName: GContractNames,
typechainFactory: TContractConnectorBase<GBaseContract>,
deployedHardhatContractJson: TDeployedHardhatContractsJson
): TContractConnector<GContractNames, GBaseContract> => {
const info = extractHardhatContracts(deployedHardhatContractJson)[contractName];
if (info == null || info.abi == null) {
throw new Error(
`Contract ${contractName} not found in deployed contracts (hardhat_config.json). Check your hardhat deploy scripts and hardhat_config.json`
);
}
return {
contractName,
connect: typechainFactory.connect,
// createInterface: typechainFactory.createInterface,
abi: (info?.abi ?? typechainFactory.abi ?? []) as Record<string, any>[],
config: {
...info.config,
},
};
};
/**
* ##### Summary
* Creates a connector for any of your Foundry contracts, deployed forge scripts and forge broadcast output
*
* @category ContractAppContext
* @param contractName
* @param typechainFactory
* @param forgeBroadcastJson
* @returns
*/
export const createConnectorForFoundryContract = <
GContractNames extends string,
GBaseContract extends TBaseContractExtended<GContractNames>
>(
contractName: GContractNames,
typechainFactory: TContractConnectorBase<GBaseContract>,
forgeBroadcastJson: TForgeDeploymentBroadcastCollection
): TContractConnector<GContractNames, GBaseContract> => {
const info = extractForgeBroadcastContracts(forgeBroadcastJson)[contractName];
if (info == null || typechainFactory.abi == null) {
throw new Error(
`Contract ${contractName} not found in deployed contracts (foundry_config.json). Check your foundry deploy script`
);
}
return {
contractName,
connect: typechainFactory.connect,
// createInterface: typechainFactory.createInterface,
abi: (typechainFactory.abi ?? []) as Record<string, any>[],
config: {
...info.config,
},
};
};
/**
* #### Summary
* Creates a contract connector for any external contract
*
* ##### βοΈ Notes
* - As an example you could use this for an external contract such as DAI
*
* @category ContractAppContext
* @param contractName
* @param typechainFactory
* @param deployedContractJson
* @returns
*/
export const createConnectorForExternalContract = <
GContractNames extends string,
GBaseContract extends TBaseContractExtended<GContractNames>
>(
contractName: GContractNames,
typechainFactory: TContractConnectorBase<GBaseContract>,
deployedContractJson: TExternalContractsAddressMap
): TContractConnector<GContractNames, GBaseContract> => {
const info = extractExternalContracts(deployedContractJson)[contractName];
if (info == null) {
throw new Error(
`Contract ${contractName} not found in external contract map. Check that contractName: address map is correct. This is required by eth-sdk`
);
}
return {
contractName,
connect: typechainFactory.connect,
// : typechainFactory.createInterface,
abi: typechainFactory.abi ?? [],
config: {
...info.config,
},
};
};
/**
* #### Summary
* Create a contract connector from a ABI.
*
* ##### βοΈ Notes
* - This can be used for unverified external contracts
*
* @category ContractAppContext
* @param contractName
* @param config
* @param abi
* @param connectFunc
* @returns
*/
export const createConnectorForExternalAbi = <
GContractNames extends string,
GBaseContractExtended extends TBaseContractExtended<GContractNames> = TBaseContractExtended<GContractNames>
>(
contractName: GContractNames,
config: {
[chainId: number]: {
address: string;
};
},
abi: Record<string, any>[],
connectFunc: TContractConnectFunc<GBaseContractExtended> | undefined = undefined
): TContractConnector<GContractNames, GBaseContractExtended> => {
const deploymentConfig: TBasicContractDeployment = { ...config } as TBasicContractDeployment;
for (const k in deploymentConfig) {
deploymentConfig[k].chainId = parseInt(k);
}
if (connectFunc) {
return {
contractName,
connect: connectFunc,
abi: abi,
config: deploymentConfig,
};
} else {
const defaultConnectFunction: TContractConnectFunc<GBaseContractExtended> = (
address: string,
signerOrProvider: ethers.Signer | ethers.providers.Provider
) => {
const baseContract = new BaseContract(address, abi, signerOrProvider) as GBaseContractExtended;
baseContract.contractName = contractName;
return baseContract;
};
return {
contractName,
connect: defaultConnectFunction,
abi: abi,
config: deploymentConfig,
};
}
};