diff --git a/src/procedures/__tests__/CreateCheckpoint.ts b/src/procedures/__tests__/CreateCheckpoint.ts index a8f7c0e..3228e5a 100644 --- a/src/procedures/__tests__/CreateCheckpoint.ts +++ b/src/procedures/__tests__/CreateCheckpoint.ts @@ -89,18 +89,19 @@ describe('CreateCheckpoint', () => { describe('createCheckpoint', () => { test('should add a transaction to the queue to create a new checkpoint', async () => { - const createCheckpointArgsSpy = sinon.spy(); + const createCheckpointArgsStub = sinon.stub(); + createCheckpointArgsStub.returns([{}]); const addTransactionStub = stub(target, 'addTransaction'); securityTokenMock.mock('createCheckpoint', Promise.resolve('CreateCheckpoint')); const { createCheckpoint } = securityTokenMock.getMockInstance(); - addTransactionStub.withArgs(createCheckpoint).returns(createCheckpointArgsSpy); + addTransactionStub.withArgs(createCheckpoint).returns(createCheckpointArgsStub); // Real call await target.prepareTransactions(); // Verifications - expect(createCheckpointArgsSpy.getCall(0).args[0]).toEqual({}); - expect(createCheckpointArgsSpy.callCount).toEqual(1); + expect(createCheckpointArgsStub.getCall(0).args[0]).toEqual({}); + expect(createCheckpointArgsStub.callCount).toEqual(1); expect( addTransactionStub .getCall(0) diff --git a/src/procedures/__tests__/CreateDividendDistribution.ts b/src/procedures/__tests__/CreateDividendDistribution.ts index 4c99c4e..414ba89 100644 --- a/src/procedures/__tests__/CreateDividendDistribution.ts +++ b/src/procedures/__tests__/CreateDividendDistribution.ts @@ -127,6 +127,7 @@ describe('CreateDividendDistribution', () => { tokenAddress: params.erc20Address, }); expect(approveErc20ArgsSpy.callCount).toBe(1); + expect(createDividendWithCheckpointAndExclusionsArgsStub.getCall(0).args[0]).toEqual({ maturity: params.maturityDate, expiry: params.expiryDate, @@ -149,27 +150,35 @@ describe('CreateDividendDistribution', () => { expect(addTransactionStub.getCall(0).lastArg.tag).toEqual( PolyTransactionTag.CreateErc20DividendDistribution ); - expect(addTransactionStub.callCount).toEqual(1); + expect(addTransactionStub.callCount).toEqual(1); expect(addProcedureStub.callCount).toEqual(1); }); test('should send add a transaction to the queue to create arc20 dividend distribution with taxWitholding data', async () => { + const taxWithholdingAddress = '0x5555555555555555555555555555555555555555'; + const taxWithholdingPercentage = 50; target = new CreateDividendDistribution( { ...params, taxWithholdings: [ { - address: '0x5555555555555555555555555555555555555555', - percentage: 50, + address: taxWithholdingAddress, + percentage: taxWithholdingPercentage, }, ], }, contextMock.getMockInstance() ); - const addProcedureSpy = spy(target, 'addProcedure'); - const createDividendWithCheckpointAndExclusionsArgsSpy = sinon.spy(); - const setWithholdingArgsSpy = sinon.spy(); + const approveErc20ArgsSpy = sinon.spy(); + const addProcedureStub = stub(target, 'addProcedure'); + addProcedureStub.withArgs(ApproveErc20).returns(approveErc20ArgsSpy); + + const createDividendWithCheckpointAndExclusionsArgsStub = sinon.stub(); + createDividendWithCheckpointAndExclusionsArgsStub.returns([{}]); + const setWithholdingArgsStub = sinon.stub(); + setWithholdingArgsStub.returns([{}]); + const addTransactionStub = stub(target, 'addTransaction'); erc20DividendsMock.mock( 'createDividendWithCheckpointAndExclusions', @@ -181,16 +190,22 @@ describe('CreateDividendDistribution', () => { const { setWithholding } = erc20DividendsMock.getMockInstance(); addTransactionStub .withArgs(createDividendWithCheckpointAndExclusions) - .returns(createDividendWithCheckpointAndExclusionsArgsSpy); - addTransactionStub.withArgs(setWithholding).returns(setWithholdingArgsSpy); + .returns(createDividendWithCheckpointAndExclusionsArgsStub); + addTransactionStub.withArgs(setWithholding).returns(setWithholdingArgsStub); // Real call await target.prepareTransactions(); // Verifications - expect(addProcedureSpy.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true); + expect(addProcedureStub.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true); + expect(approveErc20ArgsSpy.getCall(0).args[0]).toEqual({ + amount: params.amount, + spender: dividendsModuleAddress, + tokenAddress: params.erc20Address, + }); + expect(approveErc20ArgsSpy.callCount).toBe(1); - expect(createDividendWithCheckpointAndExclusionsArgsSpy.getCall(0).args[0]).toEqual({ + expect(createDividendWithCheckpointAndExclusionsArgsStub.getCall(0).args[0]).toEqual({ maturity: params.maturityDate, expiry: params.expiryDate, token: params.erc20Address, @@ -199,7 +214,7 @@ describe('CreateDividendDistribution', () => { name: params.name, excluded: [], }); - expect(createDividendWithCheckpointAndExclusionsArgsSpy.callCount).toEqual(1); + expect(createDividendWithCheckpointAndExclusionsArgsStub.callCount).toEqual(1); expect( addTransactionStub .getCall(0) @@ -210,12 +225,12 @@ describe('CreateDividendDistribution', () => { expect(addTransactionStub.getCall(0).lastArg.tag).toEqual( PolyTransactionTag.CreateErc20DividendDistribution ); + expect(setWithholdingArgsStub.getCall(0).args[0].investors).toEqual([taxWithholdingAddress]); + expect(setWithholdingArgsStub.getCall(0).args[0].withholding[0].toNumber()).toEqual( + taxWithholdingPercentage + ); - expect(setWithholdingArgsSpy.getCall(0).args[0]).toEqual({ - investors: ['0x5555555555555555555555555555555555555555'], - withholding: [50], - }); - expect(setWithholdingArgsSpy.callCount).toEqual(1); + expect(setWithholdingArgsStub.callCount).toEqual(1); expect( addTransactionStub .getCall(1) @@ -224,8 +239,9 @@ describe('CreateDividendDistribution', () => { expect(addTransactionStub.getCall(1).lastArg.tag).toEqual( PolyTransactionTag.SetErc20TaxWithholding ); + expect(addTransactionStub.callCount).toEqual(2); - expect(addProcedureSpy.callCount).toEqual(1); + expect(addProcedureStub.callCount).toEqual(1); }); test('should throw if there is no valid security token supplied', async () => { diff --git a/src/procedures/__tests__/CreateSecurityToken.ts b/src/procedures/__tests__/CreateSecurityToken.ts index 62e7f3f..3399ff4 100644 --- a/src/procedures/__tests__/CreateSecurityToken.ts +++ b/src/procedures/__tests__/CreateSecurityToken.ts @@ -152,8 +152,13 @@ describe('CreateSecurityToken', () => { }); test('should add the transaction to the queue to create the security token and approve erc20 transfer', async () => { - const addProcedureSpy = spy(target, 'addProcedure'); - const generateNewSecurityTokenArgsSpy = sinon.spy(); + const approveErc20ArgsSpy = sinon.spy(); + const addProcedureStub = stub(target, 'addProcedure'); + addProcedureStub.withArgs(ApproveErc20).returns(approveErc20ArgsSpy); + + const generateNewSecurityTokenArgsStub = sinon.stub(); + generateNewSecurityTokenArgsStub.returns([{}]); + const addTransactionStub = stub(target, 'addTransaction'); securityTokenRegistryMock.mock( 'generateNewSecurityToken', @@ -162,13 +167,19 @@ describe('CreateSecurityToken', () => { const { generateNewSecurityToken } = securityTokenRegistryMock.getMockInstance(); addTransactionStub .withArgs(generateNewSecurityToken) - .returns(generateNewSecurityTokenArgsSpy); + .returns(generateNewSecurityTokenArgsStub); // Real call await target.prepareTransactions(); // Verifications - expect(generateNewSecurityTokenArgsSpy.getCall(0).args[0]).toEqual({ + expect(approveErc20ArgsSpy.getCall(0).args[0]).toEqual({ + amount: costInPoly, + spender: params.address, + }); + expect(approveErc20ArgsSpy.callCount).toBe(1); + + expect(generateNewSecurityTokenArgsStub.getCall(0).args[0]).toEqual({ name: params.name, ticker: params.symbol, tokenDetails: '', @@ -176,7 +187,7 @@ describe('CreateSecurityToken', () => { protocolVersion: '0', treasuryWallet: params.owner, }); - expect(generateNewSecurityTokenArgsSpy.callCount).toEqual(1); + expect(generateNewSecurityTokenArgsStub.callCount).toEqual(1); expect( addTransactionStub @@ -191,20 +202,71 @@ describe('CreateSecurityToken', () => { PolyTransactionTag.CreateSecurityToken ); expect(addTransactionStub.callCount).toEqual(1); - expect(addProcedureSpy.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true); - expect(addProcedureSpy.callCount).toEqual(1); + expect(addProcedureStub.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true); + expect(addProcedureStub.callCount).toEqual(1); }); test('should add the transaction to the queue to create the security token with a treasury wallet', async () => { + const customTreasuryWallet = '0x5555555555555555555555555555555555555555'; target = new CreateSecurityToken( { ...params, - treasuryWallet: '0x5', // Extra argument of treasuryWallet + treasuryWallet: customTreasuryWallet, // Extra argument of treasuryWallet }, contextMock.getMockInstance() ); + const approveErc20ArgsSpy = sinon.spy(); + const addProcedureStub = stub(target, 'addProcedure'); + addProcedureStub.withArgs(ApproveErc20).returns(approveErc20ArgsSpy); + + const generateNewSecurityTokenArgsStub = sinon.stub(); + generateNewSecurityTokenArgsStub.returns([{}]); + + const addTransactionStub = stub(target, 'addTransaction'); + securityTokenRegistryMock.mock( + 'generateNewSecurityToken', + Promise.resolve('GenerateNewSecurityToken') + ); + const { generateNewSecurityToken } = securityTokenRegistryMock.getMockInstance(); + addTransactionStub + .withArgs(generateNewSecurityToken) + .returns(generateNewSecurityTokenArgsStub); + // Real call await target.prepareTransactions(); + + // Verifications + expect(approveErc20ArgsSpy.getCall(0).args[0]).toEqual({ + amount: costInPoly, + spender: params.address, + }); + expect(approveErc20ArgsSpy.callCount).toBe(1); + + expect(generateNewSecurityTokenArgsStub.getCall(0).args[0]).toEqual({ + name: params.name, + ticker: params.symbol, + tokenDetails: '', + divisible: params.divisible, + protocolVersion: '0', + treasuryWallet: customTreasuryWallet, + }); + expect(generateNewSecurityTokenArgsStub.callCount).toEqual(1); + + expect( + addTransactionStub + .getCall(0) + .calledWith(securityTokenRegistryMock.getMockInstance().generateNewSecurityToken) + ).toEqual(true); + expect(addTransactionStub.getCall(0).lastArg.fees).toEqual({ + usd: costInUsd, + poly: costInPoly, + }); + expect(addTransactionStub.getCall(0).lastArg.tag).toEqual( + PolyTransactionTag.CreateSecurityToken + ); + expect(addTransactionStub.callCount).toEqual(1); + expect(addProcedureStub.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true); + expect(addProcedureStub.callCount).toEqual(1); }); }); });