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