RewardSwap.sol 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
  4. import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
  5. import "torn-token/contracts/ENS.sol";
  6. import "./utils/FloatMath.sol";
  7. /**
  8. Let's imagine we have 1M TORN tokens for anonymity mining to distribute during 1 year (~31536000 seconds).
  9. The contract should constantly add liquidity to a pool of claimed rewards to TORN (REWD/TORN). At any time user can exchange REWD->TORN using
  10. this pool. The rate depends on current available TORN liquidity - the more TORN are withdrawn the worse the swap rate is.
  11. The contract starts with some virtual balance liquidity and adds some TORN tokens every second to the balance. Users will decrease
  12. this balance by swaps.
  13. Exchange rate can be calculated as following:
  14. BalanceAfter = BalanceBefore * e^(-rewardAmount/poolWeight)
  15. tokens = BalanceBefore - BalanceAfter
  16. */
  17. contract RewardSwap is EnsResolve {
  18. using SafeMath for uint256;
  19. uint256 public constant DURATION = 365 days;
  20. IERC20 public immutable torn;
  21. address public immutable miner;
  22. uint256 public immutable startTimestamp;
  23. uint256 public immutable initialLiquidity;
  24. uint256 public immutable liquidity;
  25. uint256 public tokensSold;
  26. uint256 public poolWeight;
  27. event Swap(address indexed recipient, uint256 pTORN, uint256 TORN);
  28. event PoolWeightUpdated(uint256 newWeight);
  29. modifier onlyMiner() {
  30. require(msg.sender == miner, "Only Miner contract can call");
  31. _;
  32. }
  33. constructor(
  34. bytes32 _torn,
  35. bytes32 _miner,
  36. uint256 _miningCap,
  37. uint256 _initialLiquidity,
  38. uint256 _poolWeight
  39. ) public {
  40. require(_initialLiquidity <= _miningCap, "Initial liquidity should be lower than mining cap");
  41. torn = IERC20(resolve(_torn));
  42. miner = resolve(_miner);
  43. initialLiquidity = _initialLiquidity;
  44. liquidity = _miningCap.sub(_initialLiquidity);
  45. poolWeight = _poolWeight;
  46. startTimestamp = getTimestamp();
  47. }
  48. function swap(address _recipient, uint256 _amount) external onlyMiner returns (uint256) {
  49. uint256 tokens = getExpectedReturn(_amount);
  50. tokensSold += tokens;
  51. require(torn.transfer(_recipient, tokens), "transfer failed");
  52. emit Swap(_recipient, _amount, tokens);
  53. return tokens;
  54. }
  55. /**
  56. @dev
  57. */
  58. function getExpectedReturn(uint256 _amount) public view returns (uint256) {
  59. uint256 oldBalance = tornVirtualBalance();
  60. int128 pow = FloatMath.neg(FloatMath.divu(_amount, poolWeight));
  61. int128 exp = FloatMath.exp(pow);
  62. uint256 newBalance = FloatMath.mulu(exp, oldBalance);
  63. return oldBalance.sub(newBalance);
  64. }
  65. function tornVirtualBalance() public view returns (uint256) {
  66. uint256 passedTime = getTimestamp().sub(startTimestamp);
  67. if (passedTime < DURATION) {
  68. return initialLiquidity.add(liquidity.mul(passedTime).div(DURATION)).sub(tokensSold);
  69. } else {
  70. return torn.balanceOf(address(this));
  71. }
  72. }
  73. function setPoolWeight(uint256 _newWeight) external onlyMiner {
  74. poolWeight = _newWeight;
  75. emit PoolWeightUpdated(_newWeight);
  76. }
  77. function getTimestamp() public view virtual returns (uint256) {
  78. return block.timestamp;
  79. }
  80. }