Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
feat: allow not defined inputs in constructor (#7)
Browse files Browse the repository at this point in the history
* feat: allow not defined inputs in constructor

* feat: add ABI return type
  • Loading branch information
VGLoic authored Aug 15, 2022
1 parent a5ff12f commit fcbadda
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/multi-network/dynamic-multi-network-contract-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type WithoutDefaultABIs<Opts extends MultiNetworkOptions | undefined> =

type OriginalGlobalABIKey<
Config extends GenericConfiguration,
Opts extends MultiNetworkOptions
Opts extends MultiNetworkOptions | undefined
> = Extract<
| keyof Config["globalAbis"]
| (WithoutDefaultABIs<Opts> extends true
Expand All @@ -54,7 +54,7 @@ type OriginalDeploymentKey<
type OriginalABIKey<
Config extends GenericConfiguration,
ChainId extends OriginalChainId<Config>,
Opts extends MultiNetworkOptions
Opts extends MultiNetworkOptions | undefined
> = Config["networks"][ChainId] extends Network
? Extract<
| keyof Config["networks"][ChainId]["abis"]
Expand Down Expand Up @@ -349,7 +349,7 @@ export class DynamicContractStore<
*/
public getGlobalAbi<
GlobalABIKey extends OriginalGlobalABIKey<OriginalConfig, Opts>
>(key: LiteralUnion<GlobalABIKey>) {
>(key: LiteralUnion<GlobalABIKey>): ABI {
if (!this.globalAbis[key]) {
throw new Error(`Key ${key} is not associated to a global ABI.`);
}
Expand All @@ -365,7 +365,7 @@ export class DynamicContractStore<
public getAbi<
ChainId extends OriginalChainId<OriginalConfig>,
ABIKey extends OriginalABIKey<OriginalConfig, ChainId, Opts>
>(chainId: NumericUnion<ChainId>, key: LiteralUnion<ABIKey>) {
>(chainId: NumericUnion<ChainId>, key: LiteralUnion<ABIKey>): ABI {
return this.getStore(chainId).getAbi(key);
}

Expand Down
24 changes: 12 additions & 12 deletions src/multi-network/multi-network-contract-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Deployment, ABI, Contract } from "../helper-types";
import { MultiNetworkOptions } from "./common-types";

type Network = {
abis: Record<string, ABI>;
deployments: Record<string, Deployment>;
abis?: Record<string, ABI>;
deployments?: Record<string, Deployment>;
};

type WithoutDefaultABIs<Opts extends MultiNetworkOptions | undefined> =
Expand All @@ -19,7 +19,7 @@ type WithoutDefaultABIs<Opts extends MultiNetworkOptions | undefined> =

type GlobalABIKey<
GlobalABIs extends Record<string, ABI>,
Opts extends MultiNetworkOptions
Opts extends MultiNetworkOptions | undefined
> = Extract<
| keyof GlobalABIs
| (WithoutDefaultABIs<Opts> extends true
Expand All @@ -41,10 +41,10 @@ type DeploymentKey<
: never;

type ABIKey<
GlobalABIs extends Record<string, ABI>,
GlobalABIs extends Record<string, ABI> | undefined,
Networks extends Record<number, Network>,
ChainId extends AllowedChainId<Networks>,
Opts extends MultiNetworkOptions
Opts extends MultiNetworkOptions | undefined
> = Networks[ChainId] extends Network
? Extract<
| keyof Networks[ChainId]["abis"]
Expand All @@ -60,17 +60,17 @@ type Configuration<
GlobalABIs extends Record<string, ABI>,
Networks extends Record<number, Network>
> = {
globalAbis: GlobalABIs;
globalAbis?: GlobalABIs;
networks: Networks;
};

/**
* Static Contract store for managing ABIs and deployments on multiple networks
*/
export class ContractStore<
GlobalABIs extends Record<string, ABI>,
Networks extends Record<number, Network>,
Opts extends MultiNetworkOptions
GlobalABIs extends Record<string, ABI> = {},
Networks extends Record<number, Network> = {},
Opts extends MultiNetworkOptions | undefined = undefined
> {
private stores;
private globalAbis;
Expand All @@ -95,7 +95,7 @@ export class ContractStore<
abis["ERC721"] = ERC721;
abis["ERC1155"] = ERC1155;
}
Object.entries(networkConfig.abis).forEach(([key, abi]) => {
Object.entries({ ...networkConfig.abis }).forEach(([key, abi]) => {
if (abis[key]) {
throw new Error(
`An ABI already exists for key ${key} and chain ID ${chainId}`
Expand Down Expand Up @@ -131,7 +131,7 @@ export class ContractStore<
* @param key String key of the ABI
* @returns The ABI
*/
public getGlobalAbi(key: GlobalABIKey<GlobalABIs, Opts>) {
public getGlobalAbi(key: GlobalABIKey<GlobalABIs, Opts>): ABI {
if (!this.globalAbis[key]) {
throw new Error(`Key ${key} is not associated to a global ABI.`);
}
Expand All @@ -147,7 +147,7 @@ export class ContractStore<
public getAbi<ChainId extends AllowedChainId<Networks>>(
chainId: ChainId,
key: ABIKey<GlobalABIs, Networks, ChainId, Opts>
) {
): ABI {
return this.getStore(chainId).getAbi(key as never);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export class DynamicSingleNetworkContractStore<
*/
public getAbi<ABIKey extends OriginalABIKey<OriginalConfig, Opts>>(
key: LiteralUnion<ABIKey>
) {
): ABI {
const abi = this.abis[key];
if (!abi) {
throw new Error(
Expand Down
6 changes: 3 additions & 3 deletions src/single-network/single-network-contract-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type Configuration<
Opts extends Options | undefined,
Deployments extends Record<string, Deployment<ABIKey<ABIs, Opts>>>
> = {
abis: ABIs;
deployments: Deployments;
abis?: ABIs;
deployments?: Deployments;
};

/**
Expand Down Expand Up @@ -88,7 +88,7 @@ export class SingleNetworkContractStore<
* @param key String key of the ABI
* @returns The ABI
*/
public getAbi(key: ABIKey<ABIs, Opts>) {
public getAbi(key: ABIKey<ABIs, Opts>): ABI {
const abi = this.abis[key];
if (!abi) {
throw new Error(
Expand Down

0 comments on commit fcbadda

Please sign in to comment.