Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ out = ".test/artifacts"
cache_path = ".test/cache"
libs = ["lib", "node_modules"]

# Foundry deploy scripts (e.g. public/samples/DataFeeds/Foundry/*.s.sol) are
# meant to run inside a user's Foundry project where forge-std and a src/ layout
# are available. They cannot compile in this repo, so skip them.
skip = ["*/DataFeeds/Foundry/*"]

optimizer = true
optimizer_runs = 1_000_000

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

/* solhint-disable no-console */

import {DataConsumerV3} from "../../src/DataFeeds/DataConsumerV3.sol";
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";
import {Script} from "forge-std/Script.sol";
import {console2} from "forge-std/console2.sol";

/**
* THIS IS EXAMPLE CODE THAT USES HARDCODED VALUES FOR CLARITY.
* THIS IS EXAMPLE CODE THAT USES UN-AUDITED CODE.
* DO NOT USE THIS CODE IN PRODUCTION.
*
* Deploy DataConsumerV3 and read the latest BTC/USD price on Sepolia.
*
* Usage:
* forge script script/DataFeeds/DeployAndReadDataConsumerV3.s.sol \
* --rpc-url $SEPOLIA_RPC_URL \
* --broadcast \
* --private-key $PRIVATE_KEY
*/
contract DeployAndReadDataConsumerV3 is Script {
// Sepolia BTC / USD price feed proxy address
address public constant SEPOLIA_BTC_USD = 0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43;

function run() public {
vm.startBroadcast();

// 1. Deploy the consumer contract
DataConsumerV3 consumer = new DataConsumerV3();
console2.log("DataConsumerV3 deployed at:", address(consumer));

vm.stopBroadcast();

// 2. Read the latest price through the consumer
int256 answer = consumer.getChainlinkDataFeedLatestAnswer();
console2.log("Latest answer (raw):", uint256(answer));

// 3. Read decimals directly from the feed to scale the answer
uint8 decimals = AggregatorV3Interface(SEPOLIA_BTC_USD).decimals();
console2.log("Decimals:", decimals);
console2.log("Latest price (scaled): %s", _scale(answer, decimals));
}

function _scale(
int256 answer,
uint8 decimals
) internal pure returns (string memory) {
// Convert the integer answer to a human-readable price string.
// For 8 decimals, 3030914000000 -> "30309.14000000"
uint256 magnitude = uint256(answer);
uint256 base = 10 ** decimals;
uint256 whole = magnitude / base;
uint256 fraction = magnitude % base;
return string.concat(vm.toString(whole), ".", vm.toString(fraction));
}
}
47 changes: 47 additions & 0 deletions public/samples/DataFeeds/Hardhat/DeployAndReadDataConsumerV3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { network } from "hardhat"

/**
* THIS IS EXAMPLE CODE THAT USES HARDCODED VALUES FOR CLARITY.
* THIS IS EXAMPLE CODE THAT USES UN-AUDITED CODE.
* DO NOT USE THIS CODE IN PRODUCTION.
*/

const { ethers } = await network.create()

async function main() {
// 1. Deploy the DataConsumerV3 contract
const consumer = await ethers.deployContract("DataConsumerV3")
await consumer.waitForDeployment()

const consumerAddress = await consumer.getAddress()
console.log("DataConsumerV3 deployed at:", consumerAddress)

// 2. Read the latest price through the consumer contract
const answer = await consumer.getChainlinkDataFeedLatestAnswer()
console.log("Latest answer (raw):", answer.toString())

// 3. Read decimals directly from the feed to scale the answer
// Sepolia BTC / USD price feed proxy address
const feedAddress = "0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43"
const AggregatorV3Interface = [
{
inputs: [],
name: "decimals",
outputs: [{ internalType: "uint8", name: "", type: "uint8" }],
stateMutability: "view",
type: "function",
},
]
const feed = await ethers.getContractAt(AggregatorV3Interface, feedAddress)
const decimals = await feed.decimals()
console.log("Decimals:", decimals)

// 4. Scale and print the human-readable price
const scaled = Number(answer) / 10 ** Number(decimals)
console.log("Latest price (USD):", scaled)
}

main().catch((error) => {
console.error(error)
process.exit(1)
})
Loading
Loading