Skip to content

Commit 8c6f47d

Browse files
committed
feat: vault and tokens
1 parent 40f551c commit 8c6f47d

File tree

13 files changed

+320
-27
lines changed

13 files changed

+320
-27
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "lib/forge-std"]
22
path = lib/forge-std
33
url = https://github.com/foundry-rs/forge-std
4+
[submodule "lib/openzeppelin-contracts"]
5+
path = lib/openzeppelin-contracts
6+
url = https://github.com/OpenZeppelin/openzeppelin-contracts

foundry.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
"name": "v1.15.0",
55
"rev": "0844d7e1fc5e60d77b68e469bff60265f236c398"
66
}
7+
},
8+
"lib/openzeppelin-contracts": {
9+
"tag": {
10+
"name": "v5.6.1",
11+
"rev": "5fd1781b1454fd1ef8e722282f86f9293cacf256"
12+
}
713
}
814
}

lib/openzeppelin-contracts

Submodule openzeppelin-contracts added at 5fd1781

remappings.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
@src/=src/
2-
forge-std/=lib/forge-std/src/
2+
forge-std/=lib/forge-std/src/
3+
@openzeppelin-contracts/=lib/openzeppelin-contracts/contracts/

src/week2/Redstone.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: UNLICENSED
22
pragma solidity ^0.8.13;
3-
import "./interfaces/IRedstone.sol";
3+
import {IRedstone} from "./interfaces/IRedstone.sol";
44

55
contract Redstone {
66
IRedstone public priceFeed;

src/week3/NOTES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
distribute reward, depends on protocol's strategy
2+
3+
lending to aave
4+
lending to morpho
5+
6+
multicall - Address.call{value: }(bytes calldata data)

src/week3/Token.sol

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.30;
3+
4+
import {ERC20} from "@openzeppelin-contracts/token/ERC20/ERC20.sol";
5+
import {Ownable} from "@openzeppelin-contracts/access/Ownable.sol";
6+
7+
contract Token is ERC20, Ownable {
8+
constructor(string memory name, string memory symbol) ERC20(name, symbol) Ownable(msg.sender) {}
9+
10+
function mint(address to, uint256 amount) public onlyOwner {
11+
_mint(to, amount);
12+
}
13+
14+
function burn(address from, uint256 amount) public {
15+
_burn(from, amount);
16+
}
17+
18+
function decimals() public pure override returns (uint8) {
19+
return 6;
20+
}
21+
}

src/week3/Vault.sol

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.30;
3+
4+
import {IERC20} from "@openzeppelin-contracts/token/ERC20/IERC20.sol";
5+
import {ReentrancyGuard} from "@openzeppelin-contracts/utils/ReentrancyGuard.sol";
6+
import {SafeERC20} from "@openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol";
7+
8+
contract Vault is ReentrancyGuard {
9+
using SafeERC20 for IERC20;
10+
11+
address public token;
12+
13+
uint256 public totalSupplyShares;
14+
uint256 public totalSupplyAssets;
15+
16+
mapping(address => uint256) public userSupplyShares;
17+
18+
event Deposit(address user, uint256 amount, uint256 shares);
19+
event Withdraw(address user, uint256 shares, uint256 assets);
20+
event DistributeReward(uint256 amount, address distributor);
21+
22+
constructor(address _token) {
23+
token = _token;
24+
}
25+
26+
function deposit(uint256 _amount) public nonReentrant {
27+
uint256 shares;
28+
29+
if (totalSupplyShares == 0) {
30+
shares = _amount;
31+
} else {
32+
shares = (_amount * totalSupplyShares) / totalSupplyAssets;
33+
}
34+
35+
userSupplyShares[msg.sender] += shares;
36+
totalSupplyShares += shares;
37+
totalSupplyAssets += _amount;
38+
39+
IERC20(token).safeTransferFrom(msg.sender, address(this), _amount);
40+
41+
emit Deposit(msg.sender, _amount, shares);
42+
}
43+
44+
function withdraw(uint256 _shares) public nonReentrant {
45+
uint256 assets;
46+
47+
if (totalSupplyShares == 0) {
48+
assets = _shares;
49+
} else {
50+
assets = (_shares * totalSupplyAssets) / totalSupplyShares;
51+
}
52+
53+
userSupplyShares[msg.sender] -= _shares;
54+
totalSupplyShares -= _shares;
55+
totalSupplyAssets -= assets;
56+
57+
IERC20(token).safeTransfer(msg.sender, assets);
58+
59+
emit Withdraw(msg.sender, _shares, assets);
60+
}
61+
62+
function distributeReward(uint256 _amount) public nonReentrant {
63+
totalSupplyAssets += _amount;
64+
IERC20(token).safeTransferFrom(msg.sender, address(this), _amount);
65+
66+
emit DistributeReward(_amount, msg.sender);
67+
}
68+
}

test/week2/Chainlink.t.sol

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {Chainlink} from "@src/week2/Chainlink.sol";
88
// forge test --match-contract ChainlinkTest
99
contract ChainlinkTest is Test {
1010
Chainlink public chainlink;
11-
Chainlink public chainlinkBTC;
12-
Chainlink public chainlinkETH;
11+
Chainlink public chainlinkBtc;
12+
Chainlink public chainlinkEth;
1313

1414
address public constant BTC_USD = 0x0FB99723Aee6f420beAD13e6bBB79b7E6F034298;
1515
address public constant ETH_USD = 0x4aDC67696bA383F43DD60A9e78F2C97Fbbfc7cb1;
@@ -20,8 +20,8 @@ contract ChainlinkTest is Test {
2020
function setUp() public {
2121
vm.createSelectFork("base_testnet");
2222
chainlink = new Chainlink(BTC_USD);
23-
chainlinkBTC = new Chainlink(BTC_USD);
24-
chainlinkETH = new Chainlink(ETH_USD);
23+
chainlinkBtc = new Chainlink(BTC_USD);
24+
chainlinkEth = new Chainlink(ETH_USD);
2525
}
2626

2727
// RUN
@@ -48,8 +48,8 @@ contract ChainlinkTest is Test {
4848
// RUN
4949
// forge test --match-contract ChainlinkTest --match-test testEthToBtcCalculator -vvv
5050
function testEthToBtcCalculator() public view {
51-
(, int256 priceBtc,,,) = chainlinkBTC.latestRoundData();
52-
(, int256 priceEth,,,) = chainlinkETH.latestRoundData();
51+
(, int256 priceBtc,,,) = chainlinkBtc.latestRoundData();
52+
(, int256 priceEth,,,) = chainlinkEth.latestRoundData();
5353

5454
// forge-lint: disable-next-line(unsafe-typecast)
5555
uint256 result = uint256(priceBtc) * 10 ** uint256(ETH_DECIMALS) / uint256(priceEth);
@@ -59,8 +59,8 @@ contract ChainlinkTest is Test {
5959
// RUN
6060
// forge test --match-contract ChainlinkTest --match-test testBtcToEthCalculator -vvv
6161
function testBtcToEthCalculator() public view {
62-
(, int256 priceBtc,,,) = chainlinkBTC.latestRoundData();
63-
(, int256 priceEth,,,) = chainlinkETH.latestRoundData();
62+
(, int256 priceBtc,,,) = chainlinkBtc.latestRoundData();
63+
(, int256 priceEth,,,) = chainlinkEth.latestRoundData();
6464

6565
// forge-lint: disable-next-line(unsafe-typecast)
6666
uint256 result = uint256(priceEth) * 10 ** uint256(BTC_DECIMALS) / uint256(priceBtc);

test/week2/Orakl.t.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ contract OraklTest is Test {
2323
(, int256 price,) = orakl.latestRoundData();
2424
uint8 decimals = orakl.decimals();
2525
// DECIMALS
26+
// forge-lint: disable-next-line(unsafe-typecast)
2627
console.log(uint256(price) / (10 ** uint256(decimals)));
2728
// NON DECIMALS
29+
// forge-lint: disable-next-line(unsafe-typecast)
2830
console.log(uint256(price));
2931
}
3032
}

0 commit comments

Comments
 (0)