abstract contract ERC4626 is ERC20 {
IERC20 asset;
constructor(IERC20 asset_) {
asset = asset_;
}
function totalAssets() public view returns (uint256) {
return asset.balanceOf(address(this));
}
function convertToShares(uint256 assets) public view returns (uint256) {
if (totalAssets() == 0) {
return assets;
}
return totalSupply() * assets / totalAssets();
}
function convertToAssets(uint256 shares) public view returns (uint256) {
return totalAssets() * shares / totalSupply();
}
function deposit(uint256 assets) public {
asset.transferFrom(msg.sender, address(this), assets);
_mint(msg.sender, convertToShares(assets));
}
function burn(uint256 shares) public {
_burn(msg.sender, shares);
asset.transfer(msg.sender, convertToAssets(shares));
}
}