Skip to content

Commit

Permalink
refactor(namada): replace redundant class defs with interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Jun 26, 2024
1 parent fe499e9 commit 80509a7
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 126 deletions.
86 changes: 67 additions & 19 deletions packages/namada/NamadaDecode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,64 @@ export async function initDecoder (decoder: string|URL|Uint8Array): Promise<void
export { Decode }

export interface NamadaDecoder {
u64 (_: Uint8Array): bigint
address_to_amount (_: Uint8Array): Record<string, bigint>
addresses (_: Uint8Array): string[]
address (_: Uint8Array): string
epoch_duration (_: Uint8Array): { minNumOfBlocks: number, minDuration: number }
gas_cost_table (_: Uint8Array): Record<string, string>
gov_parameters (_: Uint8Array): Partial<Gov.Parameters>
gov_proposal (_: Uint8Array): Partial<Gov.Proposal>
gov_votes (_: Uint8Array): Partial<Gov.Vote>[]
gov_result (_: Uint8Array): Partial<Gov.ProposalResult>
pgf_parameters (_: Uint8Array): Partial<PGF.Parameters>
pos_validator_set (_: Uint8Array): { bondedStake: number|bigint }[]
pos_parameters (_: Uint8Array): Partial<{
u64 (_: Uint8Array): bigint

address_to_amount (_: Uint8Array): Record<string, bigint>
addresses (_: Uint8Array): string[]
address (_: Uint8Array): string

epoch_duration (_: Uint8Array): {
minNumOfBlocks: number,
minDuration: number
}

gas_cost_table (_: Uint8Array): Record<string, string>

gov_parameters (_: Uint8Array): Partial<{
minProposalFund: bigint
maxProposalCodeSize: bigint
minProposalVotingPeriod: bigint
maxProposalPeriod: bigint
maxProposalContentSize: bigint
minProposalGraceEpochs: bigint
}>

gov_proposal (_: Uint8Array): Partial<{
id: string
content: Map<string, string>
author: string
type: unknown
votingStartEpoch: bigint
votingEndEpoch: bigint
graceEpoch: bigint
}>

gov_votes (_: Uint8Array): Partial<{
validator: string
delegator: string
data: "Yay"|"Nay"|"Abstain"
}>[]

gov_result (_: Uint8Array): Partial<{
result: "Passed"|"Rejected"
tallyType: "TwoThirds"|"OneHalfOverOneThird"|"LessOneHalfOverOneThirdNay"
totalVotingPower: bigint
totalYayPower: bigint
totalNayPower: bigint
totalAbstainPower: bigint
}>

pgf_parameters (_: Uint8Array): Partial<{
stewards: Set<string>
pgfInflationRate: bigint
stewardsInflationRate: bigint
}>

pos_validator_set (_: Uint8Array): {
bondedStake: number|bigint
}[]

pos_parameters (_: Uint8Array): Partial<{
maxProposalPeriod: bigint
maxValidatorSlots: bigint
pipelineLen: bigint
Expand All @@ -45,11 +90,14 @@ export interface NamadaDecoder {
rewardsGainP: bigint
rewardsGainD: bigint
}>

pos_validator_state (_: Uint8Array): unknown
storage_keys ():
{ epochDuration: string
, maxBlockDuration: string
, maxGasForBlock: string
, feeUnshieldingGasLimit: string
, gasCostTable: string }

storage_keys (): {
epochDuration: string,
maxBlockDuration: string,
maxGasForBlock: string,
feeUnshieldingGasLimit: string,
gasCostTable: string
}
}
107 changes: 30 additions & 77 deletions packages/namada/NamadaGov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,51 @@ import { assign } from '@hackbg/fadroma'
import type { Address } from '@hackbg/fadroma'
import { decode, u64 } from '@hackbg/borshest'
import type Connection from './NamadaConnection'
import type { NamadaDecoder } from './NamadaDecode'

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

export async function fetchGovernanceParameters (connection: Pick<Connection, 'abciQuery'|'decode'>) {
const binary = await connection.abciQuery(`/vp/governance/parameters`)
return new GovernanceParameters(connection.decode.gov_parameters(binary))
return connection.decode.gov_parameters(binary)
}

class GovernanceParameters {
minProposalFund!: bigint
maxProposalCodeSize!: bigint
minProposalVotingPeriod!: bigint
maxProposalPeriod!: bigint
maxProposalContentSize!: bigint
minProposalGraceEpochs!: bigint
constructor (properties: Partial<GovernanceParameters> = {}) {
assign(this, properties, [
'minProposalFund',
'maxProposalCodeSize',
'minProposalVotingPeriod',
'maxProposalPeriod',
'maxProposalContentSize',
'minProposalGraceEpochs',
])
}
export async function fetchProposalCount (connection: Pick<Connection, 'abciQuery'>) {
const binary = await connection.abciQuery(`/shell/value/#${INTERNAL_ADDRESS}/counter`)
return decode(u64, binary) as bigint
}

export type ProposalInfo = Awaited<ReturnType<typeof fetchProposalInfo>>

export async function fetchProposalInfo (
connection: Pick<Connection, 'abciQuery'|'decode'>, id: number|bigint
) {
const proposal = await connection.abciQuery(`/vp/governance/proposal/${id}`)
if (proposal[0] === 0) {
const proposalResponse = await connection.abciQuery(`/vp/governance/proposal/${id}`)
if (proposalResponse[0] === 0) {
return null
}
const [ votes, result ] = await Promise.all([
connection.abciQuery(`/vp/governance/proposal/${id}/votes`),
connection.abciQuery(`/vp/governance/stored_proposal_result/${id}`),
])
return {
proposal: new GovernanceProposal(
connection.decode.gov_proposal(proposal.slice(1))
),
votes: connection.decode.gov_votes(votes).map(
vote=>new GovernanceVote(vote)
),
result: (result[0] === 0) ? null : new GovernanceProposalResult(
connection.decode.gov_result(result.slice(1))
)
}
}

class GovernanceProposal {
id!: string
content!: Map<string, string>
author!: string
type!: unknown
votingStartEpoch!: bigint
votingEndEpoch!: bigint
graceEpoch!: bigint
constructor (properties: Partial<GovernanceProposal> = {}) {
assign(this, properties, [
'id',
'content',
'author',
'type',
'votingStartEpoch',
'votingEndEpoch',
'graceEpoch',
])
}
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))
const votes =
connection.decode.gov_votes(votesResponse)
const result: GovernanceProposalResult|null =
(resultResponse[0] === 0)
? null
: new GovernanceProposalResult(
connection.decode.gov_result(resultResponse.slice(1))
)
return { proposal, votes, result }
}

class GovernanceProposalResult {
class GovernanceProposalResult implements ReturnType<NamadaDecoder["gov_result"]> {
result!: "Passed"|"Rejected"
tallyType!: "TwoThirds"|"OneHalfOverOneThird"|"LessOneHalfOverOneThirdNay"
totalVotingPower!: bigint
Expand Down Expand Up @@ -111,27 +80,11 @@ class GovernanceProposalResult {
}
}

class GovernanceVote {
validator!: Address
delegator!: Address
data!: "Yay"|"Nay"|"Abstain"
constructor (properties: Partial<GovernanceVote> = {}) {
assign(this, properties, [
'validator',
'delegator',
'data',
])
}
}

const percent = (a: bigint, b: bigint) =>
((Number(a * 1000000n / b) / 10000).toFixed(2) + '%').padStart(7)

export {
GovernanceParameters as Parameters,
GovernanceProposal as Proposal,
GovernanceProposalResult as ProposalResult,
GovernanceVote as Vote,
}

export const INTERNAL_ADDRESS = "tnam1q5qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqrw33g6"
20 changes: 1 addition & 19 deletions packages/namada/NamadaPGF.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
import { assign } from '@hackbg/fadroma'
import type NamadaConnection from './NamadaConnection'

interface PGFParameters {
stewards: Set<string>
pgfInflationRate: bigint
stewardsInflationRate: bigint
}

interface PGFSteward {
/*TODO*/
}

interface PGFFunding {
/*TODO*/
}

export {
PGFParameters as Parameters,
PGFSteward as Steward,
PGFFunding as Funding,
}
export type Params = Awaited<ReturnType<typeof fetchPGFParameters>>

export async function fetchPGFParameters (connection: Pick<NamadaConnection, 'abciQuery'|'decode'>) {
const binary = await connection.abciQuery(`/vp/pgf/parameters`)
Expand Down
24 changes: 13 additions & 11 deletions packages/namada/NamadaPoS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ import type {
Connection as NamadaConnection
} from './Namada'

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

export async function fetchStakingParameters (connection: NamadaConnection) {
const binary = await connection.abciQuery("/vp/pos/pos_params")
return connection.decode.pos_parameters(binary)
}

export async function fetchTotalStaked (connection: NamadaConnection) {
const binary = await connection.abciQuery("/vp/pos/total_stake")
return decode(u64, binary)
}

class NamadaValidator extends Staking.Validator {
constructor (properties: Omit<ConstructorParameters<typeof Staking.Validator>[0], 'chain'> & {
chain: Namada, namadaAddress?: string,
Expand Down Expand Up @@ -44,17 +56,7 @@ class NamadaValidator extends Staking.Validator {
}
}

export { NamadaValidator as Validator, }

export async function fetchStakingParameters (connection: NamadaConnection) {
const binary = await connection.abciQuery("/vp/pos/pos_params")
return connection.decode.pos_parameters(binary)
}

export async function fetchTotalStaked (connection: NamadaConnection) {
const binary = await connection.abciQuery("/vp/pos/total_stake")
return decode(u64, binary)
}
export { NamadaValidator as Validator }

export async function fetchValidators (
chain: Namada,
Expand Down

0 comments on commit 80509a7

Please sign in to comment.