Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

Commit

Permalink
fix: 🐛 cappedSTO now has finalize method
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Wiebe committed Sep 25, 2019
1 parent 80abfec commit 49a36af
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
29 changes: 27 additions & 2 deletions src/contract_wrappers/modules/sto/capped_sto_wrapper/3.1.0.ts
Expand Up @@ -4,7 +4,16 @@ import functionsUtils from '../../../../utils/functions_utils';
import CappedSTOCommon, { BuyTokensWithPolyParams, BuyTokensParams } from './common';
import assert from '../../../../utils/assert';
import {
ErrorCode, ContractVersion, SubscribeAsyncParams, GetLogsAsyncParams, Subscribe, GetLogs, FULL_DECIMALS, FundRaiseType, Constructor,
ErrorCode,
ContractVersion,
SubscribeAsyncParams,
GetLogsAsyncParams,
Subscribe,
GetLogs,
FULL_DECIMALS,
FundRaiseType,
Constructor,
TxParams,
} from '../../../../types';
import {
valueToWei, bigNumberToDate, weiToValue,
Expand Down Expand Up @@ -251,6 +260,22 @@ export class CappedSTO_3_1_0 extends CappedSTOBase_3_1_0 {
);
};

/**
* Finalizes the STO and mint remaining tokens to reserve address
* Reserve address must be whitelisted to successfully finalize
*/
public finalize = async (params: TxParams) => {
assert.assert(
await this.isCallerTheSecurityTokenOwner(params.txData),
ErrorCode.Unauthorized,
'The caller must be the ST owner',
);
assert.assert(!(await this.isFinalized()), ErrorCode.PreconditionRequired, 'STO is already finalized');
// we can't execute mint to validate the method
return (await this.contract).finalize.sendTransactionAsync(params.txData, params.safetyFactor);
};


/**
* Subscribe to an event type emitted by the contract.
* @return Subscription token used later to unsubscribe
Expand Down Expand Up @@ -293,4 +318,4 @@ export class CappedSTO_3_1_0 extends CappedSTOBase_3_1_0 {

export function isCappedSTO_3_1_0(wrapper: CappedSTOCommon): wrapper is CappedSTO_3_1_0 {
return wrapper.contractVersion === ContractVersion.V3_1_0;
};
};
Expand Up @@ -3,7 +3,7 @@ import { mock, instance, reset, when, verify, objectContaining } from 'ts-mockit
import {
CappedSTOContract_3_1_0,
ISecurityTokenContract_3_0_0,
PolyTokenContract_3_0_0,
PolyTokenContract_3_0_0,
BigNumber,
Web3Wrapper,
PolyTokenEvents_3_0_0,
Expand All @@ -15,7 +15,7 @@ import ContractFactory from '../../../../../factories/contractFactory';
import { valueToWei, weiToValue, dateToBigNumber } from '../../../../../utils/convert';
import { FULL_DECIMALS, FundRaiseType } from '../../../../../types';

describe('Capped STO 3.1.0', () => {
describe('Capped STO 3.1.0', () => {
let target: CappedSTO_3_1_0;
let mockedWrapper: Web3Wrapper;
let mockedContract: CappedSTOContract_3_1_0;
Expand Down Expand Up @@ -337,6 +337,68 @@ describe('Capped STO 3.1.0', () => {
});
});

describe('Finalize', () => {
test('should finalize STO', async () => {
// Address expected
const expectedIsFinalizedResult = false;
// Mocked method
const mockedIsFinalizedMethod = mock(MockedCallMethod);
// Stub the method
when(mockedContract.isFinalized).thenReturn(instance(mockedIsFinalizedMethod));
// Stub the request
when(mockedIsFinalizedMethod.callAsync()).thenResolve(expectedIsFinalizedResult);

// Mock Only Owner and Security Token
const expectedOwnerResult = '0x5555555555555555555555555555555555555555';
// Security Token Address expected
const expectedSecurityTokenAddress = '0x3333333333333333333333333333333333333333';
// Setup get Security Token Address
const mockedGetSecurityTokenAddressMethod = mock(MockedCallMethod);
when(mockedContract.securityToken).thenReturn(instance(mockedGetSecurityTokenAddressMethod));
when(mockedGetSecurityTokenAddressMethod.callAsync()).thenResolve(expectedSecurityTokenAddress);
when(mockedContractFactory.getSecurityTokenContract(expectedSecurityTokenAddress)).thenResolve(
instance(mockedSecurityTokenContract),
);
const mockedSecurityTokenOwnerMethod = mock(MockedCallMethod);
when(mockedSecurityTokenOwnerMethod.callAsync()).thenResolve(expectedOwnerResult);
when(mockedSecurityTokenContract.owner).thenReturn(instance(mockedSecurityTokenOwnerMethod));

// Mock web3 wrapper owner
when(mockedWrapper.getAvailableAddressesAsync()).thenResolve([expectedOwnerResult]);

const mockedParams = {
txData: {},
safetyFactor: 10,
};
const expectedResult = getMockedPolyResponse();
// Mocked method
const mockedMethod = mock(MockedSendMethod);
// Stub the method
when(mockedContract.finalize).thenReturn(instance(mockedMethod));
// Stub the request
when(mockedMethod.sendTransactionAsync(mockedParams.txData, mockedParams.safetyFactor)).thenResolve(
expectedResult,
);

// Real call
const result = await target.finalize(mockedParams);

// Result expectation
expect(result).toBe(expectedResult);
// Verifications
verify(mockedContract.finalize).once();
verify(mockedMethod.sendTransactionAsync(mockedParams.txData, mockedParams.safetyFactor)).once();
verify(mockedContract.securityToken).once();
verify(mockedGetSecurityTokenAddressMethod.callAsync()).once();
verify(mockedContractFactory.getSecurityTokenContract(expectedSecurityTokenAddress)).once();
verify(mockedSecurityTokenOwnerMethod.callAsync()).once();
verify(mockedSecurityTokenContract.owner).once();
verify(mockedContract.isFinalized).once();
verify(mockedIsFinalizedMethod.callAsync()).once();
verify(mockedWrapper.getAvailableAddressesAsync()).once();
});
});

describe('SubscribeAsync', () => {
test('should throw as eventName does not belong to CappedSTOEvents', async () => {
// Mocked parameters
Expand Down

0 comments on commit 49a36af

Please sign in to comment.