-
Notifications
You must be signed in to change notification settings - Fork 19
[WIP] Credit draft implementation. #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| pragma solidity ^0.5.0; | ||
|
|
||
| // Copyright 2018 OpenST Ltd. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import "./SafeMath.sol"; | ||
| import "./TokenRules.sol"; | ||
| import "./TokenHolder.sol"; | ||
| import "./EIP20TokenInterface.sol"; | ||
|
|
||
| contract CreditRule { | ||
|
|
||
| /* Usings */ | ||
|
|
||
| using SafeMath for uint256; | ||
|
|
||
|
|
||
| /* Struct */ | ||
|
|
||
| struct CreditInfo { | ||
| uint256 amount; | ||
| bool inProgress; | ||
| } | ||
|
|
||
|
|
||
| /* Storage */ | ||
|
|
||
| address public budgetHolder; | ||
|
|
||
| TokenRules public tokenRules; | ||
|
|
||
| mapping(address => CreditInfo) public credits; | ||
|
|
||
|
|
||
| /* Modifiers */ | ||
|
|
||
| modifier onlyBudgetHolder() | ||
| { | ||
| require( | ||
| msg.sender == budgetHolder, | ||
| "Only budget holder is allowed to call." | ||
| ); | ||
|
|
||
| _; | ||
| } | ||
|
|
||
|
|
||
| /* Special Functions */ | ||
|
|
||
| constructor( | ||
| address _budgetHolder, | ||
| address _tokenRules | ||
| ) | ||
| public | ||
| { | ||
| require( | ||
| _budgetHolder != address(0), | ||
| "Budget holder's address is null." | ||
| ); | ||
|
|
||
| require( | ||
| _tokenRules != address(0), | ||
| "Token rules's address is null." | ||
| ); | ||
|
|
||
| budgetHolder = _budgetHolder; | ||
|
|
||
| tokenRules = TokenRules(_tokenRules); | ||
| } | ||
|
|
||
|
|
||
| /* External Functions */ | ||
|
|
||
| function executeRule( | ||
| uint256 _creditAmount, | ||
| address _to, // token holder address | ||
| bytes calldata _data // token holder execute rule data | ||
| ) | ||
| external | ||
| payable | ||
| onlyBudgetHolder | ||
| returns(bool executionStatus_) | ||
| { | ||
| require(_to != address(0)); | ||
|
|
||
| CreditInfo storage c = credits[_to]; | ||
|
|
||
| require( | ||
| !c.inProgress, | ||
| "Re-entrancy occured in crediting process." | ||
| ); | ||
|
|
||
| c.inProgress = true; | ||
| c.amount = _creditAmount; | ||
|
|
||
| bytes memory returnData; | ||
| // solium-disable-next-line security/no-call-value | ||
| (executionStatus_, returnData) = address(_to).call.value(msg.value)(_data); | ||
|
|
||
| c.amount = 0; | ||
| c.inProgress = false; | ||
| } | ||
|
|
||
| function executeTransfers( | ||
| address _from, | ||
| address[] calldata _transfersTo, | ||
| uint256[] calldata _transfersAmount | ||
| ) | ||
| external | ||
| { | ||
| require( | ||
| credits[_from].inProgress, | ||
| "Crediting process is not in progress." | ||
| ); | ||
|
|
||
| uint256 sumAmount = 0; | ||
|
|
||
| for(uint256 i = 0; i < _transfersAmount.length; ++i) { | ||
| sumAmount = sumAmount.add(_transfersAmount[i]); | ||
| } | ||
|
|
||
| uint256 creditAmount = credits[_from].amount; | ||
|
|
||
| uint256 amountToTransferFromBudgetHolder = ( | ||
| sumAmount > creditAmount ? creditAmount : sumAmount | ||
| ); | ||
|
|
||
| executeTransfer( | ||
| budgetHolder, | ||
| _from, | ||
| amountToTransferFromBudgetHolder | ||
| ); | ||
|
|
||
| tokenRules.executeTransfers( | ||
| _from, | ||
| _transfersTo, | ||
| _transfersAmount | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| /* Private Functions */ | ||
|
|
||
| function executeTransfer( | ||
| address _from, | ||
| address _beneficiary, | ||
| uint256 _amount | ||
| ) | ||
| private | ||
| { | ||
| address[] memory transfersTo = new address[](1); | ||
| transfersTo[0] = _beneficiary; | ||
|
|
||
| uint256[] memory transfersAmount = new uint256[](1); | ||
| transfersAmount[0] = _amount; | ||
|
|
||
| tokenRules.executeTransfers( | ||
| _from, | ||
| transfersTo, | ||
| transfersAmount | ||
| ); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
contracts/test_doubles/unit_tests/credit_rule/CustomRuleWithCredit.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| pragma solidity ^0.5.0; | ||
|
|
||
| // Copyright 2018 OpenST Ltd. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import "../../../SafeMath.sol"; | ||
| import "../../../CreditRule.sol"; | ||
|
|
||
| contract CustomRuleWithCredit { | ||
|
|
||
| /* Usings */ | ||
|
|
||
| using SafeMath for uint256; | ||
|
|
||
| event Pay( | ||
| address _to, | ||
| uint256 _amount | ||
| ); | ||
|
|
||
| /* Storage */ | ||
|
|
||
| CreditRule public creditRule; | ||
|
|
||
| bool public markedToFail; | ||
|
|
||
|
|
||
| /* Special Functions */ | ||
|
|
||
| constructor( | ||
| address _creditRule | ||
| ) | ||
| public | ||
| { | ||
| require( | ||
| address(_creditRule) != address(0), | ||
| "Credit rule's address is null." | ||
| ); | ||
|
|
||
| creditRule = CreditRule(_creditRule); | ||
| } | ||
|
|
||
|
|
||
| /* External Functions */ | ||
|
|
||
| function makeMeFail() | ||
| external | ||
| { | ||
| markedToFail = true; | ||
| } | ||
|
|
||
| function pay( | ||
| address _to, | ||
| uint256 _amount | ||
| ) | ||
| external | ||
| { | ||
| require( | ||
| !markedToFail, | ||
| "The function is marked to fail." | ||
| ); | ||
|
|
||
| address[] memory transfersTo = new address[](1); | ||
| transfersTo[0] = _to; | ||
|
|
||
| uint256[] memory transfersAmount = new uint256[](1); | ||
| transfersAmount[0] = _amount; | ||
|
|
||
| creditRule.executeTransfers( | ||
| msg.sender, | ||
| transfersTo, | ||
| transfersAmount | ||
| ); | ||
|
|
||
| emit Pay(_to, _amount); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if no credit is in progress,
executeTransfersshould just pass on the data toTokenRuleswithout transfering a credit and function normally, not revertThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.