Skip to content

Commit f5e6a19

Browse files
authored
Merge pull request #879 from PolymathNetwork/vrtm-safemath-fix
VRTM safemath fix
2 parents e6b501d + cc1b358 commit f5e6a19

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

contracts/modules/TransferManager/VRTM/VolumeRestrictionTM.sol

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,11 @@ contract VolumeRestrictionTM is VolumeRestrictionTMStorage, TransferManager {
988988
} else {
989989
_allowedAmount = _restriction.allowedTokens;
990990
}
991-
return _allowedAmount.sub(_sumOfLastPeriod);
991+
// Due to `controllerTransfer()` function there is a chances where sumOfLastPeriod can be greater than
992+
// allowedAmount, because of this if another transaction is performed it get reverted if we directly rely
993+
// on the SafeMath sub method. So to avoid revert here we are first checking whether the substration is valid or
994+
// not if not then return 0 as allowed amount otherwise the actual value of allowed amount.
995+
return _sumOfLastPeriod >_allowedAmount ? 0 : _allowedAmount - _sumOfLastPeriod;
992996
}
993997

994998
function _updateStorage(
@@ -1120,7 +1124,7 @@ contract VolumeRestrictionTM is VolumeRestrictionTMStorage, TransferManager {
11201124
else if (allowedAmountToTransact == 0)
11211125
return currentBalance;
11221126
else
1123-
return (currentBalance.sub(allowedAmountToTransact));
1127+
return currentBalance > allowedAmountToTransact ? currentBalance - allowedAmountToTransact : 0;
11241128
}
11251129
else if (_partition == UNLOCKED) {
11261130
if (allowedAmountToTransact == 0 && fromTimestamp == 0 && dailyTime == 0)

test/y_volume_restriction_tm.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,8 +1938,8 @@ contract('VolumeRestrictionTransferManager', accounts => {
19381938
await print(data, account_delegate2);
19391939
console.log(data[4].toString());
19401940

1941-
// get the trade amount using the timestamp
1942-
let amt = web3.utils.fromWei(await I_VolumeRestrictionTM.getTotalTradedByUser.call(account_delegate2, data[0].toString()));
1941+
// get the trade amount using the timestamp
1942+
let amt = web3.utils.fromWei(await I_VolumeRestrictionTM.getTotalTradedByUser.call(account_delegate2, data[0].toString()));
19431943
// Verify the storage changes
19441944
assert.equal(data[0].toNumber(), startTime + duration.days(data[2].toNumber()));
19451945
assert.equal(data[2].toNumber(), 6);
@@ -1995,6 +1995,59 @@ contract('VolumeRestrictionTransferManager', accounts => {
19951995
});
19961996
});
19971997

1998+
describe("Test the edge case which cause transfer revert instead of INVALID return because of controllerTransfer", async() => {
1999+
2000+
it("Archiving the existing VRTM and add a new one", async() => {
2001+
await I_SecurityToken.archiveModule(I_VolumeRestrictionTM.address, {from: token_owner});
2002+
let tx = await I_SecurityToken.addModule(I_VolumeRestrictionTMFactory.address, "0x0", new BN(0), new BN(0), false, {from: token_owner });
2003+
assert.equal(tx.logs[2].args._moduleFactory, I_VolumeRestrictionTMFactory.address);
2004+
assert.equal(
2005+
web3.utils.toUtf8(tx.logs[2].args._name),
2006+
"VolumeRestrictionTM",
2007+
"VolumeRestrictionTMFactory doesn not added");
2008+
I_VolumeRestrictionTM = await VolumeRestrictionTM.at(tx.logs[2].args._module);
2009+
});
2010+
2011+
it("Add default restriction", async() => {
2012+
let newLatestTime = await getLatestTime();
2013+
await I_VolumeRestrictionTM.addDefaultRestriction(
2014+
new BN(web3.utils.toWei("10")),
2015+
0,
2016+
5,
2017+
newLatestTime.add(new BN(duration.days(10))),
2018+
0,
2019+
{
2020+
from: token_owner
2021+
}
2022+
);
2023+
2024+
let data = await I_VolumeRestrictionTM.getDefaultRestriction.call();
2025+
assert.equal(data[0].toString(), new BN(web3.utils.toWei("10")));
2026+
assert.equal(data[2].toString(), 5);
2027+
let dataRestriction = await I_VolumeRestrictionTM.getDefaultRestriction.call();
2028+
console.log(`
2029+
*** Add Individual restriction data ***
2030+
Allowed Tokens: ${dataRestriction[0].div(new BN(10).pow(new BN(18))).toString()}
2031+
StartTime : ${dataRestriction[1].toString()}
2032+
Rolling Period in days : ${dataRestriction[2].toString()}
2033+
EndTime : ${dataRestriction[3].toString()}
2034+
Type of Restriction: ${dataRestriction[4].toString()}
2035+
`);
2036+
});
2037+
2038+
it("Should transfer funds using the controller transfer", async() => {
2039+
await increaseTime(duration.minutes(123));
2040+
await I_SecurityToken.setController(token_owner, {from: token_owner});
2041+
console.log(`Balance of the account investor 1: ${await I_SecurityToken.balanceOf.call(account_investor1)}`);
2042+
await I_SecurityToken.controllerTransfer(account_investor1, account_investor2, new BN(web3.utils.toWei("11")), "0x0", "0x0", {from: token_owner});
2043+
});
2044+
2045+
it("Should also allow transfer the funds using the controller transfer even it surpasses the restriction limit", async() => {
2046+
await I_SecurityToken.controllerTransfer(account_investor1, account_investor2, new BN(web3.utils.toWei("5")), "0x0", "0x0", {from: token_owner});
2047+
});
2048+
2049+
});
2050+
19982051
describe("VolumeRestriction Transfer Manager Factory test cases", async() => {
19992052

20002053
it("Should get the exact details of the factory", async() => {

0 commit comments

Comments
 (0)