Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. import 'dappsys/auth.sol';
  2. import 'misc/math.sol';
  3.  
  4. // maintain a `last_touched` with each balance, then prorate it before any time it is touched.
  5. // addPoint to change the rate
  6. // this will ensure all balances appear to have the correct continuous growth/decay even if
  7. // there were multiple rate updates since the last time it was touched
  8. // all in O(1)
  9. contract RateAccumulator is DSAuth, MathUser {
  10. uint _last_rate;
  11. uint64 _last_touched;
  12. mapping (uint64 => uint) _accumulators;
  13.  
  14. function RateAccumulator(uint64 time) {
  15. _last_rate = uint(UNIT);
  16. _last_touched = time;
  17. _accumulators[_last_touched] = _last_rate;
  18. }
  19.  
  20. function addPoint(uint64 time) auth {
  21. if (_accumulators[time] != 0) return;
  22. var old_value = _accumulators[_last_touched];
  23. var duration = time - _last_touched;
  24. var exponent = uint(UNIT) * duration / 60 / 60 / 24 / 365;
  25. var scale = uint(pow(int(_last_rate), exponent));
  26. var new_value = old_value * scale / uint(UNIT);
  27. _accumulators[time] = new_value;
  28. _last_touched = time;
  29. }
  30.  
  31. function prorate(uint64 start, uint start_amount, uint64 end)
  32. returns (uint end_amount)
  33. {
  34. var a = _accumulators[start];
  35. var b = _accumulators[end];
  36. if (a == 0 || b == 0) throw;
  37. end_amount = start_amount * b / a;
  38. }
  39.  
  40. function updateRate(uint64 time, uint new_rate) auth {
  41. addPoint(time);
  42. _last_rate = new_rate;
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement