Skip to content

Commit

Permalink
impliment AggregatorV3Consumer front end
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPereira committed Oct 12, 2023
1 parent 5ca024e commit 26aea23
Show file tree
Hide file tree
Showing 18 changed files with 418 additions and 272 deletions.
69 changes: 9 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,78 +1,27 @@
# 🏗 Scaffold-ETH 2
# Scaffold Chainlink

<h4 align="center">
<a href="https://docs.scaffoldeth.io">Documentation</a> |
<a href="https://scaffoldeth.io">Website</a>
</h4>
## Getting Started

🧪 An open-source, up-to-date toolkit for building decentralized applications (dapps) on the Ethereum blockchain. It's designed to make it easier for developers to create and deploy smart contracts and build user interfaces that interact with those contracts.

⚙️ Built using NextJS, RainbowKit, Hardhat, Wagmi, and Typescript.

-**Contract Hot Reload**: Your frontend auto-adapts to your smart contract as you edit it.
- 🔥 **Burner Wallet & Local Faucet**: Quickly test your application with a burner wallet and local faucet.
- 🔐 **Integration with Wallet Providers**: Connect to different wallet providers and interact with the Ethereum network.

![Debug Contracts tab](https://github.com/scaffold-eth/scaffold-eth-2/assets/55535804/1171422a-0ce4-4203-bcd4-d2d1941d198b)

## Requirements

Before you begin, you need to install the following tools:

- [Node (v18 LTS)](https://nodejs.org/en/download/)
- Yarn ([v1](https://classic.yarnpkg.com/en/docs/install/) or [v2+](https://yarnpkg.com/getting-started/install))
- [Git](https://git-scm.com/downloads)

## Quickstart

To get started with Scaffold-ETH 2, follow the steps below:

1. Clone this repo & install dependencies
1. Install dependencies

```
git clone https://github.com/scaffold-eth/scaffold-eth-2.git
cd scaffold-eth-2
yarn install
```

2. Run a local network in the first terminal:
2. Deploy the contracts on sepolia network. You will need funds in the deployer account. Visit the [sepolia faucet](https://sepoliafaucet.com/) and set up your .env file in the `packages/hardhat/` folder OR use `yarn generate` and send sepolia ETH to the generated address.

```
yarn chain
yarn deploy --network sepolia
```

This command starts a local Ethereum network using Hardhat. The network runs on your local machine and can be used for testing and development. You can customize the network configuration in `hardhat.config.ts`.

3. On a second terminal, deploy the test contract:

```
yarn deploy
```

This command deploys a test smart contract to the local network. The contract is located in `packages/hardhat/contracts` and can be modified to suit your needs. The `yarn deploy` command uses the deploy script located in `packages/hardhat/deploy` to deploy the contract to the network. You can also customize the deploy script.

4. On a third terminal, start your NextJS app:
3. Start the frontend

```
yarn start
```

Visit your app on: `http://localhost:3000`. You can interact with your smart contract using the contract component or the example ui in the frontend. You can tweak the app config in `packages/nextjs/scaffold.config.ts`.

Run smart contract test with `yarn hardhat:test`

- Edit your smart contract `YourContract.sol` in `packages/hardhat/contracts`
- Edit your frontend in `packages/nextjs/pages`
- Edit your deployment scripts in `packages/hardhat/deploy`

## Documentation

Visit our [docs](https://docs.scaffoldeth.io) to learn how to start building with Scaffold-ETH 2.

To know more about its features, check out our [website](https://scaffoldeth.io).

## Contributing to Scaffold-ETH 2
4. Fund the VRFConsumer and AutomationConsumer contracts with LINK (create subscription?)

We welcome contributions to Scaffold-ETH 2!
## Testing

Please see [CONTRIBUTING.MD](https://github.com/scaffold-eth/scaffold-eth-2/blob/main/CONTRIBUTING.md) for more information and guidelines for contributing to Scaffold-ETH 2.
- For local testing mock contracts have been included and configured in the deploy scripts
46 changes: 46 additions & 0 deletions packages/hardhat/contracts/AggregatorV3Consumer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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";

/**
* @dev the price feed address passed to constructor during deployment determines the asset pair
* @notice https://docs.chain.link/data-feeds/price-feeds/addresses?network=ethereum&page=1
*/
contract PriceFeedConsumer {
// Declare state variable of type AggregatorV3Interface
AggregatorV3Interface internal immutable i_priceFeed;

// Instatiate the state varible using any given price feed address
constructor(address priceFeedAddress) {
i_priceFeed = AggregatorV3Interface(priceFeedAddress);
}

// Get the latest price from the price feed contract
function getLatestPrice() public view returns (int) {
(
/* uint80 roundID */,
int answer,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = i_priceFeed.latestRoundData();
return answer;
}

// The number of decimals in the response
function getDecimals() public view returns (uint8) {
return i_priceFeed.decimals();
}

// The description of the aggregator i.e. "ETH / USD"
function getDescription() public view returns (string memory) {
return i_priceFeed.description();
}

// The version of the aggregator
function getVersion() public view returns (uint256) {
return i_priceFeed.version();
}
}
51 changes: 51 additions & 0 deletions packages/hardhat/contracts/FeedRegistryConsumer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

import "@chainlink/contracts/src/v0.8/interfaces/FeedRegistryInterface.sol";
import "@chainlink/contracts/src/v0.8/Denominations.sol";

/**Contract to consume the Chainlink FeedRegistry
*
* @notice FeedRegistry address only exists on the Ethereum Mainnet
*/
contract FeedRegistryConsumer {

FeedRegistryInterface internal immutable i_registry;

constructor(address registryAddress) {
i_registry = FeedRegistryInterface(registryAddress);
}

/**
* Get the latest price of ETH/USD
*/
function getEthUsdPrice() public view returns (int) {
(
/*uint80 roundID*/,
int price,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = i_registry.latestRoundData(Denominations.ETH, Denominations.USD);
return price;
}

/** General method capable of returning the relative price of any available asset pair
*
* @param base any denomination from the library or a valid ERC20 token address
* @param quote the quote denomination from the Denominations library or a valid ERC20 token address
*/

function getPrice(address base, address quote) public view returns (int) {
// prettier-ignore
(
/*uint80 roundID*/,
int price,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = i_registry.latestRoundData(base, quote);
return price;
}
}
28 changes: 0 additions & 28 deletions packages/hardhat/contracts/PriceFeedConsumer.sol

This file was deleted.

7 changes: 2 additions & 5 deletions packages/hardhat/deploy/00_mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ 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
/** Deploy mock contracts necessary for testing on local networks
*
* @param hre HardhatRuntimeEnvironment object.
*/
Expand Down Expand Up @@ -45,6 +44,4 @@ const deployMocks: DeployFunction = async function (hre: HardhatRuntimeEnvironme

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"];
deployMocks.tags = ["mocks", "all"];
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ const deployPriceFeedConsumer: DeployFunction = async function (hre: HardhatRunt
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;
Expand All @@ -42,4 +40,4 @@ const deployPriceFeedConsumer: DeployFunction = async function (hre: HardhatRunt

export default deployPriceFeedConsumer;

deployPriceFeedConsumer.tags = ["price-consumer", "all"];
deployPriceFeedConsumer.tags = ["aggregator", "all"];
34 changes: 34 additions & 0 deletions packages/hardhat/deploy/02_FeedRegistryConsumer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";

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

const chainId = await hre.ethers.provider.getNetwork().then(network => network.chainId);

log("------------------------------------");

// FeedRegistry only available on ethereum mainnet
if (chainId === 1) {
const registryFeedAddress = "0x47Fb2585D2C56Fe188D0E6ec628a38b74fCeeeDf";
const args = [registryFeedAddress];
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 deployRegistryFeedConsumer;

deployRegistryFeedConsumer.tags = ["registry", "all"];
20 changes: 13 additions & 7 deletions packages/nextjs/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useRouter } from "next/router";
import { Bars3Icon, BugAntIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline";
import { FaucetButton, RainbowKitCustomConnectButton } from "~~/components/scaffold-eth";
import { useOutsideClick } from "~~/hooks/scaffold-eth";
import { getTargetNetwork } from "~~/utils/scaffold-eth";

const NavLink = ({ href, children }: { href: string; children: React.ReactNode }) => {
const router = useRouter();
Expand All @@ -29,6 +30,9 @@ const NavLink = ({ href, children }: { href: string; children: React.ReactNode }
export const Header = () => {
const [isDrawerOpen, setIsDrawerOpen] = useState(false);
const burgerMenuRef = useRef<HTMLDivElement>(null);

const network = getTargetNetwork();

useOutsideClick(
burgerMenuRef,
useCallback(() => setIsDrawerOpen(false), []),
Expand All @@ -37,7 +41,7 @@ export const Header = () => {
const navLinks = (
<>
<li>
<NavLink href="/data-feeds">📈 Price Feeds</NavLink>
<NavLink href="/price-feeds">📈 Price Feeds</NavLink>
</li>
<li>
<NavLink href="/automation">🤖 Automation</NavLink>
Expand All @@ -51,12 +55,14 @@ export const Header = () => {
Debug Contracts
</NavLink>
</li>
<li>
<NavLink href="/blockexplorer">
<MagnifyingGlassIcon className="h-4 w-4" />
Block Explorer
</NavLink>
</li>
{network.id === 31337 && (
<li>
<NavLink href="/blockexplorer">
<MagnifyingGlassIcon className="h-4 w-4" />
Block Explorer
</NavLink>
</li>
)}
</>
);

Expand Down
Loading

0 comments on commit 26aea23

Please sign in to comment.