Skip to content

Maximum Volume Restrictions Manager - #320

Closed
vyomshm wants to merge 14 commits into
PolymathNetwork:development-1.5.0from
vyomshm:vol-restriction-module
Closed

vyomshm wants to merge 14 commits into
PolymathNetwork:development-1.5.0from
vyomshm:vol-restriction-module

Conversation

@vyomshm

@vyomshm vyomshm commented Oct 5, 2018 •

Copy link
Copy Markdown

Please check if the PR fulfills these requirements

  • The commit message follows our Submission guidelines
  • Tests for the changes have been added (for bug fixes / features)

What kind of change does this PR introduce?

feature - ref #267

What is the current behavior?

#267
No way to add global token volume transfer restrictions

What is the new behavior?

Feature added - A new Transfer Manager module
Issuers can now create(as well as remove and modify) multiple maximum volume transfer restrictions which will be applied globally to every transfer

Does this PR introduce a breaking change?

(What changes might users need to make in their application due to this PR?)
No

Any Other information:

@vyomshm
vyomshm force-pushed the vol-restriction-module branch from 948b1b5 to f70648d Compare October 5, 2018 13:43
@satyamakgec

Copy link
Copy Markdown
Contributor

Hey, @vyomshm let me know when should I start reviewing your code?

@vyomshm

vyomshm commented Oct 13, 2018 •

Copy link
Copy Markdown
Author

Hey @satyamakgec Please review my PR

@vyomshm
vyomshm force-pushed the vol-restriction-module branch from 1af87df to 9722fe4 Compare October 15, 2018 16:55
@vyomshm vyomshm changed the title [WIP] Maximum Volume Restrictions Manager Maximum Volume Restrictions Manager Oct 15, 2018
@vyomshm

vyomshm commented Oct 17, 2018

Copy link
Copy Markdown
Author

@satyamakgec @FOBCrypto I've made the suggested changes, please review again.

@maxsam4 maxsam4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the suggested changes, Can you please also increase the coverage of your code?
Branch coverage of MaximumVolumeTransferManager.sol is at 80% currently. We would ideally like to see 100% coverage but a minimum of 95% is required.

);

for(uint256 i = 0; i < _maximumVolumes.length; i++){
addMaxVolumeRestricion(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create an internal function and use it here as well as in addMaxVolumeRestricion(). Doing so will save gas as parameters are not copied into memory again for internal functions.

Comment thread scripts/test.sh Resolved
@maxsam4

maxsam4 commented Oct 19, 2018

Copy link
Copy Markdown
Contributor

@vyomshm Please push the changes before marking conversations as resolved.

@vyomshm

vyomshm commented Oct 21, 2018 •

Copy link
Copy Markdown
Author

@satyamakgec @maxsam4 @FOBCrypto I've increased my test coverage + implemented changes as discussed. Ready for another review👍

@maxsam4

maxsam4 commented Oct 21, 2018

Copy link
Copy Markdown
Contributor

Nice work @vyomshm !

The branch coverage is still at 92.5% with 3 uncovered branches. I would love for them to be covered as well (all of them can be covered). You can see the uncovered branches at https://coveralls.io/builds/19635412/source?filename=contracts%2Fmodules%2FTransferManager%2FMaximumVolumeTransferManager.sol

@vyomshm

vyomshm commented Oct 21, 2018

Copy link
Copy Markdown
Author

@maxsam4 @satyamakgec @FOBCrypto Coverage is now at 100%

@vyomshm

vyomshm commented Oct 21, 2018 •

Copy link
Copy Markdown
Author

@satyamakgec @FOBCrypto @maxsam4 made the changes. please have a look

@vyomshm

vyomshm commented Oct 22, 2018 •

Copy link
Copy Markdown
Author

@maxsam4 have you had a chance to look through this? Would love to see this get resolved before you guys get too busy with pre-devcon4 events :)

* @notice allows admin to delete a maximum volume transfer restriction
* @param _index representing the array index of the maximum volume transfer restriction
*/
function removeMaximumTransferRestriction(uint256 _index) public withPerm(ADMIN){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a batch/multi versions of this.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@satyamakgec @maxsam4 Currently, removing a single array element will almost always change the order of restrictions in the array( except when the restriction to be deleted in the last one). This could render some of the indices provided unusable and therefore cause the function to revert.

I could write an implementation which would preserve the order of restrictions by shifting the elements instead of swapping them(as implemented now) but doing so would make the function extremely costly in terms of gas.

string element = myArray[index];
for (uint i = index; i < myArray.length - 1; i++) {
  myArray[index] = myArray[index + 1];
}
myArray.length--;

Please advise on how to proceed with this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one way around the problem can be by adding a unique id to MaximumVolumeRestriction struct and maintaining an array of that id for parsing. maximumVolumeRestrictions can be made a mapping rather than an array then.

* @param _endTime timestamp representing when this maximum volume restriction ends
* @param _rollingPeriodInterval interval period representing one of five RollingIntervals
*/
function modifyMaximumTransferRestriction(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a batch/multi versions of this.

@maxsam4 maxsam4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going through the logic once again and it seems flawed. There can be multiple restrictions of overlapping periods which can make the logic fail. For example, consider a case when there is a monthly restriction started on 1st Jan and another monthly restriction started on 15th Jan. If anyone tries to transfer tokens on 16th of Jan, the below line will be executed twice
volumeTally[_from][monthly][0] = _amount.add(volumeTally[_from][monthly][0]);
This should have been executed only once.

There are a lot more problematic scenarios due to this. Another example with the same restrictions will be when a transfer is done on 1st Feb. volumeTally[_from][monthly][0] will now contain info about the volume of trade that happened on 1st of Jan but the second restriction will not be expecting that.

Basically, the logic will fail when there are overlapping trade restriction periods. Please, rethink about it and try to fix it.

@maxsam4

maxsam4 commented Oct 26, 2018

Copy link
Copy Markdown
Contributor

I was just going through the requirements and specs of this module and seems like your implementation and our requirements don't completely match. Please have a look at https://www.lucidchart.com/invitations/accept/75bdef55-cd39-48d9-bc1c-ac1d2fc76a39 again.

We want different people to be linked with different volume restrictions. Not all restrictions need to be imposed on every user.

@vyomshm

vyomshm commented Oct 26, 2018

Copy link
Copy Markdown
Author

@maxsam4 With regards to your first comment, periodId in both cases would not be the same as it is dependent on startTime of a restriction and defined as
uint256 periodId = (now.sub(restriction.startTime)).div(period);
therefore, the line
volumeTally[_from][uint256(restriction.rollingPeriodInterval)][periodId] = _amount.add(volumeTally[_from][uint256(restriction.rollingPeriodInterval)][periodId]);

would be executed only when relevant.

@vyomshm

vyomshm commented Oct 26, 2018 •

Copy link
Copy Markdown
Author

This applies to all token-holders and not just affiliated parties.
@maxsam4 I'll admit that from this line in "Short summary" section of #267 as posted by @CPSTL as well as the absence of any address field in the spec diagram, I inferred that all max volume transfer restrictions were supposed to be globally imposed on all token holders and not just specific addresses
screenshot 2018-10-27 at 12 34 39 am

Please clarify if my interpretation of the module is wrong, so I can rectify my codebase.

@maxsam4

maxsam4 commented Oct 27, 2018

Copy link
Copy Markdown
Contributor

@maxsam4 With regards to your first comment, periodId in both cases would not be the same as it is dependent on startTime of a restriction and defined as
uint256 periodId = (now.sub(restriction.startTime)).div(period);
would be executed only when relevant.

@vyomshm Solidity does not support decimals which means after div(period) we will have the same periodId for a lot of restrictions. All restrictions will start with periodId 0 and keep incrementing as time passes. Please let me know if I am understanding it wrong.

Please clarify if my interpretation of the module is wrong, so I can rectify my codebase.

I agree that we could have been clearer about the specs but as the second assumption in lucidchart states, the token owner should be able to impose different restrictions on different sets of token holders. Please rework your approach accordingly.
Thank you for cooperating.

@vyomshm

vyomshm commented Oct 27, 2018 •

Copy link
Copy Markdown
Author

@CPSTL @satyamakgec @maxsam4 Just to be clear, we want maxVolume transfer restrictions to exist for specific addresses only and not as Global impositions ?

This would require changing addMaxVolumeRestricion function signature to also include an address field.
Also I could modify the maximumVolumeRestrictions to be a mapping instead of a global array like -
mapping(address => MaximumVolumeRestriction) public maximumVolumeRestrictions. This would also resolve the problem you described.

Can you confirm this is the approach I should proceed with?
I believe this would lead to an implementation that is quite similar as being taken for this issue -> #268 .

@maxsam4

maxsam4 commented Oct 27, 2018

Copy link
Copy Markdown
Contributor

@vyomshm We want both the abilities. Global restrictions + restrictions on only a subset of address.
This module should cater to global restrictions and the other volume restriction module should cater to individual limits.
The limits on a subset of investors can be catered to by both or any one of these modules. I'll have a chat with the team and get back to you regarding this.

@vyomshm

vyomshm commented Oct 27, 2018 •

Copy link
Copy Markdown
Author

@maxsam4 okay, I'll wait.

Just so you know, I've figured out the easiest fix for the same periodId issue. As in my original implementation we should define periodId as follows-
uint256 periodId = now.div(period);

Also I believe we can implement the multi version of removeMaximumTransferRestriction by dynamically modifying the user provided indices according to elements already removed.

@maxsam4

maxsam4 commented Oct 27, 2018

Copy link
Copy Markdown
Contributor

@maxsam4 okay, I'll wait.

Just so you know, I've figured out the easiest fix for the same periodId issue. As in my original implementation we should define periodId as follows-
uint256 periodId = now.div(period);

That won't work for the same reasons I said when you first implemented it. Also, Even in this case, there will be collisions because solidity does not support floating points. It will round them to the nearest integer.

@vyomshm

vyomshm commented Oct 28, 2018 •

Copy link
Copy Markdown
Author

@maxsam4 I understand what you're saying. As I see it the best way to avoid any sort of collisions will be to maintain a separate volumeTally for every restriction.

One way to do this would be to add to modify the MaximumVolumeRestriction struct to also include an id and extending the multi-level volumeTally mapping another level.

uint256 internal _restrictionIdCount = 0;

struct MaximumVolumeRestriction{
        uint256 restrictionId;
        uint256 maximumVolumeAllowed;
        uint256 startTime;
        uint256 endTime;
        RollingIntervals rollingPeriodInterval;
}

// mapping from address to RollingIntervals to restrictionIds to periodIds to tokens transfered 
mapping(address => mapping(uint256 => mapping( uint256 => mapping(uint256 => uint256))) volumeTally;

restrictionId var could take its value from a global restrictionIdCount which would in turn be incremented by 1 every time a maximum volume restriction is created/modified. This would ensure that all maxVolumeRestrictions have a unique restrictionId

@vyomshm

vyomshm commented Oct 30, 2018

Copy link
Copy Markdown
Author

@maxsam4 have you had a chance to talk with the team yet?

@vyomshm vyomshm closed this Oct 30, 2018
@vyomshm vyomshm reopened this Oct 30, 2018
@maxsam4

maxsam4 commented Oct 30, 2018

Copy link
Copy Markdown
Contributor

@vyomshm Sorry, we are busy with devcon this week. I'll get back to you next week. Thanks for understanding.

@vyomshm

vyomshm commented Nov 12, 2018

Copy link
Copy Markdown
Author

@maxsam4 Could you update me on how to proceed with this?

@maxsam4

maxsam4 commented Nov 15, 2018

Copy link
Copy Markdown
Contributor

Bounty paid. Thanks a lot for your contribution @vyomshm .

@maxsam4 maxsam4 closed this Nov 15, 2018
@maxsam4 maxsam4 mentioned this pull request Nov 15, 2018
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants