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

Commit 9378266

Browse files
committed
fix: 🐛 add assertions
1 parent 7c83e64 commit 9378266

File tree

1 file changed

+68
-4
lines changed

1 file changed

+68
-4
lines changed

src/contract_wrappers/modules/wallet/vesting_escrow_wallet_wrapper.ts

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,18 @@ import {
3333
GetLogs,
3434
TransferResult,
3535
Perm,
36+
TransferStatusCode,
3637
} from '../../../types';
37-
import { numberToBigNumber, valueToWei, dateToBigNumber } from '../../../utils/convert';
38+
import {
39+
numberToBigNumber,
40+
valueToWei,
41+
weiToValue,
42+
dateToBigNumber,
43+
bytes32ToString,
44+
bytes32ArrayToStringArray,
45+
} from '../../../utils/convert';
46+
47+
const TRANSFER_SUCCESS = '0x51';
3848

3949
interface AddScheduleSubscribeAsyncParams extends SubscribeAsyncParams {
4050
eventName: VestingEscrowWalletEvents.AddSchedule;
@@ -439,6 +449,7 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
439449
* Used to change the treasury wallet address
440450
*/
441451
public changeTreasuryWallet = async (params: ChangeTreasuryWalletParams) => {
452+
assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'The caller must be the ST owner');
442453
return (await this.contract).changeTreasuryWallet.sendTransactionAsync(
443454
params.newTreasuryWallet,
444455
params.txData,
@@ -450,6 +461,18 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
450461
* Used to deposit tokens from treasury wallet to the vesting escrow wallet
451462
*/
452463
public depositTokens = async (params: DepositTokensParams) => {
464+
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
465+
466+
assert.assert(params.numberOfTokens > 0, 'Number of tokens should be > 0');
467+
468+
const canTransferFromResult = await (await this.securityTokenContract()).canTransferFrom.callAsync(
469+
await this.getCallerAddress(params.txData),
470+
await this.address(),
471+
numberToBigNumber(params.numberOfTokens),
472+
'0x00',
473+
);
474+
assert.assert(canTransferFromResult[0] === TRANSFER_SUCCESS, 'Failed transferFrom');
475+
453476
return (await this.contract).depositTokens.sendTransactionAsync(
454477
numberToBigNumber(params.numberOfTokens),
455478
params.txData,
@@ -461,6 +484,19 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
461484
* Sends unassigned tokens to the treasury wallet
462485
*/
463486
public sendToTreasury = async (params: SendToTreasuryParams) => {
487+
assert.assert(await this.isCallerAllowed(params.txData, Perm.Operator), 'Caller is not allowed');
488+
assert.assert(params.amount > 0, 'Amount cannot be zero');
489+
490+
const unassignedTokens = (await this.unassignedTokens()).toNumber();
491+
assert.assert(params.amount <= unassignedTokens, 'Amount is greater than unassigned tokens');
492+
493+
const canTransferResult = await (await this.securityTokenContract()).canTransfer.callAsync(
494+
await this.getTreasuryWallet(),
495+
numberToBigNumber(params.amount),
496+
'0x00',
497+
);
498+
assert.assert(canTransferResult[0] === TRANSFER_SUCCESS, 'Transfer failed');
499+
464500
return (await this.contract).sendToTreasury.sendTransactionAsync(
465501
numberToBigNumber(params.amount),
466502
params.txData,
@@ -479,6 +515,8 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
479515
* Pushes available tokens to the beneficiary's address
480516
*/
481517
public pushAvailableTokens = async (params: PushAvailableTokensParams) => {
518+
assert.assert(await this.isCallerAllowed(params.txData, Perm.Operator), 'Caller is not allowed');
519+
482520
return (await this.contract).pushAvailableTokens.sendTransactionAsync(
483521
params.beneficiary,
484522
params.txData,
@@ -490,13 +528,18 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
490528
* Used to withdraw available tokens by beneficiary
491529
*/
492530
public pullAvailableTokens = async (params: TxParams) => {
531+
assert.assert(!(await this.paused()), 'Contract currently paused');
493532
return (await this.contract).pullAvailableTokens.sendTransactionAsync(params.txData, params.safetyFactor);
494533
};
495534

496535
/**
497536
* Adds template that can be used for creating schedule
498537
*/
499538
public addTemplate = async (params: AddTemplateParams) => {
539+
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
540+
assert.assert(params.name !== '', 'Invalid name');
541+
assert.assert(!(await this.getAllTemplateNames()).includes(params.name), 'Template name already exists');
542+
await this.validateTemplate(params.numberOfTokens, params.duration, params.frequency);
500543
return (await this.contract).addTemplate.sendTransactionAsync(
501544
params.name,
502545
numberToBigNumber(params.numberOfTokens),
@@ -511,6 +554,10 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
511554
* Removes template with a given name
512555
*/
513556
public removeTemplate = async (params: RemoveTemplateParams) => {
557+
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
558+
assert.assert(params.name !== '', 'Invalid name');
559+
assert.assert((await this.getAllTemplateNames()).includes(params.name), 'Template not found');
560+
// TODO 3.1: require(templateToUsers[_name].length == 0, "Template is used");
514561
return (await this.contract).removeTemplate.sendTransactionAsync(params.name, params.txData, params.safetyFactor);
515562
};
516563

@@ -519,21 +566,28 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
519566
* @return Count of the templates
520567
*/
521568
public getTemplateCount = async () => {
522-
return (await this.contract).getTreasuryWallet.callAsync();
569+
const result = await (await this.contract).getTemplateCount.callAsync();
570+
return result.toNumber();
523571
};
524572

525573
/**
526574
* Gets the list of the template names those can be used for creating schedule
527-
* @return bytes32 Array of all template names were created
575+
* @return Array of all template names were created
528576
*/
529577
public getAllTemplateNames = async () => {
530-
return (await this.contract).getTreasuryWallet.callAsync();
578+
const results = await (await this.contract).getAllTemplateNames.callAsync();
579+
return bytes32ArrayToStringArray(results);
531580
};
532581

533582
/**
534583
* Adds vesting schedules for each of the beneficiary's address
535584
*/
536585
public addSchedule = async (params: AddScheduleParams) => {
586+
assert.assert(await this.isCallerAllowed(params.txData, Perm.Admin), 'Caller is not allowed');
587+
assert.assert(params.templateName !== '', 'Invalid name');
588+
assert.assert(!(await this.getAllTemplateNames()).includes(params.templateName), 'Template name already exists');
589+
await this.validateTemplate(params.numberOfTokens, params.duration, params.frequency);
590+
// TODO: _addScheduleFromTemplate assertion
537591
let startTime = new BigNumber(0);
538592
if (params.startTime) {
539593
startTime = dateToBigNumber(params.startTime);
@@ -709,6 +763,16 @@ export default class VestingEscrowWalletWrapper extends ModuleWrapper {
709763
);
710764
};
711765

766+
private validateTemplate = async (numberOfTokens: number, duration: number, frequency: number) => {
767+
assert.assert(numberOfTokens > 0, 'Zero amount');
768+
assert.assert(duration % frequency === 0, 'Invalid frequency');
769+
const periodCount = duration / frequency;
770+
assert.assert(numberOfTokens % periodCount === 0, 'Invalid period count');
771+
const amountPerPeriod = numberOfTokens / periodCount;
772+
const granularity = await (await this.securityTokenContract()).granularity.callAsync();
773+
assert.assert(amountPerPeriod % granularity.toNumber() === 0, 'Invalid granularity');
774+
};
775+
712776
/**
713777
* Subscribe to an event type emitted by the contract.
714778
* @return Subscription token used later to unsubscribe

0 commit comments

Comments
 (0)