浏览代码

完美配置web3

龚成明 3 年之前
父节点
当前提交
7dbc200e4f
共有 4 个文件被更改,包括 35 次插入12 次删除
  1. 9 1
      hardhat.config.ts
  2. 3 1
      package.json
  3. 13 9
      test/Univ3Test.ts
  4. 10 1
      tsconfig.json

+ 9 - 1
hardhat.config.ts

@@ -1,7 +1,8 @@
-import { HardhatUserConfig } from "hardhat/config";
+import { HardhatUserConfig, extendEnvironment } from "hardhat/config";
 import "@nomicfoundation/hardhat-toolbox";
 
 import deployer from './.secret'
+import {HardhatRuntimeEnvironment} from "hardhat/types";
 
 const ETH_RPC = '/ethereum/data/geth.ipc'
 const ETH_W_RPC = 'https://mainnet.ethereumpow.org'
@@ -32,4 +33,11 @@ const config: HardhatUserConfig = {
   }
 };
 
+extendEnvironment((hre: HardhatRuntimeEnvironment) => {
+  const Web3 = require("web3");
+  hre.Web3 = Web3;
+
+  hre.web3 = new Web3(hre.network.provider);
+})
+
 export default config;

+ 3 - 1
package.json

@@ -1,12 +1,14 @@
 {
   "devDependencies": {
     "@nomicfoundation/hardhat-toolbox": "^2.0.0",
+    "@nomiclabs/hardhat-web3": "^2.0.0",
     "hardhat": "^2.11.2",
     "mocha": "^10.0.0",
     "request": "^2.88.2",
     "ts-generator": "^0.1.1",
     "ts-node": "^10.9.1",
     "typechain": "^8.1.0",
-    "typescript": "^4.8.4"
+    "typescript": "^4.8.4",
+    "web3": "^1.8.0"
   }
 }

+ 13 - 9
test/Univ3Test.ts

@@ -1,18 +1,22 @@
-import {ethers} from "hardhat";
-
-import { network } from "hardhat";
+import { web3 } from "hardhat";
+import deployer from '../.secret'
+const hre = require('hardhat')
 
 describe('Univ3 Test', () => {
-
   it('Parse ether test', async () => {
-    console.log(ethers.utils.parseEther('2.123456789'))
+    console.log(web3.utils.toWei('1', 'gwei'))
   })
 
   it('Get block number test', async () => {
-    console.log(await ethers.provider.getBlockNumber())
+    console.log(await web3.eth.getBlockNumber())
+  })
+
+  it('Get balance test', async () => {
+    console.log(hre.network)
+    console.log(deployer)
+
+    // console.log(await web3.eth.getBalance(accountList[0]))
   })
 
-  // it('Get balance test', async () => {
-  //   console.log(ethers.provider.getBalance())
-  // })
+  it('Transfer test')
 })

+ 10 - 1
tsconfig.json

@@ -4,7 +4,16 @@
     "module": "commonjs",
     "strict": true,
     "esModuleInterop": true,
-    "outDir": "dist"
+    "outDir": "dist",
+    "typeRoots": [
+      "./typechain",
+      "./node_modules/@types"
+    ],
+    "types": [
+      "@nomiclabs/hardhat-ethers",
+      "@nomiclabs/hardhat-web3",
+      "@nomiclabs/hardhat-waffle"
+    ]
   },
   "include": [
     "./scripts",