Skip to content

Commit

Permalink
wip: @fadroma/tendermint
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Aug 18, 2024
1 parent d856947 commit 028469e
Show file tree
Hide file tree
Showing 30 changed files with 995 additions and 770 deletions.
File renamed without changes.
File renamed without changes.
15 changes: 7 additions & 8 deletions packages/cw/cw.ts → packages/cw/CW.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,25 @@ export * as CosmJS from '@hackbg/cosmjs-esm'
export {
CWError as Error,
CWConsole as Console
} from './cw-base'
} from './CWBase'
export {
CWChain as Chain,
CWConnection as Connection,
} from './cw-connection'
} from './CWConnection'
export {
CWBlock as Block,
CWTransaction as Transaction,
CWBatch as Batch,
} from './cw-tx'
} from './CWTX'
export {
CWIdentity as Identity,
CWSignerIdentity as SignerIdentity,
CWMnemonicIdentity as MnemonicIdentity,
encodeSecp256k1Signature
} from './cw-identity'
export * from './cw-chains'
export * as Staking from './cw-staking'

import { CWChain } from './cw-connection'
} from './CWIdentity'
export * from './CWChains'
export * as Staking from './CWStaking'
import { CWChain } from './CWConnection'

export function connect (...args: Parameters<typeof CWChain.connect>) {
return CWChain.connect(...args)
Expand Down
4 changes: 2 additions & 2 deletions packages/cw/cw-bank.ts → packages/cw/CWBank.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { optionallyParallel } from '@hackbg/fadroma'
import type { CosmWasmClient, SigningCosmWasmClient } from '@hackbg/cosmjs-esm'
import type { Address, Token, Chain, Connection, SigningConnection } from '@hackbg/fadroma'
import type { CWChain, CWConnection } from './cw-connection'
import type { CWAgent, CWSigningConnection } from './cw-identity'
import type { CWChain, CWConnection } from './CWConnection'
import type { CWAgent, CWSigningConnection } from './CWIdentity'

export async function fetchBalance (chain: CWConnection, {
parallel = false,
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions packages/cw/cw-compute.ts → packages/cw/CWCompute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { CosmWasmClient, SigningCosmWasmClient } from '@hackbg/cosmjs-esm'
import { Connection, SigningConnection, Agent, UploadedCode, Contract } from '@hackbg/fadroma'
import type { Address, CodeId, Message, Token } from '@hackbg/fadroma'
import { Amino } from '@hackbg/cosmjs-esm'
import type { CWChain, CWConnection } from './cw-connection'
import type { CWAgent, CWSigningConnection } from './cw-identity'
import type { CWChain, CWConnection } from './CWConnection'
import type { CWAgent, CWSigningConnection } from './CWIdentity'

export async function fetchCodeInfo (
chain: CWConnection, args: Parameters<Connection["fetchCodeInfoImpl"]>[0]
Expand Down
20 changes: 10 additions & 10 deletions packages/cw/cw-connection.ts → packages/cw/CWConnection.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { bold, assign, Chain, Connection } from '@hackbg/fadroma'
import type { Address, Message, CodeId, CodeHash, Token, ChainId } from '@hackbg/fadroma'
import { CWAgent, CWSigningConnection, CWIdentity, CWMnemonicIdentity, CWSignerIdentity } from './cw-identity'
import { CWConsole as Console, CWError as Error } from './cw-base'
import { CWBlock, CWBatch } from './cw-tx'
import * as CWBank from './cw-bank'
import * as CWCompute from './cw-compute'
import * as CWStaking from './cw-staking'
import { CWAgent, CWSigningConnection, CWIdentity, CWMnemonicIdentity, CWSignerIdentity } from './CWIdentity'
import { CWConsole as Console, CWError as Error } from './CWBase'
import { CWBlock, CWBatch } from './CWTX'
import * as CWBank from './CWBank'
import * as CWCompute from './CWCompute'
import * as CWStaking from './CWStaking'
import { Amino, Proto, CosmWasmClient, SigningCosmWasmClient } from '@hackbg/cosmjs-esm'
import type { Block } from '@hackbg/cosmjs-esm'

Expand Down Expand Up @@ -208,13 +208,13 @@ export class CWConnection extends Connection {
return await CWCompute.query(this, ...args) as T
}

fetchValidatorsImpl ({ details = false }: {
details?: boolean
fetchValidatorsImpl ({ fetchDetails = false }: {
fetchDetails?: boolean
} = {}) {
return this.tendermintClient.then(()=>CWStaking.getValidators(this, { details }))
return this.tendermintClient.then(()=>CWStaking.fetchValidatorList(this, { fetchDetails }))
}

fetchValidatorInfoImpl (address: Address): Promise<unknown> {
fetchValidatorInfoImpl (address: Address): Promise<CWStaking.Validator> {
return Promise.all([
this.queryClient,
this.tendermintClient
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions packages/cw/cw-identity.ts → packages/cw/CWIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import {
Batch
} from '@hackbg/fadroma'
import type { Address } from '@hackbg/fadroma'
import { CWError as Error } from './cw-base'
import { CWBatch } from './cw-tx'
import * as CWBank from './cw-bank'
import * as CWCompute from './cw-compute'
import * as CWStaking from './cw-staking'
import { CWError as Error } from './CWBase'
import { CWBatch } from './CWTX'
import * as CWBank from './CWBank'
import * as CWCompute from './CWCompute'
import * as CWStaking from './CWStaking'

export class CWAgent extends Agent {
constructor (properties: ConstructorParameters<typeof Agent>[0] & {
Expand Down
69 changes: 69 additions & 0 deletions packages/cw/CWStaking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { base16, SHA256, Validator } from '@hackbg/fadroma'
import type { Address } from '@hackbg/fadroma'
import { Amino, Proto } from '@hackbg/cosmjs-esm'
import type { CWChain, CWConnection } from './CWConnection'

export interface CWValidator extends Validator {
readonly chain: CWChain
readonly publicKey: string
readonly publicKeyBytes?: Uint8Array
readonly publicKeyHash?: string
readonly votingPower?: bigint
readonly proposerPriority?: bigint
}

export async function fetchValidatorList (connection: CWConnection, options: {
pagination?: [number, number],
fetchDetails?: boolean
} = {}): Promise<Record<CWValidator["address"], CWValidator>> {
const { pagination, fetchDetails } = options || {}
const tendermintClient = await connection.tendermintClient!
let response
if (pagination && (pagination as Array<number>).length !== 0) {
if (pagination.length !== 2) {
throw new Error("pagination format: [page, per_page]")
}
response = await tendermintClient!.validators({
page: pagination[0],
per_page: pagination[1],
})
} else {
response = await tendermintClient!.validatorsAll()
}
// Sort validators by voting power in descending order.
const validators = [...response.validators].sort((a,b)=>(
(a.votingPower < b.votingPower) ? 1 :
(a.votingPower > b.votingPower) ? -1 : 0
))
const result: Record<CWValidator["address"], CWValidator> = {}
for (const { address, pubkey, votingPower, proposerPriority } of validators) {
let validator: CWValidator = {
chain: connection.chain,
address: base16.encode(address),
publicKey: pubkey?.data,
votingPower,
proposerPriority,
}
if (fetchDetails) {
validator = await fetchValidatorDetails(connection, validator)
}
result[validator.address] = validator
}
return result
}

export async function fetchValidatorDetails (
connection: CWConnection,
validator: CWValidator
): Promise<CWValidator> {
const request = Proto.Cosmos.Staking.v1beta1.Query.QueryValidatorRequest.encode({
validatorAddr: validator.address
}).finish()
const value = await connection.abciQuery('/cosmos.staking.v1beta1.Query/Validator', request)
const decoded = Proto.Cosmos.Staking.v1beta1.Query.QueryValidatorResponse.decode(value)
return { ...validator, ...decoded }
}

export {
CWValidator as Validator
}
6 changes: 3 additions & 3 deletions packages/cw/cw-tx.ts → packages/cw/CWTX.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Block, Batch } from '@hackbg/fadroma'
import { CWError as Error } from './cw-base'
import { CWError as Error } from './CWBase'
import { Chain } from '@hackbg/fadroma'
import { Transaction } from '@hackbg/fadroma'
import type { CWChain, CWConnection } from './cw-connection'
import type { CWAgent } from './cw-identity'
import type { CWChain, CWConnection } from './CWConnection'
import type { CWAgent } from './CWIdentity'

type CWBlockParameters =
ConstructorParameters<typeof Block>[0] & Partial<Pick<CWBlock, 'rawTransactions'>>
Expand Down
6 changes: 3 additions & 3 deletions packages/cw/archway/archway.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CLI } from '../cw-base'
import { CWConnection } from '../cw-connection'
import { CWMnemonicIdentity } from '../cw-identity'
import { CLI } from '../CWBase'
import { CWConnection } from '../CWConnection'
import { CWMnemonicIdentity } from '../CWIdentity'

class ArchwayCLI extends CLI {}

Expand Down
6 changes: 3 additions & 3 deletions packages/cw/axelar/axelar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CLI } from '../cw-base'
import { CWConnection } from '../cw-connection'
import { CWMnemonicIdentity } from '../cw-identity'
import { CLI } from '../CWBase'
import { CWConnection } from '../CWConnection'
import { CWMnemonicIdentity } from '../CWIdentity'

class AxelarCLI extends CLI {}

Expand Down
21 changes: 0 additions & 21 deletions packages/cw/cw-base.ts

This file was deleted.

97 changes: 0 additions & 97 deletions packages/cw/cw-staking.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/cw/injective/injective.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CLI } from '../cw-base'
import { CWConnection } from '../cw-connection'
import { CWMnemonicIdentity } from '../cw-identity'
import { CLI } from '../CWBase'
import { CWConnection } from '../CWConnection'
import { CWMnemonicIdentity } from '../CWIdentity'

class InjectiveCLI extends CLI {}

Expand Down
7 changes: 3 additions & 4 deletions packages/cw/okp4/okp4.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CLI } from '../cw-base'
import { CWError as Error } from '../cw-base'
import { CWChain, CWConnection } from '../cw-connection'
import { CWMnemonicIdentity } from '../cw-identity'
import { CLI, CWError as Error } from '../CWBase'
import { CWChain, CWConnection } from '../CWConnection'
import { CWMnemonicIdentity } from '../CWIdentity'

import { Objectarium, objectariumCodeIds } from './okp4-objectarium'
import { Cognitarium, cognitariumCodeIds } from './okp4-cognitarium'
Expand Down
6 changes: 3 additions & 3 deletions packages/cw/osmosis/osmosis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CLI } from '../cw-base'
import { CWConnection } from '../cw-connection'
import { CWMnemonicIdentity } from '../cw-identity'
import { CLI } from '../CWBase'
import { CWConnection } from '../CWConnection'
import { CWMnemonicIdentity } from '../CWIdentity'

class OsmosisCLI extends CLI {}

Expand Down
5 changes: 1 addition & 4 deletions packages/cw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
"description": "CosmJS Stargate integration for Fadroma.",
"dependencies": {
"@hackbg/cosmjs-esm": "workspace:*",
"@hackbg/cmds": "workspace:*",
"@hackbg/borshest": "workspace:*",
"borsh": "^2.0.0",
"borsher": "^1.2.1"
"@hackbg/cmds": "workspace:*"
},
"peerDependencies": {
"@hackbg/fadroma": "workspace:*"
Expand Down
8 changes: 4 additions & 4 deletions packages/cw/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"files": [
"cw.ts",
"cw-base.ts",
"cw-identity.ts",
"cw-connection.ts",
"CW.ts",
"CWBase.ts",
"CWIdentity.ts",
"CWConnection.ts",
"okp4/okp4.ts",
"okp4/okp4-cognitarium.ts",
"okp4/okp4-law-stone.ts",
Expand Down
Loading

0 comments on commit 028469e

Please sign in to comment.