Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. pragma solidity ^0.5.8;
  2.  
  3. /*
  4. Just the interface so solidity can compile properly
  5. We could skip this if we use generic call creation or abi.encodeWithSelector
  6. */
  7. contract ERC20 {
  8. function totalSupply() public view returns (uint);
  9. function balanceOf(address tokenOwner) public view returns (uint balance);
  10. function allowance(address tokenOwner, address spender) public view returns (uint remaining);
  11. function transfer(address to, uint tokens) public returns (bool success);
  12. function approve(address spender, uint tokens) public returns (bool success);
  13. function transferFrom(address from, address to, uint tokens) public returns (bool success);
  14. event Transfer(address indexed from, address indexed to, uint tokens);
  15. event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
  16. }
  17.  
  18. /*
  19. Generic Receiver Contract
  20. */
  21. contract Receiver {
  22.  
  23. address payable public owner;
  24.  
  25. constructor() public {
  26. /*
  27. Deployer's address ( Factory in our case )
  28. do not pass this as a constructor argument because
  29. etherscan will have issues displaying our validated source code
  30. */
  31. owner = msg.sender;
  32. }
  33.  
  34. /*
  35. @notice Transfer Ownership of this contract to another address
  36. @param newOwner - Address of the next Owner of the contract
  37. */
  38. function transferOwner(address payable newOwner) public {
  39. require (msg.sender == owner);
  40. owner = newOwner;
  41. }
  42.  
  43. /*
  44. @notice Send funds owned by this contract to another address
  45. @param tracker - ERC20 token tracker ( DAI / MKR / etc. )
  46. @param amount - Amount of tokens to send
  47. @param receiver - Address we're sending these tokens to
  48. @return true if transfer succeeded, false otherwise
  49. */
  50. function sendFundsTo( address tracker, uint256 amount, address receiver) public returns ( bool ) {
  51. // callable only by the owner, not using modifiers to improve readability
  52. require(msg.sender == owner);
  53.  
  54. // Transfer tokens from this address to the receiver
  55. return ERC20(tracker).transfer(receiver, amount);
  56. }
  57.  
  58. /*
  59. @notice Self destruct's contract
  60. */
  61. function killContract() public {
  62. require(msg.sender == owner);
  63. selfdestruct(owner);
  64. }
  65.  
  66. /*
  67. @notice payable to make the contract self-destructible
  68. */
  69.  
  70. }
  71.  
  72. /*
  73. Factory Contract
  74. */
  75.  
  76. contract Factory {
  77.  
  78. address public owner;
  79. mapping ( uint256 => address ) public receiversMap;
  80. uint256 public receiverCount = 0;
  81.  
  82. constructor() public {
  83. /*
  84. Deployer's address ( Factory in our case )
  85. do not pass this as a constructor argument because
  86. etherscan will have issues displaying our validated source code
  87. */
  88. owner = msg.sender;
  89. }
  90.  
  91. /*
  92. @notice Transfer Ownership of this contract to another address
  93. @param newOwner - Address of the next Owner of the contract
  94. */
  95. function transferOwner(address payable newOwner) public {
  96. require (msg.sender == owner);
  97. owner = newOwner;
  98. }
  99.  
  100. /*
  101. @notice Create a number of receiver contracts
  102. @param number - 0-255
  103. */
  104. function createReceivers( uint8 number ) public {
  105. require(msg.sender == owner);
  106.  
  107. for(uint8 i = 0; i < number; i++) {
  108. // Create and index our new receiver
  109. receiversMap[++receiverCount] = address(new Receiver());
  110. }
  111. // add event here if you need it
  112. }
  113.  
  114. /*
  115. @notice Send funds in a receiver to another address
  116. @param ID - Receiver indexed ID
  117. @param tracker - ERC20 token tracker ( DAI / MKR / etc. )
  118. @param amount - Amount of tokens to send
  119. @param receiver - Address we're sending tokens to
  120. @return true if transfer succeeded, false otherwise
  121. */
  122. function sendFundsFromReceiverTo( uint256 ID, address tracker, uint256 amount, address receiver ) public returns (bool) {
  123. require(msg.sender == owner);
  124. return Receiver( receiversMap[ID] ).sendFundsTo( tracker, amount, receiver);
  125. }
  126.  
  127. /*
  128. Batch Collection - Should support a few hundred transansfers
  129.  
  130. @param tracker - ERC20 token tracker ( DAI / MKR / etc. )
  131. @param receiver - Address we're sending tokens to
  132. @param contractAddresses - we send an array of addresses instead of ids, so we don't need to read them ( lower gas cost )
  133. @param amounts - array of amounts
  134.  
  135. */
  136. function batchCollect( address tracker, address receiver, address[] memory contractAddresses, uint256[] memory amounts ) public {
  137. require(msg.sender == owner);
  138.  
  139. for(uint256 i = 0; i < contractAddresses.length; i++) {
  140.  
  141. // add exception handling
  142. Receiver( contractAddresses[i] ).sendFundsTo( tracker, amounts[i], receiver);
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement