ERC20Snapshot.sol 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. pragma solidity ^0.5.2;
  2. import "../math/SafeMath.sol";
  3. import "../utils/Arrays.sol";
  4. import "../drafts/Counters.sol";
  5. import "../token/ERC20/ERC20.sol";
  6. /**
  7. * @title ERC20 token with snapshots.
  8. * @dev Inspired by Jordi Baylina's MiniMeToken to record historical balances:
  9. * https://github.com/Giveth/minime/blob/ea04d950eea153a04c51fa510b068b9dded390cb/contracts/MiniMeToken.sol
  10. * When a snapshot is made, the balances and totalSupply at the time of the snapshot are recorded for later
  11. * access.
  12. * @author Validity Labs AG <info@validitylabs.org>
  13. */
  14. contract ERC20Snapshot is ERC20 {
  15. using SafeMath for uint256;
  16. using Arrays for uint256[];
  17. using Counters for Counters.Counter;
  18. // Snapshotted values have arrays of ids and the value corresponding to that id. These could be an array of a
  19. // Snapshot struct, but that would impede usage of functions that work on an array.
  20. struct Snapshots {
  21. uint256[] ids;
  22. uint256[] values;
  23. }
  24. mapping (address => Snapshots) private _accountBalanceSnapshots;
  25. Snapshots private _totalSupplySnaphots;
  26. // Snapshot ids increase monotonically, with the first value being 1. An id of 0 is invalid.
  27. Counters.Counter private _currentSnapshotId;
  28. event Snapshot(uint256 id);
  29. // Creates a new snapshot id. Balances are only stored in snapshots on demand: unless a snapshot was taken, a
  30. // balance change will not be recorded. This means the extra added cost of storing snapshotted balances is only paid
  31. // when required, but is also flexible enough that it allows for e.g. daily snapshots.
  32. function snapshot() public returns (uint256) {
  33. _currentSnapshotId.increment();
  34. uint256 currentId = _currentSnapshotId.current();
  35. emit Snapshot(currentId);
  36. return currentId;
  37. }
  38. function balanceOfAt(address account, uint256 snapshotId) public view returns (uint256) {
  39. (bool snapshotted, uint256 value) = _valueAt(snapshotId, _accountBalanceSnapshots[account]);
  40. return snapshotted ? value : balanceOf(account);
  41. }
  42. function totalSupplyAt(uint256 snapshotId) public view returns(uint256) {
  43. (bool snapshotted, uint256 value) = _valueAt(snapshotId, _totalSupplySnaphots);
  44. return snapshotted ? value : totalSupply();
  45. }
  46. // _transfer, _mint and _burn are the only functions where the balances are modified, so it is there that the
  47. // snapshots are updated. Note that the update happens _before_ the balance change, with the pre-modified value.
  48. // The same is true for the total supply and _mint and _burn.
  49. function _transfer(address from, address to, uint256 value) internal {
  50. _updateAccountSnapshot(from);
  51. _updateAccountSnapshot(to);
  52. super._transfer(from, to, value);
  53. }
  54. function _mint(address account, uint256 value) internal {
  55. _updateAccountSnapshot(account);
  56. _updateTotalSupplySnapshot();
  57. super._mint(account, value);
  58. }
  59. function _burn(address account, uint256 value) internal {
  60. _updateAccountSnapshot(account);
  61. _updateTotalSupplySnapshot();
  62. super._burn(account, value);
  63. }
  64. // When a valid snapshot is queried, there are three possibilities:
  65. // a) The queried value was not modified after the snapshot was taken. Therefore, a snapshot entry was never
  66. // created for this id, and all stored snapshot ids are smaller than the requested one. The value that corresponds
  67. // to this id is the current one.
  68. // b) The queried value was modified after the snapshot was taken. Therefore, there will be an entry with the
  69. // requested id, and its value is the one to return.
  70. // c) More snapshots were created after the requested one, and the queried value was later modified. There will be
  71. // no entry for the requested id: the value that corresponds to it is that of the smallest snapshot id that is
  72. // larger than the requested one.
  73. //
  74. // In summary, we need to find an element in an array, returning the index of the smallest value that is larger if
  75. // it is not found, unless said value doesn't exist (e.g. when all values are smaller). Arrays.findUpperBound does
  76. // exactly this.
  77. function _valueAt(uint256 snapshotId, Snapshots storage snapshots)
  78. private view returns (bool, uint256)
  79. {
  80. require(snapshotId > 0);
  81. require(snapshotId <= _currentSnapshotId.current());
  82. uint256 index = snapshots.ids.findUpperBound(snapshotId);
  83. if (index == snapshots.ids.length) {
  84. return (false, 0);
  85. } else {
  86. return (true, snapshots.values[index]);
  87. }
  88. }
  89. function _updateAccountSnapshot(address account) private {
  90. _updateSnapshot(_accountBalanceSnapshots[account], balanceOf(account));
  91. }
  92. function _updateTotalSupplySnapshot() private {
  93. _updateSnapshot(_totalSupplySnaphots, totalSupply());
  94. }
  95. function _updateSnapshot(Snapshots storage snapshots, uint256 currentValue) private {
  96. uint256 currentId = _currentSnapshotId.current();
  97. if (_lastSnapshotId(snapshots.ids) < currentId) {
  98. snapshots.ids.push(currentId);
  99. snapshots.values.push(currentValue);
  100. }
  101. }
  102. function _lastSnapshotId(uint256[] storage ids) private view returns (uint256) {
  103. if (ids.length == 0) {
  104. return 0;
  105. } else {
  106. return ids[ids.length - 1];
  107. }
  108. }
  109. }