Skip to content

Commit

Permalink
test: updated wallet/staking tests to be robust and walletAPI desc to…
Browse files Browse the repository at this point in the history
… be accurate
  • Loading branch information
hui-an-yang committed May 3, 2024
1 parent ff4e343 commit af3d5b6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { Protocols } from '@taquito/taquito';
import { ProtoGreaterOrEqual } from '@taquito/michel-codec';
import { InvalidStakingAddressError, InvalidFinalizeUnstakeAmountError } from '@taquito/core';

CONFIGS().forEach(({ lib, rpc, setup, protocol }) => {
CONFIGS().forEach(({ lib, rpc, setup, protocol, knownBaker }) => {
const Tezos = lib;
const parisAndAlpha = ProtoGreaterOrEqual(protocol, Protocols.PtParisBQ) ? test : test.skip;
describe(`Test staking pseudo operations using: ${rpc}`, () => {
beforeAll(async () => {
await setup(true);
try {
const delegateOp = await Tezos.contract.setDelegate({
delegate: 'tz1PZY3tEWmXGasYeehXYqwXuw2Z3iZ6QDnA', // can use knownBaker in future
delegate: knownBaker,
source: await Tezos.signer.publicKeyHash()
});
await delegateOp.confirmation();
Expand All @@ -21,27 +21,27 @@ CONFIGS().forEach(({ lib, rpc, setup, protocol }) => {
});

parisAndAlpha(`should be able to stake successfully: ${rpc}`, async () => {
const op = await Tezos.wallet.stake({ amount: 3000000, mutez: true }).send()
const op = await Tezos.wallet.stake({ amount: 3 }).send()
await op.confirmation();
expect(op.status).toBeTruthy();
expect(await op.status()).toBe('applied');

const stakedBalance = await Tezos.rpc.getStakedBalance(await Tezos.signer.publicKeyHash());
expect(stakedBalance.toNumber()).toEqual(3000000);
expect(Math.round(stakedBalance.toNumber()/1000000)).toEqual(3); // staked balance returned in mutez therefore dividing by 1000000
});

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

const UnstakedBalance = await Tezos.rpc.getUnstakedFrozenBalance(await Tezos.signer.publicKeyHash());
expect(UnstakedBalance.toNumber()).toEqual(1000000); // 1000000 mutez = 1 tez
expect(Math.round(UnstakedBalance.toNumber()/1000000)).toEqual(1); // unstaked balance returned in mutez therefore dividing by 1000000
});

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

parisAndAlpha('should throw error when param is against pseudo operation', async () => {
Expand Down
53 changes: 3 additions & 50 deletions packages/taquito/src/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ export class WalletOperationBatch {
) {}

/**
*
* @description Add a transaction operation to the batch
*
* @param params Transfer operation parameter
*/
withTransfer(params: WalletTransferParams) {
Expand All @@ -69,9 +67,7 @@ export class WalletOperationBatch {
}

/**
*
* @description Add a contract call to the batch
*
* @param params Call a contract method
* @param options Generic operation parameters
*/
Expand All @@ -83,9 +79,7 @@ export class WalletOperationBatch {
}

/**
*
* @description Add a delegation operation to the batch
*
* @param params Delegation operation parameter
*/
withDelegation(params: WalletDelegateParams) {
Expand All @@ -98,9 +92,7 @@ export class WalletOperationBatch {
}

/**
*
* @description Add an origination operation to the batch
*
* @param params Origination operation parameter
*/
withOrigination<TWallet extends DefaultWalletType = DefaultWalletType>(
Expand All @@ -111,9 +103,7 @@ export class WalletOperationBatch {
}

/**
*
* @description Add an IncreasePaidStorage operation to the batch
*
* @param param IncreasePaidStorage operation parameter
*/
withIncreasePaidStorage(params: WalletIncreasePaidStorageParams) {
Expand Down Expand Up @@ -145,9 +135,7 @@ export class WalletOperationBatch {
}

/**
*
* @description Add a group operation to the batch. Operation will be applied in the order they are in the params array
*
* @param params Operations parameter
* @throws {@link InvalidOperationKindError}
*/
Expand Down Expand Up @@ -175,9 +163,7 @@ export class WalletOperationBatch {
}

/**
*
* @description Submit batch operation to wallet
*
*/
async send() {
const ops: WalletParamsWithKind[] = [];
Expand All @@ -204,7 +190,6 @@ export class Wallet {

/**
* @description Retrieve the PKH of the account that is currently in use by the wallet
*
* @param option Option to use while fetching the PKH.
* If forceRefetch is specified the wallet provider implementation will refetch the PKH from the wallet
*/
Expand All @@ -217,7 +202,6 @@ export class Wallet {

/**
* @description Retrieve the PK of the account that is currently in use by the wallet
*
* @param option Option to use while fetching the PK.
* If forceRefetch is specified the wallet provider implementation will refetch the PK from the wallet
*/
Expand All @@ -235,11 +219,8 @@ export class Wallet {
};

/**
*
* @description Originate a new contract according to the script in parameters.
*
* @returns An operation handle with the result from the rpc node
*
* @param originateParams Originate operation parameter
*/
originate<TWallet extends DefaultWalletType = DefaultWalletType>(
Expand All @@ -259,11 +240,8 @@ export class Wallet {
}

/**
*
* @description Set the delegate for a contract.
*
* @returns An operation handle with the result from the rpc node
*
* @param delegateParams operation parameter
*/
setDelegate(params: WalletDelegateParams) {
Expand All @@ -281,11 +259,8 @@ export class Wallet {
}

/**
*
* @description failing_noop operation that is guaranteed to fail. DISCLAIMER: Not all wallets support signing failing_noop operations.
*
* @returns Signature for a failing_noop
*
* @param params operation parameter
*/
async signFailingNoop(params: WalletFailingNoopParams) {
Expand Down Expand Up @@ -315,11 +290,8 @@ export class Wallet {
}

/**
*
* @description Register the current address as delegate.
*
* @returns An operation handle with the result from the rpc node
*
*/
registerDelegate() {
return this.walletCommand(async () => {
Expand All @@ -333,11 +305,8 @@ export class Wallet {
}

/**
*
* @description Transfer tezos tokens from current address to a specific address or call a smart contract.
*
* @returns A wallet command from which we can send the operation to the wallet
*
* @param params operation parameter
*/
transfer(params: WalletTransferParams) {
Expand All @@ -355,11 +324,8 @@ export class Wallet {
}

/**
*
* @description Stake a given amount for the source address
*
* @returns An operation handle with the result from the rpc node
*
* @returns A wallet command from which we can send the operation to the wallet
* @param Stake pseudo-operation parameter
*/
stake(params: WalletStakeParams) {
Expand All @@ -381,13 +347,10 @@ export class Wallet {
}

/**
*
* @description Unstake the given amount. If "everything" is given as amount, unstakes everything from the staking balance.
* Unstaked tez remains frozen for a set amount of cycles (the slashing period) after the operation. Once this period is over,
* the operation "finalize unstake" must be called for the funds to appear in the liquid balance.
*
* @returns An operation handle with the result from the rpc node
*
* @returns A wallet command from which we can send the operation to the wallet
* @param Unstake pseudo-operation parameter
*/
unstake(params: WalletUnstakeParams) {
Expand All @@ -409,10 +372,8 @@ export class Wallet {
}

/**
*
* @description Transfer all the finalizable unstaked funds of the source to their liquid balance
* @returns An operation handle with the result from the rpc node
*
* @returns A wallet command from which we can send the operation to the wallet
* @param Finalize_unstake pseudo-operation parameter
*/
finalizeUnstake(params: WalletFinalizeUnstakeParams) {
Expand Down Expand Up @@ -442,11 +403,8 @@ export class Wallet {
}

/**
*
* @description Increase the paid storage of a smart contract.
*
* @returns A wallet command from which we can send the operation to the wallet
*
* @param params operation parameter
*/
increasePaidStorage(params: WalletIncreasePaidStorageParams) {
Expand All @@ -464,11 +422,8 @@ export class Wallet {
}

/**
*
* @description Create a batch of operation
*
* @returns A batch object from which we can add more operation or send a command to the wallet to execute the batch
*
* @param params List of operation to initialize the batch with
*/
batch(params?: Parameters<WalletOperationBatch['with']>[0]) {
Expand All @@ -482,10 +437,8 @@ export class Wallet {
}

/**
*
* @description Create an smart contract abstraction for the address specified. Calling entrypoints with the returned
* smart contract abstraction will leverage the wallet provider to make smart contract calls
*
* @param address Smart contract address
* @throws {@link InvalidContractAddressError} If the contract address is not valid
*/
Expand Down

0 comments on commit af3d5b6

Please sign in to comment.