From 6836edd21579d7b157869d0496b445fb7929b7b6 Mon Sep 17 00:00:00 2001 From: Adam A Date: Fri, 10 May 2024 22:18:26 +0300 Subject: [PATCH] feat(namada): errors, warnings, testnet connect --- packages/cw/cw-connection.ts | 10 +++++- packages/namada/namada-connection.ts | 35 ++++++++++++++++++- packages/namada/namada.ts | 51 +++------------------------- 3 files changed, 47 insertions(+), 49 deletions(-) diff --git a/packages/cw/cw-connection.ts b/packages/cw/cw-connection.ts index febd26d863..c0f6ffecf5 100644 --- a/packages/cw/cw-connection.ts +++ b/packages/cw/cw-connection.ts @@ -24,11 +24,16 @@ export class CWChain extends Chain { connections: [], bech32Prefix: 'tnam' }) - const connections: CWConnection[] = urls.map(async (url: string|URL)=>new this.Connection({ + const connections: CWConnection[] = urls.filter(Boolean).map(async (url: string|URL)=>new this.Connection({ api: await CosmWasmClient.connect(String(url)), chain, url: String(url) })) + if (connections.length === 0) { + new Console(this.constructor.name).warn( + 'No connection URLs provided. RPC operations will fail.' + ) + } chain.connections = await Promise.all(connections) return chain } @@ -53,6 +58,9 @@ export class CWChain extends Chain { connections: CWConnection[] getConnection (): CWConnection { + if (!this.connections[0]) { + throw new Error('No active connection.') + } return this.connections[0] } diff --git a/packages/namada/namada-connection.ts b/packages/namada/namada-connection.ts index cb88208713..8e3a4a189c 100644 --- a/packages/namada/namada-connection.ts +++ b/packages/namada/namada-connection.ts @@ -35,6 +35,9 @@ import { export class Namada extends CW.Chain { + decode = Decode + + /** Connect to Namada over one or more endpoints. */ static async connect ( properties: ({ url: string|URL }|{ urls: Iterable }) & { chainId?: ChainId @@ -43,10 +46,35 @@ export class Namada extends CW.Chain { ): Promise { if (properties.decoder) { await initDecoder(properties.decoder) + } else { + new CW.Console('Namada').warn( + "You didn't provide the 'decoder' property; trying to decode Namada objects will fail." + ) } return await super.connect(properties) as Namada } + /** Connect to Namada using `testnetChainId` and `testnetUrls`. */ + static testnet (properties: Parameters[0]) { + return this.connect({ + chainId: properties.chainId || Namada.testnetChainId, + urls: (properties as any).url + ? [(properties as any).url] + : ((properties as any).urls || [...Namada.testnetUrls]), + }) + } + + /** Default chain ID of testnet. */ + static testnetChainId = 'shielded-expedition.88f17d1d14' + + /** Default RPC endpoints for testnet. */ + static testnetUrls = new Set([ + 'https://namada-testnet-rpc.itrocket.net', + 'https://namada-rpc.stake-machine.com', + 'https://namadarpc.songfi.xyz', + 'https://rpc.testnet.one', + ]) + static get Connection () { return NamadaConnection } @@ -140,7 +168,12 @@ export class Namada extends CW.Chain { } export class NamadaConnection extends CW.Connection { - decode = Decode + get chain (): Namada { + return super.chain as unknown as Namada + } + get decode () { + return this.chain.decode + } async fetchBlockInfoImpl (wantedHeight?: number): Promise { if (!this.url) { throw new CW.Error("Can't fetch block: missing connection URL") diff --git a/packages/namada/namada.ts b/packages/namada/namada.ts index 7670011349..235e07a90b 100644 --- a/packages/namada/namada.ts +++ b/packages/namada/namada.ts @@ -1,51 +1,10 @@ -import { - bold, - pickRandom -} from '@fadroma/agent' -import { - brailleDump -} from '@hackbg/dump' -import { - NamadaConsole -} from './namada-console' -import { - Namada, - NamadaConnection, +export { + Namada as default, + NamadaConnection as Connection } from './namada-connection' -import { - NamadaMnemonicIdentity -} from './namada-identity' - -export const chainIds = { - testnet: 'shielded-expedition.88f17d1d14' -} - -export const testnets = new Set([ - 'https://namada-testnet-rpc.itrocket.net', - 'https://namada-rpc.stake-machine.com', - 'https://namadarpc.songfi.xyz', - 'https://rpc.testnet.one', -]) - -export const faucets = { - //'luminara.4d6026bc59ee20d9664d3': new Set([ - //'https://faucet.luminara.icu/' - //]) -} - -/** Connect to Namada in testnet mode. */ -export const testnet = (options: Partial = {}): Promise => { - return Namada.connect({ - ...options||{}, - chainId: chainIds.testnet, - urls: testnets, - }) -} - export { - NamadaConnection as Connection, NamadaMnemonicIdentity as MnemonicIdentity, -} +} from './namada-identity' export { initDecoder } from './namada-decode' @@ -56,5 +15,3 @@ export { Transaction } from './namada-tx' export * as TX from './namada-tx' - -export default Namada