'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?
Solution 1:[1]
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
);
Solution 2:[2]
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();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Salem Ouerdani |