Skip to content

Commit

Permalink
refactor(namada): separate proposal info/votes/result methods
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Sep 25, 2024
1 parent 1eb6c03 commit 09047ad
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 36 deletions.
1 change: 0 additions & 1 deletion packages/namada/Namada.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ export function mainnet (...args: never) {
)
}
export type { Validator } from './NamadaPoS'
export type Epoch = number|bigint|string
6 changes: 6 additions & 0 deletions packages/namada/NamadaChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ export default class NamadaChain extends CW.Chain {
fetchProposalInfo (id: number|bigint) {
return this.getConnection().fetchProposalInfoImpl(id)
}
fetchProposalVotes (id: number|bigint) {
return this.getConnection().fetchProposalVotesImpl(id)
}
fetchProposalResult (id: number|bigint) {
return this.getConnection().fetchProposalResultImpl(id)
}
fetchProposalWasm (id: number|bigint) {
return this.getConnection().fetchProposalWasmImpl(id)
}
Expand Down
17 changes: 13 additions & 4 deletions packages/namada/NamadaConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ export default class NamadaConnection extends CW.Connection {
fetchProposalInfoImpl (id: number|bigint) {
return Gov.fetchProposalInfo(this, id)
}
fetchProposalVotesImpl (id: number|bigint) {
return Gov.fetchProposalVotes(this, id)
}
fetchProposalResultImpl (id: number|bigint) {
return Gov.fetchProposalResult(this, id)
}
fetchProposalWasmImpl (id: number|bigint) {
return Gov.fetchProposalWasm(this, id)
}
Expand All @@ -100,7 +106,7 @@ export default class NamadaConnection extends CW.Connection {
return PGF.fetchPGFFundings(this)
}
isPGFStewardImpl (address: string) {
return PGF.isPGFSteward(this)
return PGF.isPGFSteward(this, address)
}

fetchStakingParametersImpl () {
Expand All @@ -109,11 +115,11 @@ export default class NamadaConnection extends CW.Connection {
fetchValidatorAddressesImpl () {
return PoS.fetchValidatorAddresses(this)
}
fetchValidatorImpl (address: string, options?: { epoch?: Epoch }) {
fetchValidatorImpl (address: string, options?: { epoch?: Epoch.Epoch }) {
return PoS.fetchValidator(this, address, options)
}
fetchValidatorsImpl (options?: {
epoch?: string|number|bigint,
epoch?: Epoch.Epoch,
details?: boolean,
pagination?: [number, number]
allStates?: boolean,
Expand All @@ -123,7 +129,10 @@ export default class NamadaConnection extends CW.Connection {
}) {
return PoS.fetchValidators(this, options)
}
fetchValidatorsIterImpl (options?: { parallel?: boolean }) {
fetchValidatorsIterImpl (options?: {
epoch?: Epoch.Epoch,
parallel?: boolean
}) {
return PoS.fetchValidatorsIter(this, options)
}
fetchValidatorsConsensusImpl () {
Expand Down
2 changes: 2 additions & 0 deletions packages/namada/NamadaEpoch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { decode, u64 } from '@hackbg/borshest'
import type { Decode } from './NamadaDecode'
import type NamadaConnection from './NamadaConnection'

export type Epoch = number|bigint|string

export async function fetchEpoch (
connection: Pick<NamadaConnection, 'abciQuery'>,
height?: number|bigint
Expand Down
68 changes: 42 additions & 26 deletions packages/namada/NamadaGov.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,59 @@
import { assign } from '@hackbg/fadroma'
import type { Address } from '@hackbg/fadroma'
import { decode, u64 } from '@hackbg/borshest'
import type Connection from './NamadaConnection'
import type NamadaConnection from './NamadaConnection'
import type { NamadaDecoder } from './NamadaDecode'

export type Params = Awaited<ReturnType<typeof fetchGovernanceParameters>>

export async function fetchGovernanceParameters (connection: Pick<Connection, 'abciQuery'|'decode'>) {
type Connection = Pick<NamadaConnection, 'abciQuery'|'decode'>

export const INTERNAL_ADDRESS = "tnam1q5qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqrw33g6"

export async function fetchGovernanceParameters (
connection: Connection
) {
const binary = await connection.abciQuery(`/vp/governance/parameters`)
return connection.decode.gov_parameters(binary)
}

export async function fetchProposalCount (connection: Pick<Connection, 'abciQuery'>) {
export async function fetchProposalCount (
connection: Connection
) {
const binary = await connection.abciQuery(`/shell/value/#${INTERNAL_ADDRESS}/counter`)
return decode(u64, binary) as bigint
}

export async function fetchProposalInfo (
connection: Pick<Connection, 'abciQuery'|'decode'>, id: number|bigint
): Promise<NamadaGovernanceProposal|null> {
const proposalResponse = await connection.abciQuery(`/vp/governance/proposal/${id}`)
if (proposalResponse[0] === 0) return null
const [ votesResponse, resultResponse ] = await Promise.all([
`/vp/governance/proposal/${id}/votes`,
`/vp/governance/stored_proposal_result/${id}`,
].map(x=>connection.abciQuery(x)))
const proposal = connection.decode.gov_proposal(proposalResponse.slice(1)) as
ReturnType<NamadaDecoder["gov_proposal"]>
const votes = connection.decode.gov_votes(votesResponse) as
ReturnType<NamadaDecoder["gov_votes"]>
const result = (resultResponse[0] === 0) ? null
: decodeResultResponse(connection.decode.gov_result(resultResponse.slice(1)) as
Required<ReturnType<NamadaDecoder["gov_result"]>>)
return { id: BigInt(id), proposal, votes, result }
connection: Connection, id: number|bigint
): Promise<ReturnType<NamadaDecoder["gov_proposal"]>|null> {
const query = `/vp/governance/proposal/${id}`
const response = await connection.abciQuery(query)
if (response[0] === 0) return null
const decoded = connection.decode.gov_proposal(response.slice(1))
return decoded as ReturnType<NamadaDecoder["gov_proposal"]>
}

export async function fetchProposalVotes (
connection: Connection, id: number|bigint
): Promise<ReturnType<NamadaDecoder["gov_votes"]>> {
const query = `/vp/governance/proposal/${id}/votes`
const response = await connection.abciQuery(query)
const decoded = connection.decode.gov_votes(response)
return decoded as ReturnType<NamadaDecoder["gov_votes"]>
}

export async function fetchProposalResult (
connection: Connection, id: number|bigint
): Promise<NamadaGovernanceProposalResult|null> {
const query = `/vp/governance/stored_proposal_result/${id}`
const response = await connection.abciQuery(query)
if (response[0] === 0) return null
const decoded = connection.decode.gov_result(response.slice(1))
const results = decodeResultResponse(decoded as Required<typeof decoded>)
return results as NamadaGovernanceProposalResult
}

export async function fetchProposalWasm (
connection: Pick<Connection, 'abciQuery'|'decode'>, id: number|bigint
connection: Connection, id: number|bigint
): Promise<NamadaGovernanceProposalWasm|null> {
id = BigInt(id)
const codeKey = connection.decode.gov_proposal_code_key(BigInt(id))
Expand Down Expand Up @@ -70,9 +87,6 @@ const decodeResultResponse = (
abstainPercent: (turnout > 0) ? percent(decoded.totalAbstainPower!, turnout) : '0',
})

export const INTERNAL_ADDRESS =
"tnam1q5qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqrw33g6"

interface NamadaGovernanceProposal {
readonly id: bigint
readonly proposal: ReturnType<NamadaDecoder["gov_proposal"]>
Expand Down Expand Up @@ -102,10 +116,12 @@ interface NamadaGovernanceProposalResult {

const percent = (a: string|number|bigint, b: string|number|bigint) =>
((Number(BigInt(a) * 1000000n / BigInt(b)) / 10000).toFixed(2) + '%')

const percent2 = (a: string|number|bigint, b: string|number|bigint) =>
((Number(BigInt(a) * 1000000n / BigInt(b)) / 1000000).toFixed(2) + '%')

export {
export type {
NamadaGovernanceProposal as Proposal,
NamadaGovernanceProposalWasm as ProposalWasm,
NamadaGovernanceProposalResult as ProposalResult,
}
11 changes: 6 additions & 5 deletions packages/namada/NamadaPGF.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { assign } from '@hackbg/fadroma'
import type NamadaConnection from './NamadaConnection'

export type Params = Awaited<ReturnType<typeof fetchPGFParameters>>

export async function fetchPGFParameters (connection: Pick<NamadaConnection, 'abciQuery'|'decode'>) {
type Connection = Pick<NamadaConnection, 'abciQuery'|'decode'>

export async function fetchPGFParameters (connection: Connection) {
const binary = await connection.abciQuery(`/vp/pgf/parameters`)
return connection.decode.pgf_parameters(binary)
}

export async function fetchPGFStewards (connection: Pick<NamadaConnection, 'abciQuery'|'decode'>) {
export async function fetchPGFStewards (connection: Connection) {
throw new Error("not implemented")
}

export async function fetchPGFFundings (connection: Pick<NamadaConnection, 'abciQuery'|'decode'>) {
export async function fetchPGFFundings (connection: Connection) {
throw new Error("not implemented")
}

export async function isPGFSteward (connection: Pick<NamadaConnection, 'abciQuery'|'decode'>) {
export async function isPGFSteward (connection: Connection, address: string) {
throw new Error("not implemented")
}

0 comments on commit 09047ad

Please sign in to comment.