Skip to content

Commit

Permalink
add mock contracts and start work on price feed page
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPereira committed Oct 11, 2023
1 parent 5b2c68d commit 5ca024e
Show file tree
Hide file tree
Showing 17 changed files with 1,504 additions and 179 deletions.
28 changes: 28 additions & 0 deletions packages/hardhat/contracts/PriceFeedConsumer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

// Import the AggregatorV3Interface from the "@chainlink/contracts" package
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";


/**
*
*
*/

contract PriceFeedConsumer {
// Declare state variable of type AggregatorV3Interface
AggregatorV3Interface internal immutable i_priceFeed;

// Create a constructor that accepts the address of the price feed as an argument
constructor(address priceFeedAddress) {
i_priceFeed = AggregatorV3Interface(priceFeedAddress);
}

// Create a function to get the latest price
function getLatestPrice() public view returns (int) {
(, int price, , , ) = i_priceFeed.latestRoundData();
return price;
}
}
87 changes: 0 additions & 87 deletions packages/hardhat/contracts/YourContract.sol

This file was deleted.

10 changes: 10 additions & 0 deletions packages/hardhat/contracts/mocks/MockV3Aggregator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@chainlink/contracts/src/v0.8/tests/MockV3Aggregator.sol";


/**
* @dev Mock contract implementation of AggregatorV3Interface
* allows for testing and debugging on local network
*/
5 changes: 5 additions & 0 deletions packages/hardhat/contracts/mocks/VRFCoordinatorV2Mock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

import "@chainlink/contracts/src/v0.8/mocks/VRFCoordinatorV2Mock.sol";
42 changes: 0 additions & 42 deletions packages/hardhat/deploy/00_deploy_your_contract.ts

This file was deleted.

50 changes: 50 additions & 0 deletions packages/hardhat/deploy/00_mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
import { developmentChains } from "../helper-hardhat-config";
import { network } from "hardhat";

/**
* Deploys mock contracts necessary for testing on local networks
*
* @param hre HardhatRuntimeEnvironment object.
*/

const deployMocks: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployer } = await hre.getNamedAccounts();
const { deploy, log } = hre.deployments;
const { ethers } = hre;

// MockV3Aggregator constructor arguments
const INTIIAL_PRICE = ethers.utils.parseUnits("1579", 8); // sets MockV3Aggregator.latestRoundData().answer
const DECIMALS = "8";

// Chainlink VRF Coordinator constructor arguments
const BASE_FEE = ethers.utils.parseEther("0.25"); // 0.25 is the premium. It costs 0.25 LINK per request
const GAS_PRICE_LINK = 1e9; // calculated value based on the gas price of the chain

if (developmentChains.includes(network.name)) {
log("Local network detected... Deploying mocks...");

log("Deploying MockV3Aggregator...");
await deploy("MockV3Aggregator", {
from: deployer,
log: true,
args: [DECIMALS, INTIIAL_PRICE],
});

log("Deploying VRFCoordinatorV2Mock...");
await deploy("VRFCoordinatorV2Mock", {
from: deployer,
log: true,
args: [BASE_FEE, GAS_PRICE_LINK],
});
log("Mock contracts successfully deployed on local network!");
log("------------------------------------");
}
};

export default deployMocks;

// Tags are useful if you have multiple deploy files and only want to run one of them.
// e.g. yarn deploy --tags YourContract
deployMocks.tags = ["all", "mocks"];
45 changes: 45 additions & 0 deletions packages/hardhat/deploy/01_PriceFeedConsumer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
import { developmentChains, networkConfig } from "../helper-hardhat-config";
import { network } from "hardhat";

/**
* Deploy PriceFeedConsumer contract
*
* @param hre HardhatRuntimeEnvironment object.
*/
const deployPriceFeedConsumer: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployer } = await hre.getNamedAccounts();
const { deploy, log } = hre.deployments;
const { ethers } = hre;

// const chainId = network.config.chainId;
const chainId = await hre.ethers.provider.getNetwork().then(network => network.chainId);
console.log(`The current chain ID is: ${chainId}`);

log("------------------------------------");
let priceFeedAddress: string | undefined;

