feat: add V1 custody accounting
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
pragma solidity 0.8.35;
|
||||
|
||||
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
||||
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import {Initializable} from "@openzeppelin/contracts/proxy/utils/Initializable.sol";
|
||||
import {UUPSUpgradeable} from "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";
|
||||
import {ReentrancyGuardTransient} from "@openzeppelin/contracts/utils/ReentrancyGuardTransient.sol";
|
||||
@@ -9,7 +10,15 @@ import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Own
|
||||
import {PausableUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/PausableUpgradeable.sol";
|
||||
|
||||
contract BankV1 is Initializable, UUPSUpgradeable, OwnableUpgradeable, PausableUpgradeable, ReentrancyGuardTransient {
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
error InvalidAsset(address asset);
|
||||
error ZeroAmount();
|
||||
error InsufficientBalance(address account, uint256 available, uint256 requested);
|
||||
error UnexpectedAssetDelta(uint256 expected, uint256 actual);
|
||||
|
||||
event Deposited(address indexed account, uint256 amount);
|
||||
event Withdrawn(address indexed account, uint256 amount);
|
||||
|
||||
IERC20 internal _asset;
|
||||
mapping(address account => uint256 balance) internal _balances;
|
||||
@@ -40,6 +49,31 @@ contract BankV1 is Initializable, UUPSUpgradeable, OwnableUpgradeable, PausableU
|
||||
_unpause();
|
||||
}
|
||||
|
||||
function deposit(uint256 amount) external whenNotPaused nonReentrant {
|
||||
if (amount == 0) revert ZeroAmount();
|
||||
|
||||
uint256 reservesBefore = _asset.balanceOf(address(this));
|
||||
_asset.safeTransferFrom(msg.sender, address(this), amount);
|
||||
uint256 reservesAfter = _asset.balanceOf(address(this));
|
||||
uint256 received = reservesAfter >= reservesBefore ? reservesAfter - reservesBefore : 0;
|
||||
if (received != amount) revert UnexpectedAssetDelta(amount, received);
|
||||
|
||||
_balances[msg.sender] += amount;
|
||||
_totalLiabilities += amount;
|
||||
emit Deposited(msg.sender, amount);
|
||||
}
|
||||
|
||||
function withdraw(uint256 amount) external whenNotPaused nonReentrant {
|
||||
if (amount == 0) revert ZeroAmount();
|
||||
uint256 available = _balances[msg.sender];
|
||||
if (amount > available) revert InsufficientBalance(msg.sender, available, amount);
|
||||
|
||||
_balances[msg.sender] = available - amount;
|
||||
_totalLiabilities -= amount;
|
||||
_asset.safeTransfer(msg.sender, amount);
|
||||
emit Withdrawn(msg.sender, amount);
|
||||
}
|
||||
|
||||
function asset() external view returns (IERC20) {
|
||||
return _asset;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user