WETH Deposit Function on a Forked Mainnet?

Viewed 87

Code below works on goerli testnet, however it does not work on a forked ETH mainnet. The code sends 1 ETH to WETH contract, and vice versa. Could someone please help an example of interacting with real contracts on a forked network? In this case swapping 1 ETH to WETH? I am using Hardhat.

const Web3 = require('web3');
const qs = require('qs');
const { ethers, getNamedAccounts, BigNumber } = require("hardhat")
const { networkConfig, developmentChains, DECIMALS } = 
require("../helper-hardhat-config");



async function main() {
    const chainId = network.config.chainId
    const provider = new 
    ethers.providers.JsonRpcProvider(networkConfig[chainId] 
   ["rpc_url"]);
    const accounts = await provider.listAccounts();
  // const signer = provider.getSigner(accounts[0]);

    const weth_abi = '[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},{"name":"wad","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint256"}],"name":"withdraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"deposit","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"guy","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Withdrawal","type":"event"}]'
    const weth_address = networkConfig[chainId]["weth_contract_adr"]
    var weth_contract = new ethers.Contract(weth_address, weth_abi, provider);

    let wallet = new ethers.Wallet(networkConfig[chainId]["private_key"], provider)
    const account = wallet.address
    const signed = weth_contract.connect(wallet);
    var balance = await signed.balanceOf(account);
    console.log(`balance before: ${balance}`);

   // await signed.deposit({value: ethers.utils.parseEther("1")})

    await signed.withdraw(ethers.utils.parseEther("1"));

}

main()

Here is the error I am getting when I try on forked mainnet...

Error: missing response (requestBody="{\"method\":\"eth_accounts\",\"params\":[],\"id\":42,\"jsonrpc\":\"2.0\"}", requestMethod="POST", serverError={"errno":-111,"code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":8545}, url="http://localhost:8545", code=SERVER_ERROR, version=web/5.7.0)
1 Answers

I got it to work, I didn't have the forked node running. Thank you for your help!

Related