1- import { AeAmountFormats } from './../../utils/amount-formatter'
21import { EncodedData , EncodingType } from './../../utils/encoder'
32import BigNumber from 'bignumber.js'
43import { decode as rlpDecode , encode as rlpEncode , NestedUint8Array } from 'rlp'
@@ -10,16 +9,13 @@ import {
109 DEFAULT_FEE ,
1110 FIELD_TYPES ,
1211 MIN_GAS_PRICE ,
13- OBJECT_ID_TX_TYPE ,
1412 RawTxObject ,
1513 TxField ,
1614 TxTypeSchemas ,
1715 TxParamsCommon ,
18- TxType ,
19- TX_DESERIALIZATION_SCHEMA ,
2016 TX_FEE_BASE_GAS ,
2117 TX_FEE_OTHER_GAS ,
22- TX_SERIALIZATION_SCHEMA ,
18+ TX_SCHEMA ,
2319 TX_TYPE ,
2420 TxSchema
2521} from './schema'
@@ -37,7 +33,7 @@ import {
3733} from './helpers'
3834import { toBytes } from '../../utils/bytes'
3935import MPTree , { MPTreeBinary } from '../../utils/mptree'
40- import { ArgumentError , InvalidTxParamsError , SchemaNotFoundError , UnexpectedTsError } from '../../utils/errors'
36+ import { ArgumentError , InvalidTxParamsError , SchemaNotFoundError , DecodeError } from '../../utils/errors'
4137import { isKeyOfObject } from '../../utils/other'
4238
4339/**
@@ -223,7 +219,7 @@ function validateField (
223219function transformParams (
224220 params : TxParamsCommon ,
225221 schema : TxField [ ] ,
226- { denomination } : { denomination ?: AeAmountFormats } = { }
222+ { denomination } : { denomination ?: AE_AMOUNT_FORMATS } = { }
227223) : any {
228224 params = schema
229225 . filter ( ( [ , t ] ) => t === FIELD_TYPES . amount )
@@ -255,7 +251,7 @@ function transformParams (
255251 )
256252}
257253
258- function getOracleRelativeTtl ( params : any , txType : TxType ) : number {
254+ function getOracleRelativeTtl ( params : any , txType : TX_TYPE ) : number {
259255 const ttlKeys = {
260256 [ TX_TYPE . oracleRegister ] : 'oracleTtl' ,
261257 [ TX_TYPE . oracleExtend ] : 'oracleTtl' ,
@@ -281,7 +277,7 @@ function getOracleRelativeTtl (params: any, txType: TxType): number {
281277 * @example calculateMinFee('spendTx', { gasLimit, params })
282278 */
283279export function calculateMinFee (
284- txType : TxType ,
280+ txType : TX_TYPE ,
285281 { params, vsn } : { params ?: Object , vsn ?: number }
286282) : string {
287283 const multiplier = new BigNumber ( 1e9 ) // 10^9 GAS_PRICE
@@ -309,7 +305,7 @@ export function calculateMinFee (
309305 * @param options.vsn
310306 */
311307function buildFee (
312- txType : TxType ,
308+ txType : TX_TYPE ,
313309 { params, multiplier, vsn } : { params : TxParamsCommon , multiplier : BigNumber , vsn ?: number }
314310) : BigNumber {
315311 const { rlpEncoded : txWithOutFee } = buildTx ( { ...params } , txType , { vsn } )
@@ -339,7 +335,7 @@ function buildFee (
339335 */
340336export function calculateFee (
341337 fee : number | BigNumber | string = 0 ,
342- txType : TxType ,
338+ txType : TX_TYPE ,
343339 { params, showWarning = true , vsn } : {
344340 gasLimit ?: number | string | BigNumber
345341 params ?: TxSchema
@@ -376,7 +372,7 @@ export function validateParams (
376372
377373interface TxOptionsRaw {
378374 excludeKeys ?: string [ ]
379- denomination ?: AeAmountFormats
375+ denomination ?: AE_AMOUNT_FORMATS
380376}
381377/**
382378 * Build binary transaction
@@ -442,7 +438,7 @@ export interface BuiltTx<Tx extends TxSchema, Prefix extends EncodingType> {
442438}
443439
444440export type TxParamsBuild = TxParamsCommon & {
445- denomination ?: AeAmountFormats
441+ denomination ?: AE_AMOUNT_FORMATS
446442}
447443/**
448444 * Build transaction hash
@@ -461,29 +457,25 @@ export type TxParamsBuild = TxParamsCommon & {
461457 */
462458export function buildTx < Prefix > (
463459 params : TxParamsBuild ,
464- type : TxType ,
460+ type : TX_TYPE ,
465461 { excludeKeys = [ ] , prefix = 'tx' , vsn, denomination = AE_AMOUNT_FORMATS . AETTOS } : {
466462 excludeKeys ?: string [ ]
467463 prefix ?: EncodingType
468464 vsn ?: number
469- denomination ?: AeAmountFormats
465+ denomination ?: AE_AMOUNT_FORMATS
470466 } = { }
471467) : Prefix extends EncodingType
472468 ? BuiltTx < TxSchema , Prefix >
473469 : BuiltTx < TxSchema , 'tx' > {
474- const schemas = TX_SERIALIZATION_SCHEMA [ type ]
475- if ( schemas == null ) throw new UnexpectedTsError ( )
470+ const schemas = TX_SCHEMA [ type ]
476471
477472 vsn ??= Math . max ( ...Object . keys ( schemas ) . map ( a => + a ) )
478- if ( ! isKeyOfObject ( vsn , schemas ) ) throw new SchemaNotFoundError ( 'serialization' , type . toString ( ) , vsn )
473+ if ( ! isKeyOfObject ( vsn , schemas ) ) throw new SchemaNotFoundError ( 'serialization' , TX_TYPE [ type ] , vsn )
479474
480475 const schema = schemas [ vsn ]
481476
482- const tags = Object . entries ( OBJECT_ID_TX_TYPE ) . find ( ( [ , t ] ) => t === type )
483- if ( tags == null ) { throw new UnexpectedTsError ( ) }
484- const tag = tags [ 0 ]
485477 const binary = buildRawTx (
486- { ...params , VSN : vsn , tag } ,
478+ { ...params , VSN : vsn , tag : type } ,
487479 schema ,
488480 { excludeKeys, denomination : params . denomination ?? denomination }
489481 ) . filter ( e => e !== undefined )
@@ -494,12 +486,12 @@ export function buildTx<Prefix> (
494486 tx,
495487 rlpEncoded,
496488 binary,
497- txObject : unpackRawTx < TxTypeSchemas [ TxType ] > ( binary , schema )
489+ txObject : unpackRawTx < TxTypeSchemas [ TX_TYPE ] > ( binary , schema )
498490 } as any
499491}
500492
501493export interface TxUnpacked < Tx extends TxSchema > {
502- txType : TxType
494+ txType : TX_TYPE
503495 tx : RawTxObject < Tx >
504496 rlpEncoded : Uint8Array
505497 binary : Uint8Array | NestedUint8Array
@@ -516,7 +508,7 @@ export interface TxUnpacked<Tx extends TxSchema> {
516508 * @returns object.rlpEncoded rlp encoded transaction
517509 * @returns object.binary binary transaction
518510 */
519- export function unpackTx < TxType extends keyof TxTypeSchemas > (
511+ export function unpackTx < TxType extends TX_TYPE > (
520512 encodedTx : EncodedData < 'tx' > | Uint8Array ,
521513 { txType, fromRlpBinary = false } :
522514 { txType ?: TxType , fromRlpBinary ?: boolean } = {
@@ -525,16 +517,15 @@ export function unpackTx<TxType extends keyof TxTypeSchemas> (
525517) : TxUnpacked < TxTypeSchemas [ TxType ] > {
526518 const rlpEncoded = fromRlpBinary ? encodedTx as Uint8Array : decode ( encodedTx as EncodedData < 'tx' > )
527519 const binary = rlpDecode ( rlpEncoded )
528- const objId = readInt ( binary [ 0 ] as Buffer ) as unknown as keyof typeof TX_DESERIALIZATION_SCHEMA
529- txType ??= OBJECT_ID_TX_TYPE [ objId ] as TxType
530- const vsn = readInt ( binary [ 1 ] as Buffer ) as keyof typeof TX_DESERIALIZATION_SCHEMA [ typeof objId ]
531- const schema = TX_DESERIALIZATION_SCHEMA [ objId ] [ vsn ]
532- if ( schema == null ) {
533- throw new SchemaNotFoundError ( 'deserialization' , `tag ${ objId } ` , vsn )
534- }
520+ const objId = + readInt ( binary [ 0 ] as Buffer )
521+ if ( ! isKeyOfObject ( objId , TX_SCHEMA ) ) throw new DecodeError ( `Unknown transaction tag: ${ objId } ` )
522+ if ( txType != null && txType !== objId ) throw new DecodeError ( `Expected transaction to have ${ TX_TYPE [ txType ] } tag, got ${ TX_TYPE [ objId ] } instead` )
523+ const vsn = + readInt ( binary [ 1 ] as Buffer )
524+ if ( ! isKeyOfObject ( vsn , TX_SCHEMA [ objId ] ) ) throw new SchemaNotFoundError ( 'deserialization' , `tag ${ objId } ` , vsn )
525+ const schema = TX_SCHEMA [ objId ] [ vsn ]
535526 return {
536- txType,
537- tx : unpackRawTx < TxTypeSchemas [ typeof txType ] > ( binary , schema ) ,
527+ txType : objId ,
528+ tx : unpackRawTx < TxTypeSchemas [ TxType ] > ( binary , schema ) ,
538529 rlpEncoded,
539530 binary
540531 }
@@ -560,8 +551,8 @@ export function buildTxHash (rawTx: EncodedData<'tx'> | Uint8Array): string {
560551 * @return Contract public key
561552 */
562553export function buildContractIdByContractTx ( contractTx : EncodedData < 'tx' > ) : EncodedData < 'ct' > {
563- const { txType, tx } = unpackTx ( contractTx , { txType : TX_TYPE . contractCreate } )
564- if ( ! [ TX_TYPE . contractCreate , TX_TYPE . gaAttach ] . includes ( txType as any ) ) {
554+ const { txType, tx } = unpackTx < TX_TYPE . contractCreate | TX_TYPE . gaAttach > ( contractTx )
555+ if ( ! [ TX_TYPE . contractCreate , TX_TYPE . gaAttach ] . includes ( txType ) ) {
565556 throw new ArgumentError ( 'contractCreateTx' , 'a contractCreateTx or gaAttach' , txType )
566557 }
567558 return buildContractId ( tx . ownerId , + tx . nonce )
0 commit comments