Skip to content

Commit

Permalink
refactor(tx-builder): improve types of Tag.GaMetaTx and Tag.PayingForTx
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Feb 15, 2023
1 parent 0e9c552 commit a179a30
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/account/Generalized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 1 addition & 8 deletions src/tx/builder/field-types/fee.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}

Expand Down
12 changes: 10 additions & 2 deletions src/tx/builder/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TxParams & { tag: Tag.MtreeValue } | Uint8Array | Encoded.Transaction>,
Expand Down Expand Up @@ -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<typeof txSchema>;
Expand Down
5 changes: 1 addition & 4 deletions src/tx/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions test/integration/ga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -100,7 +98,6 @@ describe('Generalized Account', () => {
});
const txParams = unpackTx(rawTx, Tag.SignedTx);
ensureEqual<Tag.GaMetaTx>(txParams.encodedTx.tag, Tag.GaMetaTx);
ensureEqual<Tag.SignedTx>(txParams.encodedTx.tx.tag, Tag.SignedTx);
expect(buildTx(txParams.encodedTx.tx.encodedTx)).to.be.equal(spendTx);
});

Expand Down

0 comments on commit a179a30

Please sign in to comment.