Skip to content

Commit

Permalink
breaking: wip: fix(namada): switching block/tx to bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Jul 4, 2024
1 parent ddf9ef7 commit f4d5ff1
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 256 deletions.
37 changes: 17 additions & 20 deletions packages/cw/cw-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import { Amino, Proto, CosmWasmClient, SigningCosmWasmClient } from '@hackbg/cos
import type { Block } from '@hackbg/cosmjs-esm'

export class CWChain extends Chain {
constructor (
properties: ConstructorParameters<typeof Chain>[0]
& Pick<CWChain, 'coinType'|'bech32Prefix'|'hdAccountIndex'|'connections'>
) {
super(properties)
this.coinType = properties.coinType
this.bech32Prefix = properties.bech32Prefix
this.hdAccountIndex = properties.hdAccountIndex
this.connections = properties.connections
}

static get Connection () {
return CWConnection
Expand Down Expand Up @@ -47,17 +57,6 @@ export class CWChain extends Chain {
return chain
}

constructor (
properties: ConstructorParameters<typeof Chain>[0]
& Pick<CWChain, 'coinType'|'bech32Prefix'|'hdAccountIndex'|'connections'>
) {
super(properties)
this.coinType = properties.coinType
this.bech32Prefix = properties.bech32Prefix
this.hdAccountIndex = properties.hdAccountIndex
this.connections = properties.connections
}

/** The bech32 prefix for the account's address */
bech32Prefix: string
/** The coin type in the HD derivation path */
Expand Down Expand Up @@ -153,17 +152,16 @@ export class CWConnection extends Connection {
})
}

async fetchBlockImpl (parameter?: { height: number }|{ hash: string }):
async fetchBlockImpl (parameter?: { height: number|bigint }|{ hash: string }):
Promise<CWBlock>
{
const api = await this.api
if ((parameter as { height: number })?.height) {
if ((parameter as { height: number|bigint })?.height) {
const { id, header, txs } = await api.getBlock((parameter as { height: number }).height)
return new CWBlock({
chain: this.chain,
id,
height: header.height,
timestamp: header.time,
hash: id,
height: BigInt(header.height),
transactions: [],
rawTransactions: txs as Uint8Array[],
})
Expand All @@ -173,9 +171,8 @@ export class CWConnection extends Connection {
const { id, header, txs } = await api.getBlock()
return new CWBlock({
chain: this.chain,
id,
height: header.height,
timestamp: header.time,
hash: id,
height: BigInt(header.height),
transactions: [],
rawTransactions: txs as Uint8Array[],
})
Expand All @@ -184,7 +181,7 @@ export class CWConnection extends Connection {

async fetchHeightImpl () {
const { height } = await this.fetchBlockImpl()
return height
return height!
}

/** Query native token balance. */
Expand Down
37 changes: 16 additions & 21 deletions packages/cw/cw-tx.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
import { Block, Batch } from '@hackbg/fadroma'
import { CWError as Error } from './cw-base'
import { Chain } from '@hackbg/fadroma'
import { Transaction } from '@hackbg/fadroma'
import type { CWChain, CWConnection } from './cw-connection'
import type { CWAgent } from './cw-identity'

type CWBlockParameters =
ConstructorParameters<typeof Block>[0] & Partial<Pick<CWBlock, 'rawTransactions'>>

export class CWBlock extends Block {
constructor (
properties: ConstructorParameters<typeof Block>[0] & Partial<Pick<CWBlock, 'rawTransactions'>>
) {
super(properties)
if (properties.rawTransactions) {
this.rawTransactions = properties.rawTransactions
}
constructor (props: CWBlockParameters) {
super(props)
this.rawTransactions = props?.rawTransactions
}
/** Undecoded transactions. */
rawTransactions?: Uint8Array[]
readonly rawTransactions?: Uint8Array[]
}

/** Transaction batch for CosmWasm-enabled chains. */
export class CWBatch extends Batch {
declare agent: CWAgent

upload (
code: Parameters<Batch["upload"]>[0],
options: Parameters<Batch["upload"]>[1],
) {
upload (...args: Parameters<Batch["upload"]>) {
throw new Error("CWBatch#upload: not implemented")
return this
}
instantiate (
code: Parameters<Batch["instantiate"]>[0],
options: Parameters<Batch["instantiate"]>[1]
) {
instantiate (...args: Parameters<Batch["instantiate"]>) {
throw new Error("CWBatch#instantiate: not implemented")
return this
}
execute (
contract: Parameters<Batch["execute"]>[0],
options: Parameters<Batch["execute"]>[1]
) {
execute (...args: Parameters<Batch["execute"]>) {
throw new Error("CWBatch#execute: not implemented")
return this
}
async submit () {}
async submit () {
throw new Error("CWBatch#submit: not implemented")
}
}

export class CWTransaction extends Transaction {}
5 changes: 3 additions & 2 deletions packages/cw/cw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export {
CWConnection as Connection,
} from './cw-connection'
export {
CWBlock as Block,
CWBatch as Batch,
CWBlock as Block,
CWTransaction as Transaction,
CWBatch as Batch,
} from './cw-tx'
export {
CWIdentity as Identity,
Expand Down
29 changes: 15 additions & 14 deletions packages/namada/Namada.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import NamadaConsole from './NamadaConsole'
import NamadaChain from './NamadaChain'
import NamadaConnection from './NamadaConnection'
import NamadaTransaction from './NamadaTransaction'
import Console from './NamadaConsole'
import Chain from './NamadaChain'
import Connection from './NamadaConnection'
import Block, { Transaction } from './NamadaBlock'
import { Decode, initDecoder } from './NamadaDecode'
import * as Identity from './NamadaIdentity'
export {
Decode,
initDecoder,
NamadaConsole as Console,
NamadaChain as Chain,
NamadaConnection as Connection,
NamadaTransaction as Transaction,
Console,
Chain,
Connection,
Block,
Transaction,
Identity
}
export const testnetChainId = NamadaChain.testnetChainId
export const testnetURLs = NamadaChain.testnetURLs
export function connect (...args: Parameters<typeof NamadaChain.connect>) {
return NamadaChain.connect(...args)
export const testnetChainId = Chain.testnetChainId
export const testnetURLs = Chain.testnetURLs
export function connect (...args: Parameters<typeof Chain.connect>) {
return Chain.connect(...args)
}
export function testnet (...args: Parameters<typeof NamadaChain.testnet>) {
return NamadaChain.testnet(...args)
export function testnet (...args: Parameters<typeof Chain.testnet>) {
return Chain.testnet(...args)
}
export function mainnet (...args: never) {
throw new Error(
Expand Down
118 changes: 69 additions & 49 deletions packages/namada/NamadaBlock.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,39 @@
import { Block } from '@fadroma/cw'
import type { Chain as Namada } from './Namada'
import type * as Namada from './Namada'
import { Decode } from './NamadaDecode'
import NamadaTransaction, { NamadaUndecodedTransaction } from './NamadaTransaction'
import { Block, Transaction } from '@fadroma/cw'

export default class NamadaBlock extends Block {
type NamadaBlockParameters =
ConstructorParameters<typeof Block>[0]
& Pick<NamadaBlock, 'chain'|'hash'|'header'|'transactions'|'responses'>

constructor ({
chain, hash, header, transactions, responses
}: Omit<
ConstructorParameters<typeof Block>[0], 'id'
> & Pick<
NamadaBlock, 'chain'|'hash'|'header'|'transactions'|'responses'
>) {
super({ chain, id: hash, header, transactions })
class NamadaBlock extends Block {
constructor ({ responses, ...props }: NamadaBlockParameters) {
super(props)
this.#responses = responses
}

get hash (): string {
return this.id
}
get chain (): Namada|undefined {
return super.chain as Namada|undefined
get chain (): Namada.Chain|undefined {
return super.chain as Namada.Chain|undefined
}

#responses?: {
block: { url: string, response: string }
results: { url: string, response: string }
}

get responses () {
return this.#responses
}

/** Block header. */
declare header: {
version: object
chainId: string
height: bigint
time: string
lastBlockId: string
lastCommitHash: string
dataHash: string
validatorsHash: string
nextValidatorsHash: string
consensusHash: string
appHash: string
lastResultsHash: string
evidenceHash: string
proposerAddress: string
}
/** Monotonically incrementing ID of block. */
get height () {
return Number((this.header as any)?.height)
}
declare header: NamadaBlockHeader
/** Timestamp of block */
get time () {
return (this.header as any)?.time
}

/** Transaction in block. */
declare transactions: NamadaTransaction[]

/** Responses from block API endpoints. */

static async fetchByHeight (
{ url, decode = Decode, chain }: {
url: string|URL, decode?: typeof Decode, chain?: Namada
url: string|URL, decode?: typeof Decode, chain?: Namada.Chain
},
{ height, raw }: {
height?: number|string|bigint,
Expand All @@ -89,7 +57,7 @@ export default class NamadaBlock extends Block {
}

static async fetchByHash (
_1: { url: string|URL, decode?: typeof Decode, chain?: Namada },
_1: { url: string|URL, decode?: typeof Decode, chain?: Namada.Chain },
_2: { hash: string, raw?: boolean },
): Promise<NamadaBlock> {
throw new Error('NamadaBlock.fetchByHash: not implemented')
Expand All @@ -99,7 +67,7 @@ export default class NamadaBlock extends Block {
responses: NonNullable<NamadaBlock["responses"]>,
{ decode = Decode, chain, height, raw = false }: {
decode?: typeof Decode
chain?: Namada,
chain?: Namada.Chain,
height?: string|number|bigint,
raw?: boolean
},
Expand All @@ -110,17 +78,69 @@ export default class NamadaBlock extends Block {
) as {
hash: string,
header: NamadaBlock["header"]
transactions: Partial<NamadaTransaction[]>[]
transactions: Array<Partial<NamadaTransaction> & {id: string}>
}
const block = new NamadaBlock({
chain, hash, header, transactions: [], responses
chain, hash, header, responses, transactions: [],
})
return Object.assign(block, {
transactions: transactions.map(tx=>new NamadaTransaction({
id: tx?.id,
hash: tx?.id,
...tx,
block
}))
})
}
}

type NamadaBlockHeader = {
version: object
chainId: string
height: bigint
time: string
lastBlockId: string
lastCommitHash: string
dataHash: string
validatorsHash: string
nextValidatorsHash: string
consensusHash: string
appHash: string
lastResultsHash: string
evidenceHash: string
proposerAddress: string
}

type NamadaTransactionParameters =
Pick<NamadaTransaction, 'hash'|'block'> & NamadaTransaction['data']

class NamadaTransaction extends Transaction {
constructor ({ hash, block, ...data }: NamadaTransactionParameters) {
super({ hash, block, data })
}
get block (): NamadaBlock|undefined {
return super.block as NamadaBlock|undefined
}
declare data: {
expiration?: string|null
timestamp?: string
feeToken?: string
feeAmountPerGasUnit?: string
multiplier?: BigInt
gasLimitMultiplier?: BigInt
atomic?: boolean
txType?: 'Raw'|'Wrapper'|'Decrypted'|'Protocol'
sections?: object[]
content?: object
batch?: Array<{
hash: string,
codeHash: string,
dataHash: string,
memoHash: string
}>
}|undefined
}

export {
NamadaBlock as default,
NamadaTransaction as Transaction,
}
4 changes: 2 additions & 2 deletions packages/namada/NamadaConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class NamadaConnection extends CW.Connection {
}

override async fetchBlockImpl (
parameter?: ({ height: number }|{ hash: string }) & { raw?: boolean }
parameter?: ({ height: bigint|number }|{ hash: string }) & { raw?: boolean }
): Promise<Block> {
if (!this.url) {
throw new CW.Error("Can't fetch block: missing connection URL")
Expand Down Expand Up @@ -56,7 +56,7 @@ export default class NamadaConnection extends CW.Connection {
return Gov.fetchProposalCount(this)
}
fetchProposalInfoImpl (id: number|bigint) {
return Gov.fetchProposalInfo(this, id)
return Gov.Proposal.fetch(this, id)
}

fetchPGFParametersImpl () {
Expand Down
2 changes: 1 addition & 1 deletion packages/namada/NamadaConsole.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Console, bold } from '@hackbg/fadroma'
import type Transaction from './NamadaTransaction'
import type { Transaction } from './NamadaBlock'
import type { Proposal } from './NamadaGov'
import type { Validator } from './NamadaPoS'

Expand Down
Loading

0 comments on commit f4d5ff1

Please sign in to comment.