Giter Site home page Giter Site logo

01_testuniswapv2's Introduction

Test UniswapV2 Swap function

This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, and a Hardhat Ignition module that deploys that contract.

Try running some of the following tasks:

npx hardhat help
npx hardhat test
REPORT_GAS=true npx hardhat test
npx hardhat node
npx hardhat ignition deploy ./ignition/modules/Lock.ts

SwapExactETHForTokens SwapExactTokensForETH

Test Swap

scripts 目录下的脚本程序,第一个V2代表的是Uniswap的版本,第二个v2代表的是js程序的版本

1.scripts/testUniswapV2v2.ts

使用uniswap/sdk 库实现交换代币的功能,全部swap函数都在MAINNET 上测试过

2.scripts/testUniswapV2v3.ts

使用uniswap/v2-sdk库 实现交换代币的功能,并且在fork MAINNET 链上测试过。 目前已经完成了UniswapV2 所有代币交换函数的测试工作 包含以下交换代币函数:

  • swapExactTokensForTokens 用指定数量的代币交换其他代币
  • swapTokensForExactTokens 使用代币交换指定数量的其他代币
  • swapTokensForExactETH 使用代币交换指定数量的ETH
  • swapExactTokensForETH 使用指定数量的代币交换ETH
  • swapETHForExactTokens 使用ETH交换指定数量的代币
  • swapExactTokensForTokensSupportingFeeOnTransferTokens 使用指定数量的代币交换其他代币,支持转账时扣费
  • swapExactETHForTokensSupportingFeeOnTransferTokens 使用ETH交换指定数量的代币,支持转账时扣费
  • swapExactTokensForETHSupportingFeeOnTransferTokens 使用指定数量的代币交换ETH,支持转账时扣费

3.scripts/uniswapv3/test.ts

测试uniswapv3 代币交换的功能,目前还不完善

npx hardhat run ./scripts/uniswapv3/test.ts

运行测试程序

npx hardhat run ./scripts/testUniswapV2v3.ts

Fork MAINNET

为了测试代币交换功能,需要在本地fork 以太坊主链 --forking 指定以太坊节点 --fork-block-number 指定特定区块

npx hardhat node --fork https://eth-mainnet.g.alchemy.com/v2/<key> --fork-block-number 20145880

配置文件

运行项目需要配置以下参数

ETHERSCAN_API_KEY=
ALCHEMY_API_URL=
ALCHEMY_PRIVATE_KEY=
ETHMAINNET_API_URL=
ETHMAINNET_PRIVATE_KEY=
COINMARKETCAP_API_KEY=
SEPOLIA_API_URL=
SEPOLIA_WS_URL=
SEPOLIA_PRIVATE_KEY=
HOLESKY_API_URL=
HOLESKY_WS_URL=
HOLESKY_PRIVATE_KEY=

UniswapV3

目前ethers.js 版本已经更新到6.13.1 在该版本的基础上实现了单个代币的交换和多跳代币的交换

npx hardhat run ./scripts/uniswapv3_ethersv6/testUniswapV3SingleHopSwap.ts

solidity 版本的代币交换功能

合约代码:
./contract/UniswapV3SingleHopSwap.sol
测试代码:
单跳交换:./test/UniswapV3SingleHopSwap.ts 多跳交换:./test/UniswapV3MultiHopSwap.ts
运行

npx hardhat test ./test/UniswapV3SingleHopSwap.ts 
npx hardhat test ./test/UniswapV3MultiHopSwap.ts 
npx hardhat test ./test/UniswapV2SingleHopSwap.ts

生产环境

1. 修改.env 文件中以下变量
ETHMAINNET_PRIVATE_KEY

2. 修改config.ts 文件中的节点url

3. 修改constant.ts 文件中的CHAINID

合约

contract/UniswapV3FlashLoad.sol

展示闪电贷借币还币以及支付手续费的流程,借哪个币就还哪个币,并且用这个币支付利息  

contract/UniswapV3FlashSwap.sol

目前该合约尚不成熟       
1. 在pool0 中使用Flash loan 借1个WETH  
2. 在pool1 中使用1个WETH购买DAI  
3. 使用DAI还款给pool0  
4. profit = 在pool1购买到的DAI - 必须还给pool0 的DAI  

contract/UniswapV3FlashSwapArbitrage.sol

基于Flash loan 的套利合约,该合约仅仅用于展示,不能用于实盘
该套利程序用的是借DAI,赚DAI的策略方案
1. 从Medium pool 中借500DAI(amountIn),兑换为WETH
2. 在Low pool 中,使用上步骤购买到的WETH 兑换为DAI(buyAmount)
3. 计算盈利
    profit = BuyAMount - amountIn
4. 还款amounIn 数量的DAI给Medium Pool

数据下载

run.py 负责启动recordPoolData.ts 脚本,该脚本负责监控同一个交易对的价差信息
1. 在lib/constant.ts 文件中修改CHAINID为主链
2. 在config.ts 文件中修改rpc.local url
3. 运行程序
    nohup python run.py 1>run.o.log 2>run.e.log&

01_testuniswapv2's People

Contributors

xfunture avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

yosom

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.