forked from rkalis/ethroulette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackingContract.sol
56 lines (48 loc) · 1.81 KB
/
BackingContract.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
pragma solidity ^0.5.0;
import "./BackedToken.sol";
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
/**
* @title BackingContract
* @author Rosco Kalis <roscokalis@gmail.com>
*/
contract BackingContract {
using SafeMath for uint256;
BackedToken public backedToken;
uint256 public balanceForBacking;
modifier onlyBackedToken() {
require(msg.sender == address(backedToken), "Can only be called by the backed token contract");
_;
}
/**
* @dev Fallback payable function, adding the new funds to balanceForBacking.
*/
function() external payable {
balanceForBacking = balanceForBacking.add(msg.value);
}
/**
* @notice Sets the backed token and backs it.
* @param backedTokenAddress The address of the deployed backed token.
*/
constructor(address payable backedTokenAddress) public {
backedToken = BackedToken(backedTokenAddress);
backedToken.back(address(this));
}
/**
* @notice Allows the backed token to deposit funds into the contract.
* @dev Funds are added to balanceForBacking as well.
*/
function deposit() external payable onlyBackedToken {
balanceForBacking = balanceForBacking.add(msg.value);
}
/**
* @notice Allows the backed token to withdraw funds from the contract.
* @dev Funds are removed from balanceForBacking as well.
* @param ethAmount The amount of eth to withdraw.
*/
function withdraw(uint256 ethAmount) external onlyBackedToken {
require(ethAmount <= address(this).balance, "Can not withdraw more than balance");
require(ethAmount <= balanceForBacking, "Can not withdraw more than balance for backing");
balanceForBacking = balanceForBacking.sub(ethAmount);
backedToken.deposit.value(ethAmount)();
}
}