Update collateral totals and balance at the same time

This commit is contained in:
Mark Spanbroek 2022-02-14 17:05:05 +01:00 committed by markspanbroek
parent c86fdfbec1
commit 9e0d05965d
1 changed files with 15 additions and 7 deletions

View File

@ -16,18 +16,26 @@ contract Collateral {
return balances[account];
}
function deposit(uint256 amount) public invariant {
token.transferFrom(msg.sender, address(this), amount);
totals.deposited += amount;
balances[msg.sender] += amount;
function add(address account, uint256 amount) private {
balances[account] += amount;
totals.balance += amount;
}
function withdraw() public invariant {
uint256 amount = balances[msg.sender];
balances[msg.sender] = 0;
function subtract(address account, uint256 amount) private {
balances[account] -= amount;
totals.balance -= amount;
}
function deposit(uint256 amount) public invariant {
token.transferFrom(msg.sender, address(this), amount);
totals.deposited += amount;
add(msg.sender, amount);
}
function withdraw() public invariant {
uint256 amount = balanceOf(msg.sender);
totals.withdrawn += amount;
subtract(msg.sender, amount);
assert(token.transfer(msg.sender, amount));
}