From 631deab5bb343ce3b6d7ae6a3ee7117da8e9ca7f Mon Sep 17 00:00:00 2001 From: 0xPatrick Date: Mon, 11 Nov 2024 10:34:16 -0500 Subject: [PATCH] refactor: `getChainInfoByAddress` -> `makeChainAddress` - removes `getChainInfoByAddress` and provides `makeChainAddress`, better aligning the helper with intended usage - arguably a breaking change, but we've yet to release `getChainInfoByAddress` --- packages/orchestration/src/exos/chain-hub.js | 21 +++++++++++++------ .../orchestration/test/exos/chain-hub.test.ts | 14 ++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/packages/orchestration/src/exos/chain-hub.js b/packages/orchestration/src/exos/chain-hub.js index 6b86cfe881ab..3cc0ff20fe43 100644 --- a/packages/orchestration/src/exos/chain-hub.js +++ b/packages/orchestration/src/exos/chain-hub.js @@ -4,7 +4,11 @@ import { M } from '@endo/patterns'; import { BrandShape } from '@agoric/ertp/src/typeGuards.js'; import { VowShape } from '@agoric/vow'; -import { CosmosChainInfoShape, IBCConnectionInfoShape } from '../typeGuards.js'; +import { + ChainAddressShape, + CosmosChainInfoShape, + IBCConnectionInfoShape, +} from '../typeGuards.js'; import { getBech32Prefix } from '../utils/address.js'; /** @@ -13,7 +17,7 @@ import { getBech32Prefix } from '../utils/address.js'; * @import {Zone} from '@agoric/zone'; * @import {CosmosAssetInfo, CosmosChainInfo, IBCConnectionInfo} from '../cosmos-api.js'; * @import {ChainInfo, KnownChains} from '../chain-info.js'; - * @import {Denom} from '../orchestration-api.js'; + * @import {ChainAddress, Denom} from '../orchestration-api.js'; * @import {Remote} from '@agoric/internal'; * @import {TypedPattern} from '@agoric/internal'; */ @@ -181,7 +185,7 @@ const ChainHubI = M.interface('ChainHub', { registerAsset: M.call(M.string(), DenomDetailShape).returns(), getAsset: M.call(M.string()).returns(M.or(DenomDetailShape, M.undefined())), getDenom: M.call(BrandShape).returns(M.or(M.string(), M.undefined())), - getChainInfoByAddress: M.call(M.string()).returns(CosmosChainInfoShape), + makeChainAddress: M.call(M.string()).returns(ChainAddressShape), }); /** @@ -440,15 +444,20 @@ export const makeChainHub = (zone, agoricNames, vowTools) => { }, /** * @param {string} address bech32 address - * @returns {CosmosChainInfo} + * @returns {ChainAddress} */ - getChainInfoByAddress(address) { + makeChainAddress(address) { const prefix = getBech32Prefix(address); if (!bech32PrefixToChainName.has(prefix)) { throw makeError(`Chain info not found for bech32Prefix ${q(prefix)}`); } const chainName = bech32PrefixToChainName.get(prefix); - return chainInfos.get(chainName); + const { chainId } = chainInfos.get(chainName); + return harden({ + chainId, + value: address, + encoding: /** @type {const} */ ('bech32'), + }); }, }); diff --git a/packages/orchestration/test/exos/chain-hub.test.ts b/packages/orchestration/test/exos/chain-hub.test.ts index 2f3185cb6a3a..b4f4823be973 100644 --- a/packages/orchestration/test/exos/chain-hub.test.ts +++ b/packages/orchestration/test/exos/chain-hub.test.ts @@ -160,7 +160,7 @@ test('toward asset info in agoricNames (#9572)', async t => { } }); -test('getChainInfoByAddress', async t => { +test('makeChainAddress', async t => { const { chainHub, nameAdmin, vt } = setup(); // use fetched chain info await registerKnownChains(nameAdmin); @@ -170,24 +170,24 @@ test('getChainInfoByAddress', async t => { const MOCK_ICA_ADDRESS = 'osmo1ht7u569vpuryp6utadsydcne9ckeh2v8dkd38v5hptjl3u2ewppqc6kzgd'; - t.like(chainHub.getChainInfoByAddress(MOCK_ICA_ADDRESS), { + t.deepEqual(chainHub.makeChainAddress(MOCK_ICA_ADDRESS), { chainId: 'osmosis-1', - bech32Prefix: 'osmo', + value: MOCK_ICA_ADDRESS, + encoding: 'bech32', }); t.throws( - () => - chainHub.getChainInfoByAddress(MOCK_ICA_ADDRESS.replace('osmo1', 'foo1')), + () => chainHub.makeChainAddress(MOCK_ICA_ADDRESS.replace('osmo1', 'foo1')), { message: 'Chain info not found for bech32Prefix "foo"', }, ); - t.throws(() => chainHub.getChainInfoByAddress('notbech32'), { + t.throws(() => chainHub.makeChainAddress('notbech32'), { message: 'No separator character for "notbech32"', }); - t.throws(() => chainHub.getChainInfoByAddress('1notbech32'), { + t.throws(() => chainHub.makeChainAddress('1notbech32'), { message: 'Missing prefix for "1notbech32"', }); });