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
/
contractAppContextTypes.ts
113 lines (103 loc) 路 4.12 KB
/
contractAppContextTypes.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
import { Provider } from '@ethersproject/providers';
import { BaseContract, Signer } from 'ethers';
import { TBasicContractDeployment } from '~~/models';
/**
* #### Summary
* ethers.BaseContract extension. This is the exactly the same as `ethers.BaseContract`, but with a type guard for the contract name: `BaseContract & { contractName: 'nameofContract' };`
*/
export type TBaseContractExtended<GContractNames extends string> = BaseContract & { contractName: GContractNames };
/**
* A function that connects to a contract. Used by {@link TContractConnectorBase}. This is usually something generated by typechain and is used to connect to contract on the blockchain.
*/
export type TContractConnectFunc<GContract extends BaseContract> = (
address: string,
signerOrProvider: Signer | Provider
) => GContract;
/**
* #### Summary
* This types describes a base for a connnector. A contract connector would be a a conception that is the minimum required to connect to a contract. It has a connector function that returns a typed contract and abi that has contract information.
*/
export type TContractConnectorBase<GContract extends BaseContract> = Readonly<{
connect: TContractConnectFunc<GContract>;
abi: Readonly<Record<string, any>[]>;
}>;
/**
* #### Summary
* This type descripts a connector that has enough information to create a contract in a chain. The contractName is required to use a group of connectors to create a connection of available contracts for the app.
*
* ##### 鉁忥笍 Notes
* - The config would be a record of chain information that helps the factory create contracts for the app.
* - based on {@link TContractConnectorBase} and uses {@link TBasicContractDeployment}
*/
export type TContractConnector<
GContractNames extends string,
GContract extends TBaseContractExtended<GContractNames>
// GContractInterface extends ethers.utils.Interface
> = Readonly<
{
contractName: GContractNames;
config: TBasicContractDeployment;
} & TContractConnectorBase<GContract>
>;
/**
* #### Summary
* A Record of typed connectors that can be used to create a contract.
*
* ##### 鉁忥笍 Notes
* - used by {@link TAppContractsContext}
*/
export type TConnectorList<GContractNames extends string, GContracts extends TBaseContractExtended<GContractNames>> = {
[contractName in GContractNames]: TContractConnector<GContractNames, GContracts>;
};
/**
* #### Summary
* A type that infers contract type `(extended from BaseContract)` based on connectors and contractName. For example `DAI` from `{ DAI: { connect: ... } }`. If the contractName is not found, it will return a BaseContract
*
* ##### 鉁忥笍 Notes
* - used by {@link contractsConextFactory}
*/
export type TTypedContract<
GContractNames extends string,
GAppContractConnectorList
> = GAppContractConnectorList extends {
[key in GContractNames]: { connect: (address: string, signerOrProvider: Signer | Provider) => infer TypedContract };
}
? TypedContract
: never;
/**
* #### Summary
* A utility type for typed contracts by name and then by chain
*
* ##### 鉁忥笍 Notes
* - used by {@link TAppContractsContext}
*/
export type TContractsByName<
GContractNames extends string,
GContracts extends TBaseContractExtended<GContractNames>
> = { [contractName in GContractNames]: { [chainId in number]: GContracts | undefined } };
/**
* #### Summary
* A utility type for typed contracts by chain and then by name
*
* ##### 鉁忥笍 Notes
* - used by {@link TAppContractsContext}
*/
export type TContractsByChainId<
GContractNames extends string,
GContracts extends TBaseContractExtended<GContractNames>
> = {
[chainId in number]: {
[contractName in GContractNames]: GContracts | undefined;
};
};
/**
* Describes the current ContractsContext for the app used by the context created by {@link contractContextFactory}
*/
export type TAppContractsContext<
GContractNames extends string,
GContracts extends TBaseContractExtended<GContractNames>
> = {
contractConnectors: TConnectorList<GContractNames, GContracts>;
contractsByName: TContractsByName<GContractNames, GContracts>;
contractsByChainId: TContractsByChainId<GContractNames, GContracts>;
};