From a179a306452aece552d2d175c5d18b04e07c4af0 Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Mon, 13 Feb 2023 19:43:24 +0400 Subject: [PATCH] refactor(tx-builder): improve types of Tag.GaMetaTx and Tag.PayingForTx --- src/account/Generalized.ts | 2 +- src/tx/builder/field-types/fee.ts | 9 +-------- src/tx/builder/schema.ts | 12 ++++++++++-- src/tx/validator.ts | 5 +---- test/integration/ga.ts | 5 +---- 5 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/account/Generalized.ts b/src/account/Generalized.ts index ef18008773..7f90d2f1e9 100644 --- a/src/account/Generalized.ts +++ b/src/account/Generalized.ts @@ -70,7 +70,7 @@ export default class AccountGeneralized extends AccountBase { const gaMetaTx = await buildTxAsync({ tag: Tag.GaMetaTx, - tx: decode(buildTx({ tag: Tag.SignedTx, encodedTx: decode(tx), signatures: [] })), + tx: { tag: Tag.SignedTx, encodedTx: decode(tx), signatures: [] }, gaId: this.address, authData: authCallData, gasLimit, diff --git a/src/tx/builder/field-types/fee.ts b/src/tx/builder/field-types/fee.ts index e7f6bf1c34..39f73906f9 100644 --- a/src/tx/builder/field-types/fee.ts +++ b/src/tx/builder/field-types/fee.ts @@ -1,5 +1,5 @@ import BigNumber from 'bignumber.js'; -import { ArgumentError, IllegalArgumentError } from '../../../utils/errors'; +import { IllegalArgumentError } from '../../../utils/errors'; import { MIN_GAS_PRICE, Tag } from '../constants'; import coinAmount from './coin-amount'; import { isKeyOfObject } from '../../../utils/other'; @@ -103,13 +103,6 @@ export function buildFee( let innerTxSize = 0; if (txObject.tag === Tag.GaMetaTx || txObject.tag === Tag.PayingForTx) { - if (txObject.tx.tag !== Tag.SignedTx) { - throw new ArgumentError( - `Payload of ${Tag[txObject.tag]}`, - Tag[Tag.SignedTx], - Tag[txObject.tx.tag], - ); - } innerTxSize = decode(buildTx(txObject.tx.encodedTx)).length; } diff --git a/src/tx/builder/schema.ts b/src/tx/builder/schema.ts index 2d7ba39845..3f44d533b6 100644 --- a/src/tx/builder/schema.ts +++ b/src/tx/builder/schema.ts @@ -44,6 +44,14 @@ interface EntryAny { const entryAny = entry() as unknown as EntryAny; +interface EntrySignedTx { + serialize: (value: TxParams & { tag: Tag.SignedTx } | Uint8Array | Encoded.Transaction) => Buffer; + deserialize: (value: Buffer) => TxUnpacked & { tag: Tag.SignedTx }; + recursiveType: true; +} + +const entrySignedTx = entry(Tag.SignedTx) as unknown as EntrySignedTx; + interface EntryMtreeValueArray { serialize: ( value: Array, @@ -556,14 +564,14 @@ export const txSchema = [{ fee, gasLimit, gasPrice, - tx: entryAny, + tx: entrySignedTx, }, { tag: shortUIntConst(Tag.PayingForTx), version: shortUIntConst(1, true), payerId: address(Encoding.AccountAddress), nonce: nonce('payerId'), fee, - tx: entryAny, + tx: entrySignedTx, }] as const; type TxSchema = SchemaTypes; diff --git a/src/tx/validator.ts b/src/tx/validator.ts index a2afa3656d..91c60968bd 100644 --- a/src/tx/validator.ts +++ b/src/tx/validator.ts @@ -9,7 +9,7 @@ import { Encoded, decode } from '../utils/encoder'; import Node, { TransformNodeType } from '../Node'; import { Account } from '../apis/node'; import { genAggressiveCacheGetResponsesPolicy } from '../utils/autorest'; -import { ArgumentError, UnexpectedTsError } from '../utils/errors'; +import { UnexpectedTsError } from '../utils/errors'; export interface ValidatorResult { message: string; @@ -133,9 +133,6 @@ validators.push( if (account == null) return []; let extraFee = '0'; if (tx.tag === Tag.PayingForTx) { - if (tx.tx.tag !== Tag.SignedTx) { - throw new ArgumentError('Payload of PayingForTx', Tag[Tag.SignedTx], Tag[tx.tx.tag]); - } // TODO: calculate nested tx fee more accurate if ('fee' in tx.tx.encodedTx) { extraFee = tx.tx.encodedTx.fee; diff --git a/test/integration/ga.ts b/test/integration/ga.ts index 538aa382fd..f77b916ece 100644 --- a/test/integration/ga.ts +++ b/test/integration/ga.ts @@ -82,9 +82,7 @@ describe('Generalized Account', () => { const { rawTx } = await aeSdk .spend(10000, publicKey, { authData: { sourceCode, args: [genSalt()] } }); const gaMetaTxParams = unpackTx(rawTx, Tag.SignedTx).encodedTx; - if (gaMetaTxParams.tag !== Tag.GaMetaTx || gaMetaTxParams.tx.tag !== Tag.SignedTx) { - throw new Error('Unexpected nested transaction'); - } + if (gaMetaTxParams.tag !== Tag.GaMetaTx) throw new Error('Unexpected nested transaction'); const spendTx = buildTx(gaMetaTxParams.tx.encodedTx); expect(await aeSdk.buildAuthTxHash(spendTx)).to.be .eql((await authContract.getTxHash()).decodedResult); @@ -100,7 +98,6 @@ describe('Generalized Account', () => { }); const txParams = unpackTx(rawTx, Tag.SignedTx); ensureEqual(txParams.encodedTx.tag, Tag.GaMetaTx); - ensureEqual(txParams.encodedTx.tx.tag, Tag.SignedTx); expect(buildTx(txParams.encodedTx.tx.encodedTx)).to.be.equal(spendTx); });