From c1fb669a202eea90f6fad5244195d13be6c75561 Mon Sep 17 00:00:00 2001 From: Adam A Date: Thu, 6 Jun 2024 18:30:49 +0300 Subject: [PATCH] refactor(namada): bye namada-tx-base --- packages/namada/namada-connection.ts | 6 +- packages/namada/namada-tx-base.ts | 170 ------------------------ packages/namada/namada-tx.ts | 189 +++++++++++++++++++++++++-- 3 files changed, 184 insertions(+), 181 deletions(-) delete mode 100644 packages/namada/namada-tx-base.ts diff --git a/packages/namada/namada-connection.ts b/packages/namada/namada-connection.ts index daf8c97119..de7ca346cc 100644 --- a/packages/namada/namada-connection.ts +++ b/packages/namada/namada-connection.ts @@ -177,16 +177,16 @@ export class NamadaConnection extends CW.Connection { } override async fetchBlockImpl ( parameter?: ({ height: number }|{ hash: string }) & { raw?: boolean } - ): Promise { + ): Promise { if (!this.url) { throw new CW.Error("Can't fetch block: missing connection URL") } if ((!parameter) || ('height' in parameter)) { - return TX.NamadaBlock.fetchByHeight( + return TX.Block.fetchByHeight( this, parameter?.height || '', parameter?.raw ) } else if ('hash' in parameter) { - return TX.NamadaBlock.fetchByHash( + return TX.Block.fetchByHash( this, parameter.hash || '', parameter.raw ) } else { diff --git a/packages/namada/namada-tx-base.ts b/packages/namada/namada-tx-base.ts deleted file mode 100644 index 7c0f877737..0000000000 --- a/packages/namada/namada-tx-base.ts +++ /dev/null @@ -1,170 +0,0 @@ -import { assign } from '@hackbg/fadroma' -import * as Sections from './namada-tx-section' - -class NamadaTransaction { - - static fromDecoded = ({ id, sections, type, ...header }: { - id: string, - type: 'Raw'|'Wrapper'|'Decrypted'|'Protocol', - sections: Array< - | Partial - | Partial - | Partial - | Partial - | Partial - | Partial - | Partial - | Partial - | Partial - > - }) => new this({ - ...header, - id, - txType: type, - sections: sections.map(section=>{ - switch (section.type) { - case 'Data': - return new Sections.Data(section) - case 'ExtraData': - return new Sections.ExtraData(section) - case 'Code': - return new Sections.Code(section) - case 'Signature': - return new Sections.Signature(section) - case 'Ciphertext': - return new Sections.Ciphertext() - case 'MaspBuilder': - return new Sections.MaspBuilder(section) - case 'Header': - return new Sections.Header(section) - case 'MaspTx': - return new Sections.MaspTx(section) - default: - return new Sections.Unknown(section) - } - }) - }) - - id!: string - height?: number - chainId!: string - expiration!: string|null - timestamp!: string - codeHash!: string - dataHash!: string - memoHash!: string - txType!: 'Raw'|'Wrapper'|'Decrypted'|'Protocol' - sections!: Sections.Section[] - content?: object - - constructor (properties: Partial = {}) { - assign(this, properties, [ - 'id', - 'height', - 'chainId', - 'expiration', - 'timestamp', - 'codeHash', - 'dataHash', - 'memoHash', - 'txType', - 'sections', - 'content' - ]) - } -} - -export default NamadaTransaction - -class NamadaUndecodedTransaction extends NamadaTransaction { - data!: unknown - error!: Error - constructor (properties: Partial = {}) { - super() - assign(this, properties, [ "data", "error" ]) - } -} - -class NamadaRawTransaction extends NamadaTransaction { - txType = 'Raw' as 'Raw' - //constructor (header: object, details: object, sections: object[]) { - //super(header, sections) - //this.txType = 'Raw' - //} -} - -class NamadaWrapperTransaction extends NamadaTransaction { - txType = 'Wrapper' as 'Wrapper' - //declare fee: { - //token: string - //amountPerGasUnit: { - //amount: bigint, - //denomination: number - //}, - //} - //declare pk: string - //declare epoch: bigint - //declare gasLimit: bigint - //declare unshieldSectionHash: string|null - //constructor (header: object, details: object, sections: object[]) { - //super(header, sections) - //assignCamelCase(this, details, wrapperTransactionFields.map(x=>x[0] as string)) - //this.txType = 'Wrapper' - //} - -//export const wrapperTransactionFields: Fields = [ - //["fee", struct( - //["amountPerGasUnit", struct( - //["amount", u256], - //["denomination", u8], - //)], - //["token", addr], - //)], - //["pk", pubkey], - //["epoch", u64], - //["gasLimit", u64], - //["unshieldSectionHash", option(hashSchema)], -//] -} - -class NamadaDecryptedTransaction extends NamadaTransaction { - txType = 'Decrypted' as 'Decrypted' - //undecryptable: boolean -} - -class NamadaProtocolTransaction extends NamadaTransaction { - txType = 'Protocol' as 'Protocol' - //pk: string - //tx: |'EthereumEvents' - //|'BridgePool' - //|'ValidatorSetUpdate' - //|'EthEventsVext' - //|'BridgePoolVext' - //|'ValSetUpdateVext' - //constructor (header: object, details: object, sections: object[]) { - //super(header, sections) - //assignCamelCase(this, details, protocolTransactionFields.map(x=>x[0] as string)) - //this.txType = 'Protocol' - //} - -//export const protocolTransactionFields: Fields = [ - //["pk", pubkey], - //["tx", variants( - //['EthereumEvents', unit], - //['BridgePool', unit], - //['ValidatorSetUpdate', unit], - //['EthEventsVext', unit], - //['BridgePoolVext', unit], - //['ValSetUpdateVext', unit], - //)], -//] - -} - -export { - NamadaUndecodedTransaction as Undecoded, - NamadaRawTransaction as Raw, - NamadaWrapperTransaction as Wrapper, - NamadaDecryptedTransaction as Decrypted, - NamadaProtocolTransaction as Protocol, -} diff --git a/packages/namada/namada-tx.ts b/packages/namada/namada-tx.ts index 9ad0cec1e6..a0e3c5d426 100644 --- a/packages/namada/namada-tx.ts +++ b/packages/namada/namada-tx.ts @@ -1,16 +1,16 @@ -export { default as Transaction } from './namada-tx-base' -export * as Transactions from './namada-tx-base' export { default as Section } from './namada-tx-section' export * as Sections from './namada-tx-section' export { default as wasmToContent } from './namada-tx-content' export * as Contents from './namada-tx-content' +import { assign } from '@hackbg/fadroma' import { Block } from '@fadroma/cw' import { Decode } from './namada-decode' -import Transaction, { Undecoded } from './namada-tx-base' import type { Namada } from './namada-connection' +import type Section from './namada-tx-section' +import * as Sections from './namada-tx-section' -export class NamadaBlock extends Block { +class NamadaBlock extends Block { constructor ({ header, @@ -45,7 +45,7 @@ export class NamadaBlock extends Block { } /** Transaction in block. */ - declare transactions: Transaction[] + declare transactions: NamadaTransaction[] /** Response from block API endpoint. */ rawBlockResponse?: string @@ -75,7 +75,7 @@ export class NamadaBlock extends Block { const { id, header, txs } = decode.block(block, results) as { id: string, - txs: Partial[] + txs: Partial[] header: NamadaBlock["header"] } @@ -87,14 +87,14 @@ export class NamadaBlock extends Block { timestamp: header.time, transactions: txs.map((tx, i)=>{ try { - return Transaction.fromDecoded({ + return NamadaTransaction.fromDecoded({ height, ...tx as any }) } catch (error) { console.error(error) console.warn(`Failed to decode transaction #${i} in block ${height}, see above for details.`) - return new Undecoded({ + return new NamadaUndecodedTransaction({ error: error as any, data: tx as any, }) @@ -117,3 +117,176 @@ export class NamadaBlock extends Block { } } + +class NamadaTransaction { + + id!: string + height?: number + chainId!: string + expiration!: string|null + timestamp!: string + codeHash!: string + dataHash!: string + memoHash!: string + txType!: 'Raw'|'Wrapper'|'Decrypted'|'Protocol' + sections!: Section[] + content?: object + + constructor (properties: Partial = {}) { + assign(this, properties, [ + 'id', + 'height', + 'chainId', + 'expiration', + 'timestamp', + 'codeHash', + 'dataHash', + 'memoHash', + 'txType', + 'sections', + 'content' + ]) + } + + static fromDecoded ({ id, sections, type, ...header }: { + id: string, + type: 'Raw'|'Wrapper'|'Decrypted'|'Protocol', + sections: Array< + | Partial + | Partial + | Partial + | Partial + | Partial + | Partial + | Partial + | Partial + | Partial + > + }) { + return new this({ + ...header, + id, + txType: type, + sections: sections.map(section=>{ + switch (section.type) { + case 'Data': + return new Sections.Data(section) + case 'ExtraData': + return new Sections.ExtraData(section) + case 'Code': + return new Sections.Code(section) + case 'Signature': + return new Sections.Signature(section) + case 'Ciphertext': + return new Sections.Ciphertext() + case 'MaspBuilder': + return new Sections.MaspBuilder(section) + case 'Header': + return new Sections.Header(section) + case 'MaspTx': + return new Sections.MaspTx(section) + default: + return new Sections.Unknown(section) + } + }) + }) + } +} + +class NamadaUndecodedTransaction extends NamadaTransaction { + data!: unknown + error!: Error + constructor (properties: Partial = {}) { + super() + assign(this, properties, [ "data", "error" ]) + } +} + +class NamadaRawTransaction extends NamadaTransaction { + txType = 'Raw' as 'Raw' + //constructor (header: object, details: object, sections: object[]) { + //super(header, sections) + //this.txType = 'Raw' + //} +} + +class NamadaWrapperTransaction extends NamadaTransaction { + txType = 'Wrapper' as 'Wrapper' + //declare fee: { + //token: string + //amountPerGasUnit: { + //amount: bigint, + //denomination: number + //}, + //} + //declare pk: string + //declare epoch: bigint + //declare gasLimit: bigint + //declare unshieldSectionHash: string|null + //constructor (header: object, details: object, sections: object[]) { + //super(header, sections) + //assignCamelCase(this, details, wrapperTransactionFields.map(x=>x[0] as string)) + //this.txType = 'Wrapper' + //} + +//export const wrapperTransactionFields: Fields = [ + //["fee", struct( + //["amountPerGasUnit", struct( + //["amount", u256], + //["denomination", u8], + //)], + //["token", addr], + //)], + //["pk", pubkey], + //["epoch", u64], + //["gasLimit", u64], + //["unshieldSectionHash", option(hashSchema)], +//] +} + +class NamadaDecryptedTransaction extends NamadaTransaction { + txType = 'Decrypted' as 'Decrypted' + //undecryptable: boolean +} + +class NamadaProtocolTransaction extends NamadaTransaction { + txType = 'Protocol' as 'Protocol' + //pk: string + //tx: |'EthereumEvents' + //|'BridgePool' + //|'ValidatorSetUpdate' + //|'EthEventsVext' + //|'BridgePoolVext' + //|'ValSetUpdateVext' + //constructor (header: object, details: object, sections: object[]) { + //super(header, sections) + //assignCamelCase(this, details, protocolTransactionFields.map(x=>x[0] as string)) + //this.txType = 'Protocol' + //} + +//export const protocolTransactionFields: Fields = [ + //["pk", pubkey], + //["tx", variants( + //['EthereumEvents', unit], + //['BridgePool', unit], + //['ValidatorSetUpdate', unit], + //['EthEventsVext', unit], + //['BridgePoolVext', unit], + //['ValSetUpdateVext', unit], + //)], +//] + +} + +export { + NamadaBlock as Block, + NamadaTransaction as Transaction +} + +export const Transactions = { + Undecoded: NamadaUndecodedTransaction, + Raw: NamadaRawTransaction, + Wrapper: NamadaWrapperTransaction, + Decrypted: NamadaDecryptedTransaction, + Protocol: NamadaProtocolTransaction, +}