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

Commit

Permalink
feat: add object export method (#6)
Browse files Browse the repository at this point in the history
feat: add object export
  • Loading branch information
VGLoic authored Jul 30, 2022
1 parent dae163b commit a5ff12f
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/multi-network/dynamic-multi-network-contract-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,46 @@ describe("Multi network contract store", () => {
address,
abi: otherTestAbi,
});
expect(store.toObject()).toEqual({
globalAbis: {
GLOB: testAbi,
ERC20,
ERC721,
ERC1155,
},
networks: {
1: {
abis: {
FOO: testAbi,
GLOB: testAbi,
ERC20,
ERC721,
ERC1155,
},
deployments: {
BAR: {
abiKey: "ERC20",
address,
},
},
},
2: {
abis: {
FOO2: otherTestAbi,
GLOB: testAbi,
ERC20,
ERC721,
ERC1155,
},
deployments: {
BAR2: {
abiKey: "FOO2",
address,
},
},
},
},
});
});
});

Expand Down
15 changes: 15 additions & 0 deletions src/multi-network/dynamic-multi-network-contract-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,21 @@ export class DynamicContractStore<
return this.getStore(chainId).getAddresses();
}

/**
* Convert the store to an object
* @returns The store abis and deployments by networks with the global abis
*/
public toObject() {
const chainIds = this.getChainIds();
return {
globalAbis: this.globalAbis,
networks: chainIds.reduce((acc, chainId) => {
acc[chainId] = this.getStore(chainId).toObject();
return acc;
}, {} as Record<number, Network>),
};
}

private getStore<ChainId extends OriginalChainId<OriginalConfig>>(
chainId: NumericUnion<ChainId>
) {
Expand Down
40 changes: 40 additions & 0 deletions src/multi-network/multi-network-contract-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,46 @@ describe("Dynamic Multi network contract store", () => {
});
expect(store.getAddress(2, "BAR2")).toEqual(address);
expect(() => store.getAddresses(3 as 1)).toThrow();
expect(store.toObject()).toEqual({
globalAbis: {
GLOB: testAbi,
ERC20,
ERC721,
ERC1155,
},
networks: {
1: {
abis: {
FOO: testAbi,
GLOB: testAbi,
ERC20,
ERC721,
ERC1155,
},
deployments: {
BAR: {
abiKey: "ERC20",
address,
},
},
},
2: {
abis: {
FOO2: otherTestAbi,
GLOB: testAbi,
ERC20,
ERC721,
ERC1155,
},
deployments: {
BAR2: {
abiKey: "FOO2",
address,
},
},
},
},
});
});

test("it should not include the default ABIs if specified", () => {
Expand Down
17 changes: 16 additions & 1 deletion src/multi-network/multi-network-contract-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class ContractStore<
* @returns The array of configured chain IDs
*/
public getChainIds() {
return Object.keys(this.stores).map(Number);
return Object.keys(this.stores).map(Number) as AllowedChainId<Networks>[];
}

/**
Expand Down Expand Up @@ -186,6 +186,21 @@ export class ContractStore<
return this.getStore(chainId).getAddresses();
}

/**
* Convert the store to an object
* @returns The store abis and deployments by networks with the global abis
*/
public toObject() {
const chainIds = this.getChainIds();
return {
globalAbis: this.globalAbis,
networks: chainIds.reduce((acc, chainId) => {
acc[chainId] = this.getStore(chainId).toObject();
return acc;
}, {} as Record<AllowedChainId<Networks>, Network>),
};
}

private getStore(chainId: AllowedChainId<Networks>) {
const store = this.stores[chainId];
if (!store) {
Expand Down
15 changes: 15 additions & 0 deletions src/single-network/dynamic-single-network-contract-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ describe("Dynamic Single Network Contract Store", () => {
address,
abi: ERC20,
});

expect(store.toObject()).toEqual({
abis: {
FOO: testAbi,
ERC20,
ERC721,
ERC1155,
},
deployments: {
BAR: {
abiKey: "ERC20",
address,
},
},
});
});
});

Expand Down
11 changes: 11 additions & 0 deletions src/single-network/dynamic-single-network-contract-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,17 @@ export class DynamicSingleNetworkContractStore<
);
}

/**
* Convert the store to an object
* @returns The store abis and deployments
*/
public toObject() {
return {
abis: this.abis,
deployments: this.deployments,
};
}

/**
* Get a deployment
* @param key String key of the deployment
Expand Down
15 changes: 15 additions & 0 deletions src/single-network/single-network-contract-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ describe("Static Single Network Contract Store", () => {
expect(store.getAddress("BAR")).toEqual(address);
expect(store.getAddresses()).toEqual([address]);
expect(() => store.getContract("BAR2" as "BAR")).toThrow();

expect(store.toObject()).toEqual({
abis: {
FOO: testAbi,
ERC20: ERC20,
ERC721: ERC721,
ERC1155: ERC1155,
},
deployments: {
BAR: {
abiKey: "ERC20",
address,
},
},
});
});

test("it should fail to if a deployment is given with an unknown ABI key", () => {
Expand Down
11 changes: 11 additions & 0 deletions src/single-network/single-network-contract-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ export class SingleNetworkContractStore<
);
}

/**
* Convert the store to an object
* @returns The store abis and deployments
*/
public toObject() {
return {
abis: this.abis,
deployments: this.deployments,
};
}

/**
* Get a deployment
* @param key String key of the deployment
Expand Down

0 comments on commit a5ff12f

Please sign in to comment.