Skip to content

Commit

Permalink
wip: refactor(cw): converting to @hackbg/borshest
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Mar 11, 2024
1 parent 22b5cde commit b94bc57
Show file tree
Hide file tree
Showing 9 changed files with 832 additions and 837 deletions.
7 changes: 3 additions & 4 deletions packages/cw/namada/namada-epoch.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import * as Borsher from 'borsher'
import { decode, struct, u64 } from '@hackbg/borshest'

const Schema = Borsher.BorshSchema

type Connection = { abciQuery: (path: string) => Promise<Uint8Array> }

export async function getCurrentEpoch (connection: Connection) {
const binary = await connection.abciQuery("/shell/epoch")
return Borsher.borshDeserialize(epochSchema, binary)
return decode(epochSchema, binary)
}

const epochSchema = Schema.Struct({
epoch: Schema.u64
})
const epochSchema = struct(["epoch", u64])
232 changes: 107 additions & 125 deletions packages/cw/namada/namada-gov.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
import * as Borsher from 'borsher'
import { Core } from '@fadroma/agent'
import type { Address } from '@fadroma/agent'
import type { Address as NamadaAddress } from './namada-address'
import { addressSchema, InternalAddresses, decodeAddressFields } from './namada-address'
import {
Schema, fromBorshStruct, schemaEnum, enumVariant, u256Schema, decodeU256Fields
} from './namada-types'
decode, Struct,
map, set, vec, option, struct, variants, u256, u64, string, unit
} from '@hackbg/borshest'

type Connection = { abciQuery: (path: string)=>Promise<Uint8Array> }

export async function getGovernanceParameters (connection: Connection) {
const binary = await connection.abciQuery(`/vp/governance/parameters`)
return GovernanceParameters.fromBorsh(binary) as GovernanceParameters
return GovernanceParameters.decode(binary) as GovernanceParameters
}

export class GovernanceParameters extends fromBorshStruct({
min_proposal_fund: u256Schema,
max_proposal_code_size: Schema.u64,
min_proposal_voting_period: Schema.u64,
max_proposal_period: Schema.u64,
max_proposal_content_size: Schema.u64,
min_proposal_grace_epochs: Schema.u64
}) {
minProposalFund!: bigint
maxProposalCodeSize!: bigint
minProposalVotingPeriod!: bigint
maxProposalPeriod!: bigint
maxProposalContentSize!: bigint
minProposalGraceEpochs!: bigint
constructor (data) {
super(data)
decodeU256Fields(this, ["minProposalFund"])
}
export class GovernanceParameters extends Struct(
["minProposalFund", u256],
["maxProposalCodeSize", u64],
["minProposalVotingPeriod", u64],
["maxProposalPeriod", u64],
["maxProposalContentSize", u64],
["minProposalGraceEpochs", u64],
) {
declare minProposalFund: bigint
declare maxProposalCodeSize: bigint
declare minProposalVotingPeriod: bigint
declare maxProposalPeriod: bigint
declare maxProposalContentSize: bigint
declare minProposalGraceEpochs: bigint
}

export async function getProposalCount (connection: Connection) {
const binary = await connection.abciQuery(`/shell/value/#${InternalAddresses.Governance}/counter`)
return Borsher.borshDeserialize(Schema.u64, binary) as bigint
return decode(u64, binary) as bigint
}

export async function getProposalInfo (connection: Connection, id: number) {
Expand All @@ -46,59 +42,55 @@ export async function getProposalInfo (connection: Connection, id: number) {
connection.abciQuery(`/vp/governance/stored_proposal_result/${id}`),
])
return {
proposal: Proposal.fromBorsh(proposal) as Proposal,
proposal: Proposal.decode(proposal) as Proposal,
votes: ProposalVotes.fromBorsh(votes) as ProposalVotes,
result: (result.length === 1)
? null
: ProposalResult.fromBorsh(result) as ProposalResult,
: ProposalResult.decode(result) as ProposalResult,
}
}

const addRemoveSchema = t => Schema.Enum({
Add: t,
Remove: t,
})

const pgfTargetSchema = Schema.Enum({
Internal: Schema.Struct({
target: addressSchema,
amount: u256Schema,
}),
Ibc: Schema.Struct({
target: Schema.String,
amount: u256Schema,
port_id: Schema.String,
channel_id: Schema.String,
})
})

