14

I'm trying to fetch the price of ETH from KyberSwap, using Ethers.js, but I'm receiving the following error:

Error: unknown account #0 (operation="getAddress", code=UNSUPPORTED_OPERATION, version=providers/5.5.3)

I'm connected to an Infura web socket to fetch the data. Here's my script:

const { ethers } = require("hardhat");
const kyberABI = require('./kyberABI.json')

const provider = new ethers.providers.WebSocketProvider(
    "wss://mainnet.infura.io/ws/v3/<project_id>")

const kyberNetworkProxyAddress = "0x818E6FECD516Ecc3849DAf6845e3EC868087B755"
const daiAddress = "0x6b175474e89094c44da98b954eedeac495271d0f"

const kyber = new ethers.Contract(
    kyberNetworkProxyAddress,
    kyberABI.kyberNetworkProxy,
    provider.getSigner(),
);

async function main() {
    // Update eth price from Kyber to reflect current market value
    let curEthPriceUSD
    const updateEthPrice = async () => {
        const results = await kyber.getExpectedRate(
            '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
            daiAddress,
            1
        )
        curEthPriceUSD = results.expectedRate
    }
    await updateEthPrice()
    console.log('Current Ethereum price in USD is: ',
        ethers.utils.formatEther(curEthPriceUSD))
}

main()

How do I fix this error?

4 Answers 4

15

Having the exact same error message, in my case, it was due to the Metamask account not being connected. 2nd line here fixed it:

const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send('eth_requestAccounts', []); // <- this promps user to connect metamask
const signer = provider.getSigner();
13

Found a fix for this in a related issue on Github.

Instead of using provider.getSigner() to initialize the kyber contract, I used a Wallet object and grabbed the signer from there:

const wallet = new ethers.Wallet(ethPrivkey, provider);
const signer = wallet.provider.getSigner(wallet.address);

const kyber = new ethers.Contract(
    kyberNetworkProxyAddress,
    kyberABI.kyberNetworkProxy,
    signer
);
1

PROBABLE ISSUE : The most probable issue here is that your metamask wallet is not connected to the application you are creating.

SOLUTION : To connect your metamask wallet you may go with the following

const connectWallet = async () => {
    const accounts= await window.ethereum.request({method: 'eth_requestAccounts'});
    console.log(accounts)
}

then call the function above.

1
  • Connecting to MetaMast solved it for me, although since I was deploying a contract using Hardhat and not a browser, I used this NPM package to connect.
    – nikojpapa
    Commented Apr 19, 2023 at 19:27
0
provider = new ethers.providers.Web3Provider(window.ethereum)
window.ethereum.enable()
signer = provider.getSigner()

I wasn't calling window.ethereum.enable()

Visit: https://github.com/ethers-io/ethers.js/issues/948#issuecomment-1124501921

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.