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]; return balances[account];
} }
function deposit(uint256 amount) public invariant { function add(address account, uint256 amount) private {
token.transferFrom(msg.sender, address(this), amount); balances[account] += amount;
totals.deposited += amount;
balances[msg.sender] += amount;
totals.balance += amount; totals.balance += amount;
} }
function withdraw() public invariant { function subtract(address account, uint256 amount) private {
uint256 amount = balances[msg.sender]; balances[account] -= amount;
balances[msg.sender] = 0;
totals.balance -= 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; totals.withdrawn += amount;
subtract(msg.sender, amount);
assert(token.transfer(msg.sender, amount)); assert(token.transfer(msg.sender, amount));
} }