Skip to content

Commit

Permalink
wip: refactor(cw): converting to @hackbg/borshest, pt.2
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Mar 12, 2024
1 parent b94bc57 commit 2d37293
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 162 deletions.
18 changes: 9 additions & 9 deletions packages/cw/namada/namada-address.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Core } from '@fadroma/agent'
import * as Borsher from 'borsher'
import { Schema, schemaEnum } from './namada-types'
import { array, u8, variants } from '@hackbg/borshest'
import type { Fields } from '@hackbg/borshest'

export const InternalAddresses = {
Governance: "tnam1q5qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqrw33g6"
Expand All @@ -17,11 +17,11 @@ export type Address =
| { Implicit: number[] }
| { Internal: {} }

export const addressSchema = Schema.Array(Schema.u8, 21)
export const addr = array(21, u8)

const twentyBytesSchema = Schema.Array(Schema.u8, 20)
const twentyBytesSchema = array(20, u8)

const rawAddressSchema = schemaEnum([
const rawAddressSchema = variants(...[
'Implicit', // 0 // FIXME: switched around
'Established', // 1 // FIXME: switched around
'Internal_PoS', // 2
Expand All @@ -37,7 +37,7 @@ const rawAddressSchema = schemaEnum([
'Internal_Nut', // 12
'Internal_IbcToken', // 13
'Internal_Masp', // 14
].map(variant=>[variant, twentyBytesSchema]))
].map(variant=>[variant, twentyBytesSchema]) as Fields)

export const decodeAddress = (address: number[]|Uint8Array) => {
if (!(
Expand All @@ -61,7 +61,7 @@ export const decodeAddress = (address: number[]|Uint8Array) => {
//if (Object.keys(address).length !== 1) {
//throw new Core.Error("address variant must have exactly 1 key")
//}
//return Core.bech32m.encode('tnam', Core.bech32m.toWords(Borsher.borshSerialize(addressSchema, address)))
//return Core.bech32m.encode('tnam', Core.bech32m.toWords(Borsher.borshSerialize(addr, address)))
//}

export function decodeAddressFields <T> (object: T, fields: (keyof T)[]) {
Expand All @@ -79,11 +79,11 @@ export function decodeAddressFields <T> (object: T, fields: (keyof T)[]) {
//if (Object.keys(address).length !== 1) {
//throw new Core.Error("address variant must have exactly 1 key")
//}
//return Core.bech32m.encode('tnam', Core.bech32m.toWords(Borsher.borshSerialize(addressSchema, address)))
//return Core.bech32m.encode('tnam', Core.bech32m.toWords(Borsher.borshSerialize(addr, address)))
//}


//export const addressSchema = schemaEnum([
//export const addr = schemaEnum([
//'Implicit', // 0 // FIXME: switched around
//'Established', // 1 // FIXME: switched around
//'Internal_PoS', // 2
Expand Down
4 changes: 3 additions & 1 deletion packages/cw/namada/namada-epoch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ export async function getCurrentEpoch (connection: Connection) {
return decode(epochSchema, binary)
}

const epochSchema = struct(["epoch", u64])
const epochSchema = struct(
["epoch", u64]
)
16 changes: 8 additions & 8 deletions packages/cw/namada/namada-gov.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 { addr, InternalAddresses, decodeAddressFields } from './namada-address'
import {
decode, Struct,
map, set, vec, option, struct, variants, u256, u64, string, unit
Expand Down Expand Up @@ -57,7 +57,7 @@ const addRemove = t => variants(

const pgfTarget = variants(
["Internal", struct(
["target", addressSchema],
["target", addr],
["amount", u256],
)],
["Ibc", struct(
Expand All @@ -71,10 +71,10 @@ const pgfTarget = variants(
export class Proposal extends Struct(
["id", u64],
["content", map(string, string)],
["author", addressSchema],
["author", addr],
["type", variants(
["Default", option(string)],
["PGFSteward", set(addRemove(addressSchema))],
["PGFSteward", set(addRemove(addr))],
["PGFPayment", set(variants(
["Continuous", addRemove(pgfTarget)],
["Retro", pgfTarget],
Expand Down Expand Up @@ -140,8 +140,8 @@ const voteValueSchema = variants(
)

const voteSchema = struct(
["validator", addressSchema],
["delegator", addressSchema],
["validator", addr],
["delegator", addr],
["data", voteValueSchema],
)

Expand Down Expand Up @@ -207,8 +207,8 @@ export class InitProposal extends Struct() {
export class VoteProposal extends Struct(
['id', u64],
['vote', voteValueSchema],
['voter', addressSchema],
['delegations', vec(addressSchema)]
['voter', addr],
['delegations', vec(addr)]
) {
declare id: bigint
declare vote
Expand Down
24 changes: 5 additions & 19 deletions packages/cw/namada/namada-pgf.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { Core } from '@fadroma/agent'
import * as Borsher from 'borsher'
import type { Address } from './namada-address'
import { addressSchema } from './namada-address'
import {
schemaEnum,
enumVariant,
u256Schema,
i256Schema,
decodeU256Fields
} from './namada-types'
import { addr } from './namada-address'
import { Struct, set, u256, i256, map } from '@hackbg/borshest'

type Connection = { abciQuery: (path: string)=>Promise<Uint8Array> }
Expand All @@ -19,20 +12,13 @@ export async function getPGFParameters (connection: Connection) {
}

class PGFParameters extends Struct(
["stewards", set(addressSchema)],
["stewards", set(addr)],
["pgf_inflation_rate", u256],
["stewards_inflation_rate", u256],
) {
declare stewards: Set<Address>
declare pgfInflationRate: bigint
declare stewardsInflationRate: bigint
constructor (data) {
super(data)
decodeU256Fields(this, [
"pgfInflationRate",
"stewardsInflationRate"
])
}
}

export async function getPGFStewards (connection: Connection) {
Expand All @@ -52,15 +38,15 @@ export async function isPGFSteward (connection: Connection) {
}

export class UpdateStewardCommission extends Struct(
['steward', addressSchema],
['commission', map(addressSchema, i256)]
['steward', addr],
['commission', map(addr, i256)]

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

View workflow job for this annotation

GitHub Actions / PNPM CI

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

export class ResignSteward extends Struct(
["steward", addressSchema],
["steward", addr],
) {
declare steward: Address
}
Expand Down
64 changes: 29 additions & 35 deletions packages/cw/namada/namada-pos.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import * as Borsher from 'borsher'
import type { Address } from '@fadroma/agent'
import { Core } from '@fadroma/agent'
import { addressSchema, InternalAddresses, decodeAddress } from './namada-address'
import { addr, InternalAddresses, decodeAddress } from './namada-address'
import type { Address as NamadaAddress } from './namada-address'
import {
Schema,
i256Schema,
decodeU256,
decodeU256Fields
} from './namada-types'
import type { NamadaConnection } from './namada-connection'
import * as Staking from '../cw-staking'
import {
Expand Down Expand Up @@ -42,10 +36,10 @@ const ownedPosParamsFields: Array<[string, AnyField]> = [
["rewardsGainD", u256],
]

export class PosParams extends Struct([
["owned", struct(...ownedPosParamsFields)],
export class PosParams extends Struct(
["owned", struct(...ownedPosParamsFields)],
["maxProposalPeriod", u64],
]) {
) {
declare maxProposalPeriod: bigint
declare owned: OwnedPosParams
constructor (data) {
Expand All @@ -56,7 +50,7 @@ export class PosParams extends Struct([
}
}

class OwnedPosParams extends Struct(ownedPosParamsFields) {
class OwnedPosParams extends Struct(...ownedPosParamsFields) {
maxValidatorSlots!: bigint
pipelineLen!: bigint
unbondingLen!: bigint
Expand Down Expand Up @@ -137,7 +131,7 @@ export class NamadaValidator extends Staking.Validator {
connection.abciQuery(`/vp/pos/validator/state/${this.namadaAddress}`)
.then(binary => this.state = decode(stateSchema, binary)),
connection.abciQuery(`/vp/pos/validator/stake/${this.namadaAddress}`)
.then(binary => this.stake = decodeU256(decode(stakeSchema, binary))),
.then(binary => this.stake = decode(stakeSchema, binary)),
]
if (this.namadaAddress && !this.publicKey) {
requests.push(connection.abciQuery(`/vp/pos/validator/consensus_key/${this.namadaAddress}`)

Check failure on line 137 in packages/cw/namada/namada-pos.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Argument of type 'Promise<string | bigint | object>' is not assignable to parameter of type 'Promise<object> | Promise<bigint>'.
Expand Down Expand Up @@ -174,7 +168,7 @@ export async function getValidatorAddresses (connection: NamadaConnection): Prom
.map(bytes=>decodeAddress(bytes))
}

const getValidatorsSchema = set(addressSchema)
const getValidatorsSchema = set(addr)

export async function getValidatorsConsensus (connection: NamadaConnection) {
const binary = await connection.abciQuery("/vp/pos/validator_set/consensus")
Expand Down Expand Up @@ -204,7 +198,7 @@ export async function getValidatorsBelowCapacity (connection: NamadaConnection)

const validatorSetMemberFields: Array<[string, AnyField]> = [
["bonded_stake", u256],
["address", addressSchema],
["address", addr],
]

const validatorSetSchema = set(struct(...validatorSetMemberFields))
Expand Down Expand Up @@ -259,17 +253,17 @@ const stateSchema = option(variants(

const stakeSchema = option(u256)

const publicKeySchema = option(variants(
const pubkey = option(variants(
['Ed25519', array(32, u8)],
['Secp256k1', array(33, u8)],
))

export class BecomeValidator extends Struct(
["address", addressSchema],
["consensus_key", publicKeySchema],
["eth_cold_key", publicKeySchema],
["eth_hot_key", publicKeySchema],
["protocol_key", publicKeySchema],
["address", addr],
["consensus_key", pubkey],
["eth_cold_key", pubkey],
["eth_hot_key", pubkey],
["protocol_key", pubkey],
["commission_rate", u256],
["max_commission_rate_change", u256],
["email", string],
Expand All @@ -293,41 +287,41 @@ export class BecomeValidator extends Struct(
}

export class Bond extends Struct(
["validator", addressSchema],
["validator", addr],
["amount", u256],
["source", option(addressSchema)],
["source", option(addr)],
) {
validator: Address
amount: bigint
source: null|Address
}

export class ClaimRewards extends Struct(
["validator", addressSchema],
["source", option(addressSchema)],
["validator", addr],
["source", option(addr)],
) {
validator: Address
source: null|Address
}

export class ConsensusKeyChange extends Struct(
["validator", addressSchema],
["consensus_key", publicKeySchema],
["validator", addr],
["consensus_key", pubkey],
) {
validator: Address
consensusKey: unknown
}

export class CommissionChange extends Struct(
["validator", addressSchema],
["new_rate", i256Schema],
["validator", addr],
["new_rate", i256],
) {
validator: Address
newRate: bigint
}

export class MetaDataChange extends Struct(
["validator", addressSchema],
["validator", addr],
["email", option(string)],
["description", option(string)],
["website", option(string)],
Expand All @@ -345,10 +339,10 @@ export class MetaDataChange extends Struct(
}

export class Redelegation extends Struct(
["src_validator", addressSchema],
["dest_validator", addressSchema],
["owner", addressSchema],
["amount", i256Schema],
["src_validator", addr],
["dest_validator", addr],
["owner", addr],
["amount", i256],
) {
srcValidator: Address
destValidator: Address
Expand All @@ -359,8 +353,8 @@ export class Redelegation extends Struct(
export class Unbond extends Struct() {}

export class Withdraw extends Struct(
["validator", addressSchema],
["source", option(addressSchema)],
["validator", addr],
["source", option(addr)],
) {
validator: Address
source: null|Address
Expand Down
15 changes: 8 additions & 7 deletions packages/cw/namada/namada-tx-section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
bool
} from '@hackbg/borshest'
import type { Fields } from '@hackbg/borshest'
import { addr } from './namada-address'

const hashSchema = array(32, u8)

const publicKeySchema = variants(
export const pubkey = variants(
['Ed25519', array(32, u8)],
['Secp256k1', array(33, u8)],
)
Expand All @@ -18,16 +19,16 @@ export const wrapperTransactionFields: Fields = [
["amount", u256],
["denomination", u8],
)],
["token", addressSchema],
["token", addr],
)],
["pk", publicKeySchema],
["pk", pubkey],
["epoch", u64],
["gasLimit", u64],
["unshieldSectionHash", option(hashSchema)],
]

export const protocolTransactionFields: Fields = [
["pk", publicKeySchema],
["pk", pubkey],
["tx", variants(
['EthereumEvents', unit],
['BridgePool', unit],
Expand Down Expand Up @@ -180,8 +181,8 @@ export class SignatureSection extends Section {
export const signatureSectionFields: Fields = [
["targets", vec(hashSchema)],
["signer", variants(
['Address', addressSchema],
['PubKeys', vec(publicKeySchema)],
['Address', addr],
['PubKeys', vec(pubkey)],
)],
["signatures", map(u8, variants(

Check failure on line 187 in packages/cw/namada/namada-tx-section.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Argument of type 'Field<bigint>' is not assignable to parameter of type 'Field<string | number | symbol>'.
['Ed25519', array(64, u8)],
Expand Down Expand Up @@ -315,7 +316,7 @@ const merklePathSchema = struct(
export const maspBuilderSectionFields: Fields = [
["hash", hashSchema],
["asset_types", set(struct(
["token", addressSchema],
["token", addr],
["denomination", u8],
["position", variants(
["Zero", unit],
Expand Down
Loading

0 comments on commit 2d37293

Please sign in to comment.