场景
这段时间很多小伙伴加我、都咨询到了一个类似的业务场景、
如下:
- 1、第一步业务里面调用授权函数approve 、给指定address
- 2、第二步是由授权的address调用transferFrom转移给指定的接受地址。
案例DEMO如下(这里test2肯定是会执行失败的)
我问他为什么不只用Token的标准approve 函数外部提前调用一次、然后直接执行第二步就行了、他们都有各自的理由、咱也不好细问。
先解释标准合约中approve 的实现原理,咱们就知道为什么test2会调用失败;
以及要完成这个业务需求该如何改造。
一、标准Toekn
一般标准toen都会实现IERC20协议
1.1 IERC20
协议
/*** @dev Interface of the ERC20 standard as defined in the EIP.*/
interface IERC20 {/*** @dev Emitted when `value` tokens are moved from one account (`from`) to* another (`to`).** Note that `value` may be zero.*/event Transfer(address indexed from, address indexed to, uint256 value);/*** @dev Emitted when the allowance of a `spender` for an `owner` is set by* a call to {approve}. `value` is the new allowance.*/event Approval(address indexed owner, address indexed spender, uint256 value);/*** @dev Returns the value of tokens in existence.*/function totalSupply() external view returns (uint256);/*** @dev Returns the value of tokens owned by `account`.*/function balanceOf(address account) external view returns (uint256);/*** @dev Moves a `value` amount of tokens from the caller's account to `to`.** Returns a boolean value indicating whether the operation succeeded.** Emits a {Transfer} event.*/function transfer(address to, uint256 value) external returns (bool);/*** @dev Returns the remaining number of tokens that `spender` will be* allowed to spend on behalf of `owner` through {transferFrom}. This is* zero by default.** This value changes when {approve} or {transferFrom} are called.*/function allowance(address owner, address spender) external view returns (uint256);/*** @dev Sets a `value` amount of tokens as the allowance of `spender` over the* caller's tokens.** Returns a boolean value indicating whether the operation succeeded.** IMPORTANT: Beware that changing an allowance with this method brings the risk* that someone may use both the old and the new allowance by unfortunate* transaction ordering. One possible solution to mitigate this race* condition is to first reduce the spender's allowance to 0 and set the* desired value afterwards:* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729** Emits an {Approval} event.*/function approve(address spender, uint256 value) external returns (bool);/*** @dev Moves a `value` amount of tokens from `from` to `to` using the* allowance mechanism. `value` is then deducted from the caller's* allowance.** Returns a boolean value indicating whether the operation succeeded.** Emits a {Transfer} event.*/function transferFrom(address from, address to, uint256 value) external returns (bool);
}
1.2 approve 授权函数
解释:external
这里的接口的函数都有一个特点都是由 external
修饰的。
一般在智能合约中,external
修饰符用于限定方法的可见性和调用方式。当一个方法被标记为 external
时,它表示该方法只能从外部账户(即非智能合约账户)调用。这意味着该方法不能被同一个智能合约内部的其他方法直接调用,也不能被其他智能合约调用。
1.3 标准的Token合约实现IERC20
的下这样的、
如下
contract BEP20Token is Context, IERC20, Ownable {using SafeMath for uint256;mapping (address => uint256) private _balances;mapping (address => mapping (address => uint256)) private _allowances;uint256 private _totalSupply;uint8 private _decimals;string private _symbol;string private _name;constructor() public {_name = " ";_symbol =" " ;_decimals = 18;_totalSupply = 10000000 *10**18;_balances[msg.sender] = _totalSupply;emit Transfer(address(0), msg.sender, _totalSupply);}/*** @dev Returns the bep token owner.*/function getOwner() external view returns (address) {return owner();}/*** @dev Returns the token decimals.*/function decimals() external view returns (uint8) {return _decimals;}/*** @dev Returns the token symbol.*/function symbol() external view returns (string memory) {return _symbol;}/*** @dev Returns the token name.*/function name() external view returns (string memory) {return _name;}/*** @dev See {BEP20-totalSupply}.*/function totalSupply() external view returns (uint256) {return _totalSupply;}/*** @dev See {BEP20-balanceOf}.*/function balanceOf(address account) external view returns (uint256) {return _balances[account];}/*** @dev See {BEP20-transfer}.** Requirements:** - `recipient` cannot be the zero address.* - the caller must have a balance of at least `amount`.*/function transfer(address recipient, uint256 amount) external returns (bool) {_transfer(_msgSender(), recipient, amount);return true;}/*** @dev See {BEP20-allowance}.*/function allowance(address owner, address spender) external view returns (uint256) {return _allowances[owner][spender];}/*** @dev See {BEP20-approve}.** Requirements:** - `spender` cannot be the zero address.*/function approve(address spender, uint256 amount) external returns (bool) {_approve(_msgSender(), spender, amount);return true;}/*** @dev See {BEP20-transferFrom}.** Emits an {Approval} event indicating the updated allowance. This is not* required by the EIP. See the note at the beginning of {BEP20};** Requirements:* - `sender` and `recipient` cannot be the zero address.* - `sender` must have a balance of at least `amount`.* - the caller must have allowance for `sender`'s tokens of at least* `amount`.*/function transferFrom(address sender, address recipient, uint256 amount) external returns (bool) {_transfer(sender, recipient, amount);_approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "BEP20: transfer amount exceeds allowance"));return true;}/*** @dev Atomically increases the allowance granted to `spender` by the caller.** This is an alternative to {approve} that can be used as a mitigation for* problems described in {BEP20-approve}.** Emits an {Approval} event indicating the updated allowance.** Requirements:** - `spender` cannot be the zero address.*/function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {_approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));return true;}/*** @dev Atomically decreases the allowance granted to `spender` by the caller.** This is an alternative to {approve} that can be used as a mitigation for* problems described in {BEP20-approve}.** Emits an {Approval} event indicating the updated allowance.** Requirements:** - `spender` cannot be the zero address.* - `spender` must have allowance for the caller of at least* `subtractedValue`.*/function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {_approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "BEP20: decreased allowance below zero"));return true;}/*** @dev Moves tokens `amount` from `sender` to `recipient`.** This is internal function is equivalent to {transfer}, and can be used to* e.g. implement automatic token fees, slashing mechanisms, etc.** Emits a {Transfer} event.** Requirements:** - `sender` cannot be the zero address.* - `recipient` cannot be the zero address.* - `sender` must have a balance of at least `amount`.*/function _transfer(address sender, address recipient, uint256 amount) internal {require(sender != address(0), "BEP20: transfer from the zero address");require(recipient != address(0), "BEP20: transfer to the zero address");_balances[sender] = _balances[sender].sub(amount, "BEP20: transfer amount exceeds balance");_balances[recipient] = _balances[recipient].add(amount);emit Transfer(sender, recipient, amount);}/*** @dev Destroys `amount` tokens from `account`, reducing the* total supply.** Emits a {Transfer} event with `to` set to the zero address.** Requirements** - `account` cannot be the zero address.* - `account` must have at least `amount` tokens.*/function _burn(address account, uint256 amount) internal {require(account != address(0), "BEP20: burn from the zero address");_balances[account] = _balances[account].sub(amount, "BEP20: burn amount exceeds balance");_totalSupply = _totalSupply.sub(amount);emit Transfer(account, address(0), amount);}/*** @dev Sets `amount` as the allowance of `spender` over the `owner`s tokens.** This is internal function is equivalent to `approve`, and can be used to* e.g. set automatic allowances for certain subsystems, etc.** Emits an {Approval} event.** Requirements:** - `owner` cannot be the zero address.* - `spender` cannot be the zero address.*/function _approve(address owner, address spender, uint256 amount) internal {require(owner != address(0), "BEP20: approve from the zero address");require(spender != address(0), "BEP20: approve to the zero address");_allowances[owner][spender] = amount;emit Approval(owner, spender, amount);}/*** @dev Destroys `amount` tokens from `account`.`amount` is then deducted* from the caller's allowance.** See {_burn} and {_approve}.*/function _burnFrom(address account, uint256 amount) internal {_burn(account, amount);_approve(account, _msgSender(), _allowances[account][_msgSender()].sub(amount, "BEP20: burn amount exceeds allowance"));}
}
咱们看一下Token里面标准的函数
1.4 案例出现的问题分析
function test() public {toekn.approve( address(this), 10000000000000000000);}
执行步骤
1、外部调用test函数时
2、函数再调用 toekn.approve
3、此时toekn的approve
函数再调用 _approve
(_msgSender(), spender, amount);
执行步骤分析:
这里有一个 _msgSender()
函数、关键就是这里了。当外部合约调用的Token合约的之时、 _msgSender()
函数本身调用结果就是 外部合约本身地址!!!。
很多同学以为的触发外部函数的 地址、故此from地址把token授权给了合约地址。殊不知是 外部合约 授权给了授权地址。
到此大部分同学已经明白了原理了、但…很快又有同学问、既然如此、我们何不直接调用
_approve
函数,直接传入 咱们的 参数不就好了么…
咱们再来看看 _approve
函数
internal
修饰符的作用
- 访问控制:
internal
方法只能在同一个智能合约内部被其他方法调用。- 这有助于保护智能合约内部的逻辑不受外部调用的影响,从而增强安全性。
在智能合约中,internal
修饰符用于限定方法的可见性和调用方式。当一个方法被标记为 internal
时,它表示该方法只能在同一个智能合约内部被其他方法调用。这意味着该方法不能被外部账户或来自其他智能合约的调用直接访问。
由此可知 _approve
函数是不支持外部调用的,故此是行不通。
二、标准Toekn改造
其实经过上面的原理解释、改造的核心就是对外提供的approve
函数。
function approve(address sendr,address spender, uint256 amount) external returns (bool) {_approve(sendr, spender, amount);return true;}
把sendr函数暴露出来,给外部调用
contract ArrayDeletionByMovingElements {IERC20 public toekn;event Approval(address indexed owner, address indexed spender, uint256 value);event Transfer(address indexed from, address indexed to, uint256 value);constructor(IERC20 _token) {toekn = _token;}function test() public {toekn.approve( msg.sender,address(this), 10000000000000000000);}function test2() public {toekn.transferFrom(msg.sender, address(this), 1000);emit Transfer(msg.sender, address(this), 1000);}
}
不过还是建议走外部函数授权的形式,改造标准Token不可取…、参考市场主流的操作流程较好。