export class Proposal extends fromBorshStruct({
id: Schema.u64,
content: Schema.HashMap(Schema.String, Schema.String),
author: addressSchema,
type: Schema.Enum({
Default: Schema.Option(Schema.String),
PGFSteward: Schema.HashSet(addRemoveSchema(addressSchema)),
PGFPayment: Schema.HashSet(Schema.Enum({
Continuous: addRemoveSchema(pgfTargetSchema),
Retro: pgfTargetSchema,
}))
}),
voting_start_epoch: Schema.u64,
voting_end_epoch: Schema.u64,
grace_epoch: Schema.u64,
}) {
id!: string
content!: Map<string, string>
author!: string
type!: unknown
votingStartEpoch!: bigint
votingEndEpoch!: bigint
graceEpoch!: bigint
constructor (data) {
super(data)
decodeAddressFields(this, ["author"])
}
const addRemove = t => variants(
["Add", t],
["Remove", t]
)

const pgfTarget = variants(
["Internal", struct(
["target", addressSchema],

Check failure on line 60 in packages/cw/namada/namada-gov.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Type 'BorshSchema' is not assignable to type 'AnyField'.
["amount", u256],
)],
["Ibc", struct(
["target", string],
["amount", u256],
["portId", string],
["channelId", string],
)]
)

export class Proposal extends Struct(
["id", u64],
["content", map(string, string)],
["author", addressSchema],
["type", variants(
["Default", option(string)],
["PGFSteward", set(addRemove(addressSchema))],
["PGFPayment", set(variants(
["Continuous", addRemove(pgfTarget)],
["Retro", pgfTarget],
))]
)],
["votingStartEpoch", u64],
["votingEndEpoch", u64],
["grace_epoch", u64],
) {
declare id: string
declare content: Map<string, string>
declare author: string
declare type: unknown
declare votingStartEpoch: bigint
declare votingEndEpoch: bigint
declare graceEpoch: bigint
}

export class ProposalVotes extends Array<Vote> {
Expand Down Expand Up @@ -141,63 +133,53 @@ export class Vote {
}
}

const voteValueSchema = schemaEnum([
['Yay', Schema.Unit],
['Nay', Schema.Unit],
['Abstain', Schema.Unit],
])
const voteValueSchema = variants(
['Yay', unit],
['Nay', unit],
['Abstain', unit],
)

const voteSchema = Schema.Struct({
validator: addressSchema,
delegator: addressSchema,
data: voteValueSchema,
})
const voteSchema = struct(
["validator", addressSchema],

Check failure on line 143 in packages/cw/namada/namada-gov.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Type 'BorshSchema' is not assignable to type 'AnyField'.
["delegator", addressSchema],
["data", voteValueSchema],
)

const proposalStatusSchema = Schema.Enum({
Pending: Schema.Unit,
OnGoing: Schema.Unit,
Ended: Schema.Unit,
})
const proposalStatusSchema = variants(
["Pending", unit],
["OnGoing", unit],
["Ended", unit],
)

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

export class ProposalResult extends fromBorshStruct({
result: Schema.Enum({
Passed: Schema.Unit,
Rejected: Schema.Unit,
}),
tally_type: Schema.Enum({
TwoThirds: Schema.Unit,
OneHalfOverOneThird: Schema.Unit,
LessOneHalfOverOneThirdNay: Schema.Unit,
}),
total_voting_power: u256Schema,
total_yay_power: u256Schema,
total_nay_power: u256Schema,
total_abstain_power: u256Schema,
}) {
result!:
export class ProposalResult extends Struct(
["result", variants(
["Passed", unit],
["Rejected", unit],
)],
["tallyType", variants(
["TwoThirds", unit],
["OneHalfOverOneThird", unit],
["LessOneHalfOverOneThirdNay", unit],
)],
["totalVotingPower", u256],
["totalYayPower", u256],
["totalNayPower", u256],
["totalAbstainPower", u256],
) {
declare result:
| { Passed: {} }
| { Rejected: {} }
tallyType!:
| { TwoThirds: {} }
| { OneHalfOverOneThird: {} }
declare tallyType:
| { TwoThirds: {} }
| { OneHalfOverOneThird: {} }
| { LessOneHalfOverOneThirdNay: {} }
totalVotingPower!: bigint
totalYayPower!: bigint
totalNayPower!: bigint
totalAbstainPower!: bigint

constructor (data) {
super(data)
decodeU256Fields(this, [
"totalVotingPower",
"totalYayPower",
"totalNayPower",
"totalAbstainPower",
])
}
declare totalVotingPower: bigint
declare totalYayPower: bigint
declare totalNayPower: bigint
declare totalAbstainPower: bigint

get turnout () {
return this.totalYayPower + this.totalNayPower + this.totalAbstainPower
Expand All @@ -216,18 +198,18 @@ export class ProposalResult extends fromBorshStruct({
}
}

export class InitProposal extends fromBorshStruct({}) {
export class InitProposal extends Struct() {
print (console) {
throw new Error('print InitProposal: not implemented')
}
}

export class VoteProposal extends fromBorshStruct({
id: Schema.u64,
vote: voteValueSchema,
voter: addressSchema,
delegations: Schema.Vec(addressSchema)
}) {
export class VoteProposal extends Struct(
['id', u64],
['vote', voteValueSchema],
['voter', addressSchema],
['delegations', vec(addressSchema)]

Check failure on line 211 in packages/cw/namada/namada-gov.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Argument of type 'BorshSchema' is not assignable to parameter of type 'Field<unknown>'.
) {
declare id: bigint
declare vote
declare voter
Expand Down
45 changes: 23 additions & 22 deletions packages/cw/namada/namada-pgf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@ import * as Borsher from 'borsher'
import type { Address } from './namada-address'
import { addressSchema } from './namada-address'
import {
Schema,
fromBorshStruct,
schemaEnum,
enumVariant,
u256Schema,
i256Schema,
decodeU256Fields
} from './namada-types'
import { Struct, set, u256, i256, map } from '@hackbg/borshest'

type Connection = { abciQuery: (path: string)=>Promise<Uint8Array> }

export async function getPGFParameters (connection: Connection) {
const binary = await connection.abciQuery(`/vp/pgf/parameters`)
return PGFParameters.fromBorsh(binary) as PGFParameters
return PGFParameters.decode(binary) as PGFParameters
}

class PGFParameters extends fromBorshStruct({
stewards: Schema.HashSet(addressSchema),
pgf_inflation_rate: u256Schema,
stewards_inflation_rate: u256Schema,
}) {
stewards!: Set<Address>
pgfInflationRate!: bigint
stewardsInflationRate!: bigint
class PGFParameters extends Struct(
["stewards", set(addressSchema)],

Check failure on line 22 in packages/cw/namada/namada-pgf.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Argument of type 'BorshSchema' is not assignable to parameter of type 'Field<unknown>'.
["pgf_inflation_rate", u256],
["stewards_inflation_rate", u256],
) {
declare stewards: Set<Address>
declare pgfInflationRate: bigint
declare stewardsInflationRate: bigint
constructor (data) {
super(data)
decodeU256Fields(this, [
Expand All @@ -40,29 +39,31 @@ export async function getPGFStewards (connection: Connection) {
throw new Error("not implemented")
}

class PGFSteward extends fromBorshStruct({}) { /*TODO*/ }
class PGFSteward extends Struct() { /*TODO*/ }

export async function getPGFFundings (connection: Connection) {
throw new Error("not implemented")
}

class PGFFunding extends fromBorshStruct({}) { /*TODO*/ }
class PGFFunding extends Struct() { /*TODO*/ }

export async function isPGFSteward (connection: Connection) {
throw new Error("not implemented")
}

export class UpdateStewardCommission extends fromBorshStruct({
steward: addressSchema,
commission: Schema.HashMap(addressSchema, i256Schema)
}) {
steward: Address
commission: Map<string, bigint>
export class UpdateStewardCommission extends Struct(
['steward', addressSchema],
['commission', map(addressSchema, i256)]

Check failure on line 56 in packages/cw/namada/namada-pgf.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Argument of type 'BorshSchema' is not assignable to parameter of type 'Field<string | number | symbol>'.
) {
declare steward: Address
declare commission: Map<string, bigint>
}

export class ResignSteward extends fromBorshStruct({
steward: addressSchema,
}) {}
export class ResignSteward extends Struct(
["steward", addressSchema],
) {
declare steward: Address
}

export {
PGFParameters as Parameters,
Expand Down
Loading

0 comments on commit b94bc57

Please sign in to comment.