在Remix中将BaseERC20合约部署到Sepolia并编写和部署TokenBank合约对代币进行存取操作
要在 Remix 中将 BaseERC20
合约部署到 Sepolia 并编写和部署 TokenBank
合约,您可以按照以下步骤操作:
BaseERC20
合约到 Sepolia设置 Remix 编译器和环境:
BaseERC20
合约代码。0.8.0
或以上版本,然后点击 "Compile" 按钮。连接到 Sepolia 网络:
部署 BaseERC20
合约:
BaseERC20
。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
合约: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 部分查看存款和取款事件。
存取如下图所示:
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!