Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. pragma solidity ^0.4.8;
  2.  
  3. import "../lib/Owned.sol";
  4. import "../lib/SafeMath.sol";
  5.  
  6. // This contract can be used when a smart contract has a
  7. // role that can be fullfilled by one account, but you would
  8. // like several accounts to fullfill the role.
  9. contract Multiplexer is Owned {
  10.  
  11. address private target;
  12. mapping (address => bool) private managers;
  13.  
  14. modifier managers_only() {
  15. if(managers[msg.sender]) {
  16. _;
  17. }
  18. }
  19.  
  20. function Multiplexer(address target_, address[] managers_) {
  21. target = target;
  22. for(uint i = 0; i < managers_.length; i++) {
  23. managers[managers_[i]] = true;
  24. }
  25. }
  26.  
  27. // Proxy/relay the target contract
  28. function () external payable managers_only {
  29. if(!target.call.value(msg.value)(msg.data)) {
  30. throw;
  31. }
  32. }
  33.  
  34. function multiplex_target(address target_) external owner_only {
  35. target = target_;
  36. }
  37.  
  38. function multiplex_add(address manager) external owner_only {
  39. managers[manager] = true;
  40. }
  41.  
  42. function multiplex_remove(address manager) external owner_only {
  43. delete managers[manager];
  44. }
  45. }
  46.  
  47. // Smart contract to keep accounts funded with enough
  48. // eth to pay for transactions
  49. contract IFaucet {
  50.  
  51. function minimum_balance() constant public returns (uint256);
  52.  
  53. // Check my balance and top up if allowed
  54. function withdraw() external;
  55.  
  56. // An account has been funded
  57. event Withdrawal(address indexed addr, uint256 amount);
  58.  
  59. // Account has reached daily limit
  60. event AccountLimited(address indexed addr, uint256 limit);
  61. }
  62.  
  63. // Smart contract to keep accounts funded
  64. // with enough eth to pay for transactions
  65. contract Faucet is IFaucet, Owned, SafeMath {
  66.  
  67. // TODO: Enforce daily limits
  68.  
  69. uint256 pool_minimum = 10 ether;
  70. uint256 public minimum_balance = 10 finney;
  71. uint256 public target_balance = 100 finney;
  72. address manager;
  73. mapping (address => bool) accounts;
  74. mapping (address => uint) daily_usage;
  75.  
  76. modifier accounts_only() {
  77. if(accounts[msg.sender]) {
  78. _;
  79. }
  80. }
  81.  
  82. modifier manager_only() {
  83. if(msg.sender == manager) {
  84. _;
  85. }
  86. }
  87.  
  88. function Faucet(
  89. address manager_
  90. ) payable {
  91. manager = manager_;
  92. assert(minimum_balance < target_balance);
  93. Pool_funded(this.balance, msg.value);
  94. }
  95.  
  96. function set_manager(address new_manager) external owner_only {
  97. manager = new_manager;
  98. }
  99.  
  100. function set_parameters(
  101. uint256 pool_minimum_,
  102. uint256 minimum_balance_,
  103. uint256 target_balance_
  104. ) external owner_only {
  105. pool_minimum = pool_minimum_;
  106. minimum_balance = minimum_balance_;
  107. target_balance = target_balance_;
  108. }
  109.  
  110. function register(address account) external manager_only {
  111. accounts[account] = true;
  112. withdraw_for(account);
  113. }
  114.  
  115. function unregister(address account) external manager_only {
  116. delete accounts[account];
  117. }
  118.  
  119. function () external payable {
  120. Pool_funded(this.balance, msg.value);
  121. }
  122.  
  123. // If account balance at address is below min_balance,
  124. // fill it up to target balance,
  125. // not exceeding the daily limit.
  126. function withdraw_for(address account) internal {
  127. assert(accounts[account]);
  128. if(account.balance < minimum_balance) {
  129. uint256 transfer = safeSub(target_balance, account.balance);
  130. uint256 new_balance = safeSub(this.balance, transfer);
  131. Withdrawal(account, transfer);
  132. if(new_balance < pool_minimum) {
  133. Pool_low(this.balance, pool_minimum);
  134. }
  135.  
  136. // This call is safe because of 2300 send gas limit
  137. // Additionally, it is a tail call
  138. // TODO Solidity 0.4.10: account.transfer(transfer);
  139. if(!account.send(transfer)) {
  140. throw;
  141. }
  142. }
  143. }
  144.  
  145. // Allow accounts to top themselves up
  146. function withdraw() accounts_only external {
  147. withdraw_for(msg.sender);
  148. }
  149.  
  150. event Pool_funded(uint256 balance, uint256 amount); // The pool is running low and needs to be refunded
  151. event Pool_low(uint256 balance, uint256 minimum); // The pool is running low and needs to be refunded
  152. }
  153.  
  154. contract FaucetManager is Multiplexer {
  155.  
  156. function FaucetManager(address faucet, address[] managers)
  157. Multiplexer(faucet, managers)
  158. {
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement