Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some logs and simplify some functions #306

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stupid-beds-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperlane-xyz/registry': minor
---

Add logs to functions and simplifies some others
5 changes: 2 additions & 3 deletions src/registry/BaseRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ export abstract class BaseRegistry implements IRegistry {
abstract getChainAddresses(chainName: ChainName): MaybePromise<ChainAddresses | null>;

async getChainLogoUri(chainName: ChainName): Promise<string | null> {
const registryContent = await this.listRegistryContent();
const chain = registryContent.chains[chainName];
return chain?.logo ?? null;
const { chains } = await this.listRegistryContent();
return chains[chainName]?.logo ?? null;
}

abstract addChain(chain: UpdateChainParams): MaybePromise<void>;
Expand Down
3 changes: 1 addition & 2 deletions src/registry/FileSystemRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export class FileSystemRegistry extends SynchronousRegistry implements IRegistry
}

getUri(itemPath?: string): string {
if (!itemPath) return super.getUri();
return path.join(this.uri, itemPath);
return itemPath ? path.join(this.uri, itemPath) : super.getUri();
}

listRegistryContent(): RegistryContent {
Expand Down
6 changes: 2 additions & 4 deletions src/registry/GithubRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,8 @@ export class GithubRegistry extends BaseRegistry implements IRegistry {
}

async getWarpRoute(routeId: string): Promise<WarpCoreConfig | null> {
const repoContents = await this.listRegistryContent();
const routeConfigUrl = repoContents.deployments.warpRoutes[routeId];
if (!routeConfigUrl) return null;
return this.fetchYamlFile(routeConfigUrl);
const routeConfigUrl = (await this.listRegistryContent()).deployments.warpRoutes[routeId];
return routeConfigUrl ? this.fetchYamlFile(routeConfigUrl) : null;
}

async getWarpRoutes(filter?: WarpRouteFilterParams): Promise<WarpRouteConfigMap> {
Expand Down
69 changes: 56 additions & 13 deletions src/registry/MergedRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,63 @@ export class MergedRegistry implements IRegistry {
}

async getChains(): Promise<Array<ChainName>> {
return Object.keys(await this.getMetadata());
try {
const metadata = await this.getMetadata();
return Object.keys(metadata);
} catch (error) {
this.logger.error('Failed to retrieve chains', error);
return [];
}
}

async getMetadata(): Promise<ChainMap<ChainMetadata>> {
const results = await this.multiRegistryRead((r) => r.getMetadata());
return results.reduce((acc, content) => objMerge(acc, content), {});
try {
const results = await this.multiRegistryRead((r) => r.getMetadata());
return results.reduce((acc, content) => objMerge(acc, content), {});
} catch (error) {
this.logger.error('Failed to retrieve metadata', error);
return {};
}
}

async getChainMetadata(chainName: ChainName): Promise<ChainMetadata | null> {
return (await this.getMetadata())[chainName] || null;
try {
const metadata = await this.getMetadata();
return metadata[chainName] ?? null;
} catch (error) {
this.logger.error(`Failed to get metadata for chain: ${chainName}`, error);
return null;
}
}

async getAddresses(): Promise<ChainMap<ChainAddresses>> {
const results = await this.multiRegistryRead((r) => r.getAddresses());
return results.reduce((acc, content) => objMerge(acc, content), {});
try {
const results = await this.multiRegistryRead((r) => r.getAddresses());
return results.reduce((acc, content) => objMerge(acc, content), {});
} catch (error) {
this.logger.error('Failed to retrieve addresses', error);
return {};
}
}

async getChainAddresses(chainName: ChainName): Promise<ChainAddresses | null> {
return (await this.getAddresses())[chainName] || null;
try {
const addresses = await this.getAddresses();
return addresses[chainName] ?? null;
} catch (error) {
this.logger.error(`Failed to retrieve addresses for chain: ${chainName}`, error);
return null;
}
}

async getChainLogoUri(chainName: ChainName): Promise<string | null> {
const results = await this.multiRegistryRead((r) => r.getChainLogoUri(chainName));
return results.find((uri) => !!uri) || null;
try {
const results = await this.multiRegistryRead((r) => r.getChainLogoUri(chainName));
return results.find((uri) => !!uri) ?? null;
} catch (error) {
this.logger.error(`Failed to retrieve logo URI for chain: ${chainName}`, error);
return null;
}
}

async addChain(chain: UpdateChainParams): Promise<void> {
Expand All @@ -98,13 +131,23 @@ export class MergedRegistry implements IRegistry {
}

async getWarpRoute(id: WarpRouteId): Promise<WarpCoreConfig | null> {
const results = await this.multiRegistryRead((r) => r.getWarpRoute(id));
return results.find((r) => !!r) || null;
try {
const results = await this.multiRegistryRead((r) => r.getWarpRoute(id));
return results.find((r) => !!r) ?? null;
} catch (error) {
this.logger.error(`Failed to retrieve warp route for ID: ${id}`, error);
return null;
}
}

async getWarpRoutes(filter?: WarpRouteFilterParams): Promise<WarpRouteConfigMap> {
const results = await this.multiRegistryRead((r) => r.getWarpRoutes(filter));
return results.reduce((acc, content) => objMerge(acc, content), {});
try {
const results = await this.multiRegistryRead((r) => r.getWarpRoutes(filter));
return results.reduce((acc, content) => objMerge(acc, content), {});
} catch (error) {
this.logger.error('Failed to retrieve warp routes', error);
return {};
}
}

async addWarpRoute(config: WarpCoreConfig): Promise<void> {
Expand Down
23 changes: 16 additions & 7 deletions src/registry/PartialRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ export class PartialRegistry extends SynchronousRegistry implements IRegistry {
public chainAddresses: ChainMap<DeepPartial<ChainAddresses>>;
public warpRoutes: Array<DeepPartial<WarpCoreConfig>>;

constructor({ chainMetadata, chainAddresses, warpRoutes, logger }: PartialRegistryOptions) {
constructor({
chainMetadata = {},
chainAddresses = {},
warpRoutes = [],
logger,
}: PartialRegistryOptions) {
super({ uri: PARTIAL_URI_PLACEHOLDER, logger });
this.chainMetadata = chainMetadata || {};
this.chainAddresses = chainAddresses || {};
this.warpRoutes = warpRoutes || [];
this.chainMetadata = chainMetadata;
this.chainAddresses = chainAddresses;
this.warpRoutes = warpRoutes;
}

listRegistryContent(): RegistryContent {
Expand Down Expand Up @@ -71,9 +76,13 @@ export class PartialRegistry extends SynchronousRegistry implements IRegistry {
}

removeChain(chainName: ChainName): void {
super.removeChain(chainName);
if (this.chainMetadata?.[chainName]) delete this.chainMetadata[chainName];
if (this.chainAddresses?.[chainName]) delete this.chainAddresses[chainName];
try {
super.removeChain(chainName);
delete this.chainMetadata?.[chainName];
delete this.chainAddresses?.[chainName];
} catch (error) {
this.logger.error(`Failed to remove chain: ${chainName}`, error);
}
}

addWarpRoute(_config: WarpCoreConfig): void {
Expand Down
12 changes: 7 additions & 5 deletions src/registry/SynchronousRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ export abstract class SynchronousRegistry extends BaseRegistry implements IRegis

removeChain(chainName: ChainName): void {
const currentChains = this.listRegistryContent().chains;
if (!currentChains[chainName]) throw new Error(`Chain ${chainName} does not exist in registry`);

if (this.listContentCache?.chains[chainName]) delete this.listContentCache.chains[chainName];
if (this.metadataCache?.[chainName]) delete this.metadataCache[chainName];
if (this.addressCache?.[chainName]) delete this.addressCache[chainName];
if (!currentChains[chainName]) {
throw new Error(`Chain ${chainName} does not exist in registry`);
}

delete this.listContentCache?.chains[chainName];
delete this.metadataCache?.[chainName];
delete this.addressCache?.[chainName];
}

getWarpRoute(routeId: string): WarpCoreConfig | null {
Expand Down
23 changes: 15 additions & 8 deletions src/registry/warp-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@ export function warpConfigToWarpAddresses(config: WarpCoreConfig): ChainMap<Chai
}, {});
}

const COLLATERAL_KEY = 'collateral';
const SYNTHETIC_KEY = 'synthetic';
const NATIVE_KEY = 'native';
const XERC20_LOCKBOX_KEY = 'xERC20Lockbox';
const XERC20_KEY = 'xERC20';

function getWarpAddressKey(standard: TokenStandard): string | null {
const standardValue = standard.toLowerCase();
if (standardValue.includes('collateral')) return 'collateral';
if (standardValue.includes('synthetic')) return 'synthetic';
if (standardValue.includes('native')) return 'native';
if (standardValue.includes('xerc20lockbox')) return 'xERC20Lockbox';
if (standardValue.includes('xerc20')) return 'xERC20';
else return null;

if (standardValue.includes(COLLATERAL_KEY)) return COLLATERAL_KEY;
if (standardValue.includes(SYNTHETIC_KEY)) return SYNTHETIC_KEY;
if (standardValue.includes(NATIVE_KEY)) return NATIVE_KEY;
if (standardValue.includes(XERC20_LOCKBOX_KEY.toLowerCase())) return XERC20_LOCKBOX_KEY;
if (standardValue.includes(XERC20_KEY.toLowerCase())) return XERC20_KEY;

return null;
}

/**
Expand Down Expand Up @@ -54,8 +62,7 @@ export function warpRouteConfigToId(config: WarpCoreConfig): WarpRouteId {
}

export function createWarpRouteConfigId(tokenSymbol: string, chains: ChainName[]): WarpRouteId {
const sortedChains = [...chains].sort();
return `${tokenSymbol}/${sortedChains.join('-')}`;
return `${tokenSymbol}/${chains.sort().join('-')}`;
}

export function parseWarpRouteConfigId(routeId: WarpRouteId): {
Expand Down