tp官网下载钱包|uniswap地址
Home | Uniswap Protocol
| Uniswap ProtocolEcosystemCommunityGovernanceDevelopersBlogFAQJobsLaunch AppMenuUNISWAP PROTOCOLSwap, earn, and build on the leading decentralized crypto trading protocol.Launch App$489B+Trade Volume71M+All Time Trades300+Integrations4,400+Community DelegatesUNISWAP ECOSYSTEM →A growing network of DeFi Apps.Developers, traders, and liquidity providers participate together in a financial marketplace that is open and accessible to all.300+IntegrationsExplore all ↗DEVELOPERS →Superpowers for DeFi developers.Build Defi apps and tools on the largest crypto project on Ethereum. Get started with quick start guides, protocol documentation, a Javascript SDK, and fully open source code.Documentation ↗V3 WhitepaperGithubApply for funding from the Uniswap Grants ProgramGet paid to build the future of finance. Uniswap Governance offers grant funding for people building apps, tools, and activities on the Uniswap Protocol.Learn more ↗PROTOCOL GOVERNANCE ->Governed by the community.The Uniswap Protocol is managed by a global community of UNI token holders and delegates.Read more ↗Governance Forum Participate by proposing upgrades and discussing the future of the protocol with the Uniswap community.Sybil Vote on offchain proposals with the Snapshot interface. Votes are weighted by the number of UNI delegates.Governance Portal Vote on official Uniswap governance proposals and view past proposals. EcosystemCommunityGovernanceDevelopersBlogFAQPrivacy PolicyTrademark PolicySecurityMedia inquires for Uniswap Labs - Contact [email protectHome | Uniswap Protocol
| Uniswap ProtocolEcosystemCommunityGovernanceDevelopersBlogFAQJobsLaunch AppMenuUNISWAP PROTOCOLSwap, earn, and build on the leading decentralized crypto trading protocol.Launch App$489B+Trade Volume71M+All Time Trades300+Integrations4,400+Community DelegatesUNISWAP ECOSYSTEM →A growing network of DeFi Apps.Developers, traders, and liquidity providers participate together in a financial marketplace that is open and accessible to all.300+IntegrationsExplore all ↗DEVELOPERS →Superpowers for DeFi developers.Build Defi apps and tools on the largest crypto project on Ethereum. Get started with quick start guides, protocol documentation, a Javascript SDK, and fully open source code.Documentation ↗V3 WhitepaperGithubApply for funding from the Uniswap Grants ProgramGet paid to build the future of finance. Uniswap Governance offers grant funding for people building apps, tools, and activities on the Uniswap Protocol.Learn more ↗PROTOCOL GOVERNANCE ->Governed by the community.The Uniswap Protocol is managed by a global community of UNI token holders and delegates.Read more ↗Governance Forum Participate by proposing upgrades and discussing the future of the protocol with the Uniswap community.Sybil Vote on offchain proposals with the Snapshot interface. Votes are weighted by the number of UNI delegates.Governance Portal Vote on official Uniswap governance proposals and view past proposals. EcosystemCommunityGovernanceDevelopersBlogFAQPrivacy PolicyTrademark PolicySecurityMedia inquires for Uniswap Labs - Contact [email protectDeFi工具—UNISWAP使用教程 - 知乎
DeFi工具—UNISWAP使用教程 - 知乎首发于DeFi挖矿教程大全切换模式写文章登录/注册DeFi工具—UNISWAP使用教程派派什么是Uniswap?Uniswap是 2018 年 11 月发布在以太坊主网上的去中心化交易所协议,它的创建者是 Hayden Adams。当前Uniswap在DeFi Pluses上拍名第11位,较之前有下降。不同于其它挂单式去中心化交易所,Uniswap采用了全新的AMM自动做市商算法来执行交易的设计。相比于其他交易所,Uniswap 交易所的智能合约设计能够大幅减少 gas 的用量。如何理解Uniswap的自动做市机制?传统的交易所一般是提供一个挂单平台,通过程序(撮合引擎)撮合买卖双方达成交易。它是一个自由买卖的市场,具有买卖意愿的人们自行挂出“买单”和“卖单”,通过交易所“中介”实现双方订单的成交。传统交易所有以下特点:市场上必须要有用户进行挂单,要有一定量的订单(市场深度)。订单必须重叠才能成交,即买价高于或等于卖价。需要将资产存储在交易所。而Uniswap的模型却完全不同。简单来讲,Uniswap的撮合机制基于一个最基本的公式:K=X*Y。其中,X代表一种资产,Y代表与X等值的另一种资产, 而K则是它俩相乘后的一个函数。当K值不变的情况下,X与Y的值就成反比,即X增加,Y就会减小(反之亦然)。而因为资产Y的较少会导致相对市场的溢价,则会吸引用户来进行搬砖套利,从而抹平价差,使系统重归平衡。下面我们就以ETH与MakerDAO的算法稳定币DAI为例,来为大家解释下这个模型(不考虑手续费)。假设当前ETH价格为150USD,DAI的价格与美金挂钩,等于1USD。现在往系统里注入100ETH与15,000 DAI,则K=100*15,000=1,500,000。当用户将2个ETH兑换成DAI后,ETH总量减少为98,则此时DAI的总量=1,500,000/98=15,306。而因此增加的306个DAI就等于2个ETH的价格,即1ETH=153USD。此时,ETH就会相对市场有溢价,会吸引用户来进行搬砖套利。用户将自己的ETH卖给系统,补充资金池里的ETH,使系统再次回到最初的稳定状态。Uniswap有什么不足之处?在挂单机制下,如果价格达不到挂单者的设置价格,系统是不会成交的。但是在Uniswap的模式下,用户要注意当前资金池的流动性,以免滑点太多,造成资产损失。这里给大家进行了一个简单计算,从表中我们可以看出,随着系统内ETH数量的增加,用户购买单个DAI相对于ETH的成本就会增高。当数量为10ETH时,点差可以达到9.89%。而前段时间UMA上线Uniswap,价格从0.26一路走高到1.2美金的事情也印证了这一点。针对这一点,Balancer做了进一步的修改。但是总体来看,这类AMM机制的DEXes难免会因为流动性问题而出现滑点。不过,对于初创企业来说,此类的DEXes是它们为Token定价并进行销售的福音,Uniswap为他们省去了一大笔要支付给交易所跟做市商的钱。如何在比特派里使用Uniswap?第一步,打开比特派并找到Uniswap安卓版本可以在发现页下的推荐/热门DApp里找到Uniswap。iOS版请更新到bitpie PRO 版本,在发现页的搜索框里输入:https://uniswap.bitpie.com/* Uniswap因为使用的是新加坡域名,所以经常会遇到加载缓慢或者访问出错的情况,而比特派则对此做了优化,当前使用比特派访问Uniswap的体验十分顺畅。第二步,进入DApp主页面,选择要使用的功能图为改版后的Uniswap页面,个人感觉比之前更简洁了。第三步,选择兑换,输入兑换金额及币种,选择浮点后进行兑换这里以ETH兑换DAI为例,并选择接受增加最多0.5%的偏差。设定完毕后,点击“兑换”。待订单确认后,你的钱包地址里就会收到DAI了。从主页面我们可以看到,除了兑换还有“发送”与“资金池”功能。这两个功能都属于V2版本,因为使用时系统会自动切换到V2下。其中,发送与兑换功能大同小异,只是增加了一个收款地址,让你在兑换完成后可以将币发到自己/他人的指定地址,或者ENS域名。而资金池功能,是允许用户通过为系统注入ETH增加系统流动性而获取收益的。下图则展示了如何使用资金池功能为系统注入流动性。以上就是对Uniswap的简介及使用体验了。DeFi市场最近因为Compound的借贷挖矿又火了一把,引来的市场的广泛关注,这对DeFi市场来说是一件好事。同时,DeFi产品因其自身具有去中心化、抗审查、公开透明等特性也正逐渐受到市场的青睐。比特派也会持续关注DeFi生态,为大家带来更多的DeFi产品介绍及体验。本文仅为第三个工具在钱包端的操作教程,不构成任何投资建议。投资有风险,请您谨慎评估。安全、强大钱包就用比特派发布于 2020-09-02 17:08交易所智能合约赞同 243 条评论分享喜欢收藏申请转载文章被以下专栏收录DeFi挖矿教程大全持续更新各类 DeFi 工具使用,微博@比特
小白课堂 | 一文读懂Uniswap,附Uniswap使用教程 - 知乎
小白课堂 | 一文读懂Uniswap,附Uniswap使用教程 - 知乎首发于数字货币切换模式写文章登录/注册小白课堂 | 一文读懂Uniswap,附Uniswap使用教程书匠火火玄学一、加密货币交易形式 当我们要进行加密货币交易时,使用最早也是目前使用最多的形式还是中心化交易所,在中心化交易所,我们首先需要注册,然后加密货币也需要存入到交易所,由交易所进行托管,如果要提现加密货币出来,也需要经过交易所审核同意。虽然中心化交易所有诸多优势,例如交易速度较快、用户不需要管理私钥,降低了用户的使用门槛,但是它的弊端也是显而易见的,用户的加密货币由交易所托管,交易所是有跑路风险的。也确实发生过多起交易所跑路的事件,几乎每年都有发生。那么,有没有更好的加密货币交易形式呢? 随着区块链技术的不断发展,加密货币交易形式也变得越来越多样化,我们不但可以使用中心化交易所进行交易,也可以使用去中心化交易所进行交易。在去中心化交易所进行交易时,不需要注册,只需要使用数字钱包连接去中心化交易所就可以进行加密货币的交易了,交易完成后,相应的加密货币会自动转入到用户的数字钱包中,用户的资产始终在自己的钱包中,并非像中心化交易所那样托管在交易所,所以,在去中心化交易所进行交易,安全性大大提高了。 目前,去中心化交易所主要有两种形式,一种是交易所撮合买方用户和卖方用户的订单,只不过操作过程发生在链上,典型代表有 EOS 去中心化交易所 Newdex,Newdex 依旧承担了中心化交易所的订单撮合模式。在 Newdex 交易,用户无需注册、资产无需托管,撮合发生在 EOS 链上,数据公开透明。 去中心化交易所的另一种形式是基于兑换池,而非基于买方和卖方订单所构成的订单簿,它类似于人机交易,采取的是柜台模式,就像我们去银行兑换不同国家的法币。典型的代表是 Uniswap。二、Uniswap 是什么Uniswap 是基于以太坊的代币交换协议,是基于兑换池,而不是订单簿的去中心化交易协议。而所谓的兑换池,指的则是一个资金池,用户在 Uniswap 中交易的价格则由这个资金池中的代币比例和算法来决定。Uniswap 是去中心化的,不仅跟传统的加密货币交易所不同,也跟普通的去中心化代币交易所不同。Uniswap 是一组部署到以太坊网络的合约,所有的交易都在链上进行。Uniswap 的交易对手并不是其他交易用户,而是跟代币池进行交易,且有自动做市的模型来计算交易价格,代币兑换价格与代币兑换池中代币的比例有关。三、Uniswap 的功能和优势 在 Uniswap 中,使用最多的功能就是代币之间的币币兑换交易了,由于 Uniswap 是基于以太坊的,所以在 Uniswap 中只能交易以太坊上的加密货币资产。目前 Uniswap 支持的币种大概在150种左右,以太坊上的主流资产基本上都可以在上面进行交易了。当然了,用户也可以自行添加交易对,也就是做市商,这样可以赚取一定的手续费。 使用 Uniswap 这种交易形式,比订单撮合模式速度更快,因为它是基于兑换池的人机交易,节省了撮合时间。而且 Uniswap 支持的币种间可以实现两两兑换交易,交易对明显比中心化交易所更丰富。 例如: LRC 和 KNC 之间的交易,在 Uniswap 是可以一步完成交易(虽然系统可能需要转换,但在用户看来就是一步交易)的。而在中心化交易所,LRC 和 KNC 没有直接的交易对,用户最少需要两次交易,即把 LRC 兑换成 ETH ,再用 ETH 和 KNC 交易。很显然,对于用户来说,Uniswap 更便捷。四、如何使用 Uniswap 进行币币兑换交易 我们可以通过多链钱包 TokenPocket 来连接 Uniswap 进行交易。在 TP 钱包的发现界面的 DeFi 区,找到 Uniswap 并点击进入。1、兑换我们可以选择 V2 版本。在兑换界面中,输入表示的是拿来用于兑换的币种,而输出表示的是兑换后的币种。例如:拿 LRC 兑换 KNC,输入部分要选择 LRC,而输出部分要选择 KNC。在选择好输入输出币种后,输入输填写数量,即用于兑换 KNC 的 LRC 数量,可以收到的 KNC 数量就会根据 LRC/KNC 当前的汇率自动计算出来并填充。输入完成后,点击"授权 LRC"按钮,然后点击右侧的"兑换"就可以完成 LRC 和 KNC 之间的兑换了。2、发送 在 Uniswap 的发送界面中,主要有两个功能。一个是转账功能。选择要转出的币种,输入转账数量以及接收地址,然后点击"send"就可以转账了,这和一般的钱包转账也没什么区别。另外一个功能是"兑换+转账",挺有意思的一个功能。 例如我们要把 LRC 兑换为 KNC,并转账到另外一个 ETH 地址中,就可以使用该功能,还是挺方便的。首先,在发送界面中点击"添加兑换"按钮。 然后选择兑换的两个币种和用于兑换 KNC 的 LRC 数量,并输入接收地址。最后,点击"授权LRC"按钮后,点击 "Send"按钮就可以完成代币的兑换以及转账了。3、资金池 在 Uniswap 中,除了可以用于代币兑换之外,还可以加入 Uniswap 的资金池,通过为 Uniswap 提供流动性而赚取收益。首先在"资金池"界面中点击"加入一个资金池"按钮,然后选择一个流动性池,例如选择 ETH/USDT,在选择了流动性池后,输入要存入资金池的币种数量。注意,交易对是成比例存入资金池的,例如上例中,如果存入 0.091216 ETH 到资金池,那么USDT就要存入21.751个。 输入完成后,点击下面的"授权 USDT",然后点击"供应"就可以加入资金池了。总之,通过 Uniswap 进行以太坊上数字资产之间的交易,还是非常方便的,不需要注册,也不需要托管在交易所,通过钱包连接到 Uniswap 就可以进行交易了,交易完成后,相应的数字资产也会自动转入到用户的 ETH 钱包中,而且在兑换时还可以选择转到其他地址中。如果要使用 Uniswap 进行交易,可以到多链钱包 TokenPocket 中体验和使用。源自公众号:库尔班区块链发布于 2020-12-15 18:58Uniswap去中心化交易所(DEX)赞同 8332 条评论分享喜欢收藏申请转载文章被以下专栏收录数字货币在这里,读懂
手把手教你部署自己的uniswap交易所 - 知乎
手把手教你部署自己的uniswap交易所 - 知乎首发于登链社区切换模式写文章登录/注册手把手教你部署自己的uniswap交易所登链社区区块链技术爱好者的家园本文作者:崔棉大师演示地址: https://fankouzu.github.io/cuiswap/ (请在测试网使用)准备Uniswap合约源码源码结构Uniswap在Github上面开源了全部合约代码,其中包括核心合约,周边合约两部分.Uniswap还开源了前端代码,前端代码使用React开发核心合约周边合约前端代码在Uniswap的核心代码中,主要包含3个合约:工厂合约,配对合约,ERC20合约.其中配对合约继承了ERC20合约,我们可以把它们看作一个合约.工厂合约通过create2方法部署配对合约,所以在部署合约时只需要部署工厂合约.周边合约中包括一些示例代码,例如价格预言机,闪电交换,其中最重要的是路由合约.在周边合约的代码库中,包含两个路由合约:UnsiwapV2Router01,UnsiwapV2Router02.工厂合约和配对合约需要通过路由合约调用才能更好的完成交易所的全部功能,所以我们还要部署路由合约两个合约大部分相同,有小部分不同,如果将两个合约的差异化合并成一个合约,部署的时候将会出现out of gas,所以才被分成了两个合约.常用功能两个合约中都包括,所以我们部署其中任意一个路由合约都可以继承引用调用ERC20合约配对合约工厂合约路由合约从浏览器中下载合约源码如果你对合约代码并不熟悉,也可以跳过上面这部分,接下来我们将从以太坊浏览器中直接拷贝线上版合约源码工厂合约路由合约01[可选]路由合约02部署合约准备部署账户Uniswap的路由合约部署在以太坊的主网和Ropsten,Rinkeby,Goerli,Kovan几个测试网的合约地址都是相同的,这样可以使Uniswap的前端不管切换到任何一个网络,路由地址都不会变.要想实现这个相同地址的部署,我们需要准备一个全新的账户用来部署合约.全新的账户指的是在部署合约之前的nonce值为0.因为合约的地址是根据你的账户地址和nonce值计算出来的,所以在不同网络中,如果nonce值相同,部署出的合约地址也相同.通过助记词生成新账户可以通过我之前录制的视频学习操作方法B站视频油管视频生成好助记词之后,记得用英文助记词,保存好助记词,还有助记词对应的账户地址向新地址转帐ETH部署合约需要的gas费约为0.18个Ether,目前主网可能需要的更多.通过一个已有Ether的账户向新账户转帐.测试网的Ether可以通过每个测试网的水龙头申请到测试币.获取测试币方法转账完成后,将助记词导入到Metamask中准备WETH合约地址在部署路由合约时,构造函数中需要填入工厂合约的地址和WETH合约的地址,由于WETH合约的地址在主网和测试网的地址都不相同,所以需要找到每个网络中WETH合约的地址.WETH合约用于将Eth交换为erc20的Eth,由于Eth不是erc20的token,所以我们必须使用WETH作为交换媒介{
mainnet:'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
ropsten:'0xc778417E063141139Fce010982780140Aa0cD5Ab',
rinkeby:'0xc778417E063141139Fce010982780140Aa0cD5Ab',
goerli:'0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6',
kovan:'0xd0A1E359811322d97991E03f863a0C30C2cF029C'
}申请infuraKey在部署合约之前,我们还需要使用infura作为免费节点,所以需要申请一个infuraKey申请地址:infura.io申请方法使用remix部署合约将工厂合约和路由合约的线上版本导入到remix中,在编译合约的选项中,EVM VERSION选择istanbul,COMPILER CONFIGURATION中选择Enable optimization部署顺序和构造函数部署工厂合约构造函数的参数是一个自己常用的账户地址部署路由合约01 [可选], 02构造函数的参数1是工厂合约的地址参数2 是当前网络中WETH合约的地址,参考前文部署Uniswap前端克隆前端代码在项目目录运行命令:$ git clone https://github.com/Uniswap/uniswap-interface.git安装依赖库在项目目录运行命令:$ cd uniswap-interface
$ yarn安装完成后,可以先测试运行一下,在uniswap-interface目录运行命令$ yarn start如果运行成功,将会打开一个浏览器,同时打开Uniswap的前端界面修改路由地址在Uniswap的前端中以常量的形式定义了Uniswap的路由地址,我们只需要修改路由地址就可以让前端链接到你的路由合约中修改文件: 项目目录/uniswap-interface/src/constants/index.ts 第6行import { AbstractConnector } from '@web3-react/abstract-connector'
import { ChainId, JSBI, Percent, Token, WETH } from '@uniswap/sdk'
import { fortmatic, injected, portis, walletconnect, walletlink } from '../connectors'
export const ROUTER_ADDRESS = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D' //修改成你的路由合约地址
......保存后运行yarn start即可看到效果将代码部署到GitHub Pages创建GitHub项目创建项目的方法就不在这里讲了,不会的同学可以去搜索一下将前端代码添加到GitHub项目仓库首先要删除原先Uniswap项目中的.git目录,在项目目录运行命令:$ cd uniswap-interface
$ rm -rf .git然后初始化git,并将Unsiwap前端代码添加到自己的项目仓库中git init
git remote add origin https://github.com/用户名/项目名.git安装并部署gh-pages我们将通过gh-pages模块将前端代码部署到github.io,在前端代码的目录运行:$ yarn add gh-pages接下来要编译react和部署gh-pages,在前端代码的目录运行:$ yarn build修改前端代码目录中的package.json$ vim package.json
{
"name": "@uniswap/interface",
"description": "Uniswap Interface",
"homepage": "https://用户名.github.io/项目名称",//修改这里
......
// 添加部署的脚本,还是在package.json中
......
"scripts": {
......
"deploy": "gh-pages -d build" //添加这一行
},保存退出之后,在前端代码的目录运行:$ git add .
$ git commit -m "first commit"
$ git push
$ yarn deploy现在在浏览器中打开https://用户名.github.io/项目名称/index.html就可以打开自己的交易所啦.如果不输入地址结尾的index.html在项目刚部署之后会报错,过一段时间就可以不输入了.扩展部署自己的weth可以将以太坊浏览器中的weth源码拷贝下来,自己部署一个属于自己的weth合约可信token列表Uniswap有一个自己的可信token列表,同样被设置在项目目录/uniswap-interface/src/constants/index.ts文件中,在最后一行就是.你可以将这个链接地址的文件拷贝下来,设置成自己需要的可信token地址列表,然后上传到github目录中,再修改index.ts文件中的链接地址,这样就可以让你自己的交易所中拥有自己设置的可信token列表了原文链接:https://learnblockchain.cn...作者主页:https://learnblockchain.cn... ,欢迎阅读作者更多好文登链社区-区块链技术爱好者的家园发布于 2020-09-02 15:19交易所区块链技术赞同 162 条评论分享喜欢收藏申请转载文章被以下专栏收录登链社区高质量区块链技术文
Uniswap Interface
wap InterfaceYou need to enable JavaScript to run this apUniSwap 学习笔记1: 概览 以及 交易对地址计算 | 登链社区 | 区块链技术社区
UniSwap 学习笔记1: 概览 以及 交易对地址计算 | 登链社区 | 区块链技术社区
文章
问答
讲堂
专栏
集市
更多
提问
发表文章
活动
文档
招聘
发现
Toggle navigation
首页 (current)
文章
问答
讲堂
专栏
活动
招聘
文档
集市
搜索
登录/注册
UniSwap 学习笔记1: 概览 以及 交易对地址计算
tonyh
更新于 2022-04-20 11:23
阅读 5052
Uniswap core 有3个合约: Factory, Pair, WETH。Uniswap periphery 有1个合约: Router
hello everyone, 我是tony, 今天开始为大家分享我最近学习Uniswap的小小心得, 希望各位看官多多提意见, 大家一起进步.
Uniswap V2 代码地址:
核心代码: 包括 Factory, Pair, WETH 3个合约
https://github.com/Uniswap/v2-core
外围代码: Router合约
https://github.com/Uniswap/v2-periphery
## 概览
Uniswap core 有3个合约: Factory, Pair, WETH
Uniswap periphery 有1个合约: Router
* Factory合约 (UniswapV2Factory.sol):
负责创建交易对, 保存所有交易对的地址 .
* Pair合约 (UniswapV2Pair.sol) :
保存单个交易对的资金池信息, 包括每个交易对中两个ERC20 代币的地址, 以及各自资金余额(reserve).
处理 mint, burn 和 交易(swap) 操作. Pair合约是Uniswap代码的核心
UniswapV2Pair 本身也是ERC20, 当添加流动性时会mint新的pair代币(pair token, or LP token), 删除流动性时会销毁一定数量的代币, 确保 pair token的 totalSupply 始终等于池子中的两个交易对代币余额的几何平均值: totalSupply of pair token === sqrt(reserve1 * reserve2)
* WETH合约:
这个合约是一个特殊的ERC20, 它的目的是方便用户直接用 ETH和ERC20进行交易,
你可以把 WETH 看作是一个全额担保的债券: 这个合约里面存储的 ETH 总是等于它发行的代币 WETH, (除非有人直接使用 WETH_address.transfer 向它发送了ETH ).
Pair合约的交易对总是ERC20代币, 不能进行 ETH 和 ERC20 之间的交易, 所以 WETH就被创造出来, 它有两个方法, deposit() 和 withdraw(), 前者是存入 ETH获得 WETH, 后者是 销毁 WETH获得ETH. 但是这两个方法通常是由 Router来调用的, 例如 Router中的 swapExactETHForTokens 函数:
```
function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
external
virtual
override
payable
ensure(deadline)
returns (uint[] memory amounts)
{
require(path[0] == WETH, 'UniswapV2Router: INVALID_PATH');
amounts = UniswapV2Library.getAmountsOut(factory, msg.value, path);
require(amounts[amounts.length - 1] >= amountOutMin, 'UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT');
IWETH(WETH).deposit{value: amounts[0]}();
assert(IWETH(WETH).transfer(UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]));
_swap(amounts, path, to);
}
```
上面的代码中, 用户需要用 ETH 换成 ERC20代币, 那么只能使用 WETH-ERC20 交易对, 用户将ETH发送给 Router, Router获得 ETH后用它换出 WETH ( 调用WETH.deposit() ), 再将 WETH 发送给 Pair, 换出目标 ERC20代币发送给参数 to 中指定的接收账户.
* Router合约: (UniswapV2Router02.sol)
这个合约包含了几乎所有提供给应用层使用的函数 (除了 Factory 的 getPair和 createPair), 包括
<添加,撤销流动性> 和 <交易>, 它和 Pair的区别如下:
Pair中没有添加, 撤销流动性操作, 只有 mint 和 burn.
Pair中的 swap操作是单个交易对的操作, 而Router的 swap 是可以给定路径, 进行一系列交易后得到最终目标代币
token1 -> token2 ... -> token_target
在此过程中, Router会依次调用 (token_i, token_i+1) 所对应的交易对的 swap函数, 并将换出的代币发送到
(token_i+1, token_i+2)交易对合约地址, 再次执行swap, 发送... 直到换出最后的 token_target
在后面的介绍中, 我们会阅读源码, 研究 swap的流程细节.
## Pair的创建 和 create2 的地址计算:
交易对的创建是在 Factory中完成, UniswapV2Factory.ceatePair 函数定义如下:
```
function createPair(address tokenA, address tokenB) external returns (address pair) {
require(tokenA != tokenB, 'UniswapV2: IDENTICAL_ADDRESSES');
(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), 'UniswapV2: ZERO_ADDRESS');
require(getPair[token0][token1] == address(0), 'UniswapV2: PAIR_EXISTS'); // single check is sufficient
bytes memory bytecode = type(UniswapV2Pair).creationCode;
bytes32 salt = keccak256(abi.encodePacked(token0, token1));
assembly {
pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
}
IUniswapV2Pair(pair).initialize(token0, token1);
getPair[token0][token1] = pair;
getPair[token1][token0] = pair; // populate mapping in the reverse direction
allPairs.push(pair);
emit PairCreated(token0, token1, pair, allPairs.length);
}
```
这里Factory使用了 create2操作码部署新的 Pair 合约, 而不是 pair = new UniswapV2Pair(tokenA, tokenB):
```
assembly {
pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
}
```
这样做的好处是: 它可以得到一个确定的pair地址, 使得 Router中就可以通过 tokenA, tokenB 计算出pair地址, 不再需要执行一次 Factory.getPair(tokenA, tokenB) 的跨合约调用, create2生成地址的规则是:
```
keccak256(abi.encodePacked(
hex'ff',
deployer_address, // 由Factory创建, 这里就是 factory合约地址
salt, // salt 是创建者自定义的数据, 避免重复, 这里的
// salt = keccak256(abi.encodePacked(token0, token1)), 且 token0 < token1
keccak256(type(Contract_to_Deploy).creationCode) // 此处 Contract_to_Deploy = UniswapV2Pair
))))
```
参考 periphery的 libraries/UniswapV2Library.sol
```
function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
(address token0, address token1) = sortTokens(tokenA, tokenB);
pair = address(uint(keccak256(abi.encodePacked(
hex'ff',
factory,
keccak256(abi.encodePacked(token0, token1)),
hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
))));
}
```
在这里, Router为了节省gas, 使用 pairFor获取 tokenA 和 tokenB 的交易对地址, 而且最后的字节码哈希也是预先计算出来, 硬编码到代码里面.
如果我们自己修改了 Pair的源代码合约, 重新部署后, Router就无法找到这个Pair地址了, 解决办法要么直接返回 factory.getPair(tokenA, tokenB), 要么就把以前部署的 Pair全部舍弃, 重新部署Factory和所有Pair, 再重新部署Router.
下面的 python示例代码用于计算 pair的hash, 我们可以在重新部署 Pair后调用这个函数, 再把hash复制到 UniswapV2Library.pairFor() 进行替换:
同时修改 UniswapV2Factory.sol , 增加两个函数用于测试:
```
contract UniswapV2Factory is IUniswapV2Factory {
...
/*
* 返回 Pair的字节码
*/
function pair_code() public view returns(bytes memory){
return type(UniswapV2Pair).creationCode;
}
/*
* 返回 Pair的字节码哈希
*/
function pair_codehash() public view returns(bytes32 ){
return keccak256( type(UniswapV2Pair).creationCode );
}
...
}
```
以下是python代码, 用于计算pair的 code_hash, 并且模拟 create2计算交易对地址, 如果与 factory.getPair得到的地址相同, 证明我们的 code_hash计算正确, 可以拷贝到 UniswapV2Library.sol 的 pairFor函数
```
###########################################################################################
#
# 重新编译并部署 pair 和 Factory 后, 调用此函数计算出hash, 放到 peripery/libraries/UniswapV2Library.sol 的
# 'pairFor' 函数, 替换原来的hash,
# 然后重新编译并部署Router
#
###########################################################################################
def calc_pair_address():
# 执行本函数前, 确保已经重新编译并部署 Factory 和 Pair, 同时部署测试代币 WBTC, USDT及交易对
# 执行完本函数后, 需要修改 Router源代码, 重新编译部署Router
abi, bytecode = _helper.get_contract_info(r"core/artifacts/UniswapV2Pair.json")
WBTC, USDT = _contracts["WBTC"], _contracts["USDT"]
factory = _contracts["Factory"]
pair_codehash = Web3.keccak(bytes.fromhex(bytecode))
addr1, addr2 = sorted((WBTC.address, USDT.address))
salt = Web3.solidityKeccak(['address' , 'address'], [addr1, addr2])
pair_address = Web3.solidityKeccak(['uint8', 'address' , 'bytes', 'bytes'],
[0xff,
factory.address,
salt,
pair_codehash])
print(f"pair code(python): 0x{bytecode}", )
evm_code = factory.functions.pair_code().call()
print(f"pair code (evm): 0x{evm_code.hex()}")
print(f"code hash(python): \n\t{pair_codehash.hex()}", )
evm_codehash = factory.functions.pair_codehash().call()
print(f"code hash(evm): \n\t0x{evm_codehash.hex()}")
print(f"pair address of WBTC-USDT(calculated from python): \n\t{pair_address.hex()}")
evm_pair_address = factory.functions.getPair(WBTC.address, USDT.address).call()
print(f"pair address of WBTC-USDT(in EVM create2): \n\t{evm_pair_address}")
```
得到以下结果, 证明我们的 python端计算 pair地址的方法是正确的:
```
pair code(python): 0x60806040526001600c5534801561001557600080fd5b50604051469080605261240d8239604080519182900360520182208282018252600a8352692ab734b9bbb0b8102b1960b11b6020938401528151808301835260018152603160f81b908401528151808401919091527fbfcc8ef98ffbf7b6c3fec7bf5185b566b9863e35a9d83acd49ad6824b5969738818301527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6606082015260808101949094523060a0808601919091528151808603909101815260c09094019052825192019190912060035550600580546001600160a01b03191633179055612308806101056000396000f3fe608060405234801561001057600080fd5b50600436106101a95760003560e01c80636a627842116100f9578063ba9a7a5611610097578063d21220a711610071578063d21220a714610534578063d505accf1461053c578063dd62ed3e1461058d578063fff6cae9146105bb576101a9565b8063ba9a7a56146104fe578063bc25cf7714610506578063c45a01551461052c576101a9565b80637ecebe00116100d35780637ecebe001461046557806389afcb441461048b57806395d89b41146104ca578063a9059cbb146104d2576101a9565b80636a6278421461041157806370a08231146104375780637464fc3d1461045d576101a9565b806323b872dd116101665780633644e515116101405780633644e515146103cb578063485cc955146103d35780635909c0d5146104015780635a3d549314610409576101a9565b806323b872dd1461036f57806330adf81f146103a5578063313ce567146103ad576101a9565b8063022c0d9f146101ae57806306fdde031461023c5780630902f1ac146102b9578063095ea7b3146102f15780630dfe16811461033157806318160ddd14610355575b600080fd5b61023a600480360360808110156101c457600080fd5b8135916020810135916001600160a01b0360408301351691908101906080810160608201356401000000008111156101fb57600080fd5b82018360208201111561020d57600080fd5b8035906020019184600183028401116401000000008311171561022f57600080fd5b5090925090506105c3565b005b610244610afe565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561027e578181015183820152602001610266565b50505050905090810190601f1680156102ab5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6102c1610b24565b604080516001600160701b03948516815292909316602083015263ffffffff168183015290519081900360600190f35b61031d6004803603604081101561030757600080fd5b506001600160a01b038135169060200135610b4e565b604080519115158252519081900360200190f35b610339610b65565b604080516001600160a01b039092168252519081900360200190f35b61035d610b74565b60408051918252519081900360200190f35b61031d6004803603606081101561038557600080fd5b506001600160a01b03813581169160208101359091169060400135610b7a565b61035d610c76565b6103b5610c9a565b6040805160ff9092168252519081900360200190f35b61035d610c9f565b61023a600480360360408110156103e957600080fd5b506001600160a01b0381358116916020013516610ca5565b61035d610d29565b61035d610d2f565b61035d6004803603602081101561042757600080fd5b50356001600160a01b0316610d35565b61035d6004803603602081101561044d57600080fd5b50356001600160a01b0316611035565b61035d611047565b61035d6004803603602081101561047b57600080fd5b50356001600160a01b031661104d565b6104b1600480360360208110156104a157600080fd5b50356001600160a01b031661105f565b6040805192835260208301919091528051918290030190f35b610244611405565b61031d600480360360408110156104e857600080fd5b506001600160a01b038135169060200135611427565b61035d611434565b61023a6004803603602081101561051c57600080fd5b50356001600160a01b031661143a565b6103396115a5565b6103396115b4565b61023a600480360360e081101561055257600080fd5b506001600160a01b03813581169160208101359091169060408101359060608101359060ff6080820135169060a08101359060c001356115c3565b61035d600480360360408110156105a357600080fd5b506001600160a01b03813581169160200135166117c5565b61023a6117e2565b600c5460011461060e576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55841515806106215750600084115b61065c5760405162461bcd60e51b81526004018080602001828103825260258152602001806121f56025913960400191505060405180910390fd5b600080610667610b24565b5091509150816001600160701b03168710801561068c5750806001600160701b031686105b6106c75760405162461bcd60e51b815260040180806020018281038252602181526020018061223e6021913960400191505060405180910390fd5b60065460075460009182916001600160a01b039182169190811690891682148015906107055750806001600160a01b0316896001600160a01b031614155b61074e576040805162461bcd60e51b8152602060048201526015602482015274556e697377617056323a20494e56414c49445f544f60581b604482015290519081900360640190fd5b8a1561075f5761075f828a8d611944565b891561077057610770818a8c611944565b861561082b57886001600160a01b03166310d1e85c338d8d8c8c6040518663ffffffff1660e01b815260040180866001600160a01b03166001600160a01b03168152602001858152602001848152602001806020018281038252848482818152602001925080828437600081840152601f19601f8201169050808301925050509650505050505050600060405180830381600087803b15801561081257600080fd5b505af1158015610826573d6000803e3d6000fd5b505050505b604080516370a0823160e01b815230600482015290516001600160a01b038416916370a08231916024808301926020929190829003018186803b15801561087157600080fd5b505afa158015610885573d6000803e3d6000fd5b505050506040513d602081101561089b57600080fd5b5051604080516370a0823160e01b815230600482015290519195506001600160a01b038316916370a0823191602480820192602092909190829003018186803b1580156108e757600080fd5b505afa1580156108fb573d6000803e3d6000fd5b505050506040513d602081101561091157600080fd5b5051925060009150506001600160701b0385168a90038311610934576000610943565b89856001600160701b03160383035b9050600089856001600160701b031603831161096057600061096f565b89856001600160701b03160383035b905060008211806109805750600081115b6109bb5760405162461bcd60e51b815260040180806020018281038252602481526020018061221a6024913960400191505060405180910390fd5b60006109ef6109d184600363ffffffff611ade16565b6109e3876103e863ffffffff611ade16565b9063ffffffff611b4116565b90506000610a076109d184600363ffffffff611ade16565b9050610a38620f4240610a2c6001600160701b038b8116908b1663ffffffff611ade16565b9063ffffffff611ade16565b610a48838363ffffffff611ade16565b1015610a8a576040805162461bcd60e51b815260206004820152600c60248201526b556e697377617056323a204b60a01b604482015290519081900360640190fd5b5050610a9884848888611b91565b60408051838152602081018390528082018d9052606081018c905290516001600160a01b038b169133917fd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d8229181900360800190a350506001600c55505050505050505050565b6040518060400160405280600a8152602001692ab734b9bbb0b8102b1960b11b81525081565b6008546001600160701b0380821692600160701b830490911691600160e01b900463ffffffff1690565b6000610b5b338484611d56565b5060015b92915050565b6006546001600160a01b031681565b60005481565b6001600160a01b0383166000908152600260209081526040808320338452909152812054821115610bdc5760405162461bcd60e51b81526004018080602001828103825260258152602001806122af6025913960400191505060405180910390fd5b6001600160a01b038416600090815260026020908152604080832033845290915290205460001914610c61576001600160a01b0384166000908152600260209081526040808320338452909152902054610c3c908363ffffffff611b4116565b6001600160a01b03851660009081526002602090815260408083203384529091529020555b610c6c848484611db8565b5060019392505050565b7f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c981565b601281565b60035481565b6005546001600160a01b03163314610cfb576040805162461bcd60e51b81526020600482015260146024820152732ab734b9bbb0b82b191d102327a92124a22222a760611b604482015290519081900360640190fd5b600680546001600160a01b039384166001600160a01b03199182161790915560078054929093169116179055565b60095481565b600a5481565b6000600c54600114610d82576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c81905580610d92610b24565b50600654604080516370a0823160e01b815230600482015290519395509193506000926001600160a01b03909116916370a08231916024808301926020929190829003018186803b158015610de657600080fd5b505afa158015610dfa573d6000803e3d6000fd5b505050506040513d6020811015610e1057600080fd5b5051600754604080516370a0823160e01b815230600482015290519293506000926001600160a01b03909216916370a0823191602480820192602092909190829003018186803b158015610e6357600080fd5b505afa158015610e77573d6000803e3d6000fd5b505050506040513d6020811015610e8d57600080fd5b505190506000610eac836001600160701b03871663ffffffff611b4116565b90506000610ec9836001600160701b03871663ffffffff611b4116565b90506000610ed78787611e72565b60005490915080610f1457610f006103e86109e3610efb878763ffffffff611ade16565b611fd0565b9850610f0f60006103e8612022565b610f63565b610f606001600160701b038916610f31868463ffffffff611ade16565b81610f3857fe5b046001600160701b038916610f53868563ffffffff611ade16565b81610f5a57fe5b046120b8565b98505b60008911610fa25760405162461bcd60e51b81526004018080602001828103825260288152602001806122876028913960400191505060405180910390fd5b610fac8a8a612022565b610fb886868a8a611b91565b8115610fe857600854610fe4906001600160701b0380821691600160701b90041663ffffffff611ade16565b600b555b6040805185815260208101859052815133927f4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f928290030190a250506001600c5550949695505050505050565b60016020526000908152604090205481565b600b5481565b60046020526000908152604090205481565b600080600c546001146110ad576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c819055806110bd610b24565b50600654600754604080516370a0823160e01b815230600482015290519496509294506001600160a01b039182169391169160009184916370a08231916024808301926020929190829003018186803b15801561111957600080fd5b505afa15801561112d573d6000803e3d6000fd5b505050506040513d602081101561114357600080fd5b5051604080516370a0823160e01b815230600482015290519192506000916001600160a01b038516916370a08231916024808301926020929190829003018186803b15801561119157600080fd5b505afa1580156111a5573d6000803e3d6000fd5b505050506040513d60208110156111bb57600080fd5b5051306000908152600160205260408120549192506111da8888611e72565b600054909150806111f1848763ffffffff611ade16565b816111f857fe5b049a508061120c848663ffffffff611ade16565b8161121357fe5b04995060008b118015611226575060008a115b6112615760405162461bcd60e51b815260040180806020018281038252602881526020018061225f6028913960400191505060405180910390fd5b61126b30846120d0565b611276878d8d611944565b611281868d8c611944565b604080516370a0823160e01b815230600482015290516001600160a01b038916916370a08231916024808301926020929190829003018186803b1580156112c757600080fd5b505afa1580156112db573d6000803e3d6000fd5b505050506040513d60208110156112f157600080fd5b5051604080516370a0823160e01b815230600482015290519196506001600160a01b038816916370a0823191602480820192602092909190829003018186803b15801561133d57600080fd5b505afa158015611351573d6000803e3d6000fd5b505050506040513d602081101561136757600080fd5b5051935061137785858b8b611b91565b81156113a7576008546113a3906001600160701b0380821691600160701b90041663ffffffff611ade16565b600b555b604080518c8152602081018c905281516001600160a01b038f169233927fdccd412f0b1252819cb1fd330b93224ca42612892bb3f4f789976e6d81936496929081900390910190a35050505050505050506001600c81905550915091565b604051806040016040528060068152602001652aa72496ab1960d11b81525081565b6000610b5b338484611db8565b6103e881565b600c54600114611485576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55600654600754600854604080516370a0823160e01b815230600482015290516001600160a01b039485169490931692611534928592879261152f926001600160701b03169185916370a0823191602480820192602092909190829003018186803b1580156114f757600080fd5b505afa15801561150b573d6000803e3d6000fd5b505050506040513d602081101561152157600080fd5b50519063ffffffff611b4116565b611944565b600854604080516370a0823160e01b8152306004820152905161159b928492879261152f92600160701b90046001600160701b0316916001600160a01b038616916370a0823191602480820192602092909190829003018186803b1580156114f757600080fd5b50506001600c5550565b6005546001600160a01b031681565b6007546001600160a01b031681565b4284101561160d576040805162461bcd60e51b8152602060048201526012602482015271155b9a5cddd85c158c8e881156141254915160721b604482015290519081900360640190fd5b6003546001600160a01b0380891660008181526004602090815260408083208054600180820190925582517f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c98186015280840196909652958d166060860152608085018c905260a085019590955260c08085018b90528151808603909101815260e08501825280519083012061190160f01b6101008601526101028501969096526101228085019690965280518085039096018652610142840180825286519683019690962095839052610162840180825286905260ff89166101828501526101a284018890526101c28401879052519193926101e280820193601f1981019281900390910190855afa158015611728573d6000803e3d6000fd5b5050604051601f1901519150506001600160a01b0381161580159061175e5750886001600160a01b0316816001600160a01b0316145b6117af576040805162461bcd60e51b815260206004820152601c60248201527f556e697377617056323a20494e56414c49445f5349474e415455524500000000604482015290519081900360640190fd5b6117ba898989611d56565b505050505050505050565b600260209081526000928352604080842090915290825290205481565b600c5460011461182d576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55600654604080516370a0823160e01b8152306004820152905161193d926001600160a01b0316916370a08231916024808301926020929190829003018186803b15801561187e57600080fd5b505afa158015611892573d6000803e3d6000fd5b505050506040513d60208110156118a857600080fd5b5051600754604080516370a0823160e01b815230600482015290516001600160a01b03909216916370a0823191602480820192602092909190829003018186803b1580156118f557600080fd5b505afa158015611909573d6000803e3d6000fd5b505050506040513d602081101561191f57600080fd5b50516008546001600160701b0380821691600160701b900416611b91565b6001600c55565b604080518082018252601981527f7472616e7366657228616464726573732c75696e74323536290000000000000060209182015281516001600160a01b0385811660248301526044808301869052845180840390910181526064909201845291810180516001600160e01b031663a9059cbb60e01b1781529251815160009460609489169392918291908083835b602083106119f15780518252601f1990920191602091820191016119d2565b6001836020036101000a0380198251168184511680821785525050505050509050019150506000604051808303816000865af19150503d8060008114611a53576040519150601f19603f3d011682016040523d82523d6000602084013e611a58565b606091505b5091509150818015611a86575080511580611a865750808060200190516020811015611a8357600080fd5b50515b611ad7576040805162461bcd60e51b815260206004820152601a60248201527f556e697377617056323a205452414e534645525f4641494c4544000000000000604482015290519081900360640190fd5b5050505050565b6000811580611af957505080820282828281611af657fe5b04145b610b5f576040805162461bcd60e51b815260206004820152601460248201527364732d6d6174682d6d756c2d6f766572666c6f7760601b604482015290519081900360640190fd5b80820382811115610b5f576040805162461bcd60e51b815260206004820152601560248201527464732d6d6174682d7375622d756e646572666c6f7760581b604482015290519081900360640190fd5b6001600160701b038411801590611baf57506001600160701b038311155b611bf6576040805162461bcd60e51b8152602060048201526013602482015272556e697377617056323a204f564552464c4f5760681b604482015290519081900360640190fd5b60085463ffffffff42811691600160e01b90048116820390811615801590611c2657506001600160701b03841615155b8015611c3a57506001600160701b03831615155b15611cab578063ffffffff16611c6885611c538661216e565b6001600160e01b03169063ffffffff61218016565b600980546001600160e01b03929092169290920201905563ffffffff8116611c9384611c538761216e565b600a80546001600160e01b0392909216929092020190555b600880546dffffffffffffffffffffffffffff19166001600160701b03888116919091176dffffffffffffffffffffffffffff60701b1916600160701b8883168102919091176001600160e01b0316600160e01b63ffffffff871602179283905560408051848416815291909304909116602082015281517f1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1929181900390910190a1505050505050565b6001600160a01b03808416600081815260026020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b038316600090815260016020526040902054611de1908263ffffffff611b4116565b6001600160a01b038085166000908152600160205260408082209390935590841681522054611e16908263ffffffff6121a516565b6001600160a01b0380841660008181526001602090815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b600080600560009054906101000a90046001600160a01b03166001600160a01b031663017e7e586040518163ffffffff1660e01b815260040160206040518083038186803b158015611ec357600080fd5b505afa158015611ed7573d6000803e3d6000fd5b505050506040513d6020811015611eed57600080fd5b5051600b546001600160a01b038216158015945091925090611fbc578015611fb7576000611f30610efb6001600160701b0388811690881663ffffffff611ade16565b90506000611f3d83611fd0565b905080821115611fb4576000611f6b611f5c848463ffffffff611b4116565b6000549063ffffffff611ade16565b90506000611f9083611f8486600563ffffffff611ade16565b9063ffffffff6121a516565b90506000818381611f9d57fe5b0490508015611fb057611fb08782612022565b5050505b50505b611fc8565b8015611fc8576000600b555b505092915050565b60006003821115612013575080600160028204015b8181101561200d57809150600281828581611ffc57fe5b04018161200557fe5b049050611fe5565b5061201d565b811561201d575060015b919050565b600054612035908263ffffffff6121a516565b60009081556001600160a01b038316815260016020526040902054612060908263ffffffff6121a516565b6001600160a01b03831660008181526001602090815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b60008183106120c757816120c9565b825b9392505050565b6001600160a01b0382166000908152600160205260409020546120f9908263ffffffff611b4116565b6001600160a01b03831660009081526001602052604081209190915554612126908263ffffffff611b4116565b60009081556040805183815290516001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef919081900360200190a35050565b6001600160701b0316600160701b0290565b60006001600160701b0382166001600160e01b0384168161219d57fe5b049392505050565b80820182811015610b5f576040805162461bcd60e51b815260206004820152601460248201527364732d6d6174682d6164642d6f766572666c6f7760601b604482015290519081900360640190fdfe556e697377617056323a20494e53554646494349454e545f4f55545055545f414d4f554e54556e697377617056323a20494e53554646494349454e545f494e5055545f414d4f554e54556e697377617056323a20494e53554646494349454e545f4c4951554944495459556e697377617056323a20494e53554646494349454e545f4c49515549444954595f4255524e4544556e697377617056323a20494e53554646494349454e545f4c49515549444954595f4d494e544544556e6973776170563245524332303a206e6f7420656e6f75676820616c6c6f77616e63652ea265627a7a72315820bf165e44001b5284076af3e56685540f3d6256d9ddeae5a8f22ed60b1397a80064736f6c63430005100032454950373132446f6d61696e28737472696e67206e616d652c737472696e672076657273696f6e2c75696e7432353620636861696e49642c6164647265737320766572696679696e67436f6e747261637429
pair code (evm): 0x60806040526001600c5534801561001557600080fd5b50604051469080605261240d8239604080519182900360520182208282018252600a8352692ab734b9bbb0b8102b1960b11b6020938401528151808301835260018152603160f81b908401528151808401919091527fbfcc8ef98ffbf7b6c3fec7bf5185b566b9863e35a9d83acd49ad6824b5969738818301527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6606082015260808101949094523060a0808601919091528151808603909101815260c09094019052825192019190912060035550600580546001600160a01b03191633179055612308806101056000396000f3fe608060405234801561001057600080fd5b50600436106101a95760003560e01c80636a627842116100f9578063ba9a7a5611610097578063d21220a711610071578063d21220a714610534578063d505accf1461053c578063dd62ed3e1461058d578063fff6cae9146105bb576101a9565b8063ba9a7a56146104fe578063bc25cf7714610506578063c45a01551461052c576101a9565b80637ecebe00116100d35780637ecebe001461046557806389afcb441461048b57806395d89b41146104ca578063a9059cbb146104d2576101a9565b80636a6278421461041157806370a08231146104375780637464fc3d1461045d576101a9565b806323b872dd116101665780633644e515116101405780633644e515146103cb578063485cc955146103d35780635909c0d5146104015780635a3d549314610409576101a9565b806323b872dd1461036f57806330adf81f146103a5578063313ce567146103ad576101a9565b8063022c0d9f146101ae57806306fdde031461023c5780630902f1ac146102b9578063095ea7b3146102f15780630dfe16811461033157806318160ddd14610355575b600080fd5b61023a600480360360808110156101c457600080fd5b8135916020810135916001600160a01b0360408301351691908101906080810160608201356401000000008111156101fb57600080fd5b82018360208201111561020d57600080fd5b8035906020019184600183028401116401000000008311171561022f57600080fd5b5090925090506105c3565b005b610244610afe565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561027e578181015183820152602001610266565b50505050905090810190601f1680156102ab5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6102c1610b24565b604080516001600160701b03948516815292909316602083015263ffffffff168183015290519081900360600190f35b61031d6004803603604081101561030757600080fd5b506001600160a01b038135169060200135610b4e565b604080519115158252519081900360200190f35b610339610b65565b604080516001600160a01b039092168252519081900360200190f35b61035d610b74565b60408051918252519081900360200190f35b61031d6004803603606081101561038557600080fd5b506001600160a01b03813581169160208101359091169060400135610b7a565b61035d610c76565b6103b5610c9a565b6040805160ff9092168252519081900360200190f35b61035d610c9f565b61023a600480360360408110156103e957600080fd5b506001600160a01b0381358116916020013516610ca5565b61035d610d29565b61035d610d2f565b61035d6004803603602081101561042757600080fd5b50356001600160a01b0316610d35565b61035d6004803603602081101561044d57600080fd5b50356001600160a01b0316611035565b61035d611047565b61035d6004803603602081101561047b57600080fd5b50356001600160a01b031661104d565b6104b1600480360360208110156104a157600080fd5b50356001600160a01b031661105f565b6040805192835260208301919091528051918290030190f35b610244611405565b61031d600480360360408110156104e857600080fd5b506001600160a01b038135169060200135611427565b61035d611434565b61023a6004803603602081101561051c57600080fd5b50356001600160a01b031661143a565b6103396115a5565b6103396115b4565b61023a600480360360e081101561055257600080fd5b506001600160a01b03813581169160208101359091169060408101359060608101359060ff6080820135169060a08101359060c001356115c3565b61035d600480360360408110156105a357600080fd5b506001600160a01b03813581169160200135166117c5565b61023a6117e2565b600c5460011461060e576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55841515806106215750600084115b61065c5760405162461bcd60e51b81526004018080602001828103825260258152602001806121f56025913960400191505060405180910390fd5b600080610667610b24565b5091509150816001600160701b03168710801561068c5750806001600160701b031686105b6106c75760405162461bcd60e51b815260040180806020018281038252602181526020018061223e6021913960400191505060405180910390fd5b60065460075460009182916001600160a01b039182169190811690891682148015906107055750806001600160a01b0316896001600160a01b031614155b61074e576040805162461bcd60e51b8152602060048201526015602482015274556e697377617056323a20494e56414c49445f544f60581b604482015290519081900360640190fd5b8a1561075f5761075f828a8d611944565b891561077057610770818a8c611944565b861561082b57886001600160a01b03166310d1e85c338d8d8c8c6040518663ffffffff1660e01b815260040180866001600160a01b03166001600160a01b03168152602001858152602001848152602001806020018281038252848482818152602001925080828437600081840152601f19601f8201169050808301925050509650505050505050600060405180830381600087803b15801561081257600080fd5b505af1158015610826573d6000803e3d6000fd5b505050505b604080516370a0823160e01b815230600482015290516001600160a01b038416916370a08231916024808301926020929190829003018186803b15801561087157600080fd5b505afa158015610885573d6000803e3d6000fd5b505050506040513d602081101561089b57600080fd5b5051604080516370a0823160e01b815230600482015290519195506001600160a01b038316916370a0823191602480820192602092909190829003018186803b1580156108e757600080fd5b505afa1580156108fb573d6000803e3d6000fd5b505050506040513d602081101561091157600080fd5b5051925060009150506001600160701b0385168a90038311610934576000610943565b89856001600160701b03160383035b9050600089856001600160701b031603831161096057600061096f565b89856001600160701b03160383035b905060008211806109805750600081115b6109bb5760405162461bcd60e51b815260040180806020018281038252602481526020018061221a6024913960400191505060405180910390fd5b60006109ef6109d184600363ffffffff611ade16565b6109e3876103e863ffffffff611ade16565b9063ffffffff611b4116565b90506000610a076109d184600363ffffffff611ade16565b9050610a38620f4240610a2c6001600160701b038b8116908b1663ffffffff611ade16565b9063ffffffff611ade16565b610a48838363ffffffff611ade16565b1015610a8a576040805162461bcd60e51b815260206004820152600c60248201526b556e697377617056323a204b60a01b604482015290519081900360640190fd5b5050610a9884848888611b91565b60408051838152602081018390528082018d9052606081018c905290516001600160a01b038b169133917fd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d8229181900360800190a350506001600c55505050505050505050565b6040518060400160405280600a8152602001692ab734b9bbb0b8102b1960b11b81525081565b6008546001600160701b0380821692600160701b830490911691600160e01b900463ffffffff1690565b6000610b5b338484611d56565b5060015b92915050565b6006546001600160a01b031681565b60005481565b6001600160a01b0383166000908152600260209081526040808320338452909152812054821115610bdc5760405162461bcd60e51b81526004018080602001828103825260258152602001806122af6025913960400191505060405180910390fd5b6001600160a01b038416600090815260026020908152604080832033845290915290205460001914610c61576001600160a01b0384166000908152600260209081526040808320338452909152902054610c3c908363ffffffff611b4116565b6001600160a01b03851660009081526002602090815260408083203384529091529020555b610c6c848484611db8565b5060019392505050565b7f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c981565b601281565b60035481565b6005546001600160a01b03163314610cfb576040805162461bcd60e51b81526020600482015260146024820152732ab734b9bbb0b82b191d102327a92124a22222a760611b604482015290519081900360640190fd5b600680546001600160a01b039384166001600160a01b03199182161790915560078054929093169116179055565b60095481565b600a5481565b6000600c54600114610d82576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c81905580610d92610b24565b50600654604080516370a0823160e01b815230600482015290519395509193506000926001600160a01b03909116916370a08231916024808301926020929190829003018186803b158015610de657600080fd5b505afa158015610dfa573d6000803e3d6000fd5b505050506040513d6020811015610e1057600080fd5b5051600754604080516370a0823160e01b815230600482015290519293506000926001600160a01b03909216916370a0823191602480820192602092909190829003018186803b158015610e6357600080fd5b505afa158015610e77573d6000803e3d6000fd5b505050506040513d6020811015610e8d57600080fd5b505190506000610eac836001600160701b03871663ffffffff611b4116565b90506000610ec9836001600160701b03871663ffffffff611b4116565b90506000610ed78787611e72565b60005490915080610f1457610f006103e86109e3610efb878763ffffffff611ade16565b611fd0565b9850610f0f60006103e8612022565b610f63565b610f606001600160701b038916610f31868463ffffffff611ade16565b81610f3857fe5b046001600160701b038916610f53868563ffffffff611ade16565b81610f5a57fe5b046120b8565b98505b60008911610fa25760405162461bcd60e51b81526004018080602001828103825260288152602001806122876028913960400191505060405180910390fd5b610fac8a8a612022565b610fb886868a8a611b91565b8115610fe857600854610fe4906001600160701b0380821691600160701b90041663ffffffff611ade16565b600b555b6040805185815260208101859052815133927f4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f928290030190a250506001600c5550949695505050505050565b60016020526000908152604090205481565b600b5481565b60046020526000908152604090205481565b600080600c546001146110ad576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c819055806110bd610b24565b50600654600754604080516370a0823160e01b815230600482015290519496509294506001600160a01b039182169391169160009184916370a08231916024808301926020929190829003018186803b15801561111957600080fd5b505afa15801561112d573d6000803e3d6000fd5b505050506040513d602081101561114357600080fd5b5051604080516370a0823160e01b815230600482015290519192506000916001600160a01b038516916370a08231916024808301926020929190829003018186803b15801561119157600080fd5b505afa1580156111a5573d6000803e3d6000fd5b505050506040513d60208110156111bb57600080fd5b5051306000908152600160205260408120549192506111da8888611e72565b600054909150806111f1848763ffffffff611ade16565b816111f857fe5b049a508061120c848663ffffffff611ade16565b8161121357fe5b04995060008b118015611226575060008a115b6112615760405162461bcd60e51b815260040180806020018281038252602881526020018061225f6028913960400191505060405180910390fd5b61126b30846120d0565b611276878d8d611944565b611281868d8c611944565b604080516370a0823160e01b815230600482015290516001600160a01b038916916370a08231916024808301926020929190829003018186803b1580156112c757600080fd5b505afa1580156112db573d6000803e3d6000fd5b505050506040513d60208110156112f157600080fd5b5051604080516370a0823160e01b815230600482015290519196506001600160a01b038816916370a0823191602480820192602092909190829003018186803b15801561133d57600080fd5b505afa158015611351573d6000803e3d6000fd5b505050506040513d602081101561136757600080fd5b5051935061137785858b8b611b91565b81156113a7576008546113a3906001600160701b0380821691600160701b90041663ffffffff611ade16565b600b555b604080518c8152602081018c905281516001600160a01b038f169233927fdccd412f0b1252819cb1fd330b93224ca42612892bb3f4f789976e6d81936496929081900390910190a35050505050505050506001600c81905550915091565b604051806040016040528060068152602001652aa72496ab1960d11b81525081565b6000610b5b338484611db8565b6103e881565b600c54600114611485576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55600654600754600854604080516370a0823160e01b815230600482015290516001600160a01b039485169490931692611534928592879261152f926001600160701b03169185916370a0823191602480820192602092909190829003018186803b1580156114f757600080fd5b505afa15801561150b573d6000803e3d6000fd5b505050506040513d602081101561152157600080fd5b50519063ffffffff611b4116565b611944565b600854604080516370a0823160e01b8152306004820152905161159b928492879261152f92600160701b90046001600160701b0316916001600160a01b038616916370a0823191602480820192602092909190829003018186803b1580156114f757600080fd5b50506001600c5550565b6005546001600160a01b031681565b6007546001600160a01b031681565b4284101561160d576040805162461bcd60e51b8152602060048201526012602482015271155b9a5cddd85c158c8e881156141254915160721b604482015290519081900360640190fd5b6003546001600160a01b0380891660008181526004602090815260408083208054600180820190925582517f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c98186015280840196909652958d166060860152608085018c905260a085019590955260c08085018b90528151808603909101815260e08501825280519083012061190160f01b6101008601526101028501969096526101228085019690965280518085039096018652610142840180825286519683019690962095839052610162840180825286905260ff89166101828501526101a284018890526101c28401879052519193926101e280820193601f1981019281900390910190855afa158015611728573d6000803e3d6000fd5b5050604051601f1901519150506001600160a01b0381161580159061175e5750886001600160a01b0316816001600160a01b0316145b6117af576040805162461bcd60e51b815260206004820152601c60248201527f556e697377617056323a20494e56414c49445f5349474e415455524500000000604482015290519081900360640190fd5b6117ba898989611d56565b505050505050505050565b600260209081526000928352604080842090915290825290205481565b600c5460011461182d576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55600654604080516370a0823160e01b8152306004820152905161193d926001600160a01b0316916370a08231916024808301926020929190829003018186803b15801561187e57600080fd5b505afa158015611892573d6000803e3d6000fd5b505050506040513d60208110156118a857600080fd5b5051600754604080516370a0823160e01b815230600482015290516001600160a01b03909216916370a0823191602480820192602092909190829003018186803b1580156118f557600080fd5b505afa158015611909573d6000803e3d6000fd5b505050506040513d602081101561191f57600080fd5b50516008546001600160701b0380821691600160701b900416611b91565b6001600c55565b604080518082018252601981527f7472616e7366657228616464726573732c75696e74323536290000000000000060209182015281516001600160a01b0385811660248301526044808301869052845180840390910181526064909201845291810180516001600160e01b031663a9059cbb60e01b1781529251815160009460609489169392918291908083835b602083106119f15780518252601f1990920191602091820191016119d2565b6001836020036101000a0380198251168184511680821785525050505050509050019150506000604051808303816000865af19150503d8060008114611a53576040519150601f19603f3d011682016040523d82523d6000602084013e611a58565b606091505b5091509150818015611a86575080511580611a865750808060200190516020811015611a8357600080fd5b50515b611ad7576040805162461bcd60e51b815260206004820152601a60248201527f556e697377617056323a205452414e534645525f4641494c4544000000000000604482015290519081900360640190fd5b5050505050565b6000811580611af957505080820282828281611af657fe5b04145b610b5f576040805162461bcd60e51b815260206004820152601460248201527364732d6d6174682d6d756c2d6f766572666c6f7760601b604482015290519081900360640190fd5b80820382811115610b5f576040805162461bcd60e51b815260206004820152601560248201527464732d6d6174682d7375622d756e646572666c6f7760581b604482015290519081900360640190fd5b6001600160701b038411801590611baf57506001600160701b038311155b611bf6576040805162461bcd60e51b8152602060048201526013602482015272556e697377617056323a204f564552464c4f5760681b604482015290519081900360640190fd5b60085463ffffffff42811691600160e01b90048116820390811615801590611c2657506001600160701b03841615155b8015611c3a57506001600160701b03831615155b15611cab578063ffffffff16611c6885611c538661216e565b6001600160e01b03169063ffffffff61218016565b600980546001600160e01b03929092169290920201905563ffffffff8116611c9384611c538761216e565b600a80546001600160e01b0392909216929092020190555b600880546dffffffffffffffffffffffffffff19166001600160701b03888116919091176dffffffffffffffffffffffffffff60701b1916600160701b8883168102919091176001600160e01b0316600160e01b63ffffffff871602179283905560408051848416815291909304909116602082015281517f1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1929181900390910190a1505050505050565b6001600160a01b03808416600081815260026020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b038316600090815260016020526040902054611de1908263ffffffff611b4116565b6001600160a01b038085166000908152600160205260408082209390935590841681522054611e16908263ffffffff6121a516565b6001600160a01b0380841660008181526001602090815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b600080600560009054906101000a90046001600160a01b03166001600160a01b031663017e7e586040518163ffffffff1660e01b815260040160206040518083038186803b158015611ec357600080fd5b505afa158015611ed7573d6000803e3d6000fd5b505050506040513d6020811015611eed57600080fd5b5051600b546001600160a01b038216158015945091925090611fbc578015611fb7576000611f30610efb6001600160701b0388811690881663ffffffff611ade16565b90506000611f3d83611fd0565b905080821115611fb4576000611f6b611f5c848463ffffffff611b4116565b6000549063ffffffff611ade16565b90506000611f9083611f8486600563ffffffff611ade16565b9063ffffffff6121a516565b90506000818381611f9d57fe5b0490508015611fb057611fb08782612022565b5050505b50505b611fc8565b8015611fc8576000600b555b505092915050565b60006003821115612013575080600160028204015b8181101561200d57809150600281828581611ffc57fe5b04018161200557fe5b049050611fe5565b5061201d565b811561201d575060015b919050565b600054612035908263ffffffff6121a516565b60009081556001600160a01b038316815260016020526040902054612060908263ffffffff6121a516565b6001600160a01b03831660008181526001602090815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b60008183106120c757816120c9565b825b9392505050565b6001600160a01b0382166000908152600160205260409020546120f9908263ffffffff611b4116565b6001600160a01b03831660009081526001602052604081209190915554612126908263ffffffff611b4116565b60009081556040805183815290516001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef919081900360200190a35050565b6001600160701b0316600160701b0290565b60006001600160701b0382166001600160e01b0384168161219d57fe5b049392505050565b80820182811015610b5f576040805162461bcd60e51b815260206004820152601460248201527364732d6d6174682d6164642d6f766572666c6f7760601b604482015290519081900360640190fdfe556e697377617056323a20494e53554646494349454e545f4f55545055545f414d4f554e54556e697377617056323a20494e53554646494349454e545f494e5055545f414d4f554e54556e697377617056323a20494e53554646494349454e545f4c4951554944495459556e697377617056323a20494e53554646494349454e545f4c49515549444954595f4255524e4544556e697377617056323a20494e53554646494349454e545f4c49515549444954595f4d494e544544556e6973776170563245524332303a206e6f7420656e6f75676820616c6c6f77616e63652ea265627a7a72315820bf165e44001b5284076af3e56685540f3d6256d9ddeae5a8f22ed60b1397a80064736f6c63430005100032454950373132446f6d61696e28737472696e67206e616d652c737472696e672076657273696f6e2c75696e7432353620636861696e49642c6164647265737320766572696679696e67436f6e747261637429
pair code hash(python):
0xd0b942f04f5999da18c6933b57c7a558fc3b364ba0e233f5828591ee21b66669
pair code hash(evm):
0xd0b942f04f5999da18c6933b57c7a558fc3b364ba0e233f5828591ee21b66669
pair address of WBTC-USDT(calculated from python):
0x841f5e40d7573fd7fbe5a8a002a10759ad5e3bf3
pair address of WBTC-USDT(from EVM create2):
0x841F5E40d7573FD7FbE5A8A002a10759aD5E3BF3
```
ok, 第一部分暂时分享到这里, 接下来我们继续一起学习吧
hello everyone, 我是tony, 今天开始为大家分享我最近学习Uniswap的小小心得, 希望各位看官多多提意见, 大家一起进步.
Uniswap V2 代码地址:
核心代码: 包括 Factory, Pair, WETH 3个合约
https://github.com/Uniswap/v2-core
外围代码: Router合约
https://github.com/Uniswap/v2-periphery
概览
Uniswap core 有3个合约: Factory, Pair, WETH
Uniswap periphery 有1个合约: Router
Factory合约 (UniswapV2Factory.sol):
负责创建交易对, 保存所有交易对的地址 .
Pair合约 (UniswapV2Pair.sol) :
保存单个交易对的资金池信息, 包括每个交易对中两个ERC20 代币的地址, 以及各自资金余额(reserve).
处理 mint, burn 和 交易(swap) 操作. Pair合约是Uniswap代码的核心
UniswapV2Pair 本身也是ERC20, 当添加流动性时会mint新的pair代币(pair token, or LP token), 删除流动性时会销毁一定数量的代币, 确保 pair token的 totalSupply 始终等于池子中的两个交易对代币余额的几何平均值: totalSupply of pair token === sqrt(reserve1 * reserve2)
WETH合约:
这个合约是一个特殊的ERC20, 它的目的是方便用户直接用 ETH和ERC20进行交易,
你可以把 WETH 看作是一个全额担保的债券: 这个合约里面存储的 ETH 总是等于它发行的代币 WETH, (除非有人直接使用 WETH_address.transfer 向它发送了ETH ).
Pair合约的交易对总是ERC20代币, 不能进行 ETH 和 ERC20 之间的交易, 所以 WETH就被创造出来, 它有两个方法, deposit() 和 withdraw(), 前者是存入 ETH获得 WETH, 后者是 销毁 WETH获得ETH. 但是这两个方法通常是由 Router来调用的, 例如 Router中的 swapExactETHForTokens 函数:
function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
external
virtual
override
payable
ensure(deadline)
returns (uint[] memory amounts)
{
require(path[0] == WETH, 'UniswapV2Router: INVALID_PATH');
amounts = UniswapV2Library.getAmountsOut(factory, msg.value, path);
require(amounts[amounts.length - 1] >= amountOutMin, 'UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT');
IWETH(WETH).deposit{value: amounts[0]}();
assert(IWETH(WETH).transfer(UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]));
_swap(amounts, path, to);
}
上面的代码中, 用户需要用 ETH 换成 ERC20代币, 那么只能使用 WETH-ERC20 交易对, 用户将ETH发送给 Router, Router获得 ETH后用它换出 WETH ( 调用WETH.deposit() ), 再将 WETH 发送给 Pair, 换出目标 ERC20代币发送给参数 to 中指定的接收账户.
Router合约: (UniswapV2Router02.sol)
这个合约包含了几乎所有提供给应用层使用的函数 (除了 Factory 的 getPair和 createPair), 包括
<添加,撤销流动性> 和 <交易>, 它和 Pair的区别如下:
Pair中没有添加, 撤销流动性操作, 只有 mint 和 burn.
Pair中的 swap操作是单个交易对的操作, 而Router的 swap 是可以给定路径, 进行一系列交易后得到最终目标代币
token1 -> token2 ... -> token_target
在此过程中, Router会依次调用 (token_i, token_i+1) 所对应的交易对的 swap函数, 并将换出的代币发送到
(token_i+1, token_i+2)交易对合约地址, 再次执行swap, 发送... 直到换出最后的 token_target
在后面的介绍中, 我们会阅读源码, 研究 swap的流程细节.
Pair的创建 和 create2 的地址计算:
交易对的创建是在 Factory中完成, UniswapV2Factory.ceatePair 函数定义如下:
function createPair(address tokenA, address tokenB) external returns (address pair) {
require(tokenA != tokenB, 'UniswapV2: IDENTICAL_ADDRESSES');
(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), 'UniswapV2: ZERO_ADDRESS');
require(getPair[token0][token1] == address(0), 'UniswapV2: PAIR_EXISTS'); // single check is sufficient
bytes memory bytecode = type(UniswapV2Pair).creationCode;
bytes32 salt = keccak256(abi.encodePacked(token0, token1));
assembly {
pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
}
IUniswapV2Pair(pair).initialize(token0, token1);
getPair[token0][token1] = pair;
getPair[token1][token0] = pair; // populate mapping in the reverse direction
allPairs.push(pair);
emit PairCreated(token0, token1, pair, allPairs.length);
}
这里Factory使用了 create2操作码部署新的 Pair 合约, 而不是 pair = new UniswapV2Pair(tokenA, tokenB):
assembly {
pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
}
这样做的好处是: 它可以得到一个确定的pair地址, 使得 Router中就可以通过 tokenA, tokenB 计算出pair地址, 不再需要执行一次 Factory.getPair(tokenA, tokenB) 的跨合约调用, create2生成地址的规则是:
keccak256(abi.encodePacked(
hex'ff',
deployer_address, // 由Factory创建, 这里就是 factory合约地址
salt, // salt 是创建者自定义的数据, 避免重复, 这里的
// salt = keccak256(abi.encodePacked(token0, token1)), 且 token0 < token1
keccak256(type(Contract_to_Deploy).creationCode) // 此处 Contract_to_Deploy = UniswapV2Pair
))))
参考 periphery的 libraries/UniswapV2Library.sol
function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
(address token0, address token1) = sortTokens(tokenA, tokenB);
pair = address(uint(keccak256(abi.encodePacked(
hex'ff',
factory,
keccak256(abi.encodePacked(token0, token1)),
hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
))));
}
在这里, Router为了节省gas, 使用 pairFor获取 tokenA 和 tokenB 的交易对地址, 而且最后的字节码哈希也是预先计算出来, 硬编码到代码里面.
如果我们自己修改了 Pair的源代码合约, 重新部署后, Router就无法找到这个Pair地址了, 解决办法要么直接返回 factory.getPair(tokenA, tokenB), 要么就把以前部署的 Pair全部舍弃, 重新部署Factory和所有Pair, 再重新部署Router.
下面的 python示例代码用于计算 pair的hash, 我们可以在重新部署 Pair后调用这个函数, 再把hash复制到 UniswapV2Library.pairFor() 进行替换:
同时修改 UniswapV2Factory.sol , 增加两个函数用于测试:
contract UniswapV2Factory is IUniswapV2Factory {
...
/*
* 返回 Pair的字节码
*/
function pair_code() public view returns(bytes memory){
return type(UniswapV2Pair).creationCode;
}
/*
* 返回 Pair的字节码哈希
*/
function pair_codehash() public view returns(bytes32 ){
return keccak256( type(UniswapV2Pair).creationCode );
}
...
}
以下是python代码, 用于计算pair的 code_hash, 并且模拟 create2计算交易对地址, 如果与 factory.getPair得到的地址相同, 证明我们的 code_hash计算正确, 可以拷贝到 UniswapV2Library.sol 的 pairFor函数
###########################################################################################
#
# 重新编译并部署 pair 和 Factory 后, 调用此函数计算出hash, 放到 peripery/libraries/UniswapV2Library.sol 的
# 'pairFor' 函数, 替换原来的hash,
# 然后重新编译并部署Router
#
###########################################################################################
def calc_pair_address():
# 执行本函数前, 确保已经重新编译并部署 Factory 和 Pair, 同时部署测试代币 WBTC, USDT及交易对
# 执行完本函数后, 需要修改 Router源代码, 重新编译部署Router
abi, bytecode = _helper.get_contract_info(r"core/artifacts/UniswapV2Pair.json")
WBTC, USDT = _contracts["WBTC"], _contracts["USDT"]
factory = _contracts["Factory"]
pair_codehash = Web3.keccak(bytes.fromhex(bytecode))
addr1, addr2 = sorted((WBTC.address, USDT.address))
salt = Web3.solidityKeccak(['address' , 'address'], [addr1, addr2])
pair_address = Web3.solidityKeccak(['uint8', 'address' , 'bytes', 'bytes'],
[0xff,
factory.address,
salt,
pair_codehash])
print(f"pair code(python): 0x{bytecode}", )
evm_code = factory.functions.pair_code().call()
print(f"pair code (evm): 0x{evm_code.hex()}")
print(f"code hash(python): \n\t{pair_codehash.hex()}", )
evm_codehash = factory.functions.pair_codehash().call()
print(f"code hash(evm): \n\t0x{evm_codehash.hex()}")
print(f"pair address of WBTC-USDT(calculated from python): \n\t{pair_address.hex()}")
evm_pair_address = factory.functions.getPair(WBTC.address, USDT.address).call()
print(f"pair address of WBTC-USDT(in EVM create2): \n\t{evm_pair_address}")
得到以下结果, 证明我们的 python端计算 pair地址的方法是正确的:
pair code(python): 0x60806040526001600c5534801561001557600080fd5b50604051469080605261240d8239604080519182900360520182208282018252600a8352692ab734b9bbb0b8102b1960b11b6020938401528151808301835260018152603160f81b908401528151808401919091527fbfcc8ef98ffbf7b6c3fec7bf5185b566b9863e35a9d83acd49ad6824b5969738818301527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6606082015260808101949094523060a0808601919091528151808603909101815260c09094019052825192019190912060035550600580546001600160a01b03191633179055612308806101056000396000f3fe608060405234801561001057600080fd5b50600436106101a95760003560e01c80636a627842116100f9578063ba9a7a5611610097578063d21220a711610071578063d21220a714610534578063d505accf1461053c578063dd62ed3e1461058d578063fff6cae9146105bb576101a9565b8063ba9a7a56146104fe578063bc25cf7714610506578063c45a01551461052c576101a9565b80637ecebe00116100d35780637ecebe001461046557806389afcb441461048b57806395d89b41146104ca578063a9059cbb146104d2576101a9565b80636a6278421461041157806370a08231146104375780637464fc3d1461045d576101a9565b806323b872dd116101665780633644e515116101405780633644e515146103cb578063485cc955146103d35780635909c0d5146104015780635a3d549314610409576101a9565b806323b872dd1461036f57806330adf81f146103a5578063313ce567146103ad576101a9565b8063022c0d9f146101ae57806306fdde031461023c5780630902f1ac146102b9578063095ea7b3146102f15780630dfe16811461033157806318160ddd14610355575b600080fd5b61023a600480360360808110156101c457600080fd5b8135916020810135916001600160a01b0360408301351691908101906080810160608201356401000000008111156101fb57600080fd5b82018360208201111561020d57600080fd5b8035906020019184600183028401116401000000008311171561022f57600080fd5b5090925090506105c3565b005b610244610afe565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561027e578181015183820152602001610266565b50505050905090810190601f1680156102ab5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6102c1610b24565b604080516001600160701b03948516815292909316602083015263ffffffff168183015290519081900360600190f35b61031d6004803603604081101561030757600080fd5b506001600160a01b038135169060200135610b4e565b604080519115158252519081900360200190f35b610339610b65565b604080516001600160a01b039092168252519081900360200190f35b61035d610b74565b60408051918252519081900360200190f35b61031d6004803603606081101561038557600080fd5b506001600160a01b03813581169160208101359091169060400135610b7a565b61035d610c76565b6103b5610c9a565b6040805160ff9092168252519081900360200190f35b61035d610c9f565b61023a600480360360408110156103e957600080fd5b506001600160a01b0381358116916020013516610ca5565b61035d610d29565b61035d610d2f565b61035d6004803603602081101561042757600080fd5b50356001600160a01b0316610d35565b61035d6004803603602081101561044d57600080fd5b50356001600160a01b0316611035565b61035d611047565b61035d6004803603602081101561047b57600080fd5b50356001600160a01b031661104d565b6104b1600480360360208110156104a157600080fd5b50356001600160a01b031661105f565b6040805192835260208301919091528051918290030190f35b610244611405565b61031d600480360360408110156104e857600080fd5b506001600160a01b038135169060200135611427565b61035d611434565b61023a6004803603602081101561051c57600080fd5b50356001600160a01b031661143a565b6103396115a5565b6103396115b4565b61023a600480360360e081101561055257600080fd5b506001600160a01b03813581169160208101359091169060408101359060608101359060ff6080820135169060a08101359060c001356115c3565b61035d600480360360408110156105a357600080fd5b506001600160a01b03813581169160200135166117c5565b61023a6117e2565b600c5460011461060e576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55841515806106215750600084115b61065c5760405162461bcd60e51b81526004018080602001828103825260258152602001806121f56025913960400191505060405180910390fd5b600080610667610b24565b5091509150816001600160701b03168710801561068c5750806001600160701b031686105b6106c75760405162461bcd60e51b815260040180806020018281038252602181526020018061223e6021913960400191505060405180910390fd5b60065460075460009182916001600160a01b039182169190811690891682148015906107055750806001600160a01b0316896001600160a01b031614155b61074e576040805162461bcd60e51b8152602060048201526015602482015274556e697377617056323a20494e56414c49445f544f60581b604482015290519081900360640190fd5b8a1561075f5761075f828a8d611944565b891561077057610770818a8c611944565b861561082b57886001600160a01b03166310d1e85c338d8d8c8c6040518663ffffffff1660e01b815260040180866001600160a01b03166001600160a01b03168152602001858152602001848152602001806020018281038252848482818152602001925080828437600081840152601f19601f8201169050808301925050509650505050505050600060405180830381600087803b15801561081257600080fd5b505af1158015610826573d6000803e3d6000fd5b505050505b604080516370a0823160e01b815230600482015290516001600160a01b038416916370a08231916024808301926020929190829003018186803b15801561087157600080fd5b505afa158015610885573d6000803e3d6000fd5b505050506040513d602081101561089b57600080fd5b5051604080516370a0823160e01b815230600482015290519195506001600160a01b038316916370a0823191602480820192602092909190829003018186803b1580156108e757600080fd5b505afa1580156108fb573d6000803e3d6000fd5b505050506040513d602081101561091157600080fd5b5051925060009150506001600160701b0385168a90038311610934576000610943565b89856001600160701b03160383035b9050600089856001600160701b031603831161096057600061096f565b89856001600160701b03160383035b905060008211806109805750600081115b6109bb5760405162461bcd60e51b815260040180806020018281038252602481526020018061221a6024913960400191505060405180910390fd5b60006109ef6109d184600363ffffffff611ade16565b6109e3876103e863ffffffff611ade16565b9063ffffffff611b4116565b90506000610a076109d184600363ffffffff611ade16565b9050610a38620f4240610a2c6001600160701b038b8116908b1663ffffffff611ade16565b9063ffffffff611ade16565b610a48838363ffffffff611ade16565b1015610a8a576040805162461bcd60e51b815260206004820152600c60248201526b556e697377617056323a204b60a01b604482015290519081900360640190fd5b5050610a9884848888611b91565b60408051838152602081018390528082018d9052606081018c905290516001600160a01b038b169133917fd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d8229181900360800190a350506001600c55505050505050505050565b6040518060400160405280600a8152602001692ab734b9bbb0b8102b1960b11b81525081565b6008546001600160701b0380821692600160701b830490911691600160e01b900463ffffffff1690565b6000610b5b338484611d56565b5060015b92915050565b6006546001600160a01b031681565b60005481565b6001600160a01b0383166000908152600260209081526040808320338452909152812054821115610bdc5760405162461bcd60e51b81526004018080602001828103825260258152602001806122af6025913960400191505060405180910390fd5b6001600160a01b038416600090815260026020908152604080832033845290915290205460001914610c61576001600160a01b0384166000908152600260209081526040808320338452909152902054610c3c908363ffffffff611b4116565b6001600160a01b03851660009081526002602090815260408083203384529091529020555b610c6c848484611db8565b5060019392505050565b7f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c981565b601281565b60035481565b6005546001600160a01b03163314610cfb576040805162461bcd60e51b81526020600482015260146024820152732ab734b9bbb0b82b191d102327a92124a22222a760611b604482015290519081900360640190fd5b600680546001600160a01b039384166001600160a01b03199182161790915560078054929093169116179055565b60095481565b600a5481565b6000600c54600114610d82576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c81905580610d92610b24565b50600654604080516370a0823160e01b815230600482015290519395509193506000926001600160a01b03909116916370a08231916024808301926020929190829003018186803b158015610de657600080fd5b505afa158015610dfa573d6000803e3d6000fd5b505050506040513d6020811015610e1057600080fd5b5051600754604080516370a0823160e01b815230600482015290519293506000926001600160a01b03909216916370a0823191602480820192602092909190829003018186803b158015610e6357600080fd5b505afa158015610e77573d6000803e3d6000fd5b505050506040513d6020811015610e8d57600080fd5b505190506000610eac836001600160701b03871663ffffffff611b4116565b90506000610ec9836001600160701b03871663ffffffff611b4116565b90506000610ed78787611e72565b60005490915080610f1457610f006103e86109e3610efb878763ffffffff611ade16565b611fd0565b9850610f0f60006103e8612022565b610f63565b610f606001600160701b038916610f31868463ffffffff611ade16565b81610f3857fe5b046001600160701b038916610f53868563ffffffff611ade16565b81610f5a57fe5b046120b8565b98505b60008911610fa25760405162461bcd60e51b81526004018080602001828103825260288152602001806122876028913960400191505060405180910390fd5b610fac8a8a612022565b610fb886868a8a611b91565b8115610fe857600854610fe4906001600160701b0380821691600160701b90041663ffffffff611ade16565b600b555b6040805185815260208101859052815133927f4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f928290030190a250506001600c5550949695505050505050565b60016020526000908152604090205481565b600b5481565b60046020526000908152604090205481565b600080600c546001146110ad576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c819055806110bd610b24565b50600654600754604080516370a0823160e01b815230600482015290519496509294506001600160a01b039182169391169160009184916370a08231916024808301926020929190829003018186803b15801561111957600080fd5b505afa15801561112d573d6000803e3d6000fd5b505050506040513d602081101561114357600080fd5b5051604080516370a0823160e01b815230600482015290519192506000916001600160a01b038516916370a08231916024808301926020929190829003018186803b15801561119157600080fd5b505afa1580156111a5573d6000803e3d6000fd5b505050506040513d60208110156111bb57600080fd5b5051306000908152600160205260408120549192506111da8888611e72565b600054909150806111f1848763ffffffff611ade16565b816111f857fe5b049a508061120c848663ffffffff611ade16565b8161121357fe5b04995060008b118015611226575060008a115b6112615760405162461bcd60e51b815260040180806020018281038252602881526020018061225f6028913960400191505060405180910390fd5b61126b30846120d0565b611276878d8d611944565b611281868d8c611944565b604080516370a0823160e01b815230600482015290516001600160a01b038916916370a08231916024808301926020929190829003018186803b1580156112c757600080fd5b505afa1580156112db573d6000803e3d6000fd5b505050506040513d60208110156112f157600080fd5b5051604080516370a0823160e01b815230600482015290519196506001600160a01b038816916370a0823191602480820192602092909190829003018186803b15801561133d57600080fd5b505afa158015611351573d6000803e3d6000fd5b505050506040513d602081101561136757600080fd5b5051935061137785858b8b611b91565b81156113a7576008546113a3906001600160701b0380821691600160701b90041663ffffffff611ade16565b600b555b604080518c8152602081018c905281516001600160a01b038f169233927fdccd412f0b1252819cb1fd330b93224ca42612892bb3f4f789976e6d81936496929081900390910190a35050505050505050506001600c81905550915091565b604051806040016040528060068152602001652aa72496ab1960d11b81525081565b6000610b5b338484611db8565b6103e881565b600c54600114611485576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55600654600754600854604080516370a0823160e01b815230600482015290516001600160a01b039485169490931692611534928592879261152f926001600160701b03169185916370a0823191602480820192602092909190829003018186803b1580156114f757600080fd5b505afa15801561150b573d6000803e3d6000fd5b505050506040513d602081101561152157600080fd5b50519063ffffffff611b4116565b611944565b600854604080516370a0823160e01b8152306004820152905161159b928492879261152f92600160701b90046001600160701b0316916001600160a01b038616916370a0823191602480820192602092909190829003018186803b1580156114f757600080fd5b50506001600c5550565b6005546001600160a01b031681565b6007546001600160a01b031681565b4284101561160d576040805162461bcd60e51b8152602060048201526012602482015271155b9a5cddd85c158c8e881156141254915160721b604482015290519081900360640190fd5b6003546001600160a01b0380891660008181526004602090815260408083208054600180820190925582517f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c98186015280840196909652958d166060860152608085018c905260a085019590955260c08085018b90528151808603909101815260e08501825280519083012061190160f01b6101008601526101028501969096526101228085019690965280518085039096018652610142840180825286519683019690962095839052610162840180825286905260ff89166101828501526101a284018890526101c28401879052519193926101e280820193601f1981019281900390910190855afa158015611728573d6000803e3d6000fd5b5050604051601f1901519150506001600160a01b0381161580159061175e5750886001600160a01b0316816001600160a01b0316145b6117af576040805162461bcd60e51b815260206004820152601c60248201527f556e697377617056323a20494e56414c49445f5349474e415455524500000000604482015290519081900360640190fd5b6117ba898989611d56565b505050505050505050565b600260209081526000928352604080842090915290825290205481565b600c5460011461182d576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55600654604080516370a0823160e01b8152306004820152905161193d926001600160a01b0316916370a08231916024808301926020929190829003018186803b15801561187e57600080fd5b505afa158015611892573d6000803e3d6000fd5b505050506040513d60208110156118a857600080fd5b5051600754604080516370a0823160e01b815230600482015290516001600160a01b03909216916370a0823191602480820192602092909190829003018186803b1580156118f557600080fd5b505afa158015611909573d6000803e3d6000fd5b505050506040513d602081101561191f57600080fd5b50516008546001600160701b0380821691600160701b900416611b91565b6001600c55565b604080518082018252601981527f7472616e7366657228616464726573732c75696e74323536290000000000000060209182015281516001600160a01b0385811660248301526044808301869052845180840390910181526064909201845291810180516001600160e01b031663a9059cbb60e01b1781529251815160009460609489169392918291908083835b602083106119f15780518252601f1990920191602091820191016119d2565b6001836020036101000a0380198251168184511680821785525050505050509050019150506000604051808303816000865af19150503d8060008114611a53576040519150601f19603f3d011682016040523d82523d6000602084013e611a58565b606091505b5091509150818015611a86575080511580611a865750808060200190516020811015611a8357600080fd5b50515b611ad7576040805162461bcd60e51b815260206004820152601a60248201527f556e697377617056323a205452414e534645525f4641494c4544000000000000604482015290519081900360640190fd5b5050505050565b6000811580611af957505080820282828281611af657fe5b04145b610b5f576040805162461bcd60e51b815260206004820152601460248201527364732d6d6174682d6d756c2d6f766572666c6f7760601b604482015290519081900360640190fd5b80820382811115610b5f576040805162461bcd60e51b815260206004820152601560248201527464732d6d6174682d7375622d756e646572666c6f7760581b604482015290519081900360640190fd5b6001600160701b038411801590611baf57506001600160701b038311155b611bf6576040805162461bcd60e51b8152602060048201526013602482015272556e697377617056323a204f564552464c4f5760681b604482015290519081900360640190fd5b60085463ffffffff42811691600160e01b90048116820390811615801590611c2657506001600160701b03841615155b8015611c3a57506001600160701b03831615155b15611cab578063ffffffff16611c6885611c538661216e565b6001600160e01b03169063ffffffff61218016565b600980546001600160e01b03929092169290920201905563ffffffff8116611c9384611c538761216e565b600a80546001600160e01b0392909216929092020190555b600880546dffffffffffffffffffffffffffff19166001600160701b03888116919091176dffffffffffffffffffffffffffff60701b1916600160701b8883168102919091176001600160e01b0316600160e01b63ffffffff871602179283905560408051848416815291909304909116602082015281517f1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1929181900390910190a1505050505050565b6001600160a01b03808416600081815260026020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b038316600090815260016020526040902054611de1908263ffffffff611b4116565b6001600160a01b038085166000908152600160205260408082209390935590841681522054611e16908263ffffffff6121a516565b6001600160a01b0380841660008181526001602090815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b600080600560009054906101000a90046001600160a01b03166001600160a01b031663017e7e586040518163ffffffff1660e01b815260040160206040518083038186803b158015611ec357600080fd5b505afa158015611ed7573d6000803e3d6000fd5b505050506040513d6020811015611eed57600080fd5b5051600b546001600160a01b038216158015945091925090611fbc578015611fb7576000611f30610efb6001600160701b0388811690881663ffffffff611ade16565b90506000611f3d83611fd0565b905080821115611fb4576000611f6b611f5c848463ffffffff611b4116565b6000549063ffffffff611ade16565b90506000611f9083611f8486600563ffffffff611ade16565b9063ffffffff6121a516565b90506000818381611f9d57fe5b0490508015611fb057611fb08782612022565b5050505b50505b611fc8565b8015611fc8576000600b555b505092915050565b60006003821115612013575080600160028204015b8181101561200d57809150600281828581611ffc57fe5b04018161200557fe5b049050611fe5565b5061201d565b811561201d575060015b919050565b600054612035908263ffffffff6121a516565b60009081556001600160a01b038316815260016020526040902054612060908263ffffffff6121a516565b6001600160a01b03831660008181526001602090815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b60008183106120c757816120c9565b825b9392505050565b6001600160a01b0382166000908152600160205260409020546120f9908263ffffffff611b4116565b6001600160a01b03831660009081526001602052604081209190915554612126908263ffffffff611b4116565b60009081556040805183815290516001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef919081900360200190a35050565b6001600160701b0316600160701b0290565b60006001600160701b0382166001600160e01b0384168161219d57fe5b049392505050565b80820182811015610b5f576040805162461bcd60e51b815260206004820152601460248201527364732d6d6174682d6164642d6f766572666c6f7760601b604482015290519081900360640190fdfe556e697377617056323a20494e53554646494349454e545f4f55545055545f414d4f554e54556e697377617056323a20494e53554646494349454e545f494e5055545f414d4f554e54556e697377617056323a20494e53554646494349454e545f4c4951554944495459556e697377617056323a20494e53554646494349454e545f4c49515549444954595f4255524e4544556e697377617056323a20494e53554646494349454e545f4c49515549444954595f4d494e544544556e6973776170563245524332303a206e6f7420656e6f75676820616c6c6f77616e63652ea265627a7a72315820bf165e44001b5284076af3e56685540f3d6256d9ddeae5a8f22ed60b1397a80064736f6c63430005100032454950373132446f6d61696e28737472696e67206e616d652c737472696e672076657273696f6e2c75696e7432353620636861696e49642c6164647265737320766572696679696e67436f6e747261637429
pair code (evm): 0x60806040526001600c5534801561001557600080fd5b50604051469080605261240d8239604080519182900360520182208282018252600a8352692ab734b9bbb0b8102b1960b11b6020938401528151808301835260018152603160f81b908401528151808401919091527fbfcc8ef98ffbf7b6c3fec7bf5185b566b9863e35a9d83acd49ad6824b5969738818301527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6606082015260808101949094523060a0808601919091528151808603909101815260c09094019052825192019190912060035550600580546001600160a01b03191633179055612308806101056000396000f3fe608060405234801561001057600080fd5b50600436106101a95760003560e01c80636a627842116100f9578063ba9a7a5611610097578063d21220a711610071578063d21220a714610534578063d505accf1461053c578063dd62ed3e1461058d578063fff6cae9146105bb576101a9565b8063ba9a7a56146104fe578063bc25cf7714610506578063c45a01551461052c576101a9565b80637ecebe00116100d35780637ecebe001461046557806389afcb441461048b57806395d89b41146104ca578063a9059cbb146104d2576101a9565b80636a6278421461041157806370a08231146104375780637464fc3d1461045d576101a9565b806323b872dd116101665780633644e515116101405780633644e515146103cb578063485cc955146103d35780635909c0d5146104015780635a3d549314610409576101a9565b806323b872dd1461036f57806330adf81f146103a5578063313ce567146103ad576101a9565b8063022c0d9f146101ae57806306fdde031461023c5780630902f1ac146102b9578063095ea7b3146102f15780630dfe16811461033157806318160ddd14610355575b600080fd5b61023a600480360360808110156101c457600080fd5b8135916020810135916001600160a01b0360408301351691908101906080810160608201356401000000008111156101fb57600080fd5b82018360208201111561020d57600080fd5b8035906020019184600183028401116401000000008311171561022f57600080fd5b5090925090506105c3565b005b610244610afe565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561027e578181015183820152602001610266565b50505050905090810190601f1680156102ab5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6102c1610b24565b604080516001600160701b03948516815292909316602083015263ffffffff168183015290519081900360600190f35b61031d6004803603604081101561030757600080fd5b506001600160a01b038135169060200135610b4e565b604080519115158252519081900360200190f35b610339610b65565b604080516001600160a01b039092168252519081900360200190f35b61035d610b74565b60408051918252519081900360200190f35b61031d6004803603606081101561038557600080fd5b506001600160a01b03813581169160208101359091169060400135610b7a565b61035d610c76565b6103b5610c9a565b6040805160ff9092168252519081900360200190f35b61035d610c9f565b61023a600480360360408110156103e957600080fd5b506001600160a01b0381358116916020013516610ca5565b61035d610d29565b61035d610d2f565b61035d6004803603602081101561042757600080fd5b50356001600160a01b0316610d35565b61035d6004803603602081101561044d57600080fd5b50356001600160a01b0316611035565b61035d611047565b61035d6004803603602081101561047b57600080fd5b50356001600160a01b031661104d565b6104b1600480360360208110156104a157600080fd5b50356001600160a01b031661105f565b6040805192835260208301919091528051918290030190f35b610244611405565b61031d600480360360408110156104e857600080fd5b506001600160a01b038135169060200135611427565b61035d611434565b61023a6004803603602081101561051c57600080fd5b50356001600160a01b031661143a565b6103396115a5565b6103396115b4565b61023a600480360360e081101561055257600080fd5b506001600160a01b03813581169160208101359091169060408101359060608101359060ff6080820135169060a08101359060c001356115c3565b61035d600480360360408110156105a357600080fd5b506001600160a01b03813581169160200135166117c5565b61023a6117e2565b600c5460011461060e576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55841515806106215750600084115b61065c5760405162461bcd60e51b81526004018080602001828103825260258152602001806121f56025913960400191505060405180910390fd5b600080610667610b24565b5091509150816001600160701b03168710801561068c5750806001600160701b031686105b6106c75760405162461bcd60e51b815260040180806020018281038252602181526020018061223e6021913960400191505060405180910390fd5b60065460075460009182916001600160a01b039182169190811690891682148015906107055750806001600160a01b0316896001600160a01b031614155b61074e576040805162461bcd60e51b8152602060048201526015602482015274556e697377617056323a20494e56414c49445f544f60581b604482015290519081900360640190fd5b8a1561075f5761075f828a8d611944565b891561077057610770818a8c611944565b861561082b57886001600160a01b03166310d1e85c338d8d8c8c6040518663ffffffff1660e01b815260040180866001600160a01b03166001600160a01b03168152602001858152602001848152602001806020018281038252848482818152602001925080828437600081840152601f19601f8201169050808301925050509650505050505050600060405180830381600087803b15801561081257600080fd5b505af1158015610826573d6000803e3d6000fd5b505050505b604080516370a0823160e01b815230600482015290516001600160a01b038416916370a08231916024808301926020929190829003018186803b15801561087157600080fd5b505afa158015610885573d6000803e3d6000fd5b505050506040513d602081101561089b57600080fd5b5051604080516370a0823160e01b815230600482015290519195506001600160a01b038316916370a0823191602480820192602092909190829003018186803b1580156108e757600080fd5b505afa1580156108fb573d6000803e3d6000fd5b505050506040513d602081101561091157600080fd5b5051925060009150506001600160701b0385168a90038311610934576000610943565b89856001600160701b03160383035b9050600089856001600160701b031603831161096057600061096f565b89856001600160701b03160383035b905060008211806109805750600081115b6109bb5760405162461bcd60e51b815260040180806020018281038252602481526020018061221a6024913960400191505060405180910390fd5b60006109ef6109d184600363ffffffff611ade16565b6109e3876103e863ffffffff611ade16565b9063ffffffff611b4116565b90506000610a076109d184600363ffffffff611ade16565b9050610a38620f4240610a2c6001600160701b038b8116908b1663ffffffff611ade16565b9063ffffffff611ade16565b610a48838363ffffffff611ade16565b1015610a8a576040805162461bcd60e51b815260206004820152600c60248201526b556e697377617056323a204b60a01b604482015290519081900360640190fd5b5050610a9884848888611b91565b60408051838152602081018390528082018d9052606081018c905290516001600160a01b038b169133917fd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d8229181900360800190a350506001600c55505050505050505050565b6040518060400160405280600a8152602001692ab734b9bbb0b8102b1960b11b81525081565b6008546001600160701b0380821692600160701b830490911691600160e01b900463ffffffff1690565b6000610b5b338484611d56565b5060015b92915050565b6006546001600160a01b031681565b60005481565b6001600160a01b0383166000908152600260209081526040808320338452909152812054821115610bdc5760405162461bcd60e51b81526004018080602001828103825260258152602001806122af6025913960400191505060405180910390fd5b6001600160a01b038416600090815260026020908152604080832033845290915290205460001914610c61576001600160a01b0384166000908152600260209081526040808320338452909152902054610c3c908363ffffffff611b4116565b6001600160a01b03851660009081526002602090815260408083203384529091529020555b610c6c848484611db8565b5060019392505050565b7f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c981565b601281565b60035481565b6005546001600160a01b03163314610cfb576040805162461bcd60e51b81526020600482015260146024820152732ab734b9bbb0b82b191d102327a92124a22222a760611b604482015290519081900360640190fd5b600680546001600160a01b039384166001600160a01b03199182161790915560078054929093169116179055565b60095481565b600a5481565b6000600c54600114610d82576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c81905580610d92610b24565b50600654604080516370a0823160e01b815230600482015290519395509193506000926001600160a01b03909116916370a08231916024808301926020929190829003018186803b158015610de657600080fd5b505afa158015610dfa573d6000803e3d6000fd5b505050506040513d6020811015610e1057600080fd5b5051600754604080516370a0823160e01b815230600482015290519293506000926001600160a01b03909216916370a0823191602480820192602092909190829003018186803b158015610e6357600080fd5b505afa158015610e77573d6000803e3d6000fd5b505050506040513d6020811015610e8d57600080fd5b505190506000610eac836001600160701b03871663ffffffff611b4116565b90506000610ec9836001600160701b03871663ffffffff611b4116565b90506000610ed78787611e72565b60005490915080610f1457610f006103e86109e3610efb878763ffffffff611ade16565b611fd0565b9850610f0f60006103e8612022565b610f63565b610f606001600160701b038916610f31868463ffffffff611ade16565b81610f3857fe5b046001600160701b038916610f53868563ffffffff611ade16565b81610f5a57fe5b046120b8565b98505b60008911610fa25760405162461bcd60e51b81526004018080602001828103825260288152602001806122876028913960400191505060405180910390fd5b610fac8a8a612022565b610fb886868a8a611b91565b8115610fe857600854610fe4906001600160701b0380821691600160701b90041663ffffffff611ade16565b600b555b6040805185815260208101859052815133927f4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f928290030190a250506001600c5550949695505050505050565b60016020526000908152604090205481565b600b5481565b60046020526000908152604090205481565b600080600c546001146110ad576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c819055806110bd610b24565b50600654600754604080516370a0823160e01b815230600482015290519496509294506001600160a01b039182169391169160009184916370a08231916024808301926020929190829003018186803b15801561111957600080fd5b505afa15801561112d573d6000803e3d6000fd5b505050506040513d602081101561114357600080fd5b5051604080516370a0823160e01b815230600482015290519192506000916001600160a01b038516916370a08231916024808301926020929190829003018186803b15801561119157600080fd5b505afa1580156111a5573d6000803e3d6000fd5b505050506040513d60208110156111bb57600080fd5b5051306000908152600160205260408120549192506111da8888611e72565b600054909150806111f1848763ffffffff611ade16565b816111f857fe5b049a508061120c848663ffffffff611ade16565b8161121357fe5b04995060008b118015611226575060008a115b6112615760405162461bcd60e51b815260040180806020018281038252602881526020018061225f6028913960400191505060405180910390fd5b61126b30846120d0565b611276878d8d611944565b611281868d8c611944565b604080516370a0823160e01b815230600482015290516001600160a01b038916916370a08231916024808301926020929190829003018186803b1580156112c757600080fd5b505afa1580156112db573d6000803e3d6000fd5b505050506040513d60208110156112f157600080fd5b5051604080516370a0823160e01b815230600482015290519196506001600160a01b038816916370a0823191602480820192602092909190829003018186803b15801561133d57600080fd5b505afa158015611351573d6000803e3d6000fd5b505050506040513d602081101561136757600080fd5b5051935061137785858b8b611b91565b81156113a7576008546113a3906001600160701b0380821691600160701b90041663ffffffff611ade16565b600b555b604080518c8152602081018c905281516001600160a01b038f169233927fdccd412f0b1252819cb1fd330b93224ca42612892bb3f4f789976e6d81936496929081900390910190a35050505050505050506001600c81905550915091565b604051806040016040528060068152602001652aa72496ab1960d11b81525081565b6000610b5b338484611db8565b6103e881565b600c54600114611485576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55600654600754600854604080516370a0823160e01b815230600482015290516001600160a01b039485169490931692611534928592879261152f926001600160701b03169185916370a0823191602480820192602092909190829003018186803b1580156114f757600080fd5b505afa15801561150b573d6000803e3d6000fd5b505050506040513d602081101561152157600080fd5b50519063ffffffff611b4116565b611944565b600854604080516370a0823160e01b8152306004820152905161159b928492879261152f92600160701b90046001600160701b0316916001600160a01b038616916370a0823191602480820192602092909190829003018186803b1580156114f757600080fd5b50506001600c5550565b6005546001600160a01b031681565b6007546001600160a01b031681565b4284101561160d576040805162461bcd60e51b8152602060048201526012602482015271155b9a5cddd85c158c8e881156141254915160721b604482015290519081900360640190fd5b6003546001600160a01b0380891660008181526004602090815260408083208054600180820190925582517f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c98186015280840196909652958d166060860152608085018c905260a085019590955260c08085018b90528151808603909101815260e08501825280519083012061190160f01b6101008601526101028501969096526101228085019690965280518085039096018652610142840180825286519683019690962095839052610162840180825286905260ff89166101828501526101a284018890526101c28401879052519193926101e280820193601f1981019281900390910190855afa158015611728573d6000803e3d6000fd5b5050604051601f1901519150506001600160a01b0381161580159061175e5750886001600160a01b0316816001600160a01b0316145b6117af576040805162461bcd60e51b815260206004820152601c60248201527f556e697377617056323a20494e56414c49445f5349474e415455524500000000604482015290519081900360640190fd5b6117ba898989611d56565b505050505050505050565b600260209081526000928352604080842090915290825290205481565b600c5460011461182d576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55600654604080516370a0823160e01b8152306004820152905161193d926001600160a01b0316916370a08231916024808301926020929190829003018186803b15801561187e57600080fd5b505afa158015611892573d6000803e3d6000fd5b505050506040513d60208110156118a857600080fd5b5051600754604080516370a0823160e01b815230600482015290516001600160a01b03909216916370a0823191602480820192602092909190829003018186803b1580156118f557600080fd5b505afa158015611909573d6000803e3d6000fd5b505050506040513d602081101561191f57600080fd5b50516008546001600160701b0380821691600160701b900416611b91565b6001600c55565b604080518082018252601981527f7472616e7366657228616464726573732c75696e74323536290000000000000060209182015281516001600160a01b0385811660248301526044808301869052845180840390910181526064909201845291810180516001600160e01b031663a9059cbb60e01b1781529251815160009460609489169392918291908083835b602083106119f15780518252601f1990920191602091820191016119d2565b6001836020036101000a0380198251168184511680821785525050505050509050019150506000604051808303816000865af19150503d8060008114611a53576040519150601f19603f3d011682016040523d82523d6000602084013e611a58565b606091505b5091509150818015611a86575080511580611a865750808060200190516020811015611a8357600080fd5b50515b611ad7576040805162461bcd60e51b815260206004820152601a60248201527f556e697377617056323a205452414e534645525f4641494c4544000000000000604482015290519081900360640190fd5b5050505050565b6000811580611af957505080820282828281611af657fe5b04145b610b5f576040805162461bcd60e51b815260206004820152601460248201527364732d6d6174682d6d756c2d6f766572666c6f7760601b604482015290519081900360640190fd5b80820382811115610b5f576040805162461bcd60e51b815260206004820152601560248201527464732d6d6174682d7375622d756e646572666c6f7760581b604482015290519081900360640190fd5b6001600160701b038411801590611baf57506001600160701b038311155b611bf6576040805162461bcd60e51b8152602060048201526013602482015272556e697377617056323a204f564552464c4f5760681b604482015290519081900360640190fd5b60085463ffffffff42811691600160e01b90048116820390811615801590611c2657506001600160701b03841615155b8015611c3a57506001600160701b03831615155b15611cab578063ffffffff16611c6885611c538661216e565b6001600160e01b03169063ffffffff61218016565b600980546001600160e01b03929092169290920201905563ffffffff8116611c9384611c538761216e565b600a80546001600160e01b0392909216929092020190555b600880546dffffffffffffffffffffffffffff19166001600160701b03888116919091176dffffffffffffffffffffffffffff60701b1916600160701b8883168102919091176001600160e01b0316600160e01b63ffffffff871602179283905560408051848416815291909304909116602082015281517f1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1929181900390910190a1505050505050565b6001600160a01b03808416600081815260026020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b038316600090815260016020526040902054611de1908263ffffffff611b4116565b6001600160a01b038085166000908152600160205260408082209390935590841681522054611e16908263ffffffff6121a516565b6001600160a01b0380841660008181526001602090815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b600080600560009054906101000a90046001600160a01b03166001600160a01b031663017e7e586040518163ffffffff1660e01b815260040160206040518083038186803b158015611ec357600080fd5b505afa158015611ed7573d6000803e3d6000fd5b505050506040513d6020811015611eed57600080fd5b5051600b546001600160a01b038216158015945091925090611fbc578015611fb7576000611f30610efb6001600160701b0388811690881663ffffffff611ade16565b90506000611f3d83611fd0565b905080821115611fb4576000611f6b611f5c848463ffffffff611b4116565b6000549063ffffffff611ade16565b90506000611f9083611f8486600563ffffffff611ade16565b9063ffffffff6121a516565b90506000818381611f9d57fe5b0490508015611fb057611fb08782612022565b5050505b50505b611fc8565b8015611fc8576000600b555b505092915050565b60006003821115612013575080600160028204015b8181101561200d57809150600281828581611ffc57fe5b04018161200557fe5b049050611fe5565b5061201d565b811561201d575060015b919050565b600054612035908263ffffffff6121a516565b60009081556001600160a01b038316815260016020526040902054612060908263ffffffff6121a516565b6001600160a01b03831660008181526001602090815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b60008183106120c757816120c9565b825b9392505050565b6001600160a01b0382166000908152600160205260409020546120f9908263ffffffff611b4116565b6001600160a01b03831660009081526001602052604081209190915554612126908263ffffffff611b4116565b60009081556040805183815290516001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef919081900360200190a35050565b6001600160701b0316600160701b0290565b60006001600160701b0382166001600160e01b0384168161219d57fe5b049392505050565b80820182811015610b5f576040805162461bcd60e51b815260206004820152601460248201527364732d6d6174682d6164642d6f766572666c6f7760601b604482015290519081900360640190fdfe556e697377617056323a20494e53554646494349454e545f4f55545055545f414d4f554e54556e697377617056323a20494e53554646494349454e545f494e5055545f414d4f554e54556e697377617056323a20494e53554646494349454e545f4c4951554944495459556e697377617056323a20494e53554646494349454e545f4c49515549444954595f4255524e4544556e697377617056323a20494e53554646494349454e545f4c49515549444954595f4d494e544544556e6973776170563245524332303a206e6f7420656e6f75676820616c6c6f77616e63652ea265627a7a72315820bf165e44001b5284076af3e56685540f3d6256d9ddeae5a8f22ed60b1397a80064736f6c63430005100032454950373132446f6d61696e28737472696e67206e616d652c737472696e672076657273696f6e2c75696e7432353620636861696e49642c6164647265737320766572696679696e67436f6e747261637429
pair code hash(python):
0xd0b942f04f5999da18c6933b57c7a558fc3b364ba0e233f5828591ee21b66669
pair code hash(evm):
0xd0b942f04f5999da18c6933b57c7a558fc3b364ba0e233f5828591ee21b66669
pair address of WBTC-USDT(calculated from python):
0x841f5e40d7573fd7fbe5a8a002a10759ad5e3bf3
pair address of WBTC-USDT(from EVM create2):
0x841F5E40d7573FD7FbE5A8A002a10759aD5E3BF3
ok, 第一部分暂时分享到这里, 接下来我们继续一起学习吧
学分: 47
分类: Uniswap
标签:
Uniswap
交易
Uniswap V2
点赞 5
收藏 7
分享
Twitter分享
微信扫码分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。
你可能感兴趣的文章
UniswapV3 部署 - - Foundry Edition
233 浏览
uniswapV2交易流程简介
452 浏览
uniswap v2
313 浏览
怎么解决BTC上交易转账矿工费设置过低,导致交易迟迟不成交的问题
2355 浏览
如何手动构造以太坊交易
1068 浏览
#DEFI# 从 BitShares 到 Uniswap:Dex 发展亲历者的去中心化交易所之旅
551 浏览
相关问题
有没有已经部署的合约去查询Uniswap V3 NFT仓位的价值
1 回答
一般在编写套利合约中通过什么能直接读取给定交易对uniswap v2中的价格?
4 回答
uniswap 多跳路由寻找相关
1 回答
怎么给BEP20虚拟币创建交易对
3 回答
Uniswap中的LP token的数值是如何计算出来的(需要详细计算过程)
2 回答
如何获取 Uniswap 代币价格,v2 v3 是否不同
1 回答
0 条评论
请先 登录 后评论
tonyh
关注
贡献值: 82
学分: 209
https://github.com/star4evar
文章目录
关于
关于我们
社区公约
学分规则
Github
伙伴们
DeCert
ChainTool
GCC
合作
广告投放
发布课程
联系我们
友情链接
关注社区
Discord
Youtube
B 站
公众号
关注不错过动态
微信群
加入技术圈子
©2024 登链社区 版权所有 |
Powered By Tipask3.5|
粤公网安备 44049102496617号
粤ICP备17140514号
粤B2-20230927
增值电信业务经营许可证
×
发送私信
请将文档链接发给晓娜,我们会尽快安排上架,感谢您的推荐!
发给:
内容:
取消
发送
×
举报此文章
垃圾广告信息:
广告、推广、测试等内容
违规内容:
色情、暴力、血腥、敏感信息等内容
不友善内容:
人身攻击、挑衅辱骂、恶意行为
其他原因:
请补充说明
举报原因:
取消
举报
×
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!
交易路径 | Uniswap V3 Book 中文版
| Uniswap V3 Book 中文版Uniswap V3 Book 中文版Milestone 0. 简介交易市场简介恒定函数做市商(CFMM)Uniswap V3开发环境Milestone 1. 第一笔交易简介计算流动性提供流动性第一笔交易管理合约部署合约用户界面Milestone 2. 第二笔交易简介输出金额计算Solidity中的数学运算Tick Bitmap Index通用mint通用swap报价合约用户界面Milestone 3. 跨tick交易简介不同价格区间跨tick交易滑点保护流动性计算关于定点数的拓展闪电贷用户界面Milestone 4. 多池子交易简介工厂合约交易路径多池子交易用户界面Tick 舍入Milestone 5. 费率和价格预言机简介交易费率闪电贷费率协议费率价格预言机用户界面Milestone 6: NFT positions简介ERC721 概述NFT 管理员合约NFT 渲染器补充资料中英名词对照
交易路径
交易路径Path 库计算路径中池子的数量判断一个路径是否有多个池子提取路径中第一个池子的参数前往路径中下一个 token 对解码第一个池子的参数交易路径
#假设我们只有以下几个池子:WETH/USDC, USDC/USDT, WBTC/USDT。如果我们想要把 WETH 换成 WBTC,我们需要进行多步交换(WETH→USDC→USDT→WBTC),因为没有直接的 WETH/WBTC 池子。我们可以手动进行这一步,或者我们可以改进我们的合约来支持这样链式的,或者叫多池子的交易。当然,我们要选择后者!当进行多池子交易时,我们会把上一笔交易的输出作为下一笔交易的输入。例如:在 WETH/USDC 池子,我们卖出 WETH 买入 USDC;在 USDC/USDT 池子,我们卖出前一笔交易得到的 USDC 买入 USDT;在 WBTC/USDT 池子,我们卖出前一笔交易得到的 USDT 买入 WBTC。我们可以把这样一个序列转换成如下路径:WETH/USDC,USDC/USDT,WBTC/USDT
并在合约中沿着这个路径进行遍历来在同一笔交易中实现多笔交易。然而,回顾一下在前一小节中我们提到我们不再需要知道池子地址,而可以通过池子参数计算出地址。因此,上述的路径可以被转换成一系列的 token:WETH, USDC, USDT, WBTC
并且由于 tick 间隔也是一个标识池子的参数,我们也需要把它包含在路径里:WETH, 60, USDC, 10, USDT, 60, WBTC
其中的 60 和 10 都是 tick 间隔。我们在波动性较大的池子(例如 ETH/USDC, WBTC, USDT)中使用 60 的间隔,在稳定币池子中(例如 USDC/USDT)使用 10 的间隔。现在,有了这样的路径,我们可以遍历这条路径来获取每个池子的参数:WETH, 60, USDC;USDC, 10, USDT;USDT, 60, WBTC.知道了这些参数,我们可以使用我们前一章实现的 PoolAddress.computeAddress 来算出池子地址。在一个池子内交易的时候我们也可以使用这个概念:路径仅包含一个池子的参数。因此,交易路径可以适用于所有类型的交易。让我们搭建一个库来进行路径相关操作。Path 库
#在代码中,一个交易路径是一个字节序列。在 Solidity 中,一个路径可以这样构建:bytes.concat(
bytes20(address(weth)),
bytes3(uint24(60)),
bytes20(address(usdc)),
bytes3(uint24(10)),
bytes20(address(usdt)),
bytes3(uint24(60)),
bytes20(address(wbtc))
);
它长这样:0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 # weth address
00003c # 60
A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 # usdc address
00000a # 10
dAC17F958D2ee523a2206206994597C13D831ec7 # usdt address
00003c # 60
2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 # wbtc address
以下是我们需要实现的函数:计算路径中池子的数量;看一个路径是否包含多个池子;提取路径中第一个池子的参数;进入路径中的下一个 token 对;解码池子参数计算路径中池子的数量
#让我们首先实现计算路径中池子的数量:// src/lib/Path.sol
library Path {
/// @dev The length the bytes encoded address
uint256 private constant ADDR_SIZE = 20;
/// @dev The length the bytes encoded tick spacing
uint256 private constant TICKSPACING_SIZE = 3;
/// @dev The offset of a single token address + tick spacing
uint256 private constant NEXT_OFFSET = ADDR_SIZE + TICKSPACING_SIZE;
/// @dev The offset of an encoded pool key (tokenIn + tick spacing + tokenOut)
uint256 private constant POP_OFFSET = NEXT_OFFSET + ADDR_SIZE;
/// @dev The minimum length of a path that contains 2 or more pools;
uint256 private constant MULTIPLE_POOLS_MIN_LENGTH =
POP_OFFSET + NEXT_OFFSET;
...
我们首先定义一系列常量:ADDR_SIZE 是地址的大小,20字节;TICKSPACING_SIZE 是 tick 间隔的大小,3字节(uint24);NEXT_OFFSET 是到下一个 token 地址的偏移——为了获取这个地址,我们要跳过当前地址和 tick 间隔;POP_OFFSET 是编码的池子参数的偏移 (token address + tick spacing + token address);MULTIPLE_POOLS_MIN_LENGTH 是包含两个或以上池子的路径长度 (一个池子的参数 + tick
spacing + token address 的集合)。为了计算路径中的池子数量,我们减去一个地址的大小(路径中的第一个或最后一个 token)并且用剩下的值除以 NEXT_OFFSET 即可:function numPools(bytes memory path) internal pure returns (uint256) {
return (path.length - ADDR_SIZE) / NEXT_OFFSET;
}
判断一个路径是否有多个池子
#为了判断一个路径中是否有多个池子,我们只需要将路径长度与 MULTIPLE_POOLS_MIN_LENGTH 比较即可:function hasMultiplePools(bytes memory path) internal pure returns (bool) {
return path.length >= MULTIPLE_POOLS_MIN_LENGTH;
}
提取路径中第一个池子的参数
#为了实现其他的函数,我们需要一个辅助的库,因为 Solidity 没有原生的 bytes 操作函数。我们需要能够从一个字节数组中提取出一个子数组的函数,以及将 address 和 uint24 转换成字节的函数。幸运的是,已经有一个叫做 solidity-bytes-utils 的开源库实现了这些。为了使用这个库,我们需要扩展 Path 库里面的 bytes 类型:library Path {
using BytesLib for bytes;
...
}
现在我们可以实现 getFirstPool 了:function getFirstPool(bytes memory path)
internal
pure
returns (bytes memory)
{
return path.slice(0, POP_OFFSET);
}
这个函数仅仅返回了 “token address + tick spacing + token address” 这一段字节。前往路径中下一个 token 对
#我们将会在遍历路径的时候使用下面这个函数,来扔掉已经处理过的池子。注意到我们移除的是"token address + tick spacing",而不是完整的池子参数,因为我们还需要另一个 token 地址来计算下一个池子的地址。function skipToken(bytes memory path) internal pure returns (bytes memory) {
return path.slice(NEXT_OFFSET, path.length - NEXT_OFFSET);
}
解码第一个池子的参数
#最后,我们需要解码路径中第一个池子的参数:function decodeFirstPool(bytes memory path)
internal
pure
returns (
address tokenIn,
address tokenOut,
uint24 tickSpacing
)
{
tokenIn = path.toAddress(0);
tickSpacing = path.toUint24(ADDR_SIZE);
tokenOut = path.toAddress(NEXT_OFFSET);
}
遗憾的是,BytesLib 没有实现 toUint24 这个函数,但我们可以自己实现它!在 BytesLib 中有很多 toUintXX 这样的函数,我们可以把其中一个转换成 uint24 类型的:library BytesLibExt {
function toUint24(bytes memory _bytes, uint256 _start)
internal
pure
returns (uint24)
{
require(_bytes.length >= _start + 3, "toUint24_outOfBounds");
uint24 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x3), _start))
}
return tempUint;
}
}
我们是在一个新的库合约中实现这个函数,可以直接在 Path 库中使用它:library Path {
using BytesLib for bytes;
using BytesLibExt for bytes;
...
}
交易路径Path 库计算路径中池子的数量判断一个路径是否有多个池子提取路径中第一个池子的参数前往路径中下一个 token 对解码第一个池子
Buy/Sell Bitcoin, Ether and Altcoins | Cryptocurrency Exchange | Binance
Buy/Sell Bitcoin, Ether and Altcoins | Cryptocurrency Exchange | Binance
Error 403 Forbidden - This request is blocked.
For security reasons you can't connect to the server for this app or website at this time.
It maybe that you have too many requests or the illegal request payload is identified as an attack.
Please try again later.
UniSwap学习笔记1: 概览以及交易对地址计算-腾讯云开发者社区-腾讯云
wap学习笔记1: 概览以及交易对地址计算-腾讯云开发者社区-腾讯云Tiny熊UniSwap学习笔记1: 概览以及交易对地址计算关注作者腾讯云开发者社区文档建议反馈控制台首页学习活动专区工具TVP最新优惠活动文章/答案/技术大牛搜索搜索关闭发布登录/注册首页学习活动专区工具TVP最新优惠活动返回腾讯云官网Tiny熊首页学习活动专区工具TVP最新优惠活动返回腾讯云官网社区首页 >专栏 >UniSwap学习笔记1: 概览以及交易对地址计算UniSwap学习笔记1: 概览以及交易对地址计算Tiny熊关注发布于 2022-05-25 15:58:321.7K1发布于 2022-05-25 15:58:32举报文章被收录于专栏:深入浅出区块链技术深入浅出区块链技术本文作者:tony.ho[1]hello everyone, 我是 tony, 今天开始为大家分享我最近学习 Uniswap[2] 的小小心得, 希望各位看官多多提意见, 大家一起进步.Uniswap V2 代码地址:核心代码: 包括 Factory, Pair, WETH 3 个合约 https://github.com/Uniswap/v2-core外围代码: Router 合约 https://github.com/Uniswap/v2-periphery概览Uniswap core 有 3 个合约: Factory, Pair, WETH Uniswap periphery 有 1 个合约: RouterFactory 合约 (UniswapV2Factory.sol): 负责创建交易对, 保存所有交易对的地址 .Pair 合约 (UniswapV2Pair.sol) : 保存单个交易对的资金池信息, 包括每个交易对中两个 ERC20 代币的地址, 以及各自资金余额(reserve). 处理 mint, burn 和 交易(swap) 操作. Pair 合约是 Uniswap 代码的核心 UniswapV2Pair 本身也是 ERC20, 当添加流动性时会 mint 新的 pair 代币(pair token, or LP token), 删除流动性时会销毁一定数量的代币, 确保 pair token 的 totalSupply 始终等于池子中的两个交易对代币余额的几何平均值: totalSupply of pair token === sqrt(reserve1 * reserve2)WETH 合约: 这个合约是一个特殊的 ERC20, 它的目的是方便用户直接用 ETH 和 ERC20 进行交易, 你可以把 WETH 看作是一个全额担保的债券: 这个合约里面存储的 ETH 总是等于它发行的代币 WETH, (除非有人直接使用 WETH_address.transfer 向它发送了 ETH ). Pair 合约的交易对总是 ERC20 代币, 不能进行 ETH 和 ERC20 之间的交易, 所以 WETH 就被创造出来, 它有两个方法, deposit() 和 withdraw(), 前者是存入 ETH 获得 WETH, 后者是 销毁 WETH 获得 ETH. 但是这两个方法通常是由 Router 来调用的, 例如 Router 中的 swapExactETHForTokens 函数:function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)external
virtual
override
payable
ensure(deadline)
returns (uint[] memory amounts)
{
require(path[0] == WETH, 'UniswapV2Router: INVALID_PATH');
amounts = UniswapV2Library.getAmountsOut(factory, msg.value, path);
require(amounts[amounts.length - 1] >= amountOutMin, 'UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT');
IWETH(WETH).deposit{value: amounts[0]}();
assert(IWETH(WETH).transfer(UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]));
_swap(amounts, path, to);
}
复制上面的代码中, 用户需要用 ETH 换成 ERC20 代币, 那么只能使用 WETH-ERC20 交易对, 用户将 ETH 发送给 Router, Router 获得 ETH 后用它换出 WETH ( 调用 WETH.deposit() ), 再将 WETH 发送给 Pair, 换出目标 ERC20 代币发送给参数 to 中指定的接收账户.Router 合约: (UniswapV2Router02.sol) 这个合约包含了几乎所有提供给应用层使用的函数 (除了 Factory 的 getPair 和 createPair), 包括 <添加,撤销流动性> 和 <交易>, 它和 Pair 的区别如下: Pair 中没有添加, 撤销流动性操作, 只有 mint 和 burn. Pair 中的 swap 操作是单个交易对的操作, 而 Router 的 swap 是可以给定路径, 进行一系列交易后得到最终目标代币 token1 -> token2 ... -> token_target 在此过程中, Router 会依次调用 (token_i, token_i+1) 所对应的交易对的 swap 函数, 并将换出的代币发送到 (token_i+1, token_i+2)交易对合约地址, 再次执行 swap, 发送... 直到换出最后的 token_target在后面的介绍中, 我们会阅读源码, 研究 swap 的流程细节.Pair 的创建 和 create2 的地址计算:交易对的创建是在 Factory 中完成, UniswapV2Factory.ceatePair 函数定义如下:function createPair(address tokenA, address tokenB) external returns (address pair) {
require(tokenA != tokenB, 'UniswapV2: IDENTICAL_ADDRESSES');
(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), 'UniswapV2: ZERO_ADDRESS');
require(getPair[token0][token1] == address(0), 'UniswapV2: PAIR_EXISTS'); // single check is sufficient
bytes memory bytecode = type(UniswapV2Pair).creationCode;
bytes32 salt = keccak256(abi.encodePacked(token0, token1));
assembly {
pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
}
IUniswapV2Pair(pair).initialize(token0, token1);
getPair[token0][token1] = pair;
getPair[token1][token0] = pair; // populate mapping in the reverse direction
allPairs.push(pair);
emit PairCreated(token0, token1, pair, allPairs.length);
}
复制这里 Factory 使用了 create2 操作码部署新的 Pair 合约, 而不是 pair = new UniswapV2Pair(tokenA, tokenB):assembly {
pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
}
复制这样做的好处是: 它可以得到一个确定的 pair 地址, 使得 Router 中就可以通过 tokenA, tokenB 计算出 pair 地址, 不再需要执行一次 Factory.getPair(tokenA, tokenB) 的跨合约调用, create2 生成地址的规则是:keccak256(abi.encodePacked(
hex'ff',
deployer_address, // 由Factory创建, 这里就是 factory合约地址
salt, // salt 是创建者自定义的数据, 避免重复, 这里的
// salt = keccak256(abi.encodePacked(token0, token1)), 且 token0 < token1
keccak256(type(Contract_to_Deploy).creationCode) // 此处 Contract_to_Deploy = UniswapV2Pair
))))
复制参考 periphery 的 libraries/UniswapV2Library.solfunction pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
(address token0, address token1) = sortTokens(tokenA, tokenB);
pair = address(uint(keccak256(abi.encodePacked(
hex'ff',
factory,
keccak256(abi.encodePacked(token0, token1)),
hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
))));
}
复制在这里, Router 为了节省 gas, 使用 pairFor 获取 tokenA 和 tokenB 的交易对地址, 而且最后的字节码哈希也是预先计算出来, 硬编码到代码里面.如果我们自己修改了 Pair 的源代码合约, 重新部署后, Router 就无法找到这个 Pair 地址了, 解决办法要么直接返回 factory.getPair(tokenA, tokenB), 要么就把以前部署的 Pair 全部舍弃, 重新部署 Factory 和所有 Pair, 再重新部署 Router.下面的 python 示例代码用于计算 pair 的 hash, 我们可以在重新部署 Pair 后调用这个函数, 再把 hash 复制到 UniswapV2Library.pairFor() 进行替换: 同时修改 UniswapV2Factory.sol , 增加两个函数用于测试:contract UniswapV2Factory is IUniswapV2Factory {
...
/*
* 返回 Pair的字节码
*/
function pair_code() public view returns(bytes memory){
return type(UniswapV2Pair).creationCode;
}
/*
* 返回 Pair的字节码哈希
*/
function pair_codehash() public view returns(bytes32 ){
return keccak256( type(UniswapV2Pair).creationCode );
}
...
}
复制以下是 python 代码, 用于计算 pair 的 code_hash, 并且模拟 create2 计算交易对地址, 如果与 factory.getPair 得到的地址相同, 证明我们的 code_hash 计算正确, 可以拷贝到 UniswapV2Library.sol 的 pairFor 函数###########################################################################################
#
# 重新编译并部署 pair 和 Factory 后, 调用此函数计算出hash, 放到 peripery/libraries/UniswapV2Library.sol 的
# 'pairFor' 函数, 替换原来的hash,
# 然后重新编译并部署Router
#
###########################################################################################
def calc_pair_address():
# 执行本函数前, 确保已经重新编译并部署 Factory 和 Pair, 同时部署测试代币 WBTC, USDT及交易对
# 执行完本函数后, 需要修改 Router源代码, 重新编译部署Router
abi, bytecode = _helper.get_contract_info(r"core/artifacts/UniswapV2Pair.json")
WBTC, USDT = _contracts["WBTC"], _contracts["USDT"]
factory = _contracts["Factory"]
pair_codehash = Web3.keccak(bytes.fromhex(bytecode))
addr1, addr2 = sorted((WBTC.address, USDT.address))
salt = Web3.solidityKeccak(['address' , 'address'], [addr1, addr2])
pair_address = Web3.solidityKeccak(['uint8', 'address' , 'bytes', 'bytes'],
[0xff,
factory.address,
salt,
pair_codehash])
print(f"pair code(python): 0x{bytecode}", )
evm_code = factory.functions.pair_code().call()
print(f"pair code (evm): 0x{evm_code.hex()}")
print(f"code hash(python): \n\t{pair_codehash.hex()}", )
evm_codehash = factory.functions.pair_codehash().call()
print(f"code hash(evm): \n\t0x{evm_codehash.hex()}")
print(f"pair address of WBTC-USDT(calculated from python): \n\t{pair_address.hex()}")
evm_pair_address = factory.functions.getPair(WBTC.address, USDT.address).call()
print(f"pair address of WBTC-USDT(in EVM create2): \n\t{evm_pair_address}")
复制得到以下结果, 证明我们的 python 端计算 pair 地址的方法是正确的:/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释
* pair code(python): 0x60806040526001600c5534801561001557600080fd5b50604051469080605261240d8239604080519182900360520182208282018252600a8352692ab734b9bbb0b8102b1960b11b6020938401528151808301835260018152603160f81b908401528151808401919091527fbfcc8ef98ffbf7b6c3fec7bf5185b566b9863e35a9d83acd49ad6824b5969738818301527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6606082015260808101949094523060a0808601919091528151808603909101815260c09094019052825192019190912060035550600580546001600160a01b03191633179055612308806101056000396000f3fe608060405234801561001057600080fd5b50600436106101a95760003560e01c80636a627842116100f9578063ba9a7a5611610097578063d21220a711610071578063d21220a714610534578063d505accf1461053c578063dd62ed3e1461058d578063fff6cae9146105bb576101a9565b8063ba9a7a56146104fe578063bc25cf7714610506578063c45a01551461052c576101a9565b80637ecebe00116100d35780637ecebe001461046557806389afcb441461048b57806395d89b41146104ca578063a9059cbb146104d2576101a9565b80636a6278421461041157806370a08231146104375780637464fc3d1461045d576101a9565b806323b872dd116101665780633644e515116101405780633644e515146103cb578063485cc955146103d35780635909c0d5146104015780635a3d549314610409576101a9565b806323b872dd1461036f57806330adf81f146103a5578063313ce567146103ad576101a9565b8063022c0d9f146101ae57806306fdde031461023c5780630902f1ac146102b9578063095ea7b3146102f15780630dfe16811461033157806318160ddd14610355575b600080fd5b61023a600480360360808110156101c457600080fd5b8135916020810135916001600160a01b0360408301351691908101906080810160608201356401000000008111156101fb57600080fd5b82018360208201111561020d57600080fd5b8035906020019184600183028401116401000000008311171561022f57600080fd5b5090925090506105c3565b005b610244610afe565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561027e578181015183820152602001610266565b50505050905090810190601f1680156102ab5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6102c1610b24565b604080516001600160701b03948516815292909316602083015263ffffffff168183015290519081900360600190f35b61031d6004803603604081101561030757600080fd5b506001600160a01b038135169060200135610b4e565b604080519115158252519081900360200190f35b610339610b65565b604080516001600160a01b039092168252519081900360200190f35b61035d610b74565b60408051918252519081900360200190f35b61031d6004803603606081101561038557600080fd5b506001600160a01b03813581169160208101359091169060400135610b7a565b61035d610c76565b6103b5610c9a565b6040805160ff9092168252519081900360200190f35b61035d610c9f565b61023a600480360360408110156103e957600080fd5b506001600160a01b0381358116916020013516610ca5565b61035d610d29565b61035d610d2f565b61035d6004803603602081101561042757600080fd5b50356001600160a01b0316610d35565b61035d6004803603602081101561044d57600080fd5b50356001600160a01b0316611035565b61035d611047565b61035d6004803603602081101561047b57600080fd5b50356001600160a01b031661104d565b6104b1600480360360208110156104a157600080fd5b50356001600160a01b031661105f565b6040805192835260208301919091528051918290030190f35b610244611405565b61031d600480360360408110156104e857600080fd5b506001600160a01b038135169060200135611427565b61035d611434565b61023a6004803603602081101561051c57600080fd5b50356001600160a01b031661143a565b6103396115a5565b6103396115b4565b61023a600480360360e081101561055257600080fd5b506001600160a01b03813581169160208101359091169060408101359060608101359060ff6080820135169060a08101359060c001356115c3565b61035d600480360360408110156105a357600080fd5b506001600160a01b03813581169160200135166117c5565b61023a6117e2565b600c5460011461060e576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55841515806106215750600084115b61065c5760405162461bcd60e51b81526004018080602001828103825260258152602001806121f56025913960400191505060405180910390fd5b600080610667610b24565b5091509150816001600160701b03168710801561068c5750806001600160701b031686105b6106c75760405162461bcd60e51b815260040180806020018281038252602181526020018061223e6021913960400191505060405180910390fd5b60065460075460009182916001600160a01b039182169190811690891682148015906107055750806001600160a01b0316896001600160a01b031614155b61074e576040805162461bcd60e51b8152602060048201526015602482015274556e697377617056323a20494e56414c49445f544f60581b604482015290519081900360640190fd5b8a1561075f5761075f828a8d611944565b891561077057610770818a8c611944565b861561082b57886001600160a01b03166310d1e85c338d8d8c8c6040518663ffffffff1660e01b815260040180866001600160a01b03166001600160a01b03168152602001858152602001848152602001806020018281038252848482818152602001925080828437600081840152601f19601f8201169050808301925050509650505050505050600060405180830381600087803b15801561081257600080fd5b505af1158015610826573d6000803e3d6000fd5b505050505b604080516370a0823160e01b815230600482015290516001600160a01b038416916370a08231916024808301926020929190829003018186803b15801561087157600080fd5b505afa158015610885573d6000803e3d6000fd5b505050506040513d602081101561089b57600080fd5b5051604080516370a0823160e01b815230600482015290519195506001600160a01b038316916370a0823191602480820192602092909190829003018186803b1580156108e757600080fd5b505afa1580156108fb573d6000803e3d6000fd5b505050506040513d602081101561091157600080fd5b5051925060009150506001600160701b0385168a90038311610934576000610943565b89856001600160701b03160383035b9050600089856001600160701b031603831161096057600061096f565b89856001600160701b03160383035b905060008211806109805750600081115b6109bb5760405162461bcd60e51b815260040180806020018281038252602481526020018061221a6024913960400191505060405180910390fd5b60006109ef6109d184600363ffffffff611ade16565b6109e3876103e863ffffffff611ade16565b9063ffffffff611b4116565b90506000610a076109d184600363ffffffff611ade16565b9050610a38620f4240610a2c6001600160701b038b8116908b1663ffffffff611ade16565b9063ffffffff611ade16565b610a48838363ffffffff611ade16565b1015610a8a576040805162461bcd60e51b815260206004820152600c60248201526b556e697377617056323a204b60a01b604482015290519081900360640190fd5b5050610a9884848888611b91565b60408051838152602081018390528082018d9052606081018c905290516001600160a01b038b169133917fd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d8229181900360800190a350506001600c55505050505050505050565b6040518060400160405280600a8152602001692ab734b9bbb0b8102b1960b11b81525081565b6008546001600160701b0380821692600160701b830490911691600160e01b900463ffffffff1690565b6000610b5b338484611d56565b5060015b92915050565b6006546001600160a01b031681565b60005481565b6001600160a01b0383166000908152600260209081526040808320338452909152812054821115610bdc5760405162461bcd60e51b81526004018080602001828103825260258152602001806122af6025913960400191505060405180910390fd5b6001600160a01b038416600090815260026020908152604080832033845290915290205460001914610c61576001600160a01b0384166000908152600260209081526040808320338452909152902054610c3c908363ffffffff611b4116565b6001600160a01b03851660009081526002602090815260408083203384529091529020555b610c6c848484611db8565b5060019392505050565b7f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c981565b601281565b60035481565b6005546001600160a01b03163314610cfb576040805162461bcd60e51b81526020600482015260146024820152732ab734b9bbb0b82b191d102327a92124a22222a760611b604482015290519081900360640190fd5b600680546001600160a01b039384166001600160a01b03199182161790915560078054929093169116179055565b60095481565b600a5481565b6000600c54600114610d82576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c81905580610d92610b24565b50600654604080516370a0823160e01b815230600482015290519395509193506000926001600160a01b03909116916370a08231916024808301926020929190829003018186803b158015610de657600080fd5b505afa158015610dfa573d6000803e3d6000fd5b505050506040513d6020811015610e1057600080fd5b5051600754604080516370a0823160e01b815230600482015290519293506000926001600160a01b03909216916370a0823191602480820192602092909190829003018186803b158015610e6357600080fd5b505afa158015610e77573d6000803e3d6000fd5b505050506040513d6020811015610e8d57600080fd5b505190506000610eac836001600160701b03871663ffffffff611b4116565b90506000610ec9836001600160701b03871663ffffffff611b4116565b90506000610ed78787611e72565b60005490915080610f1457610f006103e86109e3610efb878763ffffffff611ade16565b611fd0565b9850610f0f60006103e8612022565b610f63565b610f606001600160701b038916610f31868463ffffffff611ade16565b81610f3857fe5b046001600160701b038916610f53868563ffffffff611ade16565b81610f5a57fe5b046120b8565b98505b60008911610fa25760405162461bcd60e51b81526004018080602001828103825260288152602001806122876028913960400191505060405180910390fd5b610fac8a8a612022565b610fb886868a8a611b91565b8115610fe857600854610fe4906001600160701b0380821691600160701b90041663ffffffff611ade16565b600b555b6040805185815260208101859052815133927f4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f928290030190a250506001600c5550949695505050505050565b60016020526000908152604090205481565b600b5481565b60046020526000908152604090205481565b600080600c546001146110ad576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c819055806110bd610b24565b50600654600754604080516370a0823160e01b815230600482015290519496509294506001600160a01b039182169391169160009184916370a08231916024808301926020929190829003018186803b15801561111957600080fd5b505afa15801561112d573d6000803e3d6000fd5b505050506040513d602081101561114357600080fd5b5051604080516370a0823160e01b815230600482015290519192506000916001600160a01b038516916370a08231916024808301926020929190829003018186803b15801561119157600080fd5b505afa1580156111a5573d6000803e3d6000fd5b505050506040513d60208110156111bb57600080fd5b5051306000908152600160205260408120549192506111da8888611e72565b600054909150806111f1848763ffffffff611ade16565b816111f857fe5b049a508061120c848663ffffffff611ade16565b8161121357fe5b04995060008b118015611226575060008a115b6112615760405162461bcd60e51b815260040180806020018281038252602881526020018061225f6028913960400191505060405180910390fd5b61126b30846120d0565b611276878d8d611944565b611281868d8c611944565b604080516370a0823160e01b815230600482015290516001600160a01b038916916370a08231916024808301926020929190829003018186803b1580156112c757600080fd5b505afa1580156112db573d6000803e3d6000fd5b505050506040513d60208110156112f157600080fd5b5051604080516370a0823160e01b815230600482015290519196506001600160a01b038816916370a0823191602480820192602092909190829003018186803b15801561133d57600080fd5b505afa158015611351573d6000803e3d6000fd5b505050506040513d602081101561136757600080fd5b5051935061137785858b8b611b91565b81156113a7576008546113a3906001600160701b0380821691600160701b90041663ffffffff611ade16565b600b555b604080518c8152602081018c905281516001600160a01b038f169233927fdccd412f0b1252819cb1fd330b93224ca42612892bb3f4f789976e6d81936496929081900390910190a35050505050505050506001600c81905550915091565b604051806040016040528060068152602001652aa72496ab1960d11b81525081565b6000610b5b338484611db8565b6103e881565b600c54600114611485576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55600654600754600854604080516370a0823160e01b815230600482015290516001600160a01b039485169490931692611534928592879261152f926001600160701b03169185916370a0823191602480820192602092909190829003018186803b1580156114f757600080fd5b505afa15801561150b573d6000803e3d6000fd5b505050506040513d602081101561152157600080fd5b50519063ffffffff611b4116565b611944565b600854604080516370a0823160e01b8152306004820152905161159b928492879261152f92600160701b90046001600160701b0316916001600160a01b038616916370a0823191602480820192602092909190829003018186803b1580156114f757600080fd5b50506001600c5550565b6005546001600160a01b031681565b6007546001600160a01b031681565b4284101561160d576040805162461bcd60e51b8152602060048201526012602482015271155b9a5cddd85c158c8e881156141254915160721b604482015290519081900360640190fd5b6003546001600160a01b0380891660008181526004602090815260408083208054600180820190925582517f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c98186015280840196909652958d166060860152608085018c905260a085019590955260c08085018b90528151808603909101815260e08501825280519083012061190160f01b6101008601526101028501969096526101228085019690965280518085039096018652610142840180825286519683019690962095839052610162840180825286905260ff89166101828501526101a284018890526101c28401879052519193926101e280820193601f1981019281900390910190855afa158015611728573d6000803e3d6000fd5b5050604051601f1901519150506001600160a01b0381161580159061175e5750886001600160a01b0316816001600160a01b0316145b6117af576040805162461bcd60e51b815260206004820152601c60248201527f556e697377617056323a20494e56414c49445f5349474e415455524500000000604482015290519081900360640190fd5b6117ba898989611d56565b505050505050505050565b600260209081526000928352604080842090915290825290205481565b600c5460011461182d576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55600654604080516370a0823160e01b8152306004820152905161193d926001600160a01b0316916370a08231916024808301926020929190829003018186803b15801561187e57600080fd5b505afa158015611892573d6000803e3d6000fd5b505050506040513d60208110156118a857600080fd5b5051600754604080516370a0823160e01b815230600482015290516001600160a01b03909216916370a0823191602480820192602092909190829003018186803b1580156118f557600080fd5b505afa158015611909573d6000803e3d6000fd5b505050506040513d602081101561191f57600080fd5b50516008546001600160701b0380821691600160701b900416611b91565b6001600c55565b604080518082018252601981527f7472616e7366657228616464726573732c75696e74323536290000000000000060209182015281516001600160a01b0385811660248301526044808301869052845180840390910181526064909201845291810180516001600160e01b031663a9059cbb60e01b1781529251815160009460609489169392918291908083835b602083106119f15780518252601f1990920191602091820191016119d2565b6001836020036101000a0380198251168184511680821785525050505050509050019150506000604051808303816000865af19150503d8060008114611a53576040519150601f19603f3d011682016040523d82523d6000602084013e611a58565b606091505b5091509150818015611a86575080511580611a865750808060200190516020811015611a8357600080fd5b50515b611ad7576040805162461bcd60e51b815260206004820152601a60248201527f556e697377617056323a205452414e534645525f4641494c4544000000000000604482015290519081900360640190fd5b5050505050565b6000811580611af957505080820282828281611af657fe5b04145b610b5f576040805162461bcd60e51b815260206004820152601460248201527364732d6d6174682d6d756c2d6f766572666c6f7760601b604482015290519081900360640190fd5b80820382811115610b5f576040805162461bcd60e51b815260206004820152601560248201527464732d6d6174682d7375622d756e646572666c6f7760581b604482015290519081900360640190fd5b6001600160701b038411801590611baf57506001600160701b038311155b611bf6576040805162461bcd60e51b8152602060048201526013602482015272556e697377617056323a204f564552464c4f5760681b604482015290519081900360640190fd5b60085463ffffffff42811691600160e01b90048116820390811615801590611c2657506001600160701b03841615155b8015611c3a57506001600160701b03831615155b15611cab578063ffffffff16611c6885611c538661216e565b6001600160e01b03169063ffffffff61218016565b600980546001600160e01b03929092169290920201905563ffffffff8116611c9384611c538761216e565b600a80546001600160e01b0392909216929092020190555b600880546dffffffffffffffffffffffffffff19166001600160701b03888116919091176dffffffffffffffffffffffffffff60701b1916600160701b8883168102919091176001600160e01b0316600160e01b63ffffffff871602179283905560408051848416815291909304909116602082015281517f1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1929181900390910190a1505050505050565b6001600160a01b03808416600081815260026020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b038316600090815260016020526040902054611de1908263ffffffff611b4116565b6001600160a01b038085166000908152600160205260408082209390935590841681522054611e16908263ffffffff6121a516565b6001600160a01b0380841660008181526001602090815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b600080600560009054906101000a90046001600160a01b03166001600160a01b031663017e7e586040518163ffffffff1660e01b815260040160206040518083038186803b158015611ec357600080fd5b505afa158015611ed7573d6000803e3d6000fd5b505050506040513d6020811015611eed57600080fd5b5051600b546001600160a01b038216158015945091925090611fbc578015611fb7576000611f30610efb6001600160701b0388811690881663ffffffff611ade16565b90506000611f3d83611fd0565b905080821115611fb4576000611f6b611f5c848463ffffffff611b4116565b6000549063ffffffff611ade16565b90506000611f9083611f8486600563ffffffff611ade16565b9063ffffffff6121a516565b90506000818381611f9d57fe5b0490508015611fb057611fb08782612022565b5050505b50505b611fc8565b8015611fc8576000600b555b505092915050565b60006003821115612013575080600160028204015b8181101561200d57809150600281828581611ffc57fe5b04018161200557fe5b049050611fe5565b5061201d565b811561201d575060015b919050565b600054612035908263ffffffff6121a516565b60009081556001600160a01b038316815260016020526040902054612060908263ffffffff6121a516565b6001600160a01b03831660008181526001602090815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b60008183106120c757816120c9565b825b9392505050565b6001600160a01b0382166000908152600160205260409020546120f9908263ffffffff611b4116565b6001600160a01b03831660009081526001602052604081209190915554612126908263ffffffff611b4116565b60009081556040805183815290516001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef919081900360200190a35050565b6001600160701b0316600160701b0290565b60006001600160701b0382166001600160e01b0384168161219d57fe5b049392505050565b80820182811015610b5f576040805162461bcd60e51b815260206004820152601460248201527364732d6d6174682d6164642d6f766572666c6f7760601b604482015290519081900360640190fdfe556e697377617056323a20494e53554646494349454e545f4f55545055545f414d4f554e54556e697377617056323a20494e53554646494349454e545f494e5055545f414d4f554e54556e697377617056323a20494e53554646494349454e545f4c4951554944495459556e697377617056323a20494e53554646494349454e545f4c49515549444954595f4255524e4544556e697377617056323a20494e53554646494349454e545f4c49515549444954595f4d494e544544556e6973776170563245524332303a206e6f7420656e6f75676820616c6c6f77616e63652ea265627a7a72315820bf165e44001b5284076af3e56685540f3d6256d9ddeae5a8f22ed60b1397a80064736f6c63430005100032454950373132446f6d61696e28737472696e67206e616d652c737472696e672076657273696f6e2c75696e7432353620636861696e49642c6164647265737320766572696679696e67436f6e747261637429
*/
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释
* pair code (evm): 0x60806040526001600c5534801561001557600080fd5b50604051469080605261240d8239604080519182900360520182208282018252600a8352692ab734b9bbb0b8102b1960b11b6020938401528151808301835260018152603160f81b908401528151808401919091527fbfcc8ef98ffbf7b6c3fec7bf5185b566b9863e35a9d83acd49ad6824b5969738818301527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6606082015260808101949094523060a0808601919091528151808603909101815260c09094019052825192019190912060035550600580546001600160a01b03191633179055612308806101056000396000f3fe608060405234801561001057600080fd5b50600436106101a95760003560e01c80636a627842116100f9578063ba9a7a5611610097578063d21220a711610071578063d21220a714610534578063d505accf1461053c578063dd62ed3e1461058d578063fff6cae9146105bb576101a9565b8063ba9a7a56146104fe578063bc25cf7714610506578063c45a01551461052c576101a9565b80637ecebe00116100d35780637ecebe001461046557806389afcb441461048b57806395d89b41146104ca578063a9059cbb146104d2576101a9565b80636a6278421461041157806370a08231146104375780637464fc3d1461045d576101a9565b806323b872dd116101665780633644e515116101405780633644e515146103cb578063485cc955146103d35780635909c0d5146104015780635a3d549314610409576101a9565b806323b872dd1461036f57806330adf81f146103a5578063313ce567146103ad576101a9565b8063022c0d9f146101ae57806306fdde031461023c5780630902f1ac146102b9578063095ea7b3146102f15780630dfe16811461033157806318160ddd14610355575b600080fd5b61023a600480360360808110156101c457600080fd5b8135916020810135916001600160a01b0360408301351691908101906080810160608201356401000000008111156101fb57600080fd5b82018360208201111561020d57600080fd5b8035906020019184600183028401116401000000008311171561022f57600080fd5b5090925090506105c3565b005b610244610afe565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561027e578181015183820152602001610266565b50505050905090810190601f1680156102ab5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6102c1610b24565b604080516001600160701b03948516815292909316602083015263ffffffff168183015290519081900360600190f35b61031d6004803603604081101561030757600080fd5b506001600160a01b038135169060200135610b4e565b604080519115158252519081900360200190f35b610339610b65565b604080516001600160a01b039092168252519081900360200190f35b61035d610b74565b60408051918252519081900360200190f35b61031d6004803603606081101561038557600080fd5b506001600160a01b03813581169160208101359091169060400135610b7a565b61035d610c76565b6103b5610c9a565b6040805160ff9092168252519081900360200190f35b61035d610c9f565b61023a600480360360408110156103e957600080fd5b506001600160a01b0381358116916020013516610ca5565b61035d610d29565b61035d610d2f565b61035d6004803603602081101561042757600080fd5b50356001600160a01b0316610d35565b61035d6004803603602081101561044d57600080fd5b50356001600160a01b0316611035565b61035d611047565b61035d6004803603602081101561047b57600080fd5b50356001600160a01b031661104d565b6104b1600480360360208110156104a157600080fd5b50356001600160a01b031661105f565b6040805192835260208301919091528051918290030190f35b610244611405565b61031d600480360360408110156104e857600080fd5b506001600160a01b038135169060200135611427565b61035d611434565b61023a6004803603602081101561051c57600080fd5b50356001600160a01b031661143a565b6103396115a5565b6103396115b4565b61023a600480360360e081101561055257600080fd5b506001600160a01b03813581169160208101359091169060408101359060608101359060ff6080820135169060a08101359060c001356115c3565b61035d600480360360408110156105a357600080fd5b506001600160a01b03813581169160200135166117c5565b61023a6117e2565b600c5460011461060e576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55841515806106215750600084115b61065c5760405162461bcd60e51b81526004018080602001828103825260258152602001806121f56025913960400191505060405180910390fd5b600080610667610b24565b5091509150816001600160701b03168710801561068c5750806001600160701b031686105b6106c75760405162461bcd60e51b815260040180806020018281038252602181526020018061223e6021913960400191505060405180910390fd5b60065460075460009182916001600160a01b039182169190811690891682148015906107055750806001600160a01b0316896001600160a01b031614155b61074e576040805162461bcd60e51b8152602060048201526015602482015274556e697377617056323a20494e56414c49445f544f60581b604482015290519081900360640190fd5b8a1561075f5761075f828a8d611944565b891561077057610770818a8c611944565b861561082b57886001600160a01b03166310d1e85c338d8d8c8c6040518663ffffffff1660e01b815260040180866001600160a01b03166001600160a01b03168152602001858152602001848152602001806020018281038252848482818152602001925080828437600081840152601f19601f8201169050808301925050509650505050505050600060405180830381600087803b15801561081257600080fd5b505af1158015610826573d6000803e3d6000fd5b505050505b604080516370a0823160e01b815230600482015290516001600160a01b038416916370a08231916024808301926020929190829003018186803b15801561087157600080fd5b505afa158015610885573d6000803e3d6000fd5b505050506040513d602081101561089b57600080fd5b5051604080516370a0823160e01b815230600482015290519195506001600160a01b038316916370a0823191602480820192602092909190829003018186803b1580156108e757600080fd5b505afa1580156108fb573d6000803e3d6000fd5b505050506040513d602081101561091157600080fd5b5051925060009150506001600160701b0385168a90038311610934576000610943565b89856001600160701b03160383035b9050600089856001600160701b031603831161096057600061096f565b89856001600160701b03160383035b905060008211806109805750600081115b6109bb5760405162461bcd60e51b815260040180806020018281038252602481526020018061221a6024913960400191505060405180910390fd5b60006109ef6109d184600363ffffffff611ade16565b6109e3876103e863ffffffff611ade16565b9063ffffffff611b4116565b90506000610a076109d184600363ffffffff611ade16565b9050610a38620f4240610a2c6001600160701b038b8116908b1663ffffffff611ade16565b9063ffffffff611ade16565b610a48838363ffffffff611ade16565b1015610a8a576040805162461bcd60e51b815260206004820152600c60248201526b556e697377617056323a204b60a01b604482015290519081900360640190fd5b5050610a9884848888611b91565b60408051838152602081018390528082018d9052606081018c905290516001600160a01b038b169133917fd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d8229181900360800190a350506001600c55505050505050505050565b6040518060400160405280600a8152602001692ab734b9bbb0b8102b1960b11b81525081565b6008546001600160701b0380821692600160701b830490911691600160e01b900463ffffffff1690565b6000610b5b338484611d56565b5060015b92915050565b6006546001600160a01b031681565b60005481565b6001600160a01b0383166000908152600260209081526040808320338452909152812054821115610bdc5760405162461bcd60e51b81526004018080602001828103825260258152602001806122af6025913960400191505060405180910390fd5b6001600160a01b038416600090815260026020908152604080832033845290915290205460001914610c61576001600160a01b0384166000908152600260209081526040808320338452909152902054610c3c908363ffffffff611b4116565b6001600160a01b03851660009081526002602090815260408083203384529091529020555b610c6c848484611db8565b5060019392505050565b7f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c981565b601281565b60035481565b6005546001600160a01b03163314610cfb576040805162461bcd60e51b81526020600482015260146024820152732ab734b9bbb0b82b191d102327a92124a22222a760611b604482015290519081900360640190fd5b600680546001600160a01b039384166001600160a01b03199182161790915560078054929093169116179055565b60095481565b600a5481565b6000600c54600114610d82576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c81905580610d92610b24565b50600654604080516370a0823160e01b815230600482015290519395509193506000926001600160a01b03909116916370a08231916024808301926020929190829003018186803b158015610de657600080fd5b505afa158015610dfa573d6000803e3d6000fd5b505050506040513d6020811015610e1057600080fd5b5051600754604080516370a0823160e01b815230600482015290519293506000926001600160a01b03909216916370a0823191602480820192602092909190829003018186803b158015610e6357600080fd5b505afa158015610e77573d6000803e3d6000fd5b505050506040513d6020811015610e8d57600080fd5b505190506000610eac836001600160701b03871663ffffffff611b4116565b90506000610ec9836001600160701b03871663ffffffff611b4116565b90506000610ed78787611e72565b60005490915080610f1457610f006103e86109e3610efb878763ffffffff611ade16565b611fd0565b9850610f0f60006103e8612022565b610f63565b610f606001600160701b038916610f31868463ffffffff611ade16565b81610f3857fe5b046001600160701b038916610f53868563ffffffff611ade16565b81610f5a57fe5b046120b8565b98505b60008911610fa25760405162461bcd60e51b81526004018080602001828103825260288152602001806122876028913960400191505060405180910390fd5b610fac8a8a612022565b610fb886868a8a611b91565b8115610fe857600854610fe4906001600160701b0380821691600160701b90041663ffffffff611ade16565b600b555b6040805185815260208101859052815133927f4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f928290030190a250506001600c5550949695505050505050565b60016020526000908152604090205481565b600b5481565b60046020526000908152604090205481565b600080600c546001146110ad576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c819055806110bd610b24565b50600654600754604080516370a0823160e01b815230600482015290519496509294506001600160a01b039182169391169160009184916370a08231916024808301926020929190829003018186803b15801561111957600080fd5b505afa15801561112d573d6000803e3d6000fd5b505050506040513d602081101561114357600080fd5b5051604080516370a0823160e01b815230600482015290519192506000916001600160a01b038516916370a08231916024808301926020929190829003018186803b15801561119157600080fd5b505afa1580156111a5573d6000803e3d6000fd5b505050506040513d60208110156111bb57600080fd5b5051306000908152600160205260408120549192506111da8888611e72565b600054909150806111f1848763ffffffff611ade16565b816111f857fe5b049a508061120c848663ffffffff611ade16565b8161121357fe5b04995060008b118015611226575060008a115b6112615760405162461bcd60e51b815260040180806020018281038252602881526020018061225f6028913960400191505060405180910390fd5b61126b30846120d0565b611276878d8d611944565b611281868d8c611944565b604080516370a0823160e01b815230600482015290516001600160a01b038916916370a08231916024808301926020929190829003018186803b1580156112c757600080fd5b505afa1580156112db573d6000803e3d6000fd5b505050506040513d60208110156112f157600080fd5b5051604080516370a0823160e01b815230600482015290519196506001600160a01b038816916370a0823191602480820192602092909190829003018186803b15801561133d57600080fd5b505afa158015611351573d6000803e3d6000fd5b505050506040513d602081101561136757600080fd5b5051935061137785858b8b611b91565b81156113a7576008546113a3906001600160701b0380821691600160701b90041663ffffffff611ade16565b600b555b604080518c8152602081018c905281516001600160a01b038f169233927fdccd412f0b1252819cb1fd330b93224ca42612892bb3f4f789976e6d81936496929081900390910190a35050505050505050506001600c81905550915091565b604051806040016040528060068152602001652aa72496ab1960d11b81525081565b6000610b5b338484611db8565b6103e881565b600c54600114611485576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55600654600754600854604080516370a0823160e01b815230600482015290516001600160a01b039485169490931692611534928592879261152f926001600160701b03169185916370a0823191602480820192602092909190829003018186803b1580156114f757600080fd5b505afa15801561150b573d6000803e3d6000fd5b505050506040513d602081101561152157600080fd5b50519063ffffffff611b4116565b611944565b600854604080516370a0823160e01b8152306004820152905161159b928492879261152f92600160701b90046001600160701b0316916001600160a01b038616916370a0823191602480820192602092909190829003018186803b1580156114f757600080fd5b50506001600c5550565b6005546001600160a01b031681565b6007546001600160a01b031681565b4284101561160d576040805162461bcd60e51b8152602060048201526012602482015271155b9a5cddd85c158c8e881156141254915160721b604482015290519081900360640190fd5b6003546001600160a01b0380891660008181526004602090815260408083208054600180820190925582517f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c98186015280840196909652958d166060860152608085018c905260a085019590955260c08085018b90528151808603909101815260e08501825280519083012061190160f01b6101008601526101028501969096526101228085019690965280518085039096018652610142840180825286519683019690962095839052610162840180825286905260ff89166101828501526101a284018890526101c28401879052519193926101e280820193601f1981019281900390910190855afa158015611728573d6000803e3d6000fd5b5050604051601f1901519150506001600160a01b0381161580159061175e5750886001600160a01b0316816001600160a01b0316145b6117af576040805162461bcd60e51b815260206004820152601c60248201527f556e697377617056323a20494e56414c49445f5349474e415455524500000000604482015290519081900360640190fd5b6117ba898989611d56565b505050505050505050565b600260209081526000928352604080842090915290825290205481565b600c5460011461182d576040805162461bcd60e51b8152602060048201526011602482015270155b9a5cddd85c158c8e881313d0d2d151607a1b604482015290519081900360640190fd5b6000600c55600654604080516370a0823160e01b8152306004820152905161193d926001600160a01b0316916370a08231916024808301926020929190829003018186803b15801561187e57600080fd5b505afa158015611892573d6000803e3d6000fd5b505050506040513d60208110156118a857600080fd5b5051600754604080516370a0823160e01b815230600482015290516001600160a01b03909216916370a0823191602480820192602092909190829003018186803b1580156118f557600080fd5b505afa158015611909573d6000803e3d6000fd5b505050506040513d602081101561191f57600080fd5b50516008546001600160701b0380821691600160701b900416611b91565b6001600c55565b604080518082018252601981527f7472616e7366657228616464726573732c75696e74323536290000000000000060209182015281516001600160a01b0385811660248301526044808301869052845180840390910181526064909201845291810180516001600160e01b031663a9059cbb60e01b1781529251815160009460609489169392918291908083835b602083106119f15780518252601f1990920191602091820191016119d2565b6001836020036101000a0380198251168184511680821785525050505050509050019150506000604051808303816000865af19150503d8060008114611a53576040519150601f19603f3d011682016040523d82523d6000602084013e611a58565b606091505b5091509150818015611a86575080511580611a865750808060200190516020811015611a8357600080fd5b50515b611ad7576040805162461bcd60e51b815260206004820152601a60248201527f556e697377617056323a205452414e534645525f4641494c4544000000000000604482015290519081900360640190fd5b5050505050565b6000811580611af957505080820282828281611af657fe5b04145b610b5f576040805162461bcd60e51b815260206004820152601460248201527364732d6d6174682d6d756c2d6f766572666c6f7760601b604482015290519081900360640190fd5b80820382811115610b5f576040805162461bcd60e51b815260206004820152601560248201527464732d6d6174682d7375622d756e646572666c6f7760581b604482015290519081900360640190fd5b6001600160701b038411801590611baf57506001600160701b038311155b611bf6576040805162461bcd60e51b8152602060048201526013602482015272556e697377617056323a204f564552464c4f5760681b604482015290519081900360640190fd5b60085463ffffffff42811691600160e01b90048116820390811615801590611c2657506001600160701b03841615155b8015611c3a57506001600160701b03831615155b15611cab578063ffffffff16611c6885611c538661216e565b6001600160e01b03169063ffffffff61218016565b600980546001600160e01b03929092169290920201905563ffffffff8116611c9384611c538761216e565b600a80546001600160e01b0392909216929092020190555b600880546dffffffffffffffffffffffffffff19166001600160701b03888116919091176dffffffffffffffffffffffffffff60701b1916600160701b8883168102919091176001600160e01b0316600160e01b63ffffffff871602179283905560408051848416815291909304909116602082015281517f1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1929181900390910190a1505050505050565b6001600160a01b03808416600081815260026020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b038316600090815260016020526040902054611de1908263ffffffff611b4116565b6001600160a01b038085166000908152600160205260408082209390935590841681522054611e16908263ffffffff6121a516565b6001600160a01b0380841660008181526001602090815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b600080600560009054906101000a90046001600160a01b03166001600160a01b031663017e7e586040518163ffffffff1660e01b815260040160206040518083038186803b158015611ec357600080fd5b505afa158015611ed7573d6000803e3d6000fd5b505050506040513d6020811015611eed57600080fd5b5051600b546001600160a01b038216158015945091925090611fbc578015611fb7576000611f30610efb6001600160701b0388811690881663ffffffff611ade16565b90506000611f3d83611fd0565b905080821115611fb4576000611f6b611f5c848463ffffffff611b4116565b6000549063ffffffff611ade16565b90506000611f9083611f8486600563ffffffff611ade16565b9063ffffffff6121a516565b90506000818381611f9d57fe5b0490508015611fb057611fb08782612022565b5050505b50505b611fc8565b8015611fc8576000600b555b505092915050565b60006003821115612013575080600160028204015b8181101561200d57809150600281828581611ffc57fe5b04018161200557fe5b049050611fe5565b5061201d565b811561201d575060015b919050565b600054612035908263ffffffff6121a516565b60009081556001600160a01b038316815260016020526040902054612060908263ffffffff6121a516565b6001600160a01b03831660008181526001602090815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b60008183106120c757816120c9565b825b9392505050565b6001600160a01b0382166000908152600160205260409020546120f9908263ffffffff611b4116565b6001600160a01b03831660009081526001602052604081209190915554612126908263ffffffff611b4116565b60009081556040805183815290516001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef919081900360200190a35050565b6001600160701b0316600160701b0290565b60006001600160701b0382166001600160e01b0384168161219d57fe5b049392505050565b80820182811015610b5f576040805162461bcd60e51b815260206004820152601460248201527364732d6d6174682d6164642d6f766572666c6f7760601b604482015290519081900360640190fdfe556e697377617056323a20494e53554646494349454e545f4f55545055545f414d4f554e54556e697377617056323a20494e53554646494349454e545f494e5055545f414d4f554e54556e697377617056323a20494e53554646494349454e545f4c4951554944495459556e697377617056323a20494e53554646494349454e545f4c49515549444954595f4255524e4544556e697377617056323a20494e53554646494349454e545f4c49515549444954595f4d494e544544556e6973776170563245524332303a206e6f7420656e6f75676820616c6c6f77616e63652ea265627a7a72315820bf165e44001b5284076af3e56685540f3d6256d9ddeae5a8f22ed60b1397a80064736f6c63430005100032454950373132446f6d61696e28737472696e67206e616d652c737472696e672076657273696f6e2c75696e7432353620636861696e49642c6164647265737320766572696679696e67436f6e747261637429
*/
pair code hash(python):
0xd0b942f04f5999da18c6933b57c7a558fc3b364ba0e233f5828591ee21b66669
pair code hash(evm):
0xd0b942f04f5999da18c6933b57c7a558fc3b364ba0e233f5828591ee21b66669
pair address of WBTC-USDT(calculated from python):
0x841f5e40d7573fd7fbe5a8a002a10759ad5e3bf3
pair address of WBTC-USDT(from EVM create2):
0x841F5E40d7573FD7FbE5A8A002a10759aD5E3BF3
复制ok, 第一部分暂时分享到这里, 接下来我们继续一起学习吧.作者 mail:star4evar@gmail.com参考资料[1]tony.ho: https://learnblockchain.cn/people/8619[2]Uniswap: https://learnblockchain.cn/article/274本文参与 腾讯云自媒体分享计划,分享自微信公众号。原始发表:2022-04-22,如有侵权请联系 cloudcommunity@tencent.com 删除https网络安全python本文分享自 深入浅出区块链技术 微信公众号,前往查看如有侵权,请联系 cloudcommunity@tencent.com 删除。本文参与 腾讯云自媒体分享计划 ,欢迎热爱写作的你一起参与!https网络安全python评论登录后参与评论0 条评论热度最新登录 后参与评论推荐阅读LV.关注文章0获赞0目录概览Pair 的创建 和 create2 的地址计算:参考资料领券社区专栏文章阅读清单互动问答技术沙龙技术视频团队主页腾讯云TI平台活动自媒体分享计划邀请作者入驻自荐上首页技术竞赛资源技术周刊社区标签开发者手册开发者实验室关于社区规范免责声明联系我们友情链接腾讯云开发者扫码关注腾讯云开发者领取腾讯云代金券热门产品域名注册云服务器区块链服务消息队列网络加速云数据库域名解析云存储视频直播热门推荐人脸识别腾讯会议企业云CDN加速视频通话图像分析MySQL 数据库SSL 证书语音识别更多推荐数据安全负载均衡短信文字识别云点播商标注册小程序开发网站监控数据迁移Copyright © 2013 - 2024 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有 深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档Copyright © 2013 - 2024 Tencent Cloud.All Rights Reserved. 腾讯云 版权所有登录 后参与评论100
区块链-深入理解Uniswap协议 | 登链社区 | 区块链技术社区
区块链-深入理解Uniswap协议 | 登链社区 | 区块链技术社区
文章
问答
讲堂
专栏
集市
更多
提问
发表文章
活动
文档
招聘
发现
Toggle navigation
首页 (current)
文章
问答
讲堂
专栏
活动
招聘
文档
集市
搜索
登录/注册
区块链-深入理解Uniswap协议
Star Li
更新于 2019-11-25 10:36
阅读 14499
最近想换换脑子,看了看Uniswap协议。Uniswap协议是一种通过智能合约实现代币间自动交易的协议。本文介绍Uniswap协议,生态,交易价格以及流动性收益的计算。
最近想换换脑子,看了看Uniswap协议。Uniswap协议是一种通过智能合约实现代币间自动交易的协议。本文介绍Uniswap协议,生态,交易价格以及流动性收益的计算。
## Uniswap协议基本介绍
Uniswap协议通过智能合约实现了代币之间的自动交易。目前,Uniswap协议已经在以太坊上部署,可以实现ETH和代币以及代币和代币的之间交易。Uniswap协议的整个框架如下图所示:
![Uniswap协议](https://img.learnblockchain.cn/2019/11/25/001.jpg!/scale/50)
Uniswap Exchange Factory以及Uniswap Exchange都是由以太坊上的智能合约(vyper语言)实现,相关的代码的github地址如下:
https://github.com/Uniswap/contracts-vyper
通过UniswapExchangeFactory可以创建Uniswap Exchange。每个Uniswap Exchange实现一种代币和ETH之间的交易。
在多种代币都能和ETH交易的前提下,代币和代币之间也能交易,如下图所示:
![代币和代币之间](https://img.learnblockchain.cn/2019/11/25/002.jpg!/scale/50)
两个Exchange,一个提供了ABC和ETH的交易,一个提供了XYZ和ETH的交易。通过Uniswap协议,一个用户可以先通过ABC to ETH Exchange将ABC转成ETH,接着再通过XYZ to ETH Exchage将ETH转换成XYZ。整个过程Uniswap自动完成,从用户的角度来看,ABC代币直接转换成了XYZ代币。
## Uniswap生态
Uniswap交易生态中,除了需要交易的用户外,还有一个特别重要的角色:流动性提供者。对一个Exchange来说,流动性提供者提供了ETH以及相应的Token。只有有足够多的流动性,用户才能顺畅的交易,并能保持价格在合理的范围。
![Uniswap生态](https://img.learnblockchain.cn/2019/11/25/003.jpg!/scale/50)
普通交易用户,通过Exchange,实现一个代币和ETH之间的交换。流动性提供者,同时提供代币和ETH。交易的用户每笔交易需要支付一定的交易费用(0.3%)。这些费用被流动性提供者均分。
## x-y-k做市商模型
Uniswap协议使用的是x-y-k做市商模型,实现x和y之间的自动交易。[Uniswap协议在github上有对x-y-k模型的详细介绍](https://github.com/runtimeverification/verified-smart-contracts/blob/uniswap/uniswap/x-y-k.pdf)
### x-y-k模型
所谓的x-y-k模型,是因为在这个模型下,x*y = k。可以想象成x和y,分别是两种代币的数量。在x和y交易时,在没有交易费用的情况下,x*y永远等于k,不变:
![x-y-k模型](https://img.learnblockchain.cn/2019/11/25/004.jpg!/scale/50)
alpha和beta分别是每次交易的变化量。从上面的公式可以看出,变化前(x*y)和变化后(x‘*y')是相等的。
Uniswap协议在该模型的基础上,引入了交易费用,新的模型计算公式如下:
![模型计算公式](https://img.learnblockchain.cn/2019/11/25/005.jpg!/scale/50)
引入交易费用,增加了rho变量。很容易可以看出,引入交易费用后,x'*y'是比x*y的乘积大。
### 交易价格计算
交易价格的计算分成两种:一种是给定X的数量,计算能买到的Y的数量(Input);一种是给定Y的数量,计算需要的X数量(Output)。
getInputPrice的计算公式如下:
![getInputPrice的计算公式](https://img.learnblockchain.cn/2019/11/25/006.jpg!/scale/50)
也就是说,Delta X的代币能换取Delta Y的其他代币。此时,Y代币的价格为:
![Y代币的价格](https://img.learnblockchain.cn/2019/11/25/007.jpg!/scale/50)
简单的说,买入越多X,alpha越大,价格也越高。如果alpha为1的话(用当前流动性中X总额相等的X代币买入),也只能买差不多流动性中的一半的Y代币。如果把x/y视作当前Exchange的价格的话,一次买入后,价格变化为:
![价格变化](https://img.learnblockchain.cn/2019/11/25/008.jpg!/scale/50)
也就是说,“价格”是随着买卖的比例二次函数变化:
![二次函数变化](https://img.learnblockchain.cn/2019/11/25/009.jpg!/scale/50)
getOutputPrice的计算公式如下:
![getOutputPrice的计算公式](https://img.learnblockchain.cn/2019/11/25/010.jpg!/scale/50)
也就是说,Delta Y的代币能换取Delta X的X代币。此时,Y代币的价格为:
![Y代币的价格](https://img.learnblockchain.cn/2019/11/25/011.jpg!/scale/50)
简单的说,买入越多Y,beta越大,价格也越高。如果beta为1/2的话(买入当前流动性中一半的Y代币),大约需要当前流动性中等量的X代币。getInputPrice和getOutputPrice分别从两种代币角度计算价格,具体的价格是一致的。注意,价格计算公式只区分价格计算的两种方向,并没有制定X,Y具体代表的代币类型。举个例子,如果一个Exchange支持的是ETH和ABC交易,你可以把ETH当作X,ABC当作Y,同样你可以将ABC当作X,ETH当作Y。
### 流动性计算
流动性提供者可以随时增加/删除流动性。Uniswap协议文档,用一个三元组(e, t, l)来代表Exchange的状态,其中e代表ETH的数量,t代表Token的数量,l代表当前流动性总量。
增加流动性(addLiquidity)的计算公式如下:
![增加流动性](https://img.learnblockchain.cn/2019/11/25/012.jpg!/scale/50)
增加流动性,就是增加同等比例的e和t。
删除流动性(removeLiquidity)的计算公式如下:
![删除流动性](https://img.learnblockchain.cn/2019/11/25/013.jpg!/scale/50)
删除流动性,就是依据流动性的占比,等比例的减少e和t。
很容易看出,增加流动性和删除流通性时都是按照x/y的价格计算的。需要指出的是,在智能合约实现时,需要考虑计算的精度。
### 流动性收益计算
到目前为止,我们已经知道,增加流动性和删除流通性都是按照当时x/y的价格计算的。Exchange的买卖会导致x/y的波动。先不考虑交易费用的情况下,可以先将模型退化到x*y=k的情况,看看流动性提供者的收益:
假设初始时,Exchange的代币流通性是e和t,经过一些交易后变成e'和t',e*t=e'*t'。
![流动性收益计算](https://img.learnblockchain.cn/2019/11/25/014.jpg!/scale/50)
假设P'/P=x, 则收益曲线如下:
![收益曲线](https://img.learnblockchain.cn/2019/11/25/015.jpg!/scale/50)
很容易看出,只要x/y有变化,在没有交易费用的情况下,没有盈利可能。最好的情况,e/t不变的话,没有损失。在有交易费用的情况下,只有交易费用足够多,能抵消价格波动的损失的情况下,才有可能盈利。
## 总结
Uniswap协议是一种通过智能合约实现代币间自动交易的协议。Uniswap协议采用x-y-k交易商模型。交易的价格随着交易金额的比例成二次函数变化。流动性提供者在没有交易费用的情况下,没有盈利的可能性。只有足够多的交易费用的情况下,才有可能盈利。
我的公众号**星想法**有很多原创高质量文章,欢迎大家扫码关注。
![公众号-星想法](https://img.learnblockchain.cn/2019/15572190575887.jpg!/scale/10)
最近想换换脑子,看了看Uniswap协议。Uniswap协议是一种通过智能合约实现代币间自动交易的协议。本文介绍Uniswap协议,生态,交易价格以及流动性收益的计算。
Uniswap协议基本介绍
Uniswap协议通过智能合约实现了代币之间的自动交易。目前,Uniswap协议已经在以太坊上部署,可以实现ETH和代币以及代币和代币的之间交易。Uniswap协议的整个框架如下图所示:
Uniswap Exchange Factory以及Uniswap Exchange都是由以太坊上的智能合约(vyper语言)实现,相关的代码的github地址如下:
https://github.com/Uniswap/contracts-vyper
通过UniswapExchangeFactory可以创建Uniswap Exchange。每个Uniswap Exchange实现一种代币和ETH之间的交易。
在多种代币都能和ETH交易的前提下,代币和代币之间也能交易,如下图所示:
两个Exchange,一个提供了ABC和ETH的交易,一个提供了XYZ和ETH的交易。通过Uniswap协议,一个用户可以先通过ABC to ETH Exchange将ABC转成ETH,接着再通过XYZ to ETH Exchage将ETH转换成XYZ。整个过程Uniswap自动完成,从用户的角度来看,ABC代币直接转换成了XYZ代币。
Uniswap生态
Uniswap交易生态中,除了需要交易的用户外,还有一个特别重要的角色:流动性提供者。对一个Exchange来说,流动性提供者提供了ETH以及相应的Token。只有有足够多的流动性,用户才能顺畅的交易,并能保持价格在合理的范围。
普通交易用户,通过Exchange,实现一个代币和ETH之间的交换。流动性提供者,同时提供代币和ETH。交易的用户每笔交易需要支付一定的交易费用(0.3%)。这些费用被流动性提供者均分。
x-y-k做市商模型
Uniswap协议使用的是x-y-k做市商模型,实现x和y之间的自动交易。Uniswap协议在github上有对x-y-k模型的详细介绍
x-y-k模型
所谓的x-y-k模型,是因为在这个模型下,xy = k。可以想象成x和y,分别是两种代币的数量。在x和y交易时,在没有交易费用的情况下,xy永远等于k,不变:
alpha和beta分别是每次交易的变化量。从上面的公式可以看出,变化前(xy)和变化后(x‘y')是相等的。
Uniswap协议在该模型的基础上,引入了交易费用,新的模型计算公式如下:
引入交易费用,增加了rho变量。很容易可以看出,引入交易费用后,x'y'是比xy的乘积大。
交易价格计算
交易价格的计算分成两种:一种是给定X的数量,计算能买到的Y的数量(Input);一种是给定Y的数量,计算需要的X数量(Output)。
getInputPrice的计算公式如下:
也就是说,Delta X的代币能换取Delta Y的其他代币。此时,Y代币的价格为:
简单的说,买入越多X,alpha越大,价格也越高。如果alpha为1的话(用当前流动性中X总额相等的X代币买入),也只能买差不多流动性中的一半的Y代币。如果把x/y视作当前Exchange的价格的话,一次买入后,价格变化为:
也就是说,“价格”是随着买卖的比例二次函数变化:
getOutputPrice的计算公式如下:
也就是说,Delta Y的代币能换取Delta X的X代币。此时,Y代币的价格为:
简单的说,买入越多Y,beta越大,价格也越高。如果beta为1/2的话(买入当前流动性中一半的Y代币),大约需要当前流动性中等量的X代币。getInputPrice和getOutputPrice分别从两种代币角度计算价格,具体的价格是一致的。注意,价格计算公式只区分价格计算的两种方向,并没有制定X,Y具体代表的代币类型。举个例子,如果一个Exchange支持的是ETH和ABC交易,你可以把ETH当作X,ABC当作Y,同样你可以将ABC当作X,ETH当作Y。
流动性计算
流动性提供者可以随时增加/删除流动性。Uniswap协议文档,用一个三元组(e, t, l)来代表Exchange的状态,其中e代表ETH的数量,t代表Token的数量,l代表当前流动性总量。
增加流动性(addLiquidity)的计算公式如下:
增加流动性,就是增加同等比例的e和t。
删除流动性(removeLiquidity)的计算公式如下:
删除流动性,就是依据流动性的占比,等比例的减少e和t。
很容易看出,增加流动性和删除流通性时都是按照x/y的价格计算的。需要指出的是,在智能合约实现时,需要考虑计算的精度。
流动性收益计算
到目前为止,我们已经知道,增加流动性和删除流通性都是按照当时x/y的价格计算的。Exchange的买卖会导致x/y的波动。先不考虑交易费用的情况下,可以先将模型退化到x*y=k的情况,看看流动性提供者的收益:
假设初始时,Exchange的代币流通性是e和t,经过一些交易后变成e'和t',et=e't'。
假设P'/P=x, 则收益曲线如下:
很容易看出,只要x/y有变化,在没有交易费用的情况下,没有盈利可能。最好的情况,e/t不变的话,没有损失。在有交易费用的情况下,只有交易费用足够多,能抵消价格波动的损失的情况下,才有可能盈利。
总结
Uniswap协议是一种通过智能合约实现代币间自动交易的协议。Uniswap协议采用x-y-k交易商模型。交易的价格随着交易金额的比例成二次函数变化。流动性提供者在没有交易费用的情况下,没有盈利的可能性。只有足够多的交易费用的情况下,才有可能盈利。
我的公众号星想法有很多原创高质量文章,欢迎大家扫码关注。
学分: 22
分类: Uniswap
标签:
Uniswap
点赞 1
收藏 4
分享
Twitter分享
微信扫码分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。
你可能感兴趣的文章
UniswapV3 部署 - - Foundry Edition
233 浏览
uniswap v2
313 浏览
#DEFI# 从 BitShares 到 Uniswap:Dex 发展亲历者的去中心化交易所之旅
551 浏览
对比 Uniswap,一种新的去中心化交易所的流动性算法
973 浏览
以太坊合并一年后的MEV格局
951 浏览
EIP-1153 除了赋能 Uniswap-v4 还能做什么
906 浏览
相关问题
有没有已经部署的合约去查询Uniswap V3 NFT仓位的价值
1 回答
一般在编写套利合约中通过什么能直接读取给定交易对uniswap v2中的价格?
4 回答
uniswap 多跳路由寻找相关
1 回答
Uniswap中的LP token的数值是如何计算出来的(需要详细计算过程)
2 回答
如何获取 Uniswap 代币价格,v2 v3 是否不同
1 回答
复制以太坊钱包地址在 Uniswap 上发生交易的机器人
2 回答
0 条评论
请先 登录 后评论
Star Li
关注
贡献值: 1031
学分: 7950
Trapdoor Tech创始人,前猎豹移动技术总监,香港中文大学访问学者。
文章目录
关于
关于我们
社区公约
学分规则
Github
伙伴们
DeCert
ChainTool
GCC
合作
广告投放
发布课程
联系我们
友情链接
关注社区
Discord
Youtube
B 站
公众号
关注不错过动态
微信群
加入技术圈子
©2024 登链社区 版权所有 |
Powered By Tipask3.5|
粤公网安备 44049102496617号
粤ICP备17140514号
粤B2-20230927
增值电信业务经营许可证
×
发送私信
请将文档链接发给晓娜,我们会尽快安排上架,感谢您的推荐!
发给:
内容:
取消
发送
×
举报此文章
垃圾广告信息:
广告、推广、测试等内容
违规内容:
色情、暴力、血腥、敏感信息等内容
不友善内容:
人身攻击、挑衅辱骂、恶意行为
其他原因:
请补充说明
举报原因:
取消
举报
×
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!
DeFi工具—UNISWAP使用教程 - 知乎
DeFi工具—UNISWAP使用教程 - 知乎首发于DeFi挖矿教程大全切换模式写文章登录/注册DeFi工具—UNISWAP使用教程派派什么是Uniswap?Uniswap是 2018 年 11 月发布在以太坊主网上的去中心化交易所协议,它的创建者是 Hayden Adams。当前Uniswap在DeFi Pluses上拍名第11位,较之前有下降。不同于其它挂单式去中心化交易所,Uniswap采用了全新的AMM自动做市商算法来执行交易的设计。相比于其他交易所,Uniswap 交易所的智能合约设计能够大幅减少 gas 的用量。如何理解Uniswap的自动做市机制?传统的交易所一般是提供一个挂单平台,通过程序(撮合引擎)撮合买卖双方达成交易。它是一个自由买卖的市场,具有买卖意愿的人们自行挂出“买单”和“卖单”,通过交易所“中介”实现双方订单的成交。传统交易所有以下特点:市场上必须要有用户进行挂单,要有一定量的订单(市场深度)。订单必须重叠才能成交,即买价高于或等于卖价。需要将资产存储在交易所。而Uniswap的模型却完全不同。简单来讲,Uniswap的撮合机制基于一个最基本的公式:K=X*Y。其中,X代表一种资产,Y代表与X等值的另一种资产, 而K则是它俩相乘后的一个函数。当K值不变的情况下,X与Y的值就成反比,即X增加,Y就会减小(反之亦然)。而因为资产Y的较少会导致相对市场的溢价,则会吸引用户来进行搬砖套利,从而抹平价差,使系统重归平衡。下面我们就以ETH与MakerDAO的算法稳定币DAI为例,来为大家解释下这个模型(不考虑手续费)。假设当前ETH价格为150USD,DAI的价格与美金挂钩,等于1USD。现在往系统里注入100ETH与15,000 DAI,则K=100*15,000=1,500,000。当用户将2个ETH兑换成DAI后,ETH总量减少为98,则此时DAI的总量=1,500,000/98=15,306。而因此增加的306个DAI就等于2个ETH的价格,即1ETH=153USD。此时,ETH就会相对市场有溢价,会吸引用户来进行搬砖套利。用户将自己的ETH卖给系统,补充资金池里的ETH,使系统再次回到最初的稳定状态。Uniswap有什么不足之处?在挂单机制下,如果价格达不到挂单者的设置价格,系统是不会成交的。但是在Uniswap的模式下,用户要注意当前资金池的流动性,以免滑点太多,造成资产损失。这里给大家进行了一个简单计算,从表中我们可以看出,随着系统内ETH数量的增加,用户购买单个DAI相对于ETH的成本就会增高。当数量为10ETH时,点差可以达到9.89%。而前段时间UMA上线Uniswap,价格从0.26一路走高到1.2美金的事情也印证了这一点。针对这一点,Balancer做了进一步的修改。但是总体来看,这类AMM机制的DEXes难免会因为流动性问题而出现滑点。不过,对于初创企业来说,此类的DEXes是它们为Token定价并进行销售的福音,Uniswap为他们省去了一大笔要支付给交易所跟做市商的钱。如何在比特派里使用Uniswap?第一步,打开比特派并找到Uniswap安卓版本可以在发现页下的推荐/热门DApp里找到Uniswap。iOS版请更新到bitpie PRO 版本,在发现页的搜索框里输入:https://uniswap.bitpie.com/* Uniswap因为使用的是新加坡域名,所以经常会遇到加载缓慢或者访问出错的情况,而比特派则对此做了优化,当前使用比特派访问Uniswap的体验十分顺畅。第二步,进入DApp主页面,选择要使用的功能图为改版后的Uniswap页面,个人感觉比之前更简洁了。第三步,选择兑换,输入兑换金额及币种,选择浮点后进行兑换这里以ETH兑换DAI为例,并选择接受增加最多0.5%的偏差。设定完毕后,点击“兑换”。待订单确认后,你的钱包地址里就会收到DAI了。从主页面我们可以看到,除了兑换还有“发送”与“资金池”功能。这两个功能都属于V2版本,因为使用时系统会自动切换到V2下。其中,发送与兑换功能大同小异,只是增加了一个收款地址,让你在兑换完成后可以将币发到自己/他人的指定地址,或者ENS域名。而资金池功能,是允许用户通过为系统注入ETH增加系统流动性而获取收益的。下图则展示了如何使用资金池功能为系统注入流动性。以上就是对Uniswap的简介及使用体验了。DeFi市场最近因为Compound的借贷挖矿又火了一把,引来的市场的广泛关注,这对DeFi市场来说是一件好事。同时,DeFi产品因其自身具有去中心化、抗审查、公开透明等特性也正逐渐受到市场的青睐。比特派也会持续关注DeFi生态,为大家带来更多的DeFi产品介绍及体验。本文仅为第三个工具在钱包端的操作教程,不构成任何投资建议。投资有风险,请您谨慎评估。安全、强大钱包就用比特派发布于 2020-09-02 17:08交易所智能合约赞同 243 条评论分享喜欢收藏申请转载文章被以下专栏收录DeFi挖矿教程大全持续更新各类 DeFi 工具使用,微博@比特
手把手教你部署自己的uniswap交易所 - 知乎
手把手教你部署自己的uniswap交易所 - 知乎首发于登链社区切换模式写文章登录/注册手把手教你部署自己的uniswap交易所登链社区区块链技术爱好者的家园本文作者:崔棉大师演示地址: https://fankouzu.github.io/cuiswap/ (请在测试网使用)准备Uniswap合约源码源码结构Uniswap在Github上面开源了全部合约代码,其中包括核心合约,周边合约两部分.Uniswap还开源了前端代码,前端代码使用React开发核心合约周边合约前端代码在Uniswap的核心代码中,主要包含3个合约:工厂合约,配对合约,ERC20合约.其中配对合约继承了ERC20合约,我们可以把它们看作一个合约.工厂合约通过create2方法部署配对合约,所以在部署合约时只需要部署工厂合约.周边合约中包括一些示例代码,例如价格预言机,闪电交换,其中最重要的是路由合约.在周边合约的代码库中,包含两个路由合约:UnsiwapV2Router01,UnsiwapV2Router02.工厂合约和配对合约需要通过路由合约调用才能更好的完成交易所的全部功能,所以我们还要部署路由合约两个合约大部分相同,有小部分不同,如果将两个合约的差异化合并成一个合约,部署的时候将会出现out of gas,所以才被分成了两个合约.常用功能两个合约中都包括,所以我们部署其中任意一个路由合约都可以继承引用调用ERC20合约配对合约工厂合约路由合约从浏览器中下载合约源码如果你对合约代码并不熟悉,也可以跳过上面这部分,接下来我们将从以太坊浏览器中直接拷贝线上版合约源码工厂合约路由合约01[可选]路由合约02部署合约准备部署账户Uniswap的路由合约部署在以太坊的主网和Ropsten,Rinkeby,Goerli,Kovan几个测试网的合约地址都是相同的,这样可以使Uniswap的前端不管切换到任何一个网络,路由地址都不会变.要想实现这个相同地址的部署,我们需要准备一个全新的账户用来部署合约.全新的账户指的是在部署合约之前的nonce值为0.因为合约的地址是根据你的账户地址和nonce值计算出来的,所以在不同网络中,如果nonce值相同,部署出的合约地址也相同.通过助记词生成新账户可以通过我之前录制的视频学习操作方法B站视频油管视频生成好助记词之后,记得用英文助记词,保存好助记词,还有助记词对应的账户地址向新地址转帐ETH部署合约需要的gas费约为0.18个Ether,目前主网可能需要的更多.通过一个已有Ether的账户向新账户转帐.测试网的Ether可以通过每个测试网的水龙头申请到测试币.获取测试币方法转账完成后,将助记词导入到Metamask中准备WETH合约地址在部署路由合约时,构造函数中需要填入工厂合约的地址和WETH合约的地址,由于WETH合约的地址在主网和测试网的地址都不相同,所以需要找到每个网络中WETH合约的地址.WETH合约用于将Eth交换为erc20的Eth,由于Eth不是erc20的token,所以我们必须使用WETH作为交换媒介{
mainnet:'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
ropsten:'0xc778417E063141139Fce010982780140Aa0cD5Ab',
rinkeby:'0xc778417E063141139Fce010982780140Aa0cD5Ab',
goerli:'0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6',
kovan:'0xd0A1E359811322d97991E03f863a0C30C2cF029C'
}申请infuraKey在部署合约之前,我们还需要使用infura作为免费节点,所以需要申请一个infuraKey申请地址:infura.io申请方法使用remix部署合约将工厂合约和路由合约的线上版本导入到remix中,在编译合约的选项中,EVM VERSION选择istanbul,COMPILER CONFIGURATION中选择Enable optimization部署顺序和构造函数部署工厂合约构造函数的参数是一个自己常用的账户地址部署路由合约01 [可选], 02构造函数的参数1是工厂合约的地址参数2 是当前网络中WETH合约的地址,参考前文部署Uniswap前端克隆前端代码在项目目录运行命令:$ git clone https://github.com/Uniswap/uniswap-interface.git安装依赖库在项目目录运行命令:$ cd uniswap-interface
$ yarn安装完成后,可以先测试运行一下,在uniswap-interface目录运行命令$ yarn start如果运行成功,将会打开一个浏览器,同时打开Uniswap的前端界面修改路由地址在Uniswap的前端中以常量的形式定义了Uniswap的路由地址,我们只需要修改路由地址就可以让前端链接到你的路由合约中修改文件: 项目目录/uniswap-interface/src/constants/index.ts 第6行import { AbstractConnector } from '@web3-react/abstract-connector'
import { ChainId, JSBI, Percent, Token, WETH } from '@uniswap/sdk'
import { fortmatic, injected, portis, walletconnect, walletlink } from '../connectors'
export const ROUTER_ADDRESS = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D' //修改成你的路由合约地址
......保存后运行yarn start即可看到效果将代码部署到GitHub Pages创建GitHub项目创建项目的方法就不在这里讲了,不会的同学可以去搜索一下将前端代码添加到GitHub项目仓库首先要删除原先Uniswap项目中的.git目录,在项目目录运行命令:$ cd uniswap-interface
$ rm -rf .git然后初始化git,并将Unsiwap前端代码添加到自己的项目仓库中git init
git remote add origin https://github.com/用户名/项目名.git安装并部署gh-pages我们将通过gh-pages模块将前端代码部署到github.io,在前端代码的目录运行:$ yarn add gh-pages接下来要编译react和部署gh-pages,在前端代码的目录运行:$ yarn build修改前端代码目录中的package.json$ vim package.json
{
"name": "@uniswap/interface",
"description": "Uniswap Interface",
"homepage": "https://用户名.github.io/项目名称",//修改这里
......
// 添加部署的脚本,还是在package.json中
......
"scripts": {
......
"deploy": "gh-pages -d build" //添加这一行
},保存退出之后,在前端代码的目录运行:$ git add .
$ git commit -m "first commit"
$ git push
$ yarn deploy现在在浏览器中打开https://用户名.github.io/项目名称/index.html就可以打开自己的交易所啦.如果不输入地址结尾的index.html在项目刚部署之后会报错,过一段时间就可以不输入了.扩展部署自己的weth可以将以太坊浏览器中的weth源码拷贝下来,自己部署一个属于自己的weth合约可信token列表Uniswap有一个自己的可信token列表,同样被设置在项目目录/uniswap-interface/src/constants/index.ts文件中,在最后一行就是.你可以将这个链接地址的文件拷贝下来,设置成自己需要的可信token地址列表,然后上传到github目录中,再修改index.ts文件中的链接地址,这样就可以让你自己的交易所中拥有自己设置的可信token列表了原文链接:https://learnblockchain.cn...作者主页:https://learnblockchain.cn... ,欢迎阅读作者更多好文登链社区-区块链技术爱好者的家园发布于 2020-09-02 15:19交易所区块链技术赞同 162 条评论分享喜欢收藏申请转载文章被以下专栏收录登链社区高质量区块链技术文
DeFi工具—UNISWAP使用教程 - 知乎
DeFi工具—UNISWAP使用教程 - 知乎首发于DeFi挖矿教程大全切换模式写文章登录/注册DeFi工具—UNISWAP使用教程派派什么是Uniswap?Uniswap是 2018 年 11 月发布在以太坊主网上的去中心化交易所协议,它的创建者是 Hayden Adams。当前Uniswap在DeFi Pluses上拍名第11位,较之前有下降。不同于其它挂单式去中心化交易所,Uniswap采用了全新的AMM自动做市商算法来执行交易的设计。相比于其他交易所,Uniswap 交易所的智能合约设计能够大幅减少 gas 的用量。如何理解Uniswap的自动做市机制?传统的交易所一般是提供一个挂单平台,通过程序(撮合引擎)撮合买卖双方达成交易。它是一个自由买卖的市场,具有买卖意愿的人们自行挂出“买单”和“卖单”,通过交易所“中介”实现双方订单的成交。传统交易所有以下特点:市场上必须要有用户进行挂单,要有一定量的订单(市场深度)。订单必须重叠才能成交,即买价高于或等于卖价。需要将资产存储在交易所。而Uniswap的模型却完全不同。简单来讲,Uniswap的撮合机制基于一个最基本的公式:K=X*Y。其中,X代表一种资产,Y代表与X等值的另一种资产, 而K则是它俩相乘后的一个函数。当K值不变的情况下,X与Y的值就成反比,即X增加,Y就会减小(反之亦然)。而因为资产Y的较少会导致相对市场的溢价,则会吸引用户来进行搬砖套利,从而抹平价差,使系统重归平衡。下面我们就以ETH与MakerDAO的算法稳定币DAI为例,来为大家解释下这个模型(不考虑手续费)。假设当前ETH价格为150USD,DAI的价格与美金挂钩,等于1USD。现在往系统里注入100ETH与15,000 DAI,则K=100*15,000=1,500,000。当用户将2个ETH兑换成DAI后,ETH总量减少为98,则此时DAI的总量=1,500,000/98=15,306。而因此增加的306个DAI就等于2个ETH的价格,即1ETH=153USD。此时,ETH就会相对市场有溢价,会吸引用户来进行搬砖套利。用户将自己的ETH卖给系统,补充资金池里的ETH,使系统再次回到最初的稳定状态。Uniswap有什么不足之处?在挂单机制下,如果价格达不到挂单者的设置价格,系统是不会成交的。但是在Uniswap的模式下,用户要注意当前资金池的流动性,以免滑点太多,造成资产损失。这里给大家进行了一个简单计算,从表中我们可以看出,随着系统内ETH数量的增加,用户购买单个DAI相对于ETH的成本就会增高。当数量为10ETH时,点差可以达到9.89%。而前段时间UMA上线Uniswap,价格从0.26一路走高到1.2美金的事情也印证了这一点。针对这一点,Balancer做了进一步的修改。但是总体来看,这类AMM机制的DEXes难免会因为流动性问题而出现滑点。不过,对于初创企业来说,此类的DEXes是它们为Token定价并进行销售的福音,Uniswap为他们省去了一大笔要支付给交易所跟做市商的钱。如何在比特派里使用Uniswap?第一步,打开比特派并找到Uniswap安卓版本可以在发现页下的推荐/热门DApp里找到Uniswap。iOS版请更新到bitpie PRO 版本,在发现页的搜索框里输入:https://uniswap.bitpie.com/* Uniswap因为使用的是新加坡域名,所以经常会遇到加载缓慢或者访问出错的情况,而比特派则对此做了优化,当前使用比特派访问Uniswap的体验十分顺畅。第二步,进入DApp主页面,选择要使用的功能图为改版后的Uniswap页面,个人感觉比之前更简洁了。第三步,选择兑换,输入兑换金额及币种,选择浮点后进行兑换这里以ETH兑换DAI为例,并选择接受增加最多0.5%的偏差。设定完毕后,点击“兑换”。待订单确认后,你的钱包地址里就会收到DAI了。从主页面我们可以看到,除了兑换还有“发送”与“资金池”功能。这两个功能都属于V2版本,因为使用时系统会自动切换到V2下。其中,发送与兑换功能大同小异,只是增加了一个收款地址,让你在兑换完成后可以将币发到自己/他人的指定地址,或者ENS域名。而资金池功能,是允许用户通过为系统注入ETH增加系统流动性而获取收益的。下图则展示了如何使用资金池功能为系统注入流动性。以上就是对Uniswap的简介及使用体验了。DeFi市场最近因为Compound的借贷挖矿又火了一把,引来的市场的广泛关注,这对DeFi市场来说是一件好事。同时,DeFi产品因其自身具有去中心化、抗审查、公开透明等特性也正逐渐受到市场的青睐。比特派也会持续关注DeFi生态,为大家带来更多的DeFi产品介绍及体验。本文仅为第三个工具在钱包端的操作教程,不构成任何投资建议。投资有风险,请您谨慎评估。安全、强大钱包就用比特派发布于 2020-09-02 17:08交易所智能合约赞同 243 条评论分享喜欢收藏申请转载文章被以下专栏收录DeFi挖矿教程大全持续更新各类 DeFi 工具使用,微博@比特
DeFi工具—UNISWAP使用教程 - 知乎
DeFi工具—UNISWAP使用教程 - 知乎首发于DeFi挖矿教程大全切换模式写文章登录/注册DeFi工具—UNISWAP使用教程派派什么是Uniswap?Uniswap是 2018 年 11 月发布在以太坊主网上的去中心化交易所协议,它的创建者是 Hayden Adams。当前Uniswap在DeFi Pluses上拍名第11位,较之前有下降。不同于其它挂单式去中心化交易所,Uniswap采用了全新的AMM自动做市商算法来执行交易的设计。相比于其他交易所,Uniswap 交易所的智能合约设计能够大幅减少 gas 的用量。如何理解Uniswap的自动做市机制?传统的交易所一般是提供一个挂单平台,通过程序(撮合引擎)撮合买卖双方达成交易。它是一个自由买卖的市场,具有买卖意愿的人们自行挂出“买单”和“卖单”,通过交易所“中介”实现双方订单的成交。传统交易所有以下特点:市场上必须要有用户进行挂单,要有一定量的订单(市场深度)。订单必须重叠才能成交,即买价高于或等于卖价。需要将资产存储在交易所。而Uniswap的模型却完全不同。简单来讲,Uniswap的撮合机制基于一个最基本的公式:K=X*Y。其中,X代表一种资产,Y代表与X等值的另一种资产, 而K则是它俩相乘后的一个函数。当K值不变的情况下,X与Y的值就成反比,即X增加,Y就会减小(反之亦然)。而因为资产Y的较少会导致相对市场的溢价,则会吸引用户来进行搬砖套利,从而抹平价差,使系统重归平衡。下面我们就以ETH与MakerDAO的算法稳定币DAI为例,来为大家解释下这个模型(不考虑手续费)。假设当前ETH价格为150USD,DAI的价格与美金挂钩,等于1USD。现在往系统里注入100ETH与15,000 DAI,则K=100*15,000=1,500,000。当用户将2个ETH兑换成DAI后,ETH总量减少为98,则此时DAI的总量=1,500,000/98=15,306。而因此增加的306个DAI就等于2个ETH的价格,即1ETH=153USD。此时,ETH就会相对市场有溢价,会吸引用户来进行搬砖套利。用户将自己的ETH卖给系统,补充资金池里的ETH,使系统再次回到最初的稳定状态。Uniswap有什么不足之处?在挂单机制下,如果价格达不到挂单者的设置价格,系统是不会成交的。但是在Uniswap的模式下,用户要注意当前资金池的流动性,以免滑点太多,造成资产损失。这里给大家进行了一个简单计算,从表中我们可以看出,随着系统内ETH数量的增加,用户购买单个DAI相对于ETH的成本就会增高。当数量为10ETH时,点差可以达到9.89%。而前段时间UMA上线Uniswap,价格从0.26一路走高到1.2美金的事情也印证了这一点。针对这一点,Balancer做了进一步的修改。但是总体来看,这类AMM机制的DEXes难免会因为流动性问题而出现滑点。不过,对于初创企业来说,此类的DEXes是它们为Token定价并进行销售的福音,Uniswap为他们省去了一大笔要支付给交易所跟做市商的钱。如何在比特派里使用Uniswap?第一步,打开比特派并找到Uniswap安卓版本可以在发现页下的推荐/热门DApp里找到Uniswap。iOS版请更新到bitpie PRO 版本,在发现页的搜索框里输入:https://uniswap.bitpie.com/* Uniswap因为使用的是新加坡域名,所以经常会遇到加载缓慢或者访问出错的情况,而比特派则对此做了优化,当前使用比特派访问Uniswap的体验十分顺畅。第二步,进入DApp主页面,选择要使用的功能图为改版后的Uniswap页面,个人感觉比之前更简洁了。第三步,选择兑换,输入兑换金额及币种,选择浮点后进行兑换这里以ETH兑换DAI为例,并选择接受增加最多0.5%的偏差。设定完毕后,点击“兑换”。待订单确认后,你的钱包地址里就会收到DAI了。从主页面我们可以看到,除了兑换还有“发送”与“资金池”功能。这两个功能都属于V2版本,因为使用时系统会自动切换到V2下。其中,发送与兑换功能大同小异,只是增加了一个收款地址,让你在兑换完成后可以将币发到自己/他人的指定地址,或者ENS域名。而资金池功能,是允许用户通过为系统注入ETH增加系统流动性而获取收益的。下图则展示了如何使用资金池功能为系统注入流动性。以上就是对Uniswap的简介及使用体验了。DeFi市场最近因为Compound的借贷挖矿又火了一把,引来的市场的广泛关注,这对DeFi市场来说是一件好事。同时,DeFi产品因其自身具有去中心化、抗审查、公开透明等特性也正逐渐受到市场的青睐。比特派也会持续关注DeFi生态,为大家带来更多的DeFi产品介绍及体验。本文仅为第三个工具在钱包端的操作教程,不构成任何投资建议。投资有风险,请您谨慎评估。安全、强大钱包就用比特派发布于 2020-09-02 17:08交易所智能合约赞同 243 条评论分享喜欢收藏申请转载文章被以下专栏收录DeFi挖矿教程大全持续更新各类 DeFi 工具使用,微博@比特