Jasperan
Jasperan

Reputation: 3688

Ethers.js "Error: unknown account #0 (operation="getAddress", code=UNSUPPORTED_OPERATION)"

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?

Upvotes: 14

Views: 16446

Answers (4)

Abhigyan
Abhigyan

Reputation: 385

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.

Upvotes: 1

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

Upvotes: 0

Salem Ouerdani
Salem Ouerdani

Reputation: 7886

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();

Upvotes: 15

Jasperan
Jasperan

Reputation: 3688

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
);

Upvotes: 13

Related Questions