//SPDX-License-Identifier:MTT
pragma solidity ^0.8.24;
interface erc20{
function name()external view returns (string memory);//查看代币的名称
function symbol() external view returns(string memory);//查看代币简称
function totalSupply()external view returns(uint);//查看代币的总供应量
function decimal()external view returns(uint8);//查看代币的精度
function balanceOf(address account)external view returns(uint);//查看某账户的余额
function transfer(address recipient ,uint amount) external payable returns(bool);//合约的调用者向某地址发送代币
function approve(address spender ,uint amount) external returns(bool);//合约调用者向某一地址指定代币数量
function allowance(address owner,address spender) external view returns(uint);//查看转账授权的金额
function transferDirect(address _form,address _to,uint amount) external returns(bool);//直接执行转账操作
function transferByApproves(address _to,uint amount) external returns(bool);//通过approve执行转账操作
}
contract erc20tokens{//定义需要的状态变量
string public _name;
string public _symbol;
uint public _totalSupply;
uint8 public _decimal;
mapping(address=>uint)public balances;
mapping(address=>mapping(address=>uint)) public approves;
constructor(//给状态变量进行赋值操作,在构造函数里面赋值时候仅仅会进行一次
string memory _name_,
string memory _symbol_,
uint _totalSupply_,
uint8 _decimal_
) {
_name=_name_;
_symbol=_symbol_;
_totalSupply =_totalSupply_;
_decimal=_decimal_;
}
event Transfer(address _from,address _to,uint amount);//定义事件,记录转账操作
event Approves(address _from,address _to,uint amount);//定义事件,记录授权操作,以及授权的余额
//实现interface接口的函数
function name()external view returns (string memory)//查看代币的名称
{
return _name;
}
function symbol() external view returns(string memory)//查看代币简称
{
return _symbol;
}
function totalSupply()external view returns(uint)//查看代币的总供应量
{
return _totalSupply;
}
function decimal()external view returns(uint8)//查看代币的精度
{
return _decimal;
}
function balanceOf(address account)external view returns(uint)//查看某账户的余额
{
return balances[account];
}
function transfer(address recipient ,uint amount) external returns(bool)//合约的调用者向某地址发送代币
{
require (balances[msg.sender]<=amount,"Sorry,you have no enough amount to send");
return _transfer(msg.sender,recipient,amount);
}
function approve(address spender ,uint amount) external returns(bool)//合约调用者向某一地址授权指定数量代币
{
require(approves[msg.sender][spender]==0,"reapproved");
approves[msg.sender][spender]=amount;
return true;
}
function allowance(address owner,address spender) external view returns(uint)//查看转账授权的金额
{
return approves[owner][spender];
}
function transferByApproves(address _to,uint amount) internal returns(bool)
{
address _from=msg.sender;
require(approves[_from][_to]>=amount,"You hava no right to send too much");
emit Approves(_from,_to,amount);
approves[_from][_to]-=amount;
return _transfer(_from,_to,amount);
}
function transferDirect(address _from,address _to,uint amount) external returns(bool)//执行转账操作
{
require(balances[_from]>=amount,"You have no enough monay");
return _transfer(_from,_to,amount);
}
function _transfer(address _from,address _to,uint amount) internal returns (bool)//转账操作集成于_transfer函数,通过其他需要用到转账的函数只需要通过调用_transfer函数就可以
{
require(balances[_from]>=amount,"Sorry, the amount is too low");//只要调用_transfer 函数就会触发事件
balances[_from]-=amount;
balances[_to]+=amount;
emit Transfer(_from,_to,amount);
return true;
}
}
ps:
授权转账的主要用途包括:
-
代理转账:允许另一个地址代表你进行转账操作,这在复杂的合约交互中特别有用。例如,允许某个合约在你的代币余额中进行交易,而无需事先将代币转移到合约地址。
-
限制额度 :通过
approve
函数授权一个特定的地址可以转移的代币数量,可以帮助减少合约中的风险,因为授权的数量通常是有限的。 -
灵活性:授权机制允许你动态地管理你的资金,可以随时修改授权的地址和数量,这为复杂的合约逻辑提供了更大的灵活性和安全性。