Skip to content

Commit

Permalink
refactor: getChainInfoByAddress -> makeChainAddress
Browse files Browse the repository at this point in the history
- removes `getChainInfoByAddress`  and provides `makeChainAddress`, better aligning
  the helper with intended usage
- arguably a breaking change, but we've yet to release `getChainInfoByAddress`
  • Loading branch information
0xpatrickdev committed Nov 11, 2024
1 parent d7c0b81 commit 631deab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
21 changes: 15 additions & 6 deletions packages/orchestration/src/exos/chain-hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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';
*/
Expand Down Expand Up @@ -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),
});

/**
Expand Down Expand Up @@ -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'),
});
},
});

Expand Down
14 changes: 7 additions & 7 deletions packages/orchestration/test/exos/chain-hub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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"',
});
});

0 comments on commit 631deab

Please sign in to comment.