Uniswap V2 合约部署全攻略:Web3 实践指南

UniswapV2合约部署全攻略:Web3实践指南去中心化金融(DeFi)浪潮席卷全球,UniswapV2作为顶尖的自动化做市商(AMM)协议,是Web3开发的必学核心。本教程基于Solidity0.8.30,带你一步步在Hoodi测试网部署UniswapV2核心合约(

Uniswap V2 合约部署全攻略:Web3 实践指南

去中心化金融(DeFi)浪潮席卷全球,Uniswap V2 作为顶尖的自动化做市商(AMM)协议,是 Web3 开发的必学核心。本教程基于 Solidity 0.8.30,带你一步步在 Hoodi 测试网部署 Uniswap V2 核心合约(Factory、Router02、WETH9 等)及自定义代币,并实现流动性添加。无论你是区块链新手还是资深开发者,本文提供清晰的实操步骤和源码,助你快速上手 DeFi 合约开发,开启 Web3 创新之旅!

本教程详细指导如何基于 Solidity 0.8.30 在 Hoodi 测试网部署 Uniswap V2 核心合约(Factory、Router02、WETH9 等)及 AToken、BToken,涵盖合约编译、部署、验证及流动性添加全流程。所有步骤均经过实操验证,配有完整源码(GitHub: https://github.com/qiaopengjun5162/Uniswap-v2-08),助你快速掌握 DeFi 合约开发。适合 Web3 开发者实践,轻松构建去中心化交易所!

实操

前提准备

编译合约

Uniswap-v2-08 on  main [?] on 🐳 v28.2.2 (orbstack) 
➜ forge build
[⠒] Compiling...
[⠒] Compiling 87 files with Solc 0.8.30
[⠒] Solc 0.8.30 finished in 11.92s
Compiler run successful!

第一步:部署 UniswapV2Factory 合约

Uniswap-v2-08 on  main [✘!?] on 🐳 v28.2.2 (orbstack) took 11.5s 
➜ forge build                                                                              
[⠒] Compiling...
No files changed, compilation skipped

Uniswap-v2-08 on  main [✘!?] on 🐳 v28.2.2 (orbstack) 
➜ source .env                                                                              

Uniswap-v2-08 on  main [✘!?] on 🐳 v28.2.2 (orbstack) 
➜ forge script UniswapV2FactoryScript --rpc-url $HOODI_RPC_URL --broadcast --verify -vvvvv 
[⠒] Compiling...
No files changed, compilation skipped
Traces:
  [132] UniswapV2FactoryScript::setUp()
    └─ ← [Stop]

  [2130098] UniswapV2FactoryScript::run()
    ├─ [0] VM::envUint("PRIVATE_KEY") [staticcall]
    │   └─ ← [Return] <env var value>
    ├─ [0] VM::addr(<pk>) [staticcall]
    │   └─ ← [Return] 0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5
    ├─ [0] console::log("Deploying contracts with the account:", 0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5) [staticcall]
    │   └─ ← [Stop]
    ├─ [0] VM::startBroadcast(<pk>)
    │   └─ ← [Return]
    ├─ [2078726] → new UniswapV2Factory@0x79D86a83bFe89eC1dc5F65021f8F66A50Cc443CC
    │   └─ ← [Return] 10271 bytes of code
    ├─ [0] console::log("UniswapV2Factory deployed to:") [staticcall]
    │   └─ ← [Stop]
    ├─ [0] console::log(UniswapV2Factory: [0x79D86a83bFe89eC1dc5F65021f8F66A50Cc443CC]) [staticcall]
    │   └─ ← [Stop]
    ├─ [0] VM::toString(0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5) [staticcall]
    │   └─ ← [Return] "0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5"
    ├─ [0] VM::toString(UniswapV2Factory: [0x79D86a83bFe89eC1dc5F65021f8F66A50Cc443CC]) [staticcall]
    │   └─ ← [Return] "0x79D86a83bFe89eC1dc5F65021f8F66A50Cc443CC"
    ├─ [0] VM::writeJson("{\"deployerAddress\": \"0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5\", \"uniswapv2factory\": \"0x79D86a83bFe89eC1dc5F65021f8F66A50Cc443CC\"}", "deployed_addresses.json")
    │   └─ ← [Return]
    ├─ [0] VM::stopBroadcast()
    │   └─ ← [Return]
    └─ ← [Return]

Script ran successfully.

== Logs ==
  Deploying contracts with the account: 0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5
  UniswapV2Factory deployed to:
  0x79D86a83bFe89eC1dc5F65021f8F66A50Cc443CC

## Setting up 1 EVM.
==========================
Simulated On-chain Traces:

  [2078726] → new UniswapV2Factory@0x79D86a83bFe89eC1dc5F65021f8F66A50Cc443CC
    └─ ← [Return] 10271 bytes of code

==========================

Chain 560048

Estimated gas price: 1.896721648 gwei

Estimated total gas used for script: 2987623

Estimated amount required: 0.005666689220162704 ETH

==========================

##### hoodi
✅  [Success] Hash: 0x4dc741cbbce957709ca3e19da0369440eeb1ba1290156535575da00677fff723
Contract Address: 0x79D86a83bFe89eC1dc5F65021f8F66A50Cc443CC
Block: 586921
Paid: 0.002541952143867928 ETH (2298172 gas * 1.106075674 gwei)

✅ Sequence #1 on hoodi | Total Paid: 0.002541952143867928 ETH (2298172 gas * avg 1.106075674 gwei)

==========================

ONCHAIN EXECUTION COMPLETE & SUCCESSFUL.
##
Start verification for (1) contracts
Start verifying contract `0x79D86a83bFe89eC1dc5F65021f8F66A50Cc443CC` deployed on hoodi
EVM version: cancun
Compiler version: 0.8.30
Optimizations:    200
Constructor args: 000000000000000000000000e91e2df7ce50bca5310b7238f6b1dfcd15566be5

Submitting verification for [src/UniswapV2Factory.sol:UniswapV2Factory] 0x79D86a83bFe89eC1dc5F65021f8F66A50Cc443CC.
Submitted contract for verification:
        Response: `OK`
        GUID: `uiqh5lvi61sd2jqc7iq3swuivgwdj3j4pmjyyz35nmkdgc4vva`
        URL: https://hoodi.etherscan.io/address/0x79d86a83bfe89ec1dc5f65021f8f66a50cc443cc
Contract verification status:
Response: `NOTOK`
Details: `Pending in queue`
Warning: Verification is still pending...; waiting 15 seconds before trying again (7 tries remaining)
Contract verification status:
Response: `OK`
Details: `Pass - Verified`
Contract successfully verified
All (1) contracts were verified!

Transactions saved to: /Users/qiaopengjun/Code/Solidity/Uniswap-v2-08/broadcast/UniswapV2Factory.s.sol/560048/run-latest.json

Sensitive values saved to: /Users/qiaopengjun/Code/Solidity/Uniswap-v2-08/cache/UniswapV2Factory.s.sol/560048/run-latest.json

UniswapV2Factory 合约地址:0x79D86a83bFe89eC1dc5F65021f8F66A50Cc443CC

https://hoodi.etherscan.io/address/0x79d86a83bfe89ec1dc5f65021f8f66a50cc443cc

第二步:部署 WETH 合约

Uniswap-v2-08 on  main [✘!?] on 🐳 v28.2.2 (orbstack) took 10.8s 
➜ mkdir -p ./deployments

Uniswap-v2-08 on  main [✘!?] on 🐳 v28.2.2 (orbstack) 
➜ forge build           
[⠒] Compiling...
No files changed, compilation skipped

Uniswap-v2-08 on  main [✘!?] on 🐳 v28.2.2 (orbstack) 
➜ source .env           

Uniswap-v2-08 on  main [✘!?] on 🐳 v28.2.2 (orbstack) 
➜ forge script WETH9Script --rpc-url $HOODI_RPC_URL --broadcast --verify -vvvvv            
[⠒] Compiling...
No files changed, compilation skipped
Traces:
  [132] WETH9Script::setUp()
    └─ ← [Stop]

  [507780] WETH9Script::run()
    ├─ [0] VM::envUint("PRIVATE_KEY") [staticcall]
    │   └─ ← [Return] <env var value>
    ├─ [0] VM::addr(<pk>) [staticcall]
    │   └─ ← [Return] 0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5
    ├─ [0] console::log("Deploying contracts with the account:", 0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5) [staticcall]
    │   └─ ← [Stop]
    ├─ [0] VM::startBroadcast(<pk>)
    │   └─ ← [Return]
    ├─ [458671] → new WETH9@0x55Ab36d5Ba138478445D31D4d1A316E5D3662F17
    │   └─ ← [Return] 1958 bytes of code
    ├─ [0] console::log("WETH9 deployed to:") [staticcall]
    │   └─ ← [Stop]
    ├─ [0] console::log(WETH9: [0x55Ab36d5Ba138478445D31D4d1A316E5D3662F17]) [staticcall]
    │   └─ ← [Stop]
    ├─ [0] VM::toString(0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5) [staticcall]
    │   └─ ← [Return] "0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5"
    ├─ [0] VM::toString(WETH9: [0x55Ab36d5Ba138478445D31D4d1A316E5D3662F17]) [staticcall]
    │   └─ ← [Return] "0x55Ab36d5Ba138478445D31D4d1A316E5D3662F17"
    ├─ [0] VM::writeJson("{\"deployerAddress\": \"0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5\", \"WETH9\": \"0x55Ab36d5Ba138478445D31D4d1A316E5D3662F17\"}", "./deployments/WETH9.json")
    │   └─ ← [Return]
    ├─ [0] VM::stopBroadcast()
    │   └─ ← [Return]
    └─ ← [Return]

Script ran successfully.

== Logs ==
  Deploying contracts with the account: 0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5
  WETH9 deployed to:
  0x55Ab36d5Ba138478445D31D4d1A316E5D3662F17

## Setting up 1 EVM.
==========================
Simulated On-chain Traces:

  [458671] → new WETH9@0x55Ab36d5Ba138478445D31D4d1A316E5D3662F17
    └─ ← [Return] 1958 bytes of code

==========================

Chain 560048

Estimated gas price: 1.807610766 gwei

Estimated total gas used for script: 711925

Estimated amount required: 0.00128688329458455 ETH

==========================

##### hoodi
✅  [Success] Hash: 0x9550621e7c393f9aace08ff55bbf350729cd8093937c7f883ab7fc5090601ca6
Contract Address: 0x55Ab36d5Ba138478445D31D4d1A316E5D3662F17
Block: 587169
Paid: 0.00057881956577445 ETH (547635 gas * 1.05694407 gwei)

✅ Sequence #1 on hoodi | Total Paid: 0.00057881956577445 ETH (547635 gas * avg 1.05694407 gwei)

==========================

ONCHAIN EXECUTION COMPLETE & SUCCESSFUL.
##
Start verification for (1) contracts
Start verifying contract `0x55Ab36d5Ba138478445D31D4d1A316E5D3662F17` deployed on hoodi
EVM version: cancun
Compiler version: 0.8.30
Optimizations:    200

Submitting verification for [src/WETH9.sol:WETH9] 0x55Ab36d5Ba138478445D31D4d1A316E5D3662F17.
Warning: Could not detect the deployment.; waiting 5 seconds before trying again (4 tries remaining)

Submitting verification for [src/WETH9.sol:WETH9] 0x55Ab36d5Ba138478445D31D4d1A316E5D3662F17.
Warning: Could not detect the deployment.; waiting 5 seconds before trying again (3 tries remaining)

Submitting verification for [src/WETH9.sol:WETH9] 0x55Ab36d5Ba138478445D31D4d1A316E5D3662F17.
Submitted contract for verification:
        Response: `OK`
        GUID: `ihurcwptizd4fatmqmexdbngsfnp44sn394vl3lxgxqjkdjcq3`
        URL: https://hoodi.etherscan.io/address/0x55ab36d5ba138478445d31d4d1a316e5d3662f17
Contract verification status:
Response: `NOTOK`
Details: `Pending in queue`
Warning: Verification is still pending...; waiting 15 seconds before trying again (7 tries remaining)
Contract verification status:
Response: `NOTOK`
Details: `Pending in queue`
Warning: Verification is still pending...; waiting 15 seconds before trying again (6 tries remaining)
Contract verification status:
Response: `OK`
Details: `Pass - Verified`
Contract successfully verified
All (1) contracts were verified!

Transactions saved to: /Users/qiaopengjun/Code/Solidity/Uniswap-v2-08/broadcast/WETH9.s.sol/560048/run-latest.json

Sensitive values saved to: /Users/qiaopengjun/Code/Solidity/Uniswap-v2-08/cache/WETH9.s.sol/560048/run-latest.json

WETH 合约地址:0x55Ab36d5Ba138478445D31D4d1A316E5D3662F17

https://hoodi.etherscan.io/address/0x55ab36d5ba138478445d31d4d1a316e5d3662f17#code

第三步:部署 UniswapV2Router02 合约


Uniswap-v2-08 on  main [✘!?] on 🐳 v28.2.2 (orbstack) took 3.0s 
➜ forge build 
[⠒] Compiling...
No files changed, compilation skipped

Uniswap-v2-08 on  main [✘!?] on 🐳 v28.2.2 (orbstack) 
➜ source .env

Uniswap-v2-08 on  main [✘!?] on 🐳 v28.2.2 (orbstack) 
➜ forge script UniswapV2Router02Script --rpc-url $HOODI_RPC_URL --broadcast --verify -vvvvv        
[⠒] Compiling...
No files changed, compilation skipped
Traces:
  [132] UniswapV2Router02Script::setUp()
    └─ ← [Stop]

  [4317533] UniswapV2Router02Script::run()
    ├─ [0] VM::envAddress("UNISWAPV2FACTORY") [staticcall]
    │   └─ ← [Return] <env var value>
    ├─ [0] VM::envAddress("WETH9") [staticcall]
    │   └─ ← [Return] <env var value>
    ├─ [0] VM::envUint("PRIVATE_KEY") [staticcall]
    │   └─ ← [Return] <env var value>
    ├─ [0] VM::addr(<pk>) [staticcall]
    │   └─ ← [Return] 0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5
    ├─ [0] console::log("Deploying contracts with the account:", 0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5) [staticcall]
    │   └─ ← [Stop]
    ├─ [0] VM::startBroadcast(<pk>)
    │   └─ ← [Return]
    ├─ [4259571] → new UniswapV2Router02@0x9aB453917cfaEee2B9020537703D30B14f0FDfBa
    │   └─ ← [Return] 21269 bytes of code
    ├─ [0] console::log("UniswapV2Router02 deployed to: ", UniswapV2Router02: [0x9aB453917cfaEee2B9020537703D30B14f0FDfBa]) [staticcall]
    │   └─ ← [Stop]
    ├─ [0] console::log(UniswapV2Router02: [0x9aB453917cfaEee2B9020537703D30B14f0FDfBa]) [staticcall]
    │   └─ ← [Stop]
    ├─ [0] VM::toString(0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5) [staticcall]
    │   └─ ← [Return] "0xE91e2DF7cE50BCA5310b7238F6B1Dfcd15566bE5"
    ├─ [0] VM::toString(UniswapV2Router02: [0x9aB453917cfaEee2B9020537703D30B14f0FDfBa]) [staticcall]
    │   └─ ← [Return] "0x9aB453917cfaEee2B902053770...

剩余60%的内容订阅专栏后可查看

点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
寻月隐君
寻月隐君
0xE91e...6bE5
不要放弃,如果你喜欢这件事,就不要放弃。如果你不喜欢,那这也不好,因为一个人不应该做自己不喜欢的事。