diff --git a/agent/agent-chain.ts b/agent/agent-chain.ts index 214107e98e9..d3838f123e9 100644 --- a/agent/agent-chain.ts +++ b/agent/agent-chain.ts @@ -190,6 +190,30 @@ export abstract class Chain { /** Return self. */ get chain () { return this } + api?: unknown + + abstract getApi (): unknown + + get ready () { + if (this.isDevnet && !this.devnet) { + throw new Error("the chain is marked as a devnet but is missing the devnet handle") + } + type This = this + type ThisWithApi = This & { api: NonNullable } + const init = new Promise(async (resolve, reject)=>{ + if (this.isDevnet) { + await this.devnet!.start() + } + if (!this.api) { + if (!this.url) throw new Error("the chain's url property is not set") + this.api = await Promise.resolve(this.getApi()) + } + return resolve(this as ThisWithApi) + }) + Object.defineProperty(this, 'ready', { get () { return init } }) + return init + } + /** Wait for the block height to increment. */ get nextBlock (): Promise { return this.height.then(async startingHeight=>{ @@ -296,6 +320,11 @@ export class StubChain extends Chain { defaultDenom = 'stub' + getApi (): {} { + this.log.warn('chain.getApi: this function is stub; use a subclass of Chain') + return Promise.resolve({}) + } + /** Get the current block height. */ get height (): Promise { this.log.warn('chain.height: this getter is stub; use a subclass of Chain') diff --git a/connect/cw/cw-base.ts b/connect/cw/cw-base.ts index ca8002b4c02..33a98941eb9 100644 --- a/connect/cw/cw-base.ts +++ b/connect/cw/cw-base.ts @@ -34,27 +34,10 @@ class CWChain extends Chain { defaultDenom = '' /** Query-only API handle. */ - api?: CosmWasmClient + declare api?: CosmWasmClient - /** One-shot async initialization of chain. - * Populates the `api` property with a CosmWasmClient. */ - get ready (): Promise { - if (this.isDevnet && !this.devnet) { - throw new Error("the chain is marked as a devnet but is missing the devnet handle") - } - const init = new Promise(async (resolve, reject)=>{ - if (this.isDevnet) { - await this.devnet!.start() - } - if (!this.api) { - if (!this.url) throw new CWError("the chain's url property is not set") - const api = await CosmWasmClient.connect(this.url) - this.api = api - } - return resolve(this as this & { api: CosmWasmClient }) - }) - Object.defineProperty(this, 'ready', { get () { return init } }) - return init + async getApi (): Promise { + return await CosmWasmClient.connect(this.url) } get block (): Promise { diff --git a/connect/scrt/scrt-chain.ts b/connect/scrt/scrt-chain.ts index f1bf9cac634..49ca3c94806 100644 --- a/connect/scrt/scrt-chain.ts +++ b/connect/scrt/scrt-chain.ts @@ -35,33 +35,7 @@ class ScrtChain extends Chain { Agent: AgentClass = ScrtChain.Agent /** A fresh instance of the anonymous read-only API client. Memoize yourself. */ - api?: SecretNetworkClient - - constructor (options: Partial = { - url: ScrtChain.Config.defaultMainnetUrl, - mode: Chain.Mode.Mainnet - }) { - super(options) - this.log.label = `${this.id}` - } - - get ready (): Promise { - if (this.isDevnet && !this.devnet) { - throw new Error("the chain is marked as a devnet but is missing the devnet handle") - } - const init = new Promise(async (resolve, reject)=>{ - if (this.isDevnet) { - await this.devnet!.start() - } - if (!this.api) { - if (!this.url) throw new Error("the chain's url property is not set") - this.api = this.getApi() - } - return resolve(this as this & { api: SecretNetworkClient }) - }) - Object.defineProperty(this, 'ready', { get () { return init } }) - return init - } + declare api?: SecretNetworkClient /** @returns a fresh instance of the anonymous read-only API client. */ getApi (options: Partial = {}): SecretNetworkClient { @@ -185,13 +159,18 @@ export type { TxResponse } /** Represents a connection to the Secret Network, * authenticated as a specific address. */ class ScrtAgent extends Agent { + log = new Console('ScrtAgent') + /** Downcast chain property to Scrt only. */ declare chain: ScrtChain + /** Batch class used by this agent. */ Batch: BatchClass = ScrtAgent.Batch + /** Whether to simulate each execution first to get a more accurate gas estimate. */ simulateForGas: boolean = false + /** Default fees for this agent. */ fees = ScrtChain.defaultFees @@ -208,10 +187,10 @@ class ScrtAgent extends Agent { } get ready (): Promise { - // Require chain reference to be populated. - if (!this.chain) throw new Error.Missing.Chain() // If an API instance is already available (e.g. provided to constructor), just use that. if (this.api) return Promise.resolve(this as this & { api: SecretNetworkClient }) + // Require chain reference to be populated. + if (!this.chain) throw new Error.Missing.Chain() // Begin asynchronous init. const init = new Promise(async ( resolve, reject diff --git a/connect/scrt/scrt-mocknet.ts b/connect/scrt/scrt-mocknet.ts index 114cd302605..c4cfcfabc77 100644 --- a/connect/scrt/scrt-mocknet.ts +++ b/connect/scrt/scrt-mocknet.ts @@ -234,6 +234,10 @@ export class Mocknet extends Chain { ) } } + + getApi () { + return Promise.resolve({}) + } } class MocknetAgent extends Agent {