From 1b8a32be29a2227b3e0e535485a6a14305df2863 Mon Sep 17 00:00:00 2001 From: Adam Avramov Date: Sat, 14 Oct 2023 22:06:15 +0300 Subject: [PATCH] wip: feat(devnet): get devnet - populate connectModes for devnet - populate static devnet constructors --- agent/agent-chain.ts | 1 + connect/connect.ts | 13 ++++++++++--- connect/cw/okp4/okp4.ts | 4 ++-- connect/scrt/scrt.ts | 8 ++++---- fadroma-devnet.ts | 27 ++++++++++++++++++++------- fadroma.ts | 18 +++++++++++------- spec/CW.spec.ts.md | 4 ++-- 7 files changed, 50 insertions(+), 25 deletions(-) diff --git a/agent/agent-chain.ts b/agent/agent-chain.ts index 45295505139..33ae7166f2c 100644 --- a/agent/agent-chain.ts +++ b/agent/agent-chain.ts @@ -38,6 +38,7 @@ export interface DevnetHandle { url: URL running: boolean chainId: string + platform: string start: () => Promise getAccount: (name: string) => Promise> assertPresence: () => Promise diff --git a/connect/connect.ts b/connect/connect.ts index 98d21d95b3f..9997e0cab04 100644 --- a/connect/connect.ts +++ b/connect/connect.ts @@ -43,15 +43,22 @@ export type ConnectMode = |`Scrt${'Mocknet'|'Devnet'|'Testnet'|'Mainnet'}` |`OKP4${'Devnet'|'Testnet'}` -export const connectModes: RecordChain> = { +export const connectModes = { + // Support for Secret Network ScrtMainnet: Scrt.mainnet, ScrtTestnet: Scrt.testnet, - ScrtDevnet: () => { throw new Error('Devnets are only available via @hackbg/fadroma') }, + ScrtDevnet: (...args: Parameters): Scrt.Chain => { + throw new Error('Devnets are only available via @hackbg/fadroma') + }, ScrtMocknet: Scrt.Chain.mocknet, + // Support for OKP4 OKP4Testnet: CW.OKP4.testnet, - OKP4Devnet: () => { throw new Error('Devnets are only available via @hackbg/fadroma') }, + OKP4Devnet: (...args: Parameters): CW.OKP4.Chain => { + throw new Error('Devnets are only available via @hackbg/fadroma') + }, + // TODO: Support for custom chain } diff --git a/connect/cw/okp4/okp4.ts b/connect/cw/okp4/okp4.ts index 43500a36711..44d89aa26d0 100644 --- a/connect/cw/okp4/okp4.ts +++ b/connect/cw/okp4/okp4.ts @@ -211,7 +211,7 @@ bindChainSupport(OKP4Chain, OKP4Agent, OKP4Bundle) export { OKP4Config as Config, OKP4Chain as Chain, OKP4Agent as Agent, OKP4Bundle as Bundle } /** Connect to OKP4 testnet. */ -export const testnet = OKP4Chain.testnet +export const testnet = (...args: Parameters) => OKP4Chain.testnet(...args) /** Connect to local OKP4 devnet. */ -export const devnet = OKP4Chain.devnet +export const devnet = (...args: Parameters) => OKP4Chain.devnet(...args) diff --git a/connect/scrt/scrt.ts b/connect/scrt/scrt.ts index 1e495385b5b..a76ff07bc34 100644 --- a/connect/scrt/scrt.ts +++ b/connect/scrt/scrt.ts @@ -23,7 +23,7 @@ export * from './scrt-token' export * as SecretJS from '@hackbg/secretjs-esm' import { Chain } from './scrt-chain' -export const mainnet = Chain.mainnet -export const testnet = Chain.testnet -export const devnet = Chain.devnet -export const mocknet = Chain.mocknet +export const mainnet = (...args: Parameters) => Chain.mainnet(...args) +export const testnet = (...args: Parameters) => Chain.testnet(...args) +export const devnet = (...args: Parameters) => Chain.devnet(...args) +export const mocknet = (...args: Parameters) => Chain.mocknet(...args) diff --git a/fadroma-devnet.ts b/fadroma-devnet.ts index 271355b087f..f2c61cc6d8a 100644 --- a/fadroma-devnet.ts +++ b/fadroma-devnet.ts @@ -384,14 +384,27 @@ export class Devnet implements DevnetHandle { } } - /** Get a Chain object corresponding to this devnet. */ + /** Get a Chain object wrapping this devnet. */ getChain = > ( - $C: ChainClass = Chain as unknown as ChainClass - ): C => new $C({ - id: this.chainId, - mode: Chain.Mode.Devnet, - devnet: this - }) + $C: ChainClass = Chain as unknown as ChainClass, + options?: Partial + ): C => { + if (options?.id && options.id !== this.chainId) { + this.log.warn('Devnet#getChain: ignoring passed chain id') + } + if (options?.mode && options.mode !== Chain.Mode.Devnet) { + this.log.warn('Devnet#getChain: ignoring passed chain mode') + } + if (options?.devnet) { + this.log.warn('Devnet#getChain: ignoring passed devnet handle') + } + return new $C({ + ...options, + id: this.chainId, + mode: Chain.Mode.Devnet, + devnet: this + }) + } /** Get the info for a genesis account, including the mnemonic */ getAccount = async (name: string): Promise> => { diff --git a/fadroma.ts b/fadroma.ts index f5daad87742..36cd9d8b895 100644 --- a/fadroma.ts +++ b/fadroma.ts @@ -92,13 +92,17 @@ export function getDevnet (options: Partial = {}) { } // Installs devnets as selectable chains: -connectModes['ScrtDevnet'] = - (options: Partial = { platform: 'scrt_1.9' }): Scrt.Chain => - new Config().getDevnet(options).getChain(Scrt.Chain as ChainClass) - -connectModes['OKP4Devnet'] = - (options: Partial = { platform: 'okp4_5.0' }): CW.OKP4.Chain => - new Config().getDevnet(options).getChain(CW.OKP4.Chain as ChainClass) +connectModes['ScrtDevnet'] = Scrt.Chain.devnet = + (options: Partial|undefined): Scrt.Chain => + new Config() + .getDevnet({ platform: 'scrt_1.9' }) + .getChain(Scrt.Chain as ChainClass, options) + +connectModes['OKP4Devnet'] = CW.OKP4.Chain.devnet = + (options: Partial|undefined): CW.OKP4.Chain => + new Config() + .getDevnet({ platform: 'okp4_5.0' }) + .getChain(CW.OKP4.Chain as ChainClass, options) const console = new Console(`@hackbg/fadroma ${version}`) diff --git a/spec/CW.spec.ts.md b/spec/CW.spec.ts.md index 217acc6ac6a..abd7b30d134 100644 --- a/spec/CW.spec.ts.md +++ b/spec/CW.spec.ts.md @@ -6,8 +6,8 @@ import assert from 'node:assert' Fadroma supports connecting to any chain that supports CosmWasm. -For this, we currently use our own fork of `@cosmjs/*`, -unified into a single package, `@hackbg/cosmjs-esm`. +For this, we currently use our own fork of the `@cosmjs/*` packages, +unified into a single package, [`@hackbg/cosmjs-esm`](https://www.npmjs.com/package/@hackbg/cosmjs-esm). ## OKP4 support