Skip to content

Commit

Permalink
clear deploys
Browse files Browse the repository at this point in the history
  • Loading branch information
MihRazvan committed Sep 23, 2024
1 parent 37c37f9 commit 42da9f3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 127 deletions.
27 changes: 20 additions & 7 deletions packages/foundry/contracts/Lottery.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,48 @@ Functions */
pragma solidity ^0.8.19;

import {PriceConverter} from "./PriceConverter.sol";
import {AggregatorV3Interface} from "@chainlink/contracts/v0.8/shared/interfaces/AggregatorV3Interface.sol";

contract Lottery {
uint256 private constant MIN_AMOUNT_TO_FUND = 0.1 ether;
using PriceConverter for uint256;

uint256 private constant MIN_AMOUNT_TO_FUND = 50 * 1e18;

address[] private s_participants;
mapping(address => uint256) private s_participansToAmountFunded;
mapping(address => uint256) private s_participantsToAmountFunded;
// just so I can test price converter
uint256 private etherInUsd;

AggregatorV3Interface private s_priceFeed;

error Error_NotEnoughEthFunded();

constructor(AggregatorV3Interface priceFeed) {
s_priceFeed = priceFeed;
}

function getEtherInUsd(uint256 _ethValue) public view returns (uint256) {
return _ethValue.getConversionRate(s_priceFeed);
}

function fundLottery() public payable {
if (msg.value < MIN_AMOUNT_TO_FUND) {
uint256 ethAmountInUsd = msg.value.getConversionRate(s_priceFeed);
if (ethAmountInUsd < MIN_AMOUNT_TO_FUND) {
revert Error_NotEnoughEthFunded();
}
s_participants.push(msg.sender);
s_participansToAmountFunded[msg.sender] += msg.value;
s_participantsToAmountFunded[msg.sender] += msg.value;
}

function pickWinner() private {}

/** Getters */
function getParticipans() public view returns (address[] memory) {
function getParticipants() public view returns (address[] memory) {
return s_participants;
}

function getAmountFundedByParticipant(
address _participant
) public view returns (uint256) {
return s_participansToAmountFunded[_participant];
return s_participantsToAmountFunded[_participant];
}
}
66 changes: 18 additions & 48 deletions packages/foundry/contracts/PriceConverter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,25 @@ pragma solidity ^0.8.19;

import {AggregatorV3Interface} from "@chainlink/contracts/v0.8/shared/interfaces/AggregatorV3Interface.sol";

/**
* Network: Sepolia
* Base: BTC/USD
* Base Address: 0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43
* Quote: EUR/USD
* Quote Address: 0x1a81afB8146aeFfCFc5E50e8479e826E7D55b910
* Decimals: 8
*/

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

contract PriceConverter {
function getDerivedPrice(
address _base,
address _quote,
uint8 _decimals
) public view returns (int256) {
require(
_decimals > uint8(0) && _decimals <= uint8(18),
"Invalid _decimals"
);
int256 decimals = int256(10 ** uint256(_decimals));
(, int256 basePrice, , , ) = AggregatorV3Interface(_base)
.latestRoundData();
uint8 baseDecimals = AggregatorV3Interface(_base).decimals();
basePrice = scalePrice(basePrice, baseDecimals, _decimals);

(, int256 quotePrice, , , ) = AggregatorV3Interface(_quote)
.latestRoundData();
uint8 quoteDecimals = AggregatorV3Interface(_quote).decimals();
quotePrice = scalePrice(quotePrice, quoteDecimals, _decimals);

return (basePrice * decimals) / quotePrice;
library PriceConverter {
function getPrice(
AggregatorV3Interface priceFeed
) internal view returns (uint256) {
(, int256 answer, , , ) = priceFeed.latestRoundData();
// ETH/USD rate in 18 digit
return uint256(answer * 10000000000);
}

function scalePrice(
int256 _price,
uint8 _priceDecimals,
uint8 _decimals
) internal pure returns (int256) {
if (_priceDecimals < _decimals) {
return _price * int256(10 ** uint256(_decimals - _priceDecimals));
} else if (_priceDecimals > _decimals) {
return _price / int256(10 ** uint256(_priceDecimals - _decimals));
}
return _price;
// 1000000000
// call it get fiatConversionRate, since it assumes something about decimals
// It wouldn't work for every aggregator
function getConversionRate(
uint256 ethAmount,
AggregatorV3Interface priceFeed
) internal view returns (uint256) {
uint256 ethPrice = getPrice(priceFeed);
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
// the actual ETH/USD conversation rate, after adjusting the extra 0s.
return ethAmountInUsd;
}
}
18 changes: 5 additions & 13 deletions packages/foundry/script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,18 @@
pragma solidity ^0.8.19;

import "../contracts/Lottery.sol";
import "../contracts/PriceConverter.sol";
import "./DeployHelpers.s.sol";
import {AggregatorV3Interface} from "@chainlink/contracts/v0.8/shared/interfaces/AggregatorV3Interface.sol";

contract DeployScript is ScaffoldETHDeploy {
error InvalidPrivateKey(string);

function run() external {
uint256 deployerPrivateKey = setupLocalhostEnv();
if (deployerPrivateKey == 0) {
revert InvalidPrivateKey(
"You don't have a deployer account. Make sure you have set DEPLOYER_PRIVATE_KEY in .env or use `yarn generate` to generate a new random account"
);
}
vm.startBroadcast(deployerPrivateKey);
vm.startBroadcast();

Lottery Lottery = new Lottery();
console.logString(
string.concat(
"Lottery deployed at: ",
vm.toString(address(Lottery))
)
Lottery Lottery = new Lottery(
AggregatorV3Interface(0x694AA1769357215DE4FAC081bf1f309aDC325306)
);

vm.stopBroadcast();
Expand Down
87 changes: 28 additions & 59 deletions packages/foundry/script/DeployHelpers.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,74 +5,43 @@ import "forge-std/Script.sol";
import "forge-std/Vm.sol";

contract ScaffoldETHDeploy is Script {
error InvalidChain();
error InvalidChain();

struct Deployment {
string name;
address addr;
}

string root;
string path;
Deployment[] public deployments;

function setupLocalhostEnv() internal returns (uint256 localhostPrivateKey) {
if (block.chainid == 31337) {
root = vm.projectRoot();
path = string.concat(root, "/localhost.json");
string memory json = vm.readFile(path);
bytes memory mnemonicBytes = vm.parseJson(json, ".wallet.mnemonic");
string memory mnemonic = abi.decode(mnemonicBytes, (string));
return vm.deriveKey(mnemonic, 0);
} else {
return vm.envUint("DEPLOYER_PRIVATE_KEY");
struct Deployment {
string name;
address addr;
}
}

function exportDeployments() internal {
// fetch already existing contracts
root = vm.projectRoot();
path = string.concat(root, "/deployments/");
string memory chainIdStr = vm.toString(block.chainid);
path = string.concat(path, string.concat(chainIdStr, ".json"));
uint256 public constant ETH_SEPOLIA_CHAIN_ID = 11155111;
uint256 public constant LOCAL_CHAIN_ID = 31337;

string memory jsonWrite;
string root;
string path;
Deployment[] public deployments;

uint256 len = deployments.length;
/**
* This function generates the file containing the contracts Abi definitions.
* These definitions are used to derive the types needed in the custom scaffold-eth hooks, for example.
* This function should be called last.
*/

for (uint256 i = 0; i < len; i++) {
vm.serializeString(
jsonWrite, vm.toString(deployments[i].addr), deployments[i].name
);
}
function exportDeployments() internal {
// fetch already existing contracts
root = vm.projectRoot();
path = string.concat(root, "/deployments/");
string memory chainIdStr = vm.toString(block.chainid);
path = string.concat(path, string.concat(chainIdStr, ".json"));

string memory chainName;

try this.getChain() returns (Chain memory chain) {
chainName = chain.name;
} catch {
chainName = findChainName();
}
jsonWrite = vm.serializeString(jsonWrite, "networkName", chainName);
vm.writeJson(jsonWrite, path);
}
string memory jsonWrite;

function getChain() public returns (Chain memory) {
return getChain(block.chainid);
}
uint256 len = deployments.length;

function findChainName() public returns (string memory) {
uint256 thisChainId = block.chainid;
string[2][] memory allRpcUrls = vm.rpcUrls();
for (uint256 i = 0; i < allRpcUrls.length; i++) {
try vm.createSelectFork(allRpcUrls[i][1]) {
if (block.chainid == thisChainId) {
return allRpcUrls[i][0];
for (uint256 i = 0; i < len; i++) {
vm.serializeString(
jsonWrite,
vm.toString(deployments[i].addr),
deployments[i].name
);
}
} catch {
continue;
}
}
revert InvalidChain();
}
}

0 comments on commit 42da9f3

Please sign in to comment.