From 12f55f126b3a5a535c71907f33333dfda0c1ae10 Mon Sep 17 00:00:00 2001 From: Alireza Haghshenas Date: Tue, 26 Sep 2023 09:40:37 -0700 Subject: [PATCH] test: implement unit tests for stake, unstake, and finalizeUnstake --- .../estimate/rpc-estimate-provider.spec.ts | 97 ++++++++ .../test/prepare/prepare-provider.spec.ts | 233 ++++++++++++++++++ 2 files changed, 330 insertions(+) diff --git a/packages/taquito/test/estimate/rpc-estimate-provider.spec.ts b/packages/taquito/test/estimate/rpc-estimate-provider.spec.ts index 84683f47f8..c42326a6ee 100644 --- a/packages/taquito/test/estimate/rpc-estimate-provider.spec.ts +++ b/packages/taquito/test/estimate/rpc-estimate-provider.spec.ts @@ -1588,4 +1588,101 @@ describe('RPCEstimateProvider test wallet', () => { done(); }); }); + + describe('stake', () => { + it('return the correct estimate for stake', async (done) => { + mockRpcClient.simulateOperation.mockResolvedValue({ + contents: [ + { + kind: 'transaction', + fee: 10017, + metadata: { + operation_result: { status: 'applied', consumed_milligas: '10013000' }, + }, + parameters: { + entrypoint: 'stake', + value: { + prim: 'Unit', + }, + }, + }, + ], + }); + // Simulate real op size + mockForger.forge.mockResolvedValue(new Array(149).fill('aa').join('')); + const estimate = await estimateProvider.stake({ + amount: 10000, + }); + expect(estimate).toMatchObject({ + gasLimit: 10113, + storageLimit: 0, + suggestedFeeMutez: 1361, + }); + done(); + }); + }); + + describe('unstake', () => { + it('return the correct estimate for unstake', async (done) => { + mockRpcClient.simulateOperation.mockResolvedValue({ + contents: [ + { + kind: 'transaction', + fee: 10018, + metadata: { + operation_result: { status: 'applied', consumed_milligas: '10014000' }, + }, + parameters: { + entrypoint: 'unstake', + value: { + prim: 'Unit', + }, + }, + }, + ], + }); + // Simulate real op size + mockForger.forge.mockResolvedValue(new Array(149).fill('aa').join('')); + const estimate = await estimateProvider.unstake({ + amount: 10000, + }); + expect(estimate).toMatchObject({ + gasLimit: 10114, + storageLimit: 0, + suggestedFeeMutez: 1361, + }); + done(); + }); + }); + + describe('finalizeUnstake', () => { + it('return the correct estimate for finalizeUnstake', async (done) => { + mockRpcClient.simulateOperation.mockResolvedValue({ + contents: [ + { + kind: 'transaction', + fee: 10018, + metadata: { + operation_result: { status: 'applied', consumed_milligas: '10014000' }, + }, + parameters: { + entrypoint: 'finalize_unstake', + value: { + prim: 'Unit', + }, + }, + }, + ], + }); + // Simulate real op size + mockForger.forge.mockResolvedValue(new Array(149).fill('aa').join('')); + const estimate = await estimateProvider.finalizeUnstake({}); + expect(estimate).toMatchObject({ + gasLimit: 10114, + storageLimit: 0, + suggestedFeeMutez: 1361, + }); + done(); + }); + }); }); diff --git a/packages/taquito/test/prepare/prepare-provider.spec.ts b/packages/taquito/test/prepare/prepare-provider.spec.ts index ee2a298753..2ae5baa8de 100644 --- a/packages/taquito/test/prepare/prepare-provider.spec.ts +++ b/packages/taquito/test/prepare/prepare-provider.spec.ts @@ -1114,4 +1114,237 @@ describe('PrepareProvider test', () => { done(); }); }); + + describe('stake', () => { + it('should return a prepared stake operation with reveal operation', async () => { + mockReadProvider.isAccountRevealed.mockResolvedValue(false); + + const prepared = await prepareProvider.stake({ + amount: 10000, + }); + + expect(prepared).toEqual({ + opOb: { + branch: 'test_block_hash', + contents: [ + { + kind: 'reveal', + fee: '374', + public_key: 'test_pub_key', + source: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + gas_limit: '1100', + storage_limit: '0', + counter: '1', + }, + { + amount: '10000000000', + kind: 'transaction', + source: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + fee: '0', + gas_limit: '1040000', + storage_limit: '60000', + destination: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + counter: '2', + parameters: { + entrypoint: 'stake', + value: { + prim: 'Unit', + }, + }, + }, + ], + protocol: 'test_protocol', + }, + counter: 0, + }); + }); + + it('should return a prepared stake op without reveal operation when account is revealed', async () => { + mockReadProvider.isAccountRevealed.mockResolvedValue(true); + + const prepared = await prepareProvider.stake({ + amount: 10000, + }); + + expect(prepared).toEqual({ + opOb: { + branch: 'test_block_hash', + contents: [ + { + amount: '10000000000', + kind: 'transaction', + source: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + fee: '0', + gas_limit: '1040000', + storage_limit: '60000', + destination: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + counter: '1', + parameters: { + entrypoint: 'stake', + value: { + prim: 'Unit', + }, + }, + }, + ], + protocol: 'test_protocol', + }, + counter: 0, + }); + }); + }); + + describe('unstake', () => { + it('should return a prepared unstake operation with reveal operation', async () => { + mockReadProvider.isAccountRevealed.mockResolvedValue(false); + + const prepared = await prepareProvider.unstake({ + amount: 10000, + }); + + expect(prepared).toEqual({ + opOb: { + branch: 'test_block_hash', + contents: [ + { + kind: 'reveal', + fee: '374', + public_key: 'test_pub_key', + source: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + gas_limit: '1100', + storage_limit: '0', + counter: '1', + }, + { + amount: '0', + kind: 'transaction', + source: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + fee: '0', + gas_limit: '1040000', + storage_limit: '60000', + destination: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + counter: '2', + parameters: { + entrypoint: 'unstake', + value: { + int: '10000000000', + }, + }, + }, + ], + protocol: 'test_protocol', + }, + counter: 0, + }); + }); + + it('should return a prepared unstake op without reveal operation when account is revealed', async () => { + mockReadProvider.isAccountRevealed.mockResolvedValue(true); + + const prepared = await prepareProvider.unstake({ + amount: 10000, + }); + + expect(prepared).toEqual({ + opOb: { + branch: 'test_block_hash', + contents: [ + { + amount: '0', + kind: 'transaction', + source: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + fee: '0', + gas_limit: '1040000', + storage_limit: '60000', + destination: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + counter: '1', + parameters: { + entrypoint: 'unstake', + value: { + int: '10000000000', + }, + }, + }, + ], + protocol: 'test_protocol', + }, + counter: 0, + }); + }); + }); + + describe('finalizeUnstake', () => { + it('should return a prepared finalize_unstake operation with reveal operation', async () => { + mockReadProvider.isAccountRevealed.mockResolvedValue(false); + + const prepared = await prepareProvider.finalizeUnstake({}); + + expect(prepared).toEqual({ + opOb: { + branch: 'test_block_hash', + contents: [ + { + kind: 'reveal', + fee: '374', + public_key: 'test_pub_key', + source: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + gas_limit: '1100', + storage_limit: '0', + counter: '1', + }, + { + amount: '0', + kind: 'transaction', + source: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + fee: '0', + gas_limit: '1040000', + storage_limit: '60000', + destination: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + counter: '2', + parameters: { + entrypoint: 'finalize_unstake', + value: { + prim: 'Unit', + }, + }, + }, + ], + protocol: 'test_protocol', + }, + counter: 0, + }); + }); + + it('should return a prepared finalize_unstake op without reveal operation when account is revealed', async () => { + mockReadProvider.isAccountRevealed.mockResolvedValue(true); + + const prepared = await prepareProvider.finalizeUnstake({}); + + expect(prepared).toEqual({ + opOb: { + branch: 'test_block_hash', + contents: [ + { + amount: '0', + kind: 'transaction', + source: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + fee: '0', + gas_limit: '1040000', + storage_limit: '60000', + destination: 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', + counter: '1', + parameters: { + entrypoint: 'finalize_unstake', + value: { + prim: 'Unit', + }, + }, + }, + ], + protocol: 'test_protocol', + }, + counter: 0, + }); + }); + }); });