|
| 1 | +const { ethers } = require("ethers") |
| 2 | +const Ganache = require("ganache-core") |
| 3 | + |
| 4 | +const MAINNET_NODE_URL = "<insert mainnet node url here>" |
| 5 | +const PRIV_KEY = "<insert ANY private key here>" |
| 6 | + |
| 7 | +const startChain = async () => { |
| 8 | + const ganache = Ganache.provider({ |
| 9 | + fork: MAINNET_NODE_URL, |
| 10 | + network_id: 1, |
| 11 | + accounts: [ |
| 12 | + { |
| 13 | + secretKey: PRIV_KEY, |
| 14 | + balance: ethers.utils.hexlify(ethers.utils.parseEther("1000")), |
| 15 | + }, |
| 16 | + ], |
| 17 | + }) |
| 18 | + |
| 19 | + const provider = new ethers.providers.Web3Provider(ganache) |
| 20 | + const wallet = new ethers.Wallet(PRIV_KEY, provider) |
| 21 | + |
| 22 | + return wallet |
| 23 | +} |
| 24 | + |
| 25 | +const erc20 = require("@studydefi/money-legos/erc20").default |
| 26 | + |
| 27 | +describe("do some tests", () => { |
| 28 | + let wallet |
| 29 | + |
| 30 | + beforeAll(async () => { |
| 31 | + wallet = await startChain() |
| 32 | + }) |
| 33 | + |
| 34 | + test("initial DAI balance of 0", async () => { |
| 35 | + const daiContract = new ethers.Contract( |
| 36 | + erc20.dai.address, |
| 37 | + erc20.dai.abi, |
| 38 | + wallet |
| 39 | + ) |
| 40 | + const daiBalanceWei = await daiContract.balanceOf(wallet.address) |
| 41 | + const daiBalance = ethers.utils.formatUnits(daiBalanceWei, 18) |
| 42 | + expect(parseFloat(daiBalance)).toBe(0) |
| 43 | + }) |
| 44 | + |
| 45 | + test("initial ETH balance of 1000 ETH", async () => { |
| 46 | + const ethBalanceWei = await wallet.getBalance() |
| 47 | + const ethBalance = ethers.utils.formatEther(ethBalanceWei) |
| 48 | + expect(parseFloat(ethBalance)).toBe(1000) |
| 49 | + }) |
| 50 | +}) |
0 commit comments