Skip to content

Commit

Permalink
fix(tx-builder): calculation of gasLimitMax
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Dec 29, 2022
1 parent 3cff062 commit 0fb8a37
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/tx/builder/field-types/fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,20 @@ export default {

serializeAettos(
_value: string | undefined,
{ rebuildTx, unpackTx, _computingMinFee }: {
{
rebuildTx, unpackTx, _computingMinFee, _pickBiggerFee,
}: {
rebuildTx: (params: any) => Encoded.Transaction;
unpackTx: typeof unpackTxType;
_computingMinFee?: string;
_computingMinFee?: BigNumber;
_pickBiggerFee?: boolean;
},
): string {
if (_computingMinFee != null) return _computingMinFee;
if (_computingMinFee != null) return _computingMinFee.toFixed();
const minFee = calculateMinFee((fee) => rebuildTx({ _computingMinFee: fee }), unpackTx);
const value = new BigNumber(_value ?? minFee);
if (minFee.gt(value)) {
if (_pickBiggerFee === true) return minFee.toFixed();
throw new IllegalArgumentError(`Fee ${value.toString()} must be bigger then ${minFee}`);
}
return value.toFixed();
Expand Down
2 changes: 1 addition & 1 deletion src/tx/builder/field-types/gas-limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default {

const gasLimitMax = tag === Tag.GaMetaTx ? MAX_AUTH_FUN_GAS : calculateGasLimitMax(
gasMax,
(gasLimit) => rebuildTx({ _computingGasLimit: gasLimit }),
(gasLimit) => rebuildTx({ _computingGasLimit: gasLimit, _pickBiggerFee: true }),
unpackTx,
);
const value = _value ?? gasLimitMax;
Expand Down
20 changes: 20 additions & 0 deletions test/unit/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,26 @@ describe('Tx', () => {
expect(pi).to.satisfy((s: string) => s.startsWith('pi_'));
});

it('build ContractCreateTx with specified fee', () => {
const txParams = {
tag: Tag.ContractCreateTx,
version: 1,
ownerId: 'ak_xw6vb7yJfajDdfcXzjg6Q5bH23bSUJrud6iBBfMdegZJFbQmc',
nonce: 3,
code: 'cb_+GhGA6Csc3MTA1lWna1q0L5k4TgjcQsmHIVhaJ7qU/0CBZqpO8C4O57+RNZEHwA3ADcAGg6CPwEDP/6AeCCSADcBBwcBAQCYLwIRRNZEHxFpbml0EYB4IJIZZ2V0QXJngi8AhTcuMC4xAHO0rKc=',
ctVersion: { vmVersion: 7, abiVersion: 3 },
fee: '78500000000000',
ttl: 0,
deposit: '0',
amount: '0',
gasLimit: 76,
gasPrice: '1000000000',
callData: 'cb_KxFE1kQfP4oEp9E=',
} as const;
const tx = buildTx(txParams);
expect(unpackTx(tx, Tag.ContractCreateTx).tx.fee).to.be.equal('78500000000000');
});

it('rejects if invalid transaction version', () => {
expect(() => buildTx({ tag: Tag.SpendTx, version: 5 } as any))
.to.throw(SchemaNotFoundError, 'Transaction serialization not implemented for SpendTx version 5');
Expand Down

0 comments on commit 0fb8a37

Please sign in to comment.