if (developmentChains.includes(network.name)) {
// use mock address if on local network
const MockV3Aggregator = await ethers.getContract("MockV3Aggregator");
priceFeedAddress = MockV3Aggregator.address;
} else {
// use address from helper-hardhat-config if on testnet or live network
priceFeedAddress = networkConfig[chainId].priceFeeds?.ETH_USD;
}

const args = [priceFeedAddress];
await deploy("PriceFeedConsumer", {
from: deployer,
args: args,
log: true,
autoMine: true,
});

// * HOW TO VERIFY : https://docs.scaffoldeth.io/deploying/deploy-smart-contracts#4-verify-your-smart-contract
};

export default deployPriceFeedConsumer;

deployPriceFeedConsumer.tags = ["price-consumer", "all"];
18 changes: 11 additions & 7 deletions packages/hardhat/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ const etherscanApiKey = process.env.ETHERSCAN_API_KEY || "DNXJA8RX2Q3VZ4URQIWP7Z

const config: HardhatUserConfig = {
solidity: {
version: "0.8.17",
settings: {
optimizer: {
enabled: true,
// https://docs.soliditylang.org/en/latest/using-the-compiler.html#optimizer-options
runs: 200,
compilers: [
{
version: "0.8.17",
settings: {
optimizer: {
enabled: true,
// https://docs.soliditylang.org/en/latest/using-the-compiler.html#optimizer-options
runs: 200,
},
},
},
},
],
},
defaultNetwork: "localhost",
namedAccounts: {
Expand Down
42 changes: 42 additions & 0 deletions packages/hardhat/helper-hardhat-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
interface NetworkConfigEntryTypes {
name?: string;
vrfCoordinatorV2?: string;
subscriptionId?: string;
gasLane?: string;
callbackGasLimit?: string;
interval?: string;
mintFee?: string;
priceFeeds?: {
BTC_USD: string;
ETH_USD: string;
LINK_USD: string;
};
}

const networkConfig: { [key: number]: NetworkConfigEntryTypes } = {
31337: {
name: "hardhat",
gasLane: "0x474e34a077df58807dbe9c96d3c009b23b3c6d0cce433e59bbf5b34f823bc56c",
interval: "30",
callbackGasLimit: "500000", // 500,000 gas
mintFee: "100000000000000000", // 0.1 ETH
},
11155111: {
name: "sepolia",
vrfCoordinatorV2: "0x8103B0A8A00be2DDC778e6e7eaa21791Cd364625",
subscriptionId: "4707", // vrf.chain.link/sepolia/4707
gasLane: "0x474e34a077df58807dbe9c96d3c009b23b3c6d0cce433e59bbf5b34f823bc56c",
callbackGasLimit: "500000", // 500,000
interval: "30",
mintFee: "100000000000000000", // 0.1 ETH
priceFeeds: {
BTC_USD: "0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43",
ETH_USD: "0x694AA1769357215DE4FAC081bf1f309aDC325306",
LINK_USD: "0xc59E3633BAAC79493d908e63626716e204A45EdF",
},
},
};

const developmentChains: string[] = ["hardhat", "localhost"];

export { networkConfig, developmentChains };
1 change: 1 addition & 0 deletions packages/hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"typescript": "^5.1.6"
},
"dependencies": {
"@chainlink/contracts": "^0.7.1",
"@openzeppelin/contracts": "^4.8.1",
"dotenv": "^16.0.3",
"envfile": "^6.18.0",
Expand Down
14 changes: 14 additions & 0 deletions packages/nextjs/components/ExternalLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ArrowTopRightOnSquareIcon } from "@heroicons/react/24/outline";

interface ExternalLinkProps {
href: string;
text: string;
}

export const ExternalLink: React.FC<ExternalLinkProps> = ({ href, text }) => {
return (
<a href={href} className="capitalize text-accent hover:underline" target="_blank" rel="noopener noreferrer">
{text} <ArrowTopRightOnSquareIcon className="h-5 w-5 inline mb-1" />
</a>
);
};
2 changes: 1 addition & 1 deletion packages/nextjs/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const Header = () => {
const navLinks = (
<>
<li>
<NavLink href="/data-feeds">📈 Data Feeds</NavLink>
<NavLink href="/data-feeds">📈 Price Feeds</NavLink>
</li>
<li>
<NavLink href="/automation">🤖 Automation</NavLink>
Expand Down
Loading

0 comments on commit 5ca024e

Please sign in to comment.