WRAPPED ETHER
还是先看一下原始合约
pragma solidity 0.8.20;
import {IWrappedEther} from "./interfaces/IWrappedEther.sol";
contract WrappedEther is IWrappedEther {
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
function deposit(address to) external payable {
balanceOf[to] += msg.value;
emit Deposit(msg.sender, msg.value);
}
function withdraw(uint256 amount) external {
require(balanceOf[msg.sender] >= amount, "insufficient balance");
balanceOf[msg.sender] -= amount;
sendEth(payable(msg.sender), amount);
emit Withdraw(msg.sender, amount);
}
function withdrawAll() external {
sendEth(payable(msg.sender), balanceOf[msg.sender]);
balanceOf[msg.sender] = 0;
emit Withdraw(msg.sender, balanceOf[msg.sender]);
}
function transfer(address to, uint256 amount) external {
require(balanceOf[msg.sender] >= amount, "insufficient balance");
balanceOf[msg.sender] -= amount;
balanceOf[to] += amount;
emit Transfer(msg.sender, to, amount);
}
function transferFrom(address from, address to, uint256 amount) external {
require(balanceOf[from] >= amount, "insufficient balance");
require(
allowance[from][msg.sender] >= amount,
"insufficient allowance"
);
balanceOf[from] -= amount;
balanceOf[to] += amount;
allowance[from][msg.sender] -= amount;
emit Transfer(from, to, amount);
}
function approve(address spender, uint256 amount) external {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
}
function sendEth(address payable to, uint256 amount) private {
(bool success, ) = to.call{value: amount}("");
require(success, "failed to send ether");
}
}
经典erc20,一些功能,没什么好说的,然后题目满足条件的合约是要把合约的余额变成0。
看一下部署合约
pragma solidity 0.8.20;
import {WrappedEther} from "../src/WrappedEther.sol";
import {Script} from "forge-std/Script.sol";
import {console} from "forge-std/console.sol";
contract Deploy is Script {
function run() external {
vm.startBroadcast();
address user = vm.envAddress("USER");
WrappedEther weth = new WrappedEther();
weth.deposit{value: 1 ether}(address(uint160(user) + 1));
weth.deposit{value: 1 ether}(address(uint160(user) + 2));
weth.deposit{value: 1 ether}(address(uint160(user) + 3));
weth.deposit{value: 1 ether}(address(uint160(user) + 4));
weth.deposit{value: 1 ether}(address(uint160(user) + 5));
payable(user).transfer(1 ether);
console.log("address:WrappedEther", address(weth));
}
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容