智能合约中approve函数详解

场景

这段时间很多小伙伴加我、都咨询到了一个类似的业务场景、
如下:

  • 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 修饰符的作用
  1. 访问控制:
    • 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不可取…、参考市场主流的操作流程较好。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/3281655.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

相机标定(Camera Calibration)

什么是 相机标定(Camera Calibration)? 相机标定(CameraCalibration)是确定相机内部参数(如焦距、光学中心、畸变系数等)和外部参数(如相机在世界坐标系中的位置和姿态)的…

Jackson常用注解详解

Hi 👋, Im shy 有人见尘埃,有人见星辰 Jackson常用注解详解 文章目录 Jackson常用注解详解0. 引入依赖1. JsonProperty2. JsonIgnore3. JsonFormat4. JsonInclude5. JsonCreator6. JsonValue7. JsonIgnoreProperties结论 Jackson是Java生态系统中广泛…

【Canvas与艺术】三环莫比乌斯圈

【成图】 【代码】 <!DOCTYPE html> <html lang"utf-8"> <meta http-equiv"Content-Type" content"text/html; charsetutf-8"/> <head><title>三环莫比乌斯圈</title><style type"text/css"&g…

插单现象对PMC造成的影响有哪些?

插单&#xff0c;即在生产制造过程中&#xff0c;客户或其他部门临时增加订单&#xff0c;这一行为如同战场上的突袭&#xff0c;让生产与物料控制&#xff08;PMC&#xff09;部门措手不及&#xff0c;面临着前所未有的压力和挑战。那么&#xff0c;插单现象究竟对PMC造成了哪…

【MATLAB源码-第239期】基于matlab的孔雀优化算法(POA)机器人栅格路径规划,输出做短路径图和适应度曲线。

操作环境&#xff1a; MATLAB 2022a 1、算法描述 孔雀优化算法&#xff08;Peafowl Optimization Algorithm&#xff0c;简称POA&#xff09;以孔雀&#xff08;peafowl&#xff09;的求偶展示行为为灵感&#xff0c;通过模拟这一过程来解决复杂的优化问题。以下是对孔雀优化…

深度揭秘——TCP/IP协议

今天我们要深入探讨一个关键的网络基础知识——TCP/IP协议。无论你是刚接触网络技术的新手&#xff0c;还是希望加深理解的老手&#xff0c;本文都将为你提供全面且易懂的介绍。 TCP/IP协议是一组用于互联网的通信协议的集合&#xff0c;包含了传输控制协议&#xff08;TCP&am…

大数据信用报告查询会不会留下查询记录?怎么选择查询平台?

最近有不少网友都在咨询一个问题&#xff0c;那就是大数据信用报告查询会不会留下查询记录&#xff0c;会不会对自己的征信产生影响&#xff0c;下面本文就详细为大家介绍一下&#xff0c;希望对你了解大数据信用有帮助。 首先、大数据信用与人行征信是独立的 很多人只知道人行…

哪款加密软件好用点?迅软DSE加密软件如何?

在选择加密软件时&#xff0c;好用与否往往取决于多个因素&#xff0c;包括&#xff1a;软件的功能、稳定性、安全性、易用性、兼容性以及性价比等。 针对大家提到的迅软DSE加密软件&#xff0c;以下是一些详细的分析&#xff1a; 一、功能全面 迅软DSE加密系统集成了文件加密…

编译环境搭建(Linux)

0 Preface/Foreword 0.1 System requirement Airoha编译环境&#xff0c;对于Linux distro&#xff0c;建议使用Ubuntu 18.04 1 环境搭建 1.1 Create a working folder 在Linux中&#xff0c;创建一个文件夹&#xff0c;用来存放Airoha释放过来的开发工具链安装包。 博主当…

[数据集][目标检测]船上翻越栏杆危险行为检测数据集VOC+YOLO格式3678张1类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;3678 标注数量(xml文件个数)&#xff1a;3678 标注数量(txt文件个数)&#xff1a;3678 标注…

Linux中新添加的磁盘信息不显示-主动扫盘(刷新磁盘状态)

在Linux系统中&#xff0c;当你新添加了一个磁盘&#xff08;无论是通过物理添加还是虚拟化环境&#xff09;&#xff0c;你可能需要让系统识别这个新磁盘&#xff0c;并且可能需要更新或“刷新”磁盘的状态。这通常涉及到几个步骤&#xff0c;但没有一个直接的“刷新磁盘状态”…

CANoe:System Variables模块介绍

写了这么多文章介绍CANoe的功能和使用,发现竟然没有介绍系统变量模块的,这么重要的功能当然要单独写一篇文章介绍啦! 系统变量为CANoe提供了除数据库、环境变量外的另一种数据配置方式。在系统变量模块中定义的各种数据类型的变量,可以被CANoe其他模块或CAPL程序所引用。 …

四,系统规划

一&#xff0c;企业系统规划法BSP&#xff08;2009、2010、2018&#xff0c;步骤以及优缺点、用到的工具&#xff09; 企业系统规划法&#xff08;Business Systems Planning&#xff0c;BSP&#xff09;是IBM公司20世纪70年代提出的一种结构化的信息系统规划方法。该方法通过…

使用 Postman 进行 Trello API 自动化测试的完整指南

文章目录 前言一、自动化测试是什么&#xff1f;二、比较自动化测试与手工测试1. 自动化测试2. 手工测试 三、环境搭建1.创建Collection2.创建环境变量3.添加API请求 四、设计测试用例1. API简单调用2. 获取所有emoji3. 创建一个新看板&#xff1a;4. 获得创建的看板信息5. 在看…

四、GD32 MCU 常见外设介绍(14)

GD32 MCU内部提供了一个RTC&#xff08;实时时钟&#xff09;模块&#xff0c;通过RTC可以实现日历时钟、闹钟等功能。RTC也可以用于深度睡眠或待机模式的低功耗唤醒。不同系列的GD32 MCU在RTC设计和功能上有所区别&#xff0c;总体可以分为三大系列&#xff1a; &#xff08;…

springboot美食网站—计算机毕业设计源码11574

摘 要 随着人们生活水平的提高&#xff0c;人们对美食的要求也越来越高&#xff0c;对各类美食信息需求越来越大。因此&#xff0c;结合计算机快速发展、普及&#xff0c;在此基础上制作一个页面简单、美观,功能实用的美食网站势在必行&#xff0c;满足用户分享美食的需求。 美…

关于Handler你不知道的事

提到Handler&#xff0c;我们都会想到可以用来在子线程给UI线程发送消息&#xff0c;常用来子线程刷新UI。 而往深了问&#xff0c;你一定还知道Handler会绑定到一个Looper&#xff0c;而每个Looper会和一个MessageQ关联&#xff0c;从而达到向指定线程发送消息的功能。除此之外…

【计算机视觉学习之CV2图像操作实战:红绿灯识别1】

红绿灯识别1 步骤 高斯模糊边缘提取膨胀腐蚀中值滤波再次膨胀霍夫圆环检测区域将图片从BGR格式转换为HSV格式设计颜色阈值中值滤波统计像素点数 import cv2 import numpy as np COLOERS {red: (0, 0, 255),green: (0, 255, 0),yellow: (0, 255, 255), } def detect_color(im…

探索Netty框架的核心构件

Netty是一个高性能、异步事件驱动的网络应用程序框架&#xff0c;用于快速开发可维护的高性能、高可靠性的网络服务器和客户端程序。本文将深入探讨Netty框架的基本组件&#xff0c;帮助开发者更好地理解和使用这一强大的工具。 1. 事件循环&#xff08;EventLoop&#xff09;…