Skip to content

Commit

Permalink
test: add unit tests for staking in RpcContractProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
ac10n committed Sep 22, 2023
1 parent 9d1c4a8 commit 6ed2ed0
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 8 deletions.
14 changes: 11 additions & 3 deletions integration-tests/contract-staking.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Protocols } from "@taquito/taquito";
import { CONFIGS, isSandbox } from "./config";
import { CONFIGS, isSandbox, sleep } from "./config";

CONFIGS().forEach(({ lib, rpc, setup, protocol }) => {
const Tezos = lib;
Expand All @@ -11,18 +11,26 @@ CONFIGS().forEach(({ lib, rpc, setup, protocol }) => {
});
flextesaOxford('Should be able to stake', async (done) => {
const op = await Tezos.contract.stake({
amount: 0.1,
amount: 100,
});
await op.confirmation()
expect(op.hash).toBeDefined();

const constants = await Tezos.rpc.getConstants();
await sleep(((constants.preserved_cycles + 2) * constants.blocks_per_cycle * (constants.minimal_block_delay!.toNumber())) * 1000);

done();
});
flextesaOxford('Should be able to unstake', async (done) => {
const op = await Tezos.contract.unstake({
amount: 0.1,
amount: 100,
});
await op.confirmation()
expect(op.hash).toBeDefined();

const constants = await Tezos.rpc.getConstants();
await sleep(((constants.preserved_cycles + 2) * constants.blocks_per_cycle * (constants.minimal_block_delay!.toNumber())) * 1000);

done();
});
flextesaOxford('Should be able to finalizeUnstake', async (done) => {
Expand Down
30 changes: 25 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/taquito-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,11 @@
"collectCoverageFrom": [
"src/**/*.{js,ts}"
]
},
"dependencies": {
"json-stringify-safe": "^5.0.1"
},
"devDependencies": {
"@types/json-stringify-safe": "^5.0.1"
}
}
3 changes: 3 additions & 0 deletions packages/taquito-core/src/taquito-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
* @module @taquito/core
*/

import stringify from 'json-stringify-safe';

export * from './errors';
export { stringify };
12 changes: 12 additions & 0 deletions packages/taquito/src/contract/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ export class InvalidDelegationSource extends ParameterValidationError {
}
}

/**
* @category Error
* @description Error that indicates an invalid staking source contract address being passed or used
*/
export class InvalidStakingSource extends ParameterValidationError {
constructor(public readonly source: string) {
super();
this.name = `InvalidStakingSource`;
this.message = `The source for staking operations can not be a contract address ${source}.`;
}
}

/**
* @category Error
* @description Error that indicates an invalid smart contract code parameter being passed or used
Expand Down
6 changes: 6 additions & 0 deletions packages/taquito/src/operations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { BlockIdentifier } from '../read-provider/interface';
import { InvalidAddressError, InvalidAmountError } from '@taquito/core';
import { ValidationResult, invalidDetail, validateAddress } from '@taquito/utils';
import { InvalidStakingSource } from '../contract/errors';

export { OpKind } from '@taquito/rpc';

Expand Down Expand Up @@ -589,6 +590,8 @@ export type FinalizeUnstakeParams = Omit<StakingParams, 'parameter' | `amount`>;
/**
*
* @throws {@link InvalidAmountError}
* @throws {@link InvalidAddressError}
* @throws {@link InvalidStakingSource}
*/
export const validateStakingParams = (
params: Omit<PartialBy<StakingParams, 'amount'>, 'parameter'>,
Expand All @@ -607,6 +610,9 @@ export const validateStakingParams = (
if (sourceValidation !== ValidationResult.VALID) {
throw new InvalidAddressError(params.source, invalidDetail(sourceValidation));
}
if (/kt/i.test(params.source)) {
throw new InvalidStakingSource(params.source);
}
}
};

Expand Down
Loading

0 comments on commit 6ed2ed0

Please sign in to comment.