Skip to content

Commit

Permalink
Address book fix (#93)
Browse files Browse the repository at this point in the history
* fix: Address book bug

Signed-off-by: Carlos V <[email protected]>

* chore: bump version

Signed-off-by: Carlos V <[email protected]>
Signed-off-by: moro <[email protected]>

* feat: Add export and fix if

Signed-off-by: Carlos V <[email protected]>

* chore: improve readability of deployed contracts statement

Signed-off-by: moro <[email protected]>

---------

Signed-off-by: Carlos V <[email protected]>
Signed-off-by: moro <[email protected]>
Co-authored-by: moro <[email protected]>
  • Loading branch information
carlosvdr and ricardo-rp authored Mar 12, 2024
1 parent 1475755 commit 4dc87fc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
24 changes: 18 additions & 6 deletions bindings/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export { connectContracts };
export type { NetworkContracts, DeployedContracts };

type DeployedContracts = typeof DEPLOYED_CONTRACTS;
type AddressBook = Record<
export type AddressBook = Record<
string,
{ TAPVerifier: string; AllocationIDTracker: string; Escrow: string }
>;
Expand All @@ -38,12 +38,15 @@ const connectContracts = async (
addressBook: AddressBook | undefined
): Promise<NetworkContracts> => {
const stringifiedChainId = `${chainId}`;
if (!(stringifiedChainId in DEPLOYED_CONTRACTS))
throw new Error(`chainId: '${chainId}' has no deployed contracts`);

const deployedContracts = addressBook
? addressBook[stringifiedChainId]
: DEPLOYED_CONTRACTS[stringifiedChainId as keyof typeof DEPLOYED_CONTRACTS];
const deployedContracts = (() => {
if (addressBook !== undefined) return addressBook[stringifiedChainId];

if (chainIdHasContracts(stringifiedChainId))
return DEPLOYED_CONTRACTS[stringifiedChainId];

throw new Error(`chainId: '${chainId}' has no deployed contracts`);
})();

const getContractAddress = (contractName: keyof typeof deployedContracts) => {
if (!deployedContracts[contractName]) {
Expand Down Expand Up @@ -78,3 +81,12 @@ const connectContracts = async (

return contracts;
};

/**
* Checks if a given chainId has contracts deployed.
* @param chainId The chainId to check.
* @returns A boolean indicating if the chainId has contracts deployed.
*/
const chainIdHasContracts = (
chainId: string
): chainId is keyof DeployedContracts => chainId in DEPLOYED_CONTRACTS;
5 changes: 3 additions & 2 deletions bindings/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@semiotic-labs/tap-contracts-bindings",
"version": "1.2.0",
"version": "1.2.2",
"main": "./dist/bindings/bindings.js",
"repository": {
"type": "git",
Expand All @@ -21,4 +21,5 @@
"scripts": {
"bindings": "typechain --target=ethers-v5 --out-dir=generated/ts-bindings ../out/**/*.json"
}
}
}

0 comments on commit 4dc87fc

Please sign in to comment.