Uniswap在Github上面开源了全部合约代码,其中包括,两部分.Uniswap还开源了前端代码,前端代码使用React开发
Uniswap opened up all the contract codes on Github, including two parts. Uniswap also opened up the front-end code, which was developed using React.
- 核心合约
- 周边合约
- 前端代码
在Uniswap的核心代码中,主要包含3个合约:,,.其中配对合约继承了ERC20合约,我们可以把它们看作一个合约.工厂合约通过方法部署配对合约,所以在部署合约时.
In Uniswap's core code, there are three main contracts:...the pairing contract inherits the ERC20 contract, and we can see them as a contract. The factory contract deploys the pairing through the method, so when the contract is deployed...
周边合约中包括一些示例代码,例如价格预言机,闪电交换,其中最重要的是.在周边合约的代码库中,包含两个:,.工厂合约和配对合约需要通过路由合约调用才能更好的完成交易所的全部功能,所以我们
The adjoining contracts include a number of examples, such as price forecasters, lightning exchanges, the most important of which is... in the surrounding contract code library, there are two:...the factory contract and the pairing contract need to be called by route to better perform all the functions of the exchange, so we...
两个合约大部分相同,有小部分不同,如果将两个合约的差异化合并成一个合约,部署的时候将会出现,所以才被分成了两个合约.常用功能两个合约中都包括,所以我们部署其中任意一个路由合约都可以
The two contracts are mostly the same, there are minor differences, and if the differences between the two contracts are combined into one contract, they will occur at the time of deployment, so they are divided into two contracts. Both of the usual functions are included, so we can deploy any one of the route contracts.
如果你对合约代码并不熟悉,也可以跳过上面这部分,接下来我们将从以太坊浏览器中直接拷贝线上版合约源码
If you're not familiar with the contract code, you can skip this part. We're going to copy the contract code directly from the Etherno browser.
- 工厂合约
- 路由合约
我们可以使用truffle作为部署合约的环境,其他的环境也可以,如果已经安装过truffle可以跳过这一步
We can use truffle as an environment for deployment contracts, and other environments can skip this if they've been installed.
- 初始化目录
- 目录结构:
Uniswap的路由合约部署在以太坊的主网和Ropsten,Rinkeby,Goerli,Kovan几个测试网的,这样可以使Uniswap的前端不管切换到任何一个网络,路由地址都不会变.要想实现这个相同地址的部署,我们需要准备一个用来部署合约.全新的账户指的是在部署合约之前的.因为合约的地址是根据你的账户地址和nonce值计算出来的,所以在不同网络中,如果nonce值相同,部署出的合约地址也相同.
Uniswap's route contract is deployed on the Ethern main network and on the Ropsten, Rinkeby, Goerli, Kovan test network, which makes it possible for Uniswap's front end, regardless of switching to any network, to remain the same route address. To achieve the deployment of this same address, we need to prepare a contract for deployment. The new account refers to the pre-deployment contract. Since the contract's address is based on your account address and the nonce value, the contract address is the same in the different networks, if the nonce's value is the same.
通过助记词生成新账户
可以通过我之前录制的视频学习操作方法
You can learn how to do it through the video I recorded earlier.
- B站视频
- 油管视频
生成好助记词之后,记得用英文助记词,保存好助记词,还有助记词对应的账户地址
When you generate good notes, remember to use English notes, save good notes, and account addresses to which the notes correspond.
向新地址转帐ETH
部署合约需要的gas费约为0.18个Ether,目前主网可能需要的更多.
The cost of the deployment contract for Gas is approximately 0.18 Ether, which may be needed for the main network at this time.
通过一个已有Ether的账户向新账户转帐.测试网的Ether可以通过每个测试网的水龙头申请到测试币.
Transferred to a new account through an account that already exists with Ether. The Ether of the test network can apply for test money through the taps of each test network.
- 获取测试币方法
在部署路由合约时,构造函数中需要填入工厂合约的地址和WETH合约的地址,由于WETH合约的地址在主网和测试网的地址都不相同,所以需要找到每个网络中WETH合约的地址.
At the time of deployment of the route contract, the construction function would have to fill in the address of the factory contract and the address of the WETH contract, and since the addresses of the WETH contract are not the same on the main network and the test network, the address of the WETH contract in each network would need to be found.
WETH合约用于将Eth交换为erc20的Eth,由于Eth不是erc20的token,所以我们必须使用WETH作为交换媒介
The WETH contract is for Eth swapping Eth to erc20. Since Eth is not erc20 token, we have to use WETH as a medium of exchange.
在部署合约之前,我们还需要使用infura作为免费节点,所以需要申请一个infuraKey
We need to use infra as a free node before deployment, so we need to apply for an infrakey.
- 申请地址:infura.io
- 申请方法
安装@truffle/hdwallet-provider模块,用于打开助记词的钱包,在项目目录中运行命令:
Installs the @truffle/hdwallet-provider module to open the wallet for helpwords and to run the command in the project directory:
如果我们需要在每个网络中都部署上Uniswap合约,就需要配置truffle-congif.js,可以将以下代码全部拷贝粘贴到文件中,覆盖原有代码.
If we need to deploy Uniswap contracts in every network, we need to configure truffle-congif.js and paste all of the following codes to the file to overwrite the original code.
然后别忘了修改和
And don't forget to modify and...
在编写truffle的部署脚本之前,先准备一个你的常用账户作为设置交易所手续费收取账户的管理员地址
Prior to the preparation of a deployment script for truffle, prepare an address for your usual account as the administrator for setting up an exchange charge.
然后在项目目录中运行命令,或者用编辑器创建文件migrations/2_deploy_contract.js
Then run the command in the project directory, or create files by editor(s)
在项目目录运行命令:
Run command in project directory:
现在我们就已经将Uniswap的路由合约和工厂合约都部署在所有的网络中了,你可以在控制台的信息中找到两个合约的地址,也可以在以太坊浏览器中找到,在以太坊浏览器中搜索新账户的地址,显示出来的新账户的交易信息中,将会显示两个创建合约的交易,,将路由合约的地址记录下来
Now that we've deployed Uniswap's route and factory contracts to all networks, you can find two contract addresses in the information on the control table, or in the Etherno browser, and search for the new account's address in the Etherno browser, showing the new account's transaction information, show the two transactions that created the contract, and record the route by the address of the contract.
在项目目录运行命令:
Run command in project directory:
在项目目录运行命令:
Run command in project directory:
安装完成后,可以先测试运行一下,在uniswap-interface目录运行命令
Once the installation is complete, you can test the running command at the uniswap-interface directory.
如果运行成功,将会打开一个浏览器,同时打开Uniswap的前端界面
If running successfully, a browser will be opened, along with a front-end interface for Uniswap
在Uniswap的前端中以常量的形式定义了Uniswap的路由地址,我们只需要修改路由地址就可以让前端链接到你的路由合约中
Uniswap's route address is defined in constant form in the front end of Uniswap, and we just need to change the route address to get the front end to your route contract.
修改文件: 第6行
Modify File: Line 6
保存后运行即可看到效果
You can see effects by running after saving
创建GitHub项目
创建项目的方法就不在这里讲了,不会的同学可以去搜索一下
It's not the way to create the project. No classmates can search it.
将前端代码添加到GitHub项目仓库
首先要删除原先Uniswap项目中的.git目录,在项目目录运行命令:
First, delete the.git directory from the original Uniswap project and run the command in the project directory:
然后初始化git,并将Unsiwap前端代码添加到自己的项目仓库中
Then initialize git and add Unsiwap frontend to its own project repository
安装并部署gh-pages
我们将通过gh-pages模块将前端代码部署到github.io,在前端代码的目录运行:
We will deploy the front-end code to github.io through the gh-pages module and run it in the directory of the front-end code:
接下来要编译react和部署gh-pages,在前端代码的目录运行:
Next is the compilation and deployment of the gh-pages, running in the directory of the front-end code:
修改前端代码目录中的package.json
Modify the package.json in the front-end code directory
保存退出之后,在前端代码的目录运行:
Runs the directory of front-end code after saving exit:
现在在浏览器中打开就可以打开自己的交易所啦. 如果不输入地址结尾的index.html在项目刚部署之后会报错,过一段时间就可以不输入了.
Open it in the browser now and open your own exchange. If you don't enter the index.html at the end of the address, you'll miss it after the project's deployment.
可以将以太坊浏览器中的weth源码拷贝下来,自己部署一个属于自己的weth合约
You can copy theweth code from the Taiwan browser and deploy yourself aweth contract of your own.
Uniswap有一个自己的可信token列表,同样被设置在文件中,在最后一行就是.你可以将这个链接地址的文件拷贝下来,设置成自己需要的可信token地址列表,然后上传到github目录中,再修改index.ts文件中的链接地址,这样就可以让你自己的交易所中拥有自己设置的可信token列表了
Uniswap has its own credible token list, which is also set in the file, and in the last line, you can copy the file of this link address and set it up as the credible token list you need, upload it to the github directory, and change the link address in the index.ts file, so you can have your own credible token list in your own exchange.
温馨提示:公众号中链接不可点,请戳“阅读原文”获取
本文作者:崔棉大师
作者主页:
Homepage of author:
https://learnblockchain.cn/people/447
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论