I created a contract with the following code and deployed it to the goerli testnet at this address 0x7cEd02A72d311d7734dEF293D520752BA6CcFc02
.
Contract example:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract ReturnPriceContract {
mapping (address => AggregatorV3Interface) public priceFeeds;
constructor() {
// LINK/USD price feed on goerli
priceFeeds[0x45442CB17bD3E3C0AeaE92BF425473E582d5e740] = AggregatorV3Interface(0x48731cF7e84dc94C5f84577882c14Be11a5B7456);
}
function getLatestPrice(address currencyAddress) public view returns (uint256) {
AggregatorV3Interface priceFeed = priceFeeds[currencyAddress];
require(address(priceFeed) != address(0), "Price feed not found");
(, int price, , , ) = priceFeed.latestRoundData();
require(price > 0, "Invalid price");
uint256 scaledPrice = uint256(price) * 1e10;
return scaledPrice;
}
}
ethers.js example:
Then I created a quick Node.js script with ethers to fetch the price from getLatestPrice
for the LINK token on goerli, to show the returned value as a string:
const ethers = require("ethers");
const provider = new ethers.providers.JsonRpcProvider(
"https://goerli.infura.io/v3/<api-key>"
);
const abi = [
"function getLatestPrice(address currencyAddress) public view returns (uint256)",
];
// contract address for ReturnPriceContract deployed on goerli
const contractAddress = "0x7cEd02A72d311d7734dEF293D520752BA6CcFc02";
const contract = new ethers.Contract(contractAddress, abi, provider);
async function main() {
const price = await contract.getLatestPrice(
"0x45442CB17bD3E3C0AeaE92BF425473E582d5e740"
);
// this returns at the time of writing "8216626120000000000"
console.log(price.toString());
}
main();
Etherscan Goerli example:
Additionally, I verified the contract 0x7cEd02A72d311d7734dEF293D520752BA6CcFc02
on the goerli testnet - if I read the getLatestPrice
for 0x45442CB17bD3E3C0AeaE92BF425473E582d5e740
on the goerli etherscan contract page, you will see the LINK price returned as uint
on etherscan.

Hope this helps!