最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read properties of undefined (reading 'toHexString') in ether.js - Stack Overflow

programmeradmin8浏览0评论

Please don't judge, i've no idea what to do and how to do :)

My code: ether.js ( ^5.6.0)

import { ThirdwebSDK } from '@3rdweb/sdk'
import { ethers } from 'ethers'

const sdk = new ThirdwebSDK(
  new ethers.Wallet(
    process.env.METAMASK_PRIVATE_KEY,
    ethers.getDefaultProvider(
      '/'
    )
  ) 
)

bug

Please don't judge, i've no idea what to do and how to do :)

My code: ether.js ( ^5.6.0)

import { ThirdwebSDK } from '@3rdweb/sdk'
import { ethers } from 'ethers'

const sdk = new ThirdwebSDK(
  new ethers.Wallet(
    process.env.METAMASK_PRIVATE_KEY,
    ethers.getDefaultProvider(
      'https://rinkeby.infura.io/v3/'
    )
  ) 
)

bug

Share Improve this question edited Sep 6, 2022 at 18:37 Progman 19.6k7 gold badges54 silver badges80 bronze badges asked Mar 13, 2022 at 20:53 poroshporosh 831 gold badge1 silver badge6 bronze badges 0
Add a comment  | 

6 Answers 6

Reset to default 10

If you are using JavaScript and getting this error then You made a mistake while importing dotenv. you are using ".config" as property . Use it like a method like following

require("dotenv").config()

It will work.

Another problem you might have is where your .env is located. Make sure that your .env is in the same folder as your code.

I had a similar issue, I actually missed saving an updated .env file. When I saved the updated .env properly this worked.

You most likely messed up env variable. Check if METAMASK_PRIVATE_KEY is the correct name. Because otherwise it will throw undefined and one of the ethersjs library will try to run hexToString() method on undefined value, thus you get the error.

EDIT:

You might as well forget to include this in your code:

import {} from 'dotenv/config' 

// or if its not ESmodule 

require('dotenv').config()

Absence of this import will throw undefined as well when you access env variable.

I had this problem aswell. I used a constructor for my sol contract that looked as following:

constructor(
        address vrfCoordinatorV2,
        uint256 entranceFee,
        bytes32 gasLane,
        uint64 subscriptionId,
        uint32 callbackGasLimit,
        uint256 interval
    )

After alot of frustration i found out that my arguments for the deploy script wasn't lined up in the same order. This was causing the error:

"Cannot read properties of undefined (reading 'toHexString')"

In my case the arguments needed to be lined up as following:

const args = [
        vrfCoordinatorV2Address,
        networkConfig[chainId]["raffleEntranceFee"],
        networkConfig[chainId]["gasLane"],
        subscriptionId,
        networkConfig[chainId]["callbackGasLimit"],
        networkConfig[chainId]["keepersUpdateInterval"],
    ];

Another option is that you've edited the .env file and forgotten to save it. Hence the script won't see the new entry.

发布评论

评论列表(0)

  1. 暂无评论