-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault.sol
76 lines (61 loc) · 1.96 KB
/
vault.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import "hardhat/console.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";
contract AssetERC20 is ERC20 {
address public owner;
modifier onlyOwner() {
require(msg.sender == owner, "only Owner");
_;
}
constructor() ERC20("Asset", "ast") {
owner = msg.sender;
}
function mint(address _dest, uint256 _amount) external onlyOwner {
_mint(_dest, _amount);
}
}
contract Vault is ERC20 {
ERC20 public asset;
address public owner;
modifier onlyOwner() {
require(msg.sender == owner, "only owner");
_;
}
constructor(address _asset) ERC20("Vault", "Vlt") {
asset = ERC20(_asset);
owner = msg.sender;
}
function totalAssets() public view returns(uint256) {
return asset.balanceOf(address(this));
}
function ConvertAssetToShare(uint256 _amount) public view returns(uint256) {
if(totalSupply() == 0) {
return _amount;
}
else {
return (_amount * totalSupply()) / totalAssets();
}
}
function ConvertShareToAsset(uint256 _shares) public view returns(uint256) {
if(totalSupply() == 0) {
return _shares;
}
else {
return (_shares * totalAssets()) / totalSupply();
}
}
function deposit(uint256 _amount, address _dest) external {
uint256 shares = ConvertAssetToShare(_amount);
asset.transferFrom(msg.sender, address(this), _amount);
_mint(_dest, shares);
}
function withdraw(uint256 _shares, address _dest) external {
uint256 assetGained = ConvertShareToAsset(_shares);
asset.transfer(_dest, assetGained);
_burn(msg.sender, _shares);
}
function AddAsset(uint256 _amount) external onlyOwner {
asset.transferFrom(owner, address(this), _amount);
}
}