Squashed commit of the following:
commit fcf35eb806100de300bd9803ce3150dde1ecc424
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 17 17:16:04 2019 -0300
remove all docsite dependency
commit eeaee9a9d43d70704f6ab17b5126ddbd52b93a50
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 17 17:15:23 2019 -0300
update solidity-docgen
commit f021ff951829ea0c155186749819403c6b76e803
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 17 17:05:06 2019 -0300
update docsite script for new setup
commit ff887699d381cfbbe3acf1f1c0de8e22b58480f3
Merge: c938aa1d 84f85a41
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 17 16:46:46 2019 -0300
Merge branch 'master' into antora
commit c938aa1d9ed05ac83a34e2cebd8353f8331ad6d6
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jul 16 18:24:29 2019 -0300
make component name shorter
commit 5bbd6931e02cbbd8864c82655ad0f390ceead5f3
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 20:16:17 2019 -0300
add all info to docs templates
commit 39682c4515d7cf0f0368ed557f50d2709174208a
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 20:13:49 2019 -0300
fix npm docsite script
commit 7ae46bd4a0437abf66150d54d05adf46e3de2cab
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 18:48:05 2019 -0300
convert inline docs to asciidoc
commit cfdfd3dee4b4bf582fde22c8cb6e17a603d6e0c8
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 17:34:52 2019 -0300
add missing contract names in readmes
commit 15b6a2f9bfb546cf1d3bf4f104278b118bf1b3f4
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 17:16:47 2019 -0300
fix script path
commit 80d82b909f9460d1450d401f00b3f309da506b29
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 17:13:53 2019 -0300
update version of solidity-docgen
commit a870b6c607b9c2d0012f8a60a4ed1a1c8b7e8ebd
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 17:03:53 2019 -0300
add nav generation of api ref
commit 069cff4a25b83752650b54b86d85608c2f547e5e
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 16:32:14 2019 -0300
initial migration to asciidoc and new docgen version
commit 55216eed0a6551da913c8d1da4b2a0d0d3faa1a8
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jun 25 20:39:35 2019 -0300
add basic api doc example
commit 0cbe50ce2173b6d1d9a698329d91220f58822a53
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jun 25 19:31:31 2019 -0300
add sidebars
commit 256fc942845307258ac9dc25aace48117fa10f79
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jun 25 15:22:38 2019 -0300
add page titles
commit f4d0effa70e1fc0662729863e8ee72a8821bc458
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jun 25 15:19:41 2019 -0300
add contracts index file
commit b73b06359979f7d933df7f2b283c50cb1c31b2a0
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jun 25 15:14:52 2019 -0300
fix header levels
commit fb57d9b820f09a1b7c04eed1a205be0e45866cac
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jun 25 15:11:47 2019 -0300
switch format to preferred asciidoctor format
commit 032181d8804137332c71534753929d080a31a71f
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jun 25 15:05:38 2019 -0300
initialize antora component and convert docs to asciidoc
143 lines
5.8 KiB
Solidity
143 lines
5.8 KiB
Solidity
pragma solidity ^0.5.0;
|
|
|
|
import "../math/SafeMath.sol";
|
|
import "../utils/Arrays.sol";
|
|
import "../drafts/Counters.sol";
|
|
import "../token/ERC20/ERC20.sol";
|
|
|
|
/**
|
|
* @title ERC20 token with snapshots.
|
|
* @dev Inspired by Jordi Baylina's
|
|
* https://github.com/Giveth/minimd/blob/ea04d950eea153a04c51fa510b068b9dded390cb/contracts/MiniMeToken.sol[MiniMeToken]
|
|
* to record historical balances.
|
|
*
|
|
* When a snapshot is made, the balances and total supply at the time of the snapshot are recorded for later
|
|
* access.
|
|
*
|
|
* To make a snapshot, call the {snapshot} function, which will emit the {Snapshot} event and return a snapshot id.
|
|
* To get the total supply from a snapshot, call the function {totalSupplyAt} with the snapshot id.
|
|
* To get the balance of an account from a snapshot, call the {balanceOfAt} function with the snapshot id and the
|
|
* account address.
|
|
* @author Validity Labs AG <info@validitylabs.org>
|
|
*/
|
|
contract ERC20Snapshot is ERC20 {
|
|
using SafeMath for uint256;
|
|
using Arrays for uint256[];
|
|
using Counters for Counters.Counter;
|
|
|
|
// Snapshotted values have arrays of ids and the value corresponding to that id. These could be an array of a
|
|
// Snapshot struct, but that would impede usage of functions that work on an array.
|
|
struct Snapshots {
|
|
uint256[] ids;
|
|
uint256[] values;
|
|
}
|
|
|
|
mapping (address => Snapshots) private _accountBalanceSnapshots;
|
|
Snapshots private _totalSupplySnapshots;
|
|
|
|
// Snapshot ids increase monotonically, with the first value being 1. An id of 0 is invalid.
|
|
Counters.Counter private _currentSnapshotId;
|
|
|
|
event Snapshot(uint256 id);
|
|
|
|
// Creates a new snapshot id. Balances are only stored in snapshots on demand: unless a snapshot was taken, a
|
|
// balance change will not be recorded. This means the extra added cost of storing snapshotted balances is only paid
|
|
// when required, but is also flexible enough that it allows for e.g. daily snapshots.
|
|
function snapshot() public returns (uint256) {
|
|
_currentSnapshotId.increment();
|
|
|
|
uint256 currentId = _currentSnapshotId.current();
|
|
emit Snapshot(currentId);
|
|
return currentId;
|
|
}
|
|
|
|
function balanceOfAt(address account, uint256 snapshotId) public view returns (uint256) {
|
|
(bool snapshotted, uint256 value) = _valueAt(snapshotId, _accountBalanceSnapshots[account]);
|
|
|
|
return snapshotted ? value : balanceOf(account);
|
|
}
|
|
|
|
function totalSupplyAt(uint256 snapshotId) public view returns(uint256) {
|
|
(bool snapshotted, uint256 value) = _valueAt(snapshotId, _totalSupplySnapshots);
|
|
|
|
return snapshotted ? value : totalSupply();
|
|
}
|
|
|
|
// _transfer, _mint and _burn are the only functions where the balances are modified, so it is there that the
|
|
// snapshots are updated. Note that the update happens _before_ the balance change, with the pre-modified value.
|
|
// The same is true for the total supply and _mint and _burn.
|
|
function _transfer(address from, address to, uint256 value) internal {
|
|
_updateAccountSnapshot(from);
|
|
_updateAccountSnapshot(to);
|
|
|
|
super._transfer(from, to, value);
|
|
}
|
|
|
|
function _mint(address account, uint256 value) internal {
|
|
_updateAccountSnapshot(account);
|
|
_updateTotalSupplySnapshot();
|
|
|
|
super._mint(account, value);
|
|
}
|
|
|
|
function _burn(address account, uint256 value) internal {
|
|
_updateAccountSnapshot(account);
|
|
_updateTotalSupplySnapshot();
|
|
|
|
super._burn(account, value);
|
|
}
|
|
|
|
// When a valid snapshot is queried, there are three possibilities:
|
|
// a) The queried value was not modified after the snapshot was taken. Therefore, a snapshot entry was never
|
|
// created for this id, and all stored snapshot ids are smaller than the requested one. The value that corresponds
|
|
// to this id is the current one.
|
|
// b) The queried value was modified after the snapshot was taken. Therefore, there will be an entry with the
|
|
// requested id, and its value is the one to return.
|
|
// c) More snapshots were created after the requested one, and the queried value was later modified. There will be
|
|
// no entry for the requested id: the value that corresponds to it is that of the smallest snapshot id that is
|
|
// larger than the requested one.
|
|
//
|
|
// In summary, we need to find an element in an array, returning the index of the smallest value that is larger if
|
|
// it is not found, unless said value doesn't exist (e.g. when all values are smaller). Arrays.findUpperBound does
|
|
// exactly this.
|
|
function _valueAt(uint256 snapshotId, Snapshots storage snapshots)
|
|
private view returns (bool, uint256)
|
|
{
|
|
require(snapshotId > 0, "ERC20Snapshot: id is 0");
|
|
// solhint-disable-next-line max-line-length
|
|
require(snapshotId <= _currentSnapshotId.current(), "ERC20Snapshot: nonexistent id");
|
|
|
|
uint256 index = snapshots.ids.findUpperBound(snapshotId);
|
|
|
|
if (index == snapshots.ids.length) {
|
|
return (false, 0);
|
|
} else {
|
|
return (true, snapshots.values[index]);
|
|
}
|
|
}
|
|
|
|
function _updateAccountSnapshot(address account) private {
|
|
_updateSnapshot(_accountBalanceSnapshots[account], balanceOf(account));
|
|
}
|
|
|
|
function _updateTotalSupplySnapshot() private {
|
|
_updateSnapshot(_totalSupplySnapshots, totalSupply());
|
|
}
|
|
|
|
function _updateSnapshot(Snapshots storage snapshots, uint256 currentValue) private {
|
|
uint256 currentId = _currentSnapshotId.current();
|
|
if (_lastSnapshotId(snapshots.ids) < currentId) {
|
|
snapshots.ids.push(currentId);
|
|
snapshots.values.push(currentValue);
|
|
}
|
|
}
|
|
|
|
function _lastSnapshotId(uint256[] storage ids) private view returns (uint256) {
|
|
if (ids.length == 0) {
|
|
return 0;
|
|
} else {
|
|
return ids[ids.length - 1];
|
|
}
|
|
}
|
|
}
|