-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathCashAutoConverter.sol
39 lines (33 loc) · 1.11 KB
/
CashAutoConverter.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
pragma solidity 0.4.20;
import 'trading/ICash.sol';
import 'Controlled.sol';
import 'IAugur.sol';
/**
* @title Provides modifiers which take care of Cash/ETH conversion
*/
contract CashAutoConverter is Controlled {
/**
* @dev Convert any ETH provided in the transaction into Cash before the function executes and convert any remaining Cash balance into ETH after the function completes
*/
modifier convertToAndFromCash() {
ethToCash();
_;
cashToEth();
}
function ethToCash() private returns (bool) {
if (msg.value > 0) {
ICash(controller.lookup("Cash")).depositEtherFor.value(msg.value)(msg.sender);
}
return true;
}
function cashToEth() private returns (bool) {
ICash _cash = ICash(controller.lookup("Cash"));
uint256 _tokenBalance = _cash.balanceOf(msg.sender);
if (_tokenBalance > 0) {
IAugur augur = controller.getAugur();
augur.trustedTransfer(_cash, msg.sender, this, _tokenBalance);
_cash.withdrawEtherTo(msg.sender, _tokenBalance);
}
return true;
}
}