I have a contract that has to save the time and price of eth via chainlink. The time works and has no problems. The price, on the other hand, fails to be recorded either with an array or with a mapping. I have tried several solutions, which include push()
.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.6;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
interface EACAggregatorProxy {
function latestAnswer() external view returns (int256);
}
contract oracleLink {
uint256 deadline;
uint256 startTime = startTimes[block.timestamp];
mapping(uint => uint) startTimes;
address public chainLinkETHUSDAddress = 0x9326BFA02ADD2366b30bacB125260Af641031331;
uint public ethPrice = 0;
uint256 price = ethPrice;
mapping(uint => uint) ethPrice;
function priceOnTime() public payable {
deadline = block.timestamp (numberOfSeconds * 1 seconds);
int256 chainLinkEthPrice = EACAggregatorProxy(chainLinkETHUSDAddress).latestAnswer();
ethPrice = uint(chainLinkEthPrice / 100000000);
return ethPrice;
}
}
CodePudding user response:
The chainLinkETHUSDAddress address hardcoded in your source contains a contract only on the Kovan testnet. Meaning, this code works only on this testnet or its forks, and fails on other networks.
If you want to use the returned value in Remix, you need to create a local fork of the Kovan testnet and then connect to this local network using the Environment selectbox in Remix.
CodePudding user response:
Your code is not using the mapping correctly. I mapping is like an array, just more efficient in some ways, but also has some limitations.
so to use your mapping you need to use it as
ethPrice[x] = y;
Where both x and y are an uint (as you specified the mapping uint => uint). So each unique uint maps (refers) to another uint.
ethPrice[x] = uint(chainLinkEthPrice / 100000000);
Where x is an uint you use to lookup the value later on with.