通过remix把BaseERC20部署到sepolia,再部署TokenBank合约来存取部署的BaseERC20

  • 链创通
  • 更新于 2024-07-08 19:20
  • 阅读 609

在Remix中将BaseERC20合约部署到Sepolia并编写和部署TokenBank合约对代币进行存取操作

要在 Remix 中将 BaseERC20 合约部署到 Sepolia 并编写和部署 TokenBank 合约,您可以按照以下步骤操作:

一、部署 BaseERC20 合约到 Sepolia

  1. 设置 Remix 编译器和环境

    • 打开 Remix IDE ([https://remix.ethereum.org)。]()
    • 在左侧的文件浏览器中,创建一个新文件并粘贴 BaseERC20 合约代码。
    • 点击 "Solidity Compiler" 选项卡,选择 0.8.0 或以上版本,然后点击 "Compile" 按钮。
  2. 连接到 Sepolia 网络

    • 点击 "Deploy & Run Transactions" 选项卡。
    • 在 "Environment" 下拉菜单中,选择 "Injected Web3"。确保您已连接到 MetaMask 并且 MetaMask 已切换到 Sepolia 网络。
  3. 部署 BaseERC20 合约

  • 在 "Contract" 下拉菜单中,选择 BaseERC20
  • 点击 "Deploy" 按钮并确认 MetaMask 中的交易。

    BaseERC20 合约

确保已经部署 BaseERC20 合约并获取其地址。

pragma solidity ^0.8.0;

contract BaseERC20 {
    string public name; 
    string public symbol; 
    uint8 public decimals; 

    uint256 public totalSupply; 

    mapping (address => uint256) balances; 
    mapping (address => mapping (address => uint256)) allowances; 

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);

    constructor() {
        name = "BaseERC20";
        symbol = "BERC20";
        decimals = 18;
        totalSupply = 100000000 * (10 ** uint256(decimals));      
        balances[msg.sender] = totalSupply;  
    }

    function balanceOf(address _owner) public view returns (uint256 balance) {
        return balances[_owner];
    }

    function transfer(address _to, uint256 _value) public returns (bool success) {
        require(_to != address(0));
        require(balances[msg.sender] >= _value, "ERC20: transfer amount exceeds balance");
        require(balances[_to] + _value >= balances[_to]); // Overflow check

        balances[msg.sender] -= _value;
        balances[_to] += _value;
        emit Transfer(msg.sender, _to, _value);  
        return true;   
    }

    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        require(_to != address(0));
        require(balances[_from] >= _value, "ERC20: transfer amount exceeds balance");
        require(allowances[_from][msg.sender] >= _value, "ERC20: transfer amount exceeds allowance");
        require(balances[_to] + _value >= balances[_to]); // Overflow check

        balances[_from] -= _value;
        balances[_to] += _value;
        allowances[_from][msg.sender] -= _value;
        emit Transfer(_from, _to, _value); 
        return true; 
    }

    function approve(address _spender, uint256 _value) public returns (bool success) {
        require(_spender != address(0));
        allowances[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value); 
        return true; 
    }

    function allowance(address _owner, address _spender) public view returns (uint256 remaining) {     
        return allowances[_owner][_spender];
    }
}

二、部署 TokenBank 合约

  • 在 Remix 中创建并编译 TokenBank 合约。
  • 部署 TokenBank 合约时,需要提供 BaseERC20 合约地址作为构造函数参数。
pragma solidity ^0.8.0;

interface IERC20 {
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
}

contract TokenBank {
    IERC20 public token;
    mapping(address => uint256) public balances;

    // 定义事件
    event Deposit(address indexed user, uint256 amount);
    event Withdraw(address indexed user, uint256 amount);

    constructor(address _tokenAddress) {
        token = IERC20(_tokenAddress);
    }

    function deposit(uint256 amount) public {
        require(amount > 0, "Amount must be greater than 0");
        require(token.transferFrom(msg.sender, address(this), amount), "Transfer failed");
        balances[msg.sender] += amount;

        // 触发存款事件
        emit Deposit(msg.sender, amount);
    }

    function withdraw(uint256 amount) public {
        require(balances[msg.sender] >= amount, "Insufficient balance");
        balances[msg.sender] -= amount;
        require(token.transfer(msg.sender, amount), "Transfer failed");

        // 触发取款事件
        emit Withdraw(msg.sender, amount);
    }
}

三、与合约交互

  • 确保 BaseERC20 合约的代币已批准 TokenBank 合约花费用户的代币。

    // 在 BaseERC20 合约中调用 approve 方法
    BaseERC20.approve(TokenBankAddress, amount);
  • TokenBank 合约中调用 deposit 方法存入代币。

  • TokenBank 合约中调用 withdraw 方法提取代币。

  • 您可以在 Remix 的 Logs 部分查看存款和取款事件。

存取如下图所示:

tokenbank存取款.png

点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
链创通
链创通
0x5312...1e69
歪脖山徒步虾