take balances snapshot when depositing Ether

This commit is contained in:
haythem 2019-07-13 17:18:46 +01:00
parent 75c6437c7b
commit 98205ca9b7

View File

@ -8,10 +8,13 @@ import "@aragon/os/contracts/common/SafeERC20.sol";
import "@aragon/os/contracts/lib/token/ERC20.sol"; import "@aragon/os/contracts/lib/token/ERC20.sol";
interface IContributor { interface IContributor {
function getContributorAddressById(uint32 id) view public returns (address);
function contributorsCount() view public returns (uint32);
} }
interface IToken { interface IToken {
function balanceOf(address owner) public view returns (uint256); function balanceOf(address owner) public view returns (uint256);
function totalSupply() public view returns (uint256);
} }
contract Vault is EtherTokenConstant, AragonApp, DepositableStorage { contract Vault is EtherTokenConstant, AragonApp, DepositableStorage {
@ -22,7 +25,7 @@ contract Vault is EtherTokenConstant, AragonApp, DepositableStorage {
string private constant ERROR_VALUE_MISMATCH = "VAULT_VALUE_MISMATCH"; string private constant ERROR_VALUE_MISMATCH = "VAULT_VALUE_MISMATCH";
string private constant ERROR_TOKEN_TRANSFER_FROM_REVERTED = "VAULT_TOKEN_TRANSFER_FROM_REVERT"; string private constant ERROR_TOKEN_TRANSFER_FROM_REVERTED = "VAULT_TOKEN_TRANSFER_FROM_REVERT";
uint256 private _snapshotTotalBalance; uint256 private _snapshotTotalSupply;
mapping (address => uint256) private _snapshotBalances; mapping (address => uint256) private _snapshotBalances;
@ -59,6 +62,35 @@ contract Vault is EtherTokenConstant, AragonApp, DepositableStorage {
return k.getApp(KERNEL_APP_ADDR_NAMESPACE, appIds[uint8(Apps.Contributor)]); return k.getApp(KERNEL_APP_ADDR_NAMESPACE, appIds[uint8(Apps.Contributor)]);
} }
function getContributorAddressById(uint32 contributorId) public view returns (address) {
address contributor = getContributorContract();
return IContributor(contributor).getContributorAddressById(contributorId);
}
function getContributorsAddresses() internal view returns (address[]) {
address contributor = getContributorContract();
uint32 contributorsCount = IContributor(contributor).contributorsCount();
address[] memory contributorsAddresses = new address[](contributorsCount);
for(uint32 i = 1; i <= contributorsCount; i++) {
address contributorAddress = IContributor(contributor).getContributorAddressById(i);
contributorsAddresses[i-1] = contributorAddress;
}
return contributorsAddresses;
}
function balanceOf(address owner) public view returns (uint256) {
address token = getTokenContract();
return IToken(token).balanceOf(owner);
}
function totalSupply() public view returns (uint256) {
address token = getTokenContract();
return IToken(token).totalSupply();
}
/** /**
* @notice Deposit `_value` `_token` to the vault * @notice Deposit `_value` `_token` to the vault
* @param _token Address of the token being transferred * @param _token Address of the token being transferred
@ -104,8 +136,27 @@ contract Vault is EtherTokenConstant, AragonApp, DepositableStorage {
// Deposit is implicit in this case // Deposit is implicit in this case
require(msg.value == _value, ERROR_VALUE_MISMATCH); require(msg.value == _value, ERROR_VALUE_MISMATCH);
createSnapshot();
emit VaultDeposit(_token, msg.sender, _value); emit VaultDeposit(_token, msg.sender, _value);
} }
} }
function createSnapshot() internal {
updateSnapshotTotalSupply();
updateSnapshotBalances();
}
function updateSnapshotTotalSupply() internal {
_snapshotTotalSupply = totalSupply();
}
function updateSnapshotBalances() internal {
address[] memory contributorsAddresses = getContributorsAddresses();
for(uint32 i = 0; i < contributorsAddresses.length; i++) {
_snapshotBalances[contributorsAddresses[i]] = balanceOf(contributorsAddresses[i]);
}
}
} }