import { Core, Chain, Deploy } from '@fadroma/agent' import type { Address, Message, CodeId, CodeHash, Token } from '@fadroma/agent' import type { CWMnemonicIdentity, CWSignerIdentity } from './cw-identity' import { CWConsole as Console, CWError as Error } from './cw-base' import { CWBatch } from './cw-batch' import { Amino, Proto, CosmWasmClient, SigningCosmWasmClient } from '@hackbg/cosmjs-esm' import type { Block } from '@hackbg/cosmjs-esm' import { getBalance, send, } from './cw-bank' import { getCodes, getCodeId, getContractsByCodeId, getCodeHashOfAddress, getCodeHashOfCodeId, getLabel, upload, instantiate, execute, query } from './cw-compute' import type { SigningConnection } from './cw-compute' import { Validator, getValidators, } from './cw-staking' export class CWBlock extends Chain.Block { rawTxs: Uint8Array[] constructor ({ hash, height, rawTxs }: Partial = {}) { super({ hash, height }) this.rawTxs = [...rawTxs||[]] } } /** Generic agent for CosmWasm-enabled chains. */ export class CWConnection extends Chain.Connection { /** The bech32 prefix for the account's address */ bech32Prefix?: string /** The coin type in the HD derivation path */ coinType?: number /** The account index in the HD derivation path */ hdAccountIndex?: number /** API connects asynchronously, so API handle is a promise of either variant. */ declare api: Promise /** A supported method of authentication. */ declare identity: CWMnemonicIdentity|CWSignerIdentity constructor (properties: Partial) { super(properties) Core.assign(this, properties, [ 'coinType', 'bech32Prefix', 'hdAccountIndex' ]) if (!this.url) { throw new Error('No connection URL.') } if (this.identity?.signer) { this.log.debug('Connecting and authenticating via', Core.bold(this.url)) this.api = SigningCosmWasmClient.connectWithSigner(this.url, this.identity.signer) } else { this.log.debug('Connecting anonymously via', Core.bold(this.url)) this.api = CosmWasmClient.connect(this.url) } } /** Handle to the API's internal query client. */ get queryClient (): Promise> { return Promise.resolve(this.api).then(api=>(api as any)?.queryClient) } /** Handle to the API's internal Tendermint transaction client. */ get tendermintClient (): Promise> { return Promise.resolve(this.api).then(api=>(api as any)?.tmClient) } abciQuery (path, params = new Uint8Array()) { return this.queryClient.then(async client=>{ this.log.debug('ABCI query:', path) const { value } = await client!.queryAbci(path, params) return value }) } async doGetBlockInfo (height?: number): Promise { const api = await this.api const { id, header, txs } = await api.getBlock(height) return new CWBlock({ hash: id, height: header.height, rawTxs: txs as Uint8Array[], }) } async doGetHeight () { const { height } = await this.doGetBlockInfo() return height } /** Query native token balance. */ doGetBalance ( token: string = this.defaultDenom, address: Address|undefined = this.address ): Promise { return getBalance(this, token, address) } doGetCodes () { return getCodes(this) } doGetCodeId (address: Address): Promise { return getCodeId(this, address) } doGetContractsByCodeId (id: CodeId): Promise> { return getContractsByCodeId(this, id) } doGetCodeHashOfAddress (address: Address): Promise { return getCodeHashOfAddress(this, address) } doGetCodeHashOfCodeId (codeId: CodeId): Promise { return getCodeHashOfCodeId(this, codeId) } async getLabel (address: Address): Promise { return getLabel(this, address) } async doSend ( recipient: Address, amounts: Token.ICoin[], options?: Parameters[2] ) { return send(this as SigningConnection, recipient, amounts, options) } doSendMany ( outputs: [Address, Token.ICoin[]][], options?: Parameters[1] ): Promise { throw new Error('doSendMany: not implemented') } async doUpload (data: Uint8Array): Promise> { if (!this.address) { throw new Error("can't upload contract without sender address") } return upload(this as SigningConnection, data) } async doInstantiate ( codeId: CodeId, options: Parameters[1] ): Promise> { if (!this.address) { throw new Error("can't instantiate contract without sender address") } return instantiate(this as SigningConnection, codeId, options) } async doExecute ( contract: { address: Address }, message: Message, options: Omit[2]>, 'execFee'> & { execFee?: Token.IFee | number | 'auto' } = {} ): Promise { if (!this.address) { throw new Error("can't execute transaction without sender address") } options.execFee ??= 'auto' return execute(this as SigningConnection, contract, message, options) } async doQuery ( contract: Address|{ address: Address }, message: Message ): Promise { return query(this, contract, message) } batch (): Chain.Batch { return new CWBatch({ connection: this }) as unknown as Chain.Batch } getValidators ({ details = false }: { details?: boolean } = {}) { return this.tendermintClient.then(()=>getValidators(this, { details })) } getValidator (address: Address): Promise { return Promise.all([ this.queryClient, this.tendermintClient ]).then(()=>new Validator({ address }).fetchDetails(this)) } }