From 1daa3e238818ab92ada6bdb90349d5e55e2eac2f Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Wed, 12 Jun 2024 10:21:50 -0700 Subject: [PATCH] fix(types): chain-info const When we started fetching the data by JSON, the import of JSON lost the string values, widening to `string`. This made the string templates fail. It also widened true and false to `boolean`, which would prevent the feature inference for what a Chain can do. This wraps in `const` so the actual values propagate in the type. --- .../orchestration/scripts/fetch-chain-info.ts | 15 +++++++------- packages/orchestration/src/cosmos-api.ts | 20 ++++++++----------- packages/orchestration/src/ethereum-api.ts | 4 ++-- packages/orchestration/src/facade.js | 7 ++----- .../orchestration/src/fetched-chain-info.js | 4 ++-- .../src/proposals/start-stakeAtom.js | 6 ++---- 6 files changed, 24 insertions(+), 32 deletions(-) diff --git a/packages/orchestration/scripts/fetch-chain-info.ts b/packages/orchestration/scripts/fetch-chain-info.ts index 5c0564f8624..0b4506d6ab5 100755 --- a/packages/orchestration/scripts/fetch-chain-info.ts +++ b/packages/orchestration/scripts/fetch-chain-info.ts @@ -105,28 +105,29 @@ for (const name of chainNames) { chainId: chain.chain_id, stakingTokens: chain.staking?.staking_tokens, }; - // UNTIL https://github.com/Agoric/agoric-sdk/issues/9326 - if (name === 'osmosis') { - chainInfo[name].icqEnabled = true; - } } +// UNTIL https://github.com/Agoric/agoric-sdk/issues/9326 +chainInfo.osmosis = { ...chainInfo.osmosis, icqEnabled: true }; + // iterate this after chainInfo is filled out for (const name of chainNames) { console.log('processing connections', name); const ibcData = client.getChainIbcData(name); - chainInfo[name].connections = Object.fromEntries( + const connections = Object.fromEntries( ibcData .map(datum => toConnectionEntry(datum, name)) // sort alphbetically for consistency // eslint-disable-next-line no-nested-ternary .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)), ); + chainInfo[name] = { ...chainInfo[name], connections }; } -const src = `/** @file Generated by fetch-chain-info.ts */\nexport default ${JSON.stringify(chainInfo, null, 2)};`; +const record = JSON.stringify(chainInfo, null, 2); +const src = `/** @file Generated by fetch-chain-info.ts */\nexport default /** @type {const} } */ (${record});`; const prettySrc = await prettier.format(src, { - parser: 'typescript', + parser: 'babel', // 'typescript' fails to preserve parens for typecast singleQuote: true, trailingComma: 'all', }); diff --git a/packages/orchestration/src/cosmos-api.ts b/packages/orchestration/src/cosmos-api.ts index a900f6b7152..2eefdd7b9f8 100644 --- a/packages/orchestration/src/cosmos-api.ts +++ b/packages/orchestration/src/cosmos-api.ts @@ -13,7 +13,7 @@ import type { import type { State as IBCConnectionState } from '@agoric/cosmic-proto/ibc/core/connection/v1/connection.js'; import type { Brand, Purse } from '@agoric/ertp/src/types.js'; import type { Port } from '@agoric/network'; -import { IBCChannelID } from '@agoric/vats'; +import { IBCChannelID, type IBCConnectionID } from '@agoric/vats'; import type { LocalIbcAddress, RemoteIbcAddress, @@ -38,25 +38,21 @@ export type CosmosValidatorAddress = ChainAddress & { /** Represents an IBC Connection between two chains, which can contain multiple Channels. */ export type IBCConnectionInfo = { - // XXX really IBCConnectionID but our chain info fetcher doesn't know - id: string; // e.g. connection-0 + id: IBCConnectionID; // e.g. connection-0 client_id: string; // '07-tendermint-0' state: IBCConnectionState; counterparty: { client_id: string; - // XXX really IBCConnectionID but our chain info fetcher doesn't know - connection_id: string; + connection_id: IBCConnectionID; prefix: { key_prefix: string; }; }; transferChannel: { portId: string; - // XXX really IBCChannelID but our chain info fetcher doesn't know - channelId: string; + channelId: IBCChannelID; counterPartyPortId: string; - // XXX really IBCChannelID but our chain info fetcher doesn't know - counterPartyChannelId: string; + counterPartyChannelId: IBCChannelID; ordering: Order; state: IBCChannelState; version: string; // e.eg. 'ics20-1' @@ -66,7 +62,7 @@ export type IBCConnectionInfo = { /** * Info for a Cosmos-based chain. */ -export type CosmosChainInfo = { +export type CosmosChainInfo = Readonly<{ chainId: string; connections?: Record; // chainId or wellKnownName @@ -76,8 +72,8 @@ export type CosmosChainInfo = { /** * cf https://github.com/cosmos/chain-registry/blob/master/chain.schema.json#L117 */ - stakingTokens?: Array<{ denom: string }>; -}; + stakingTokens?: Readonly>; +}>; export interface StakingAccountQueries { /** diff --git a/packages/orchestration/src/ethereum-api.ts b/packages/orchestration/src/ethereum-api.ts index 9d0f7dc2ea5..d294033756e 100644 --- a/packages/orchestration/src/ethereum-api.ts +++ b/packages/orchestration/src/ethereum-api.ts @@ -1,7 +1,7 @@ /** * Info for an Ethereum-based chain. */ -export type EthChainInfo = { +export type EthChainInfo = Readonly<{ chainId: string; allegedName: string; -}; +}>; diff --git a/packages/orchestration/src/facade.js b/packages/orchestration/src/facade.js index 1eec138dbbb..f4bc05011b4 100644 --- a/packages/orchestration/src/facade.js +++ b/packages/orchestration/src/facade.js @@ -118,11 +118,8 @@ const makeRemoteChainFacade = ( makeAccount: async () => { const icaAccount = await E(orchestration).makeAccount( chainInfo.chainId, - // XXX IBCConnectionInfo concessions for JSON encoding - /** @type {IBCConnectionID} */ (connectionInfo.id), - /** @type {IBCConnectionID} */ ( - connectionInfo.counterparty.connection_id - ), + connectionInfo.id, + connectionInfo.counterparty.connection_id, ); const address = await E(icaAccount).getAddress(); diff --git a/packages/orchestration/src/fetched-chain-info.js b/packages/orchestration/src/fetched-chain-info.js index fc460d5a705..db50b0c495b 100644 --- a/packages/orchestration/src/fetched-chain-info.js +++ b/packages/orchestration/src/fetched-chain-info.js @@ -1,5 +1,5 @@ /** @file Generated by fetch-chain-info.ts */ -export default { +export default /** @type {const} } */ ({ agoric: { chainId: 'agoric-3', stakingTokens: [ @@ -2011,4 +2011,4 @@ export default { }, }, }, -}; +}); diff --git a/packages/orchestration/src/proposals/start-stakeAtom.js b/packages/orchestration/src/proposals/start-stakeAtom.js index 496072edd01..228b4ccc9bc 100644 --- a/packages/orchestration/src/proposals/start-stakeAtom.js +++ b/packages/orchestration/src/proposals/start-stakeAtom.js @@ -62,10 +62,8 @@ export const startStakeAtom = async ({ issuerKeywordRecord: harden({ ATOM: atomIssuer }), terms: { chainId: cosmoshub.chainId, - hostConnectionId: /** @type {IBCConnectionID} */ (connectionInfo.id), - controllerConnectionId: /** @type {IBCConnectionID} */ ( - connectionInfo.counterparty.connection_id - ), + hostConnectionId: connectionInfo.id, + controllerConnectionId: connectionInfo.counterparty.connection_id, bondDenom: cosmoshub.stakingTokens[0].denom, }, privateArgs: {