OnlyPwner- WRAPPED ETHER

OnlyPwner- WRAPPED ETHER

OnlyPwner- WRAPPED ETHER-魔法少女雪殇
OnlyPwner- WRAPPED ETHER
此内容为付费阅读,请付费后查看
10
立即购买
您当前未登录!建议登陆后购买,可保存购买订单
付费阅读

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
喜欢就支持一下吧
点赞10 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情

    暂无评论内容