Skip to content

Commit

Permalink
fix: warp route artifact symbol (#499)
Browse files Browse the repository at this point in the history
### Description

<!--
Summary of change.
Example: Add sepolia chain
-->

This PR along with a PR to the monorepo aims to fix the artifacts for
warp route deployments by taking into account the symbol field,
mentioned in [this
issue](hyperlane-xyz/hyperlane-monorepo#5164)

- Add `WarpRouteOptions` as optional parameter to `addWarpRoute`
- Change `getWarpRouteArtifactPaths` to receive a `symbol` as the folder
path
- Include unit test 
### Backward compatibility

<!--
Are these changes backward compatible? Note that additions are backwards
compatible.

Yes/No
-->

Yes

### Testing

<!--
Have any new metadata configs and deployment addresses been used with
any Hyperlane tooling, such as the CLI?
-->
CLI
  • Loading branch information
Xaroz authored Jan 17, 2025
1 parent f86f9ae commit b3d0a00
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-paws-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperlane-xyz/registry': minor
---

Add param of `AddWarpRouteOptions` to `addWarpRoute` methods
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ export {
} from './consts.js';
export { BaseRegistry } from './registry/BaseRegistry.js';
export { GithubRegistry, GithubRegistryOptions } from './registry/GithubRegistry.js';
export { ChainFiles, IRegistry, RegistryContent, RegistryType } from './registry/IRegistry.js';
export {
AddWarpRouteOptions,
ChainFiles,
IRegistry,
RegistryContent,
RegistryType,
} from './registry/IRegistry.js';
export { MergedRegistry, MergedRegistryOptions } from './registry/MergedRegistry.js';
export { PartialRegistry, PartialRegistryOptions } from './registry/PartialRegistry.js';
export {
Expand Down
11 changes: 7 additions & 4 deletions src/registry/BaseRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ChainAddresses, MaybePromise } from '../types.js';
import { WarpRouteConfigMap } from '../types.js';
import { stripLeadingSlash } from '../utils.js';
import type {
AddWarpRouteOptions,
IRegistry,
RegistryContent,
RegistryType,
Expand Down Expand Up @@ -46,12 +47,14 @@ export abstract class BaseRegistry implements IRegistry {
return 'deployments/warp_routes';
}

protected getWarpRoutesArtifactPaths({ tokens }: WarpCoreConfig) {
protected getWarpRoutesArtifactPaths({ tokens }: WarpCoreConfig, options?: AddWarpRouteOptions) {
if (!tokens.length) throw new Error('No tokens provided in config');
const symbols = new Set<string>(tokens.map((token) => token.symbol.toUpperCase()));
if (symbols.size !== 1)
throw new Error('Only one token symbol per warp config is supported for now');
const symbol = symbols.values().next().value;
if (!options?.symbol && symbols.size !== 1)
throw new Error(
'Only one token symbol per warp config is supported for now. Consider passing a symbol as a parameter',
);
const symbol = options?.symbol || symbols.values().next().value;
const chains = tokens
.map((token) => token.chainName)
.sort()
Expand Down
5 changes: 3 additions & 2 deletions src/registry/FileSystemRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { toYamlString } from '../utils.js';
import {
RegistryType,
UpdateChainParams,
type AddWarpRouteOptions,
type ChainFiles,
type IRegistry,
type RegistryContent,
Expand Down Expand Up @@ -94,8 +95,8 @@ export class FileSystemRegistry extends SynchronousRegistry implements IRegistry
this.removeFiles(Object.values(chainFiles));
}

addWarpRoute(config: WarpCoreConfig): void {
let { configPath, addressesPath } = this.getWarpRoutesArtifactPaths(config);
addWarpRoute(config: WarpCoreConfig, options?: AddWarpRouteOptions): void {
let { configPath, addressesPath } = this.getWarpRoutesArtifactPaths(config, options);

configPath = path.join(this.uri, configPath);
this.createFile({ filePath: configPath, data: toYamlString(config, SCHEMA_REF) });
Expand Down
28 changes: 14 additions & 14 deletions src/registry/GithubRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ interface TreeNode {
url: string;
}

type GithubRateResponse = {
resources : {
type GithubRateResponse = {
resources: {
core: {
limit: number,
used: number,
remaining: number,
reset: number
}
}
}
limit: number;
used: number;
remaining: number;
reset: number;
};
};
};

export const GITHUB_API_URL = 'https://api.github.com';
/**
Expand Down Expand Up @@ -185,16 +185,16 @@ export class GithubRegistry extends BaseRegistry implements IRegistry {
throw new Error(`Github API rate remaining: ${remaining}, limit reset at ${reset}.`);
apiHost = this.proxyUrl;
}
return `${apiHost}/repos/${this.repoOwner}/${this.repoName}/git/trees/${this.branch}?recursive=true`
return `${apiHost}/repos/${this.repoOwner}/${this.repoName}/git/trees/${this.branch}?recursive=true`;
}

public async getApiRateLimit(): Promise<GithubRateResponse['resources']['core']> {
const response = await fetch(`${GITHUB_API_URL}/rate_limit`, {
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
const { resources } = await response.json() as GithubRateResponse;
'X-GitHub-Api-Version': '2022-11-28',
},
});
const { resources } = (await response.json()) as GithubRateResponse;
return resources.core;
}

Expand Down
6 changes: 5 additions & 1 deletion src/registry/IRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export enum RegistryType {
Partial = 'partial',
}

export interface AddWarpRouteOptions {
symbol?: string;
}

export interface IRegistry {
type: RegistryType;
uri: string;
Expand All @@ -58,7 +62,7 @@ export interface IRegistry {

getWarpRoute(routeId: string): MaybePromise<WarpCoreConfig | null>;
getWarpRoutes(filter?: WarpRouteFilterParams): MaybePromise<WarpRouteConfigMap>;
addWarpRoute(config: WarpCoreConfig): MaybePromise<void>;
addWarpRoute(config: WarpCoreConfig, options?: AddWarpRouteOptions): MaybePromise<void>;

// TODO define more deployment artifact related methods

Expand Down
5 changes: 3 additions & 2 deletions src/registry/MergedRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ChainMap, ChainMetadata, ChainName, WarpCoreConfig } from '@hyperl
import { ChainAddresses, WarpRouteConfigMap, WarpRouteId } from '../types.js';
import { objMerge } from '../utils.js';
import {
AddWarpRouteOptions,
IRegistry,
RegistryContent,
RegistryType,
Expand Down Expand Up @@ -107,9 +108,9 @@ export class MergedRegistry implements IRegistry {
return results.reduce((acc, content) => objMerge(acc, content), {});
}

async addWarpRoute(config: WarpCoreConfig): Promise<void> {
async addWarpRoute(config: WarpCoreConfig, options?: AddWarpRouteOptions): Promise<void> {
return this.multiRegistryWrite(
async (registry) => await registry.addWarpRoute(config),
async (registry) => await registry.addWarpRoute(config, options),
'adding warp route',
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/registry/SynchronousRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ChainMap, ChainMetadata, ChainName, WarpCoreConfig } from '@hyperl
import { ChainAddresses, WarpRouteConfigMap, WarpRouteId } from '../types.js';
import { BaseRegistry } from './BaseRegistry.js';
import {
AddWarpRouteOptions,
IRegistry,
RegistryContent,
UpdateChainParams,
Expand Down Expand Up @@ -71,7 +72,7 @@ export abstract class SynchronousRegistry extends BaseRegistry implements IRegis
return Object.fromEntries(idsWithConfigs);
}

abstract addWarpRoute(config: WarpCoreConfig): void;
abstract addWarpRoute(config: WarpCoreConfig, options?: AddWarpRouteOptions): void;

protected abstract createOrUpdateChain(chain: UpdateChainParams): void;

Expand Down
22 changes: 22 additions & 0 deletions test/unit/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ describe('Registry utilities', () => {
fs.unlinkSync(addressesPath);
fs.rmdirSync(`deployments/warp_routes/${MOCK_SYMBOL}`);
}).timeout(5_000);

it(`Adds a warp route for ${registry.type} registry using the provided symbol`, async () => {
const MOCKED_OPTION_SYMBOL = 'OPTION';
await registry.addWarpRoute(
{
tokens: [
{ chainName: MOCK_CHAIN_NAME, symbol: MOCK_SYMBOL, standard: 'EvmHypCollateral' },
{ chainName: MOCK_CHAIN_NAME2, symbol: MOCK_SYMBOL, standard: 'EvmHypSynthetic' },
] as any,
options: {},
},
{ symbol: MOCKED_OPTION_SYMBOL },
);
const outputBasePath = `deployments/warp_routes/${MOCKED_OPTION_SYMBOL}/${MOCK_CHAIN_NAME}-${MOCK_CHAIN_NAME2}-`;
const configPath = `${outputBasePath}config.yaml`;
const addressesPath = `${outputBasePath}addresses.yaml`;
expect(fs.existsSync(configPath)).to.be.true;
expect(fs.existsSync(addressesPath)).to.be.true;
fs.unlinkSync(configPath);
fs.unlinkSync(addressesPath);
fs.rmdirSync(`deployments/warp_routes/${MOCKED_OPTION_SYMBOL}`);
}).timeout(5_000);
}

describe('MergedRegistry', async () => {
Expand Down

0 comments on commit b3d0a00

Please sign in to comment.