Skip to content

Commit

Permalink
update estimate overrides to throw error when decimal values are bein…
Browse files Browse the repository at this point in the history
…g passed as well as update tests
  • Loading branch information
dsawali committed Jan 31, 2023
1 parent fb3b6bd commit 3d569f5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 5 deletions.
56 changes: 56 additions & 0 deletions integration-tests/contract-override-estimate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { CONFIGS } from "./config";
import { InvalidEstimateValueError } from '@taquito/taquito';

CONFIGS().forEach(({ lib, rpc, setup, createAddress }) => {
const Tezos = lib;

describe(`Test contract API operations with overridden estimate values ${rpc}`, () => {
let pkh: string;

beforeAll(async (done) => {
await setup();

try {
const account = await createAddress();
pkh = await account.signer.publicKeyHash();
} catch(e) {
console.log(JSON.stringify(e));
}

done();
});

it('should throw an error when overriding origination estimate values with decimals', async (done) => {
expect(async () => {
const op = await Tezos.contract.originate({
balance: "1",
code: `parameter string;
storage string;
code {CAR;
PUSH string "Hello ";
CONCAT;
NIL operation; PAIR};
`,
init: `"test"`,
storageLimit: 100.22
});
await op.confirmation();
}).rejects.toThrowError(InvalidEstimateValueError);

done();
});

it('should throw an error when overriding transfer/transaction estimate values with decimal', async (done) => {
expect(async () => {
const op = await Tezos.contract.transfer({
to: pkh,
amount: 1,
fee: 10.5
});
await op.confirmation();
}).rejects.toThrowError(InvalidEstimateValueError);

done();
});
});
});
9 changes: 5 additions & 4 deletions integration-tests/contract-simple-origination.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ CONFIGS().forEach(({ lib, rpc, setup }) => {
describe(`Test contract origination of a simple contract through contract api using: ${rpc}`, () => {

beforeEach(async (done) => {
await setup()
done()
})
await setup();
done();
});

test('Verify contract.originate for a simple contract', async (done) => {
const op = await Tezos.contract.originate({
balance: "1",
Expand All @@ -32,4 +33,4 @@ CONFIGS().forEach(({ lib, rpc, setup }) => {
done();
});
});
})
});
1 change: 1 addition & 0 deletions packages/taquito/src/operations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export {
TezosOperationError,
TezosOperationErrorWithMessage,
TezosPreapplyFailureError,
InvalidEstimateValueError,
} from './operation-errors';
export { BatchOperation } from './batch-operation';
export { DelegateOperation } from './delegate-operation';
Expand Down
21 changes: 20 additions & 1 deletion packages/taquito/src/operations/operation-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import { Protocols } from '../constants';
import { Context } from '../context';
import { Estimate } from '../estimate/estimate';
import { RPCResponseError } from '../error';
import { flattenErrors, TezosOperationError, TezosPreapplyFailureError } from './operation-errors';
import {
flattenErrors,
TezosOperationError,
TezosPreapplyFailureError,
InvalidEstimateValueError,
} from './operation-errors';
import { InvalidOperationKindError, DeprecationError } from '@taquito/utils';
import {
ForgedBytes,
Expand Down Expand Up @@ -251,6 +256,20 @@ export abstract class OperationEmitter {
let calculatedGas = gasLimit;
let calculatedStorage = storageLimit;

if (calculatedFee && calculatedFee % 1 !== 0) {
throw new InvalidEstimateValueError(`Fee value must not be a decimal: ${calculatedFee}`);
}
if (calculatedGas && calculatedGas % 1 !== 0) {
throw new InvalidEstimateValueError(
`Gas Limit value must not be a decimal: ${calculatedGas}`
);
}
if (calculatedStorage && calculatedStorage % 1 !== 0) {
throw new InvalidEstimateValueError(
`Storage Limit value must not be a decimal: ${calculatedStorage}`
);
}

if (fee === undefined || gasLimit === undefined || storageLimit === undefined) {
const estimation = await estimator({ fee, gasLimit, storageLimit, ...(rest as any) });

Expand Down
11 changes: 11 additions & 0 deletions packages/taquito/src/operations/operation-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,14 @@ export class OriginationOperationError extends Error {
super(message);
}
}

/**
* @category Error
* @description Error that indicates a general failure happening during an origination operation
*/
export class InvalidEstimateValueError extends Error {
public name = 'InvalidEstimateValueError';
constructor(public message: string) {
super(message);
}
}
2 changes: 2 additions & 0 deletions packages/taquito/test/estimate/rpc-estimate-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ describe('RPCEstimateProvider test signer', () => {

done();
});

it('should throw an error with invalid source', async (done) => {
const params: TransferTicketParams = {
source: 'tz1iedjFYksExq8snZK9MNo4AvXHG',
Expand All @@ -587,6 +588,7 @@ describe('RPCEstimateProvider test signer', () => {

done();
});

it('should throw an error with invalid destination', async (done) => {
const params: TransferTicketParams = {
source: 'tz1iedjFYksExq8snZK9MNo4AvXHBdXfTsGX',
Expand Down

0 comments on commit 3d569f5

Please sign in to comment.