Skip to content

Commit

Permalink
test: imporved staking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hui-an-yang committed Nov 13, 2024
1 parent 461b41f commit 203a9c3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 51 deletions.
46 changes: 28 additions & 18 deletions integration-tests/__tests__/contract/operations/staking.spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
import { CONFIGS } from "../../../config";
import { InvalidStakingAddressError, InvalidFinalizeUnstakeAmountError } from '@taquito/core';

CONFIGS().forEach(({ lib, rpc, setup, knownBaker }) => {
CONFIGS().forEach(({ lib, rpc, setup }) => {
const Tezos = lib;
describe(`Staking pseudo operations: ${rpc}`, () => {

beforeAll(async () => {
await setup(true);
// There is no baker accept staking in betanet and weeklylnet hence tests will fail
// Currently TF is a baker that allows staking on parisnet.
if (rpc.includes('paris')) {
knownBaker = 'tz3Q67aMz7gSMiQRcW729sXSfuMtkyAHYfqc' // TF
}
const delegateOp = await Tezos.contract.setDelegate({
delegate: knownBaker,
source: await Tezos.signer.publicKeyHash()
});
await delegateOp.confirmation();
try{
const address = await Tezos.signer.publicKeyHash()
if(await Tezos.rpc.getDelegate(address) !== address){
const delegateOp = await Tezos.contract.setDelegate({
delegate: 'tz1TGKSrZrBpND3PELJ43nVdyadoeiM1WMzb', // is a delegate receiving stake on parisnet and ghostnet
source: address
});
await delegateOp.confirmation();
};
}catch(e){console.log}
});

it('should throw an error when the destination specified is not the same as source', async () => {
it('should throw error when param is against pseudo operation', async () => {
expect(async () => {
const op = await Tezos.contract.stake({ amount: 1, to: 'tz1PZY3tEWmXGasYeehXYqwXuw2Z3iZ6QDnA' });
await op.confirmation()
}).rejects.toThrow(InvalidStakingAddressError);
expect(async () => {
const op = await Tezos.contract.unstake({ amount: 1, to: 'tz1PZY3tEWmXGasYeehXYqwXuw2Z3iZ6QDnA' });
await op.confirmation()
}).rejects.toThrow(InvalidStakingAddressError);
expect(async () => {
const op = await Tezos.contract.finalizeUnstake({ to: 'tz1PZY3tEWmXGasYeehXYqwXuw2Z3iZ6QDnA' });
await op.confirmation()
}).rejects.toThrow(InvalidStakingAddressError);
expect(async () => {
const op = await Tezos.contract.stake({
amount: 0.1,
to: knownBaker
});
}).rejects.toThrow();
const op = await Tezos.contract.finalizeUnstake({ amount: 1 });
await op.confirmation()
}).rejects.toThrow(InvalidFinalizeUnstakeAmountError);
});

it('should be able to stake funds to a designated delegate', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
import { CONFIGS } from '../../config';
import { InvalidStakingAddressError, InvalidFinalizeUnstakeAmountError } from '@taquito/core';

CONFIGS().forEach(({ lib, rpc, setup, knownBaker }) => {
CONFIGS().forEach(({ lib, rpc, setup }) => {
const Tezos = lib;
describe(`Test staking pseudo operations using: ${rpc}`, () => {
beforeAll(async () => {
await setup(true);
try {
// There is no baker accept staking in betanet and weeklylnet hence tests will fail
// Currently TF is a baker that allows staking on parisnet.
if (rpc.includes('paris')) {
knownBaker = 'tz3Q67aMz7gSMiQRcW729sXSfuMtkyAHYfqc' // TF
}
const delegateOp = await Tezos.contract.setDelegate({
delegate: knownBaker,
source: await Tezos.signer.publicKeyHash()
});
await delegateOp.confirmation();
} catch (e) {
console.log(JSON.stringify(e));
}
try{
const address = await Tezos.signer.publicKeyHash()
if(await Tezos.rpc.getDelegate(address) !== address){
const delegateOp = await Tezos.contract.setDelegate({
delegate: 'tz1TGKSrZrBpND3PELJ43nVdyadoeiM1WMzb', // is a delegate receiving stake on parisnet and ghostnet
source: address
});
await delegateOp.confirmation();
};
}catch(e){console.log}
});

it('should throw error when param is against pseudo operation', async () => {
expect(async () => {
const op = await Tezos.wallet.stake({ amount: 1, to: 'tz1PZY3tEWmXGasYeehXYqwXuw2Z3iZ6QDnA' }).send();
await op.confirmation()
}).rejects.toThrow(InvalidStakingAddressError);

expect(async () => {
const op = await Tezos.wallet.unstake({ amount: 1, to: 'tz1PZY3tEWmXGasYeehXYqwXuw2Z3iZ6QDnA' }).send();
await op.confirmation()
}).rejects.toThrow(InvalidStakingAddressError);
expect(async () => {
const op = await Tezos.wallet.finalizeUnstake({ to: 'tz1PZY3tEWmXGasYeehXYqwXuw2Z3iZ6QDnA' }).send();
await op.confirmation()
}).rejects.toThrow(InvalidStakingAddressError);
expect(async () => {
const op = await Tezos.wallet.finalizeUnstake({ amount: 1 }).send();
await op.confirmation()
}).rejects.toThrow(InvalidFinalizeUnstakeAmountError);
});

it(`should be able to stake successfully: ${rpc}`, async () => {
Expand All @@ -29,7 +45,7 @@ CONFIGS().forEach(({ lib, rpc, setup, knownBaker }) => {

const stakedBalance = await Tezos.rpc.getStakedBalance(await Tezos.signer.publicKeyHash());
// staked balance returned in mutez therefore dividing by 1000000 and rounding explanation here https://tezos-dev.slack.com/archives/C05RS0MEJ9H/p1714641691368019?thread_ts=1714604532.409029&cid=C05RS0MEJ9H
expect(Math.round(stakedBalance.toNumber() / 1000000)).toEqual(3);
expect(Math.round(stakedBalance.toNumber() / 1000000)).toBeGreaterThanOrEqual(3);
});

it(`should be able to unstake successfully: ${rpc}`, async () => {
Expand All @@ -39,28 +55,13 @@ CONFIGS().forEach(({ lib, rpc, setup, knownBaker }) => {

const UnstakedBalance = await Tezos.rpc.getUnstakedFrozenBalance(await Tezos.signer.publicKeyHash());
// unstaked balance returned in mutez therefore dividing by 1000000 and rounding explanation here https://tezos-dev.slack.com/archives/C05RS0MEJ9H/p1714641691368019?thread_ts=1714604532.409029&cid=C05RS0MEJ9H
expect(Math.round(UnstakedBalance.toNumber() / 1000000)).toEqual(1);
expect(Math.round(UnstakedBalance.toNumber() / 1000000)).toBeGreaterThanOrEqual(1);
});

it(`should be able to finalizeUnstake successfully: ${rpc}`, async () => {
const op = await Tezos.wallet.finalizeUnstake({}).send()
await op.confirmation();
expect(await op.status()).toBe('applied');
});

it('should throw error when param is against pseudo operation', async () => {
expect(async () => {
const op = await Tezos.wallet.stake({ amount: 1, to: 'tz1PZY3tEWmXGasYeehXYqwXuw2Z3iZ6QDnA' }).send();
}).rejects.toThrow(InvalidStakingAddressError);
expect(async () => {
const op = await Tezos.wallet.unstake({ amount: 1, to: 'tz1PZY3tEWmXGasYeehXYqwXuw2Z3iZ6QDnA' }).send();
}).rejects.toThrow(InvalidStakingAddressError);
expect(async () => {
const op = await Tezos.wallet.finalizeUnstake({ to: 'tz1PZY3tEWmXGasYeehXYqwXuw2Z3iZ6QDnA' }).send();
}).rejects.toThrow(InvalidStakingAddressError);
expect(async () => {
const op = await Tezos.wallet.finalizeUnstake({ amount: 1 }).send();
}).rejects.toThrow(InvalidFinalizeUnstakeAmountError);
});
});
});
});

0 comments on commit 203a9c3

Please sign in to comment.