Skip to content

Commit

Permalink
feat(namada): errors, warnings, testnet connect
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed May 10, 2024
1 parent 471a809 commit 6836edd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 49 deletions.
10 changes: 9 additions & 1 deletion packages/cw/cw-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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]
}

Expand Down
35 changes: 34 additions & 1 deletion packages/namada/namada-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string|URL> }) & {
chainId?: ChainId
Expand All @@ -43,10 +46,35 @@ export class Namada extends CW.Chain {
): Promise<Namada> {
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<typeof Namada["connect"]>[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
}
Expand Down Expand Up @@ -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<TX.NamadaBlock> {
if (!this.url) {
throw new CW.Error("Can't fetch block: missing connection URL")
Expand Down
51 changes: 4 additions & 47 deletions packages/namada/namada.ts
Original file line number Diff line number Diff line change
@@ -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<NamadaConnection> = {}): Promise<Namada> => {
return Namada.connect({
...options||{},
chainId: chainIds.testnet,
urls: testnets,
})
}

export {
NamadaConnection as Connection,
NamadaMnemonicIdentity as MnemonicIdentity,
}
} from './namada-identity'
export {
initDecoder
} from './namada-decode'
Expand All @@ -56,5 +15,3 @@ export {
Transaction
} from './namada-tx'
export * as TX from './namada-tx'

export default Namada

0 comments on commit 6836edd

Please sign in to comment.