From 31f5ef763b7e4ad6cfdd098ed801a0c53b61bb51 Mon Sep 17 00:00:00 2001 From: Matthew Pereira Date: Thu, 2 Nov 2023 14:04:07 -0700 Subject: [PATCH] clean up --- .../hardhat/contracts/AutomationConsumer.sol | 27 ++++++------------- .../hardhat/deploy/01_AggregatorV3Consumer.ts | 4 +-- .../hardhat/deploy/02_FeedRegistryConsumer.ts | 1 - packages/hardhat/deploy/03_VRFConsumer.ts | 4 ++- .../hardhat/deploy/04_AutomationConsumer.ts | 9 +++---- packages/hardhat/tasks/get-token-balance.ts | 9 ++++--- packages/hardhat/tasks/send-link.ts | 17 +++++++++--- 7 files changed, 34 insertions(+), 37 deletions(-) diff --git a/packages/hardhat/contracts/AutomationConsumer.sol b/packages/hardhat/contracts/AutomationConsumer.sol index eacceea..525674c 100644 --- a/packages/hardhat/contracts/AutomationConsumer.sol +++ b/packages/hardhat/contracts/AutomationConsumer.sol @@ -43,6 +43,7 @@ contract AutomationConsumer is AutomationCompatibleInterface, Ownable { bool public s_isCounting = false; uint public s_lastTimestamp; uint public s_upkeepID; + LinkTokenInterface public immutable i_link; AutomationRegistrarInterface public immutable i_registrar; AutomationRegistryBaseInterface public immutable i_registry; @@ -58,8 +59,7 @@ contract AutomationConsumer is AutomationCompatibleInterface, Ownable { } /** This function is called automatically by chainlink keeper nodes - * - * param checkData not used in this example, but it can be set when the Upkeep is registered + * @notice param checkData not used in this example, but it can be set when the Upkeep is registered * @return upkeepNeeded - if true, performUpkeep() will be called * @return performData - data passed to performUpkeep() (can be dynamically computed within checkUpkeep()) */ @@ -79,11 +79,10 @@ contract AutomationConsumer is AutomationCompatibleInterface, Ownable { return (upkeepNeeded, performData); } - /** This function is called by chainlink keeper when the checkupkeep() function returns true - * + /** This function is only called by chainlink keeper when the checkupkeep() function returns true * @param performData returned value from checkUpkeep * - * turns off automated counter at max value to conserve LINK in upkeep subscription + * @notice turns off automated counter at max value to conserve LINK in upkeep subscription */ function performUpkeep(bytes calldata performData) external override { @@ -104,8 +103,7 @@ contract AutomationConsumer is AutomationCompatibleInterface, Ownable { emit UpkeepPerformed(block.timestamp, s_counter); } - /** This function allows the registration of a new upkeep - * + /** This function allows for the registration of a new upkeep * @param params required params for registering an upkeep * * DEBUG NOTES: @@ -126,17 +124,17 @@ contract AutomationConsumer is AutomationCompatibleInterface, Ownable { s_upkeepID = upkeepID; } - // Setters function setUpkeepID(uint256 _upkeepID) public onlyOwner { s_upkeepID = _upkeepID; } - /** + /** Funds the upkeep subscription with more link * @param amount amount of LINK to fund upkeep with */ + function fundUpkeep(uint96 amount) public { - // Transfer LINK from EOA to this contract require( + // Transfer of LINK from EOA to this contract i_link.transferFrom(msg.sender, address(this), amount), "Transfer failed. Ensure this contract is approved to spend LINK" ); @@ -164,16 +162,7 @@ contract AutomationConsumer is AutomationCompatibleInterface, Ownable { emit IntervalUpdated(s_interval); } - // Getters function getUpkeepInfo() public view returns (UpkeepInfo memory) { return i_registry.getUpkeep(s_upkeepID); } - - function getLinkBalance() public view returns (uint256) { - return i_link.balanceOf(address(this)); - } - - function withdrawLink() public onlyOwner { - i_link.transfer(msg.sender, i_link.balanceOf(address(this))); - } } diff --git a/packages/hardhat/deploy/01_AggregatorV3Consumer.ts b/packages/hardhat/deploy/01_AggregatorV3Consumer.ts index ea99d6f..de16b65 100644 --- a/packages/hardhat/deploy/01_AggregatorV3Consumer.ts +++ b/packages/hardhat/deploy/01_AggregatorV3Consumer.ts @@ -4,14 +4,12 @@ import { developmentChains, networkConfig } from "../helper-hardhat-config"; import { network } from "hardhat"; /** Deploy AggregatorV3Consumer contract - * * @param hre HardhatRuntimeEnvironment object. */ const deployAggregatorV3Consumer: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { const { deployer } = await hre.getNamedAccounts(); const { deploy, log } = hre.deployments; - const { ethers } = hre; const chainId = await hre.ethers.provider.getNetwork().then(network => network.chainId); @@ -20,7 +18,7 @@ const deployAggregatorV3Consumer: DeployFunction = async function (hre: HardhatR if (developmentChains.includes(network.name)) { // use mock address if on local network - const MockV3Aggregator = await ethers.getContract("MockV3Aggregator"); + const MockV3Aggregator = await hre.ethers.getContract("MockV3Aggregator"); priceFeedAddress = MockV3Aggregator.address; } else { // use address from helper-hardhat-config if on testnet or live network diff --git a/packages/hardhat/deploy/02_FeedRegistryConsumer.ts b/packages/hardhat/deploy/02_FeedRegistryConsumer.ts index 3a4fb41..5a36fd6 100644 --- a/packages/hardhat/deploy/02_FeedRegistryConsumer.ts +++ b/packages/hardhat/deploy/02_FeedRegistryConsumer.ts @@ -2,7 +2,6 @@ import { HardhatRuntimeEnvironment } from "hardhat/types"; import { DeployFunction } from "hardhat-deploy/types"; /** Deploy FeedRegistryConsumer contract - * * @param hre HardhatRuntimeEnvironment object. */ diff --git a/packages/hardhat/deploy/03_VRFConsumer.ts b/packages/hardhat/deploy/03_VRFConsumer.ts index 61924d0..23f0538 100644 --- a/packages/hardhat/deploy/03_VRFConsumer.ts +++ b/packages/hardhat/deploy/03_VRFConsumer.ts @@ -4,7 +4,9 @@ import { networkConfig } from "../helper-hardhat-config"; import { getTokenBalance, sendLink } from "../tasks"; /** Deploy VRFConsumer contract - * also funds contract with LINK if its a fresh deployment + * @param hre HardhatRuntimeEnvironment object. + * + * @notice funds contract with LINK if fresh deployment */ const deployVRFConsumer: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { diff --git a/packages/hardhat/deploy/04_AutomationConsumer.ts b/packages/hardhat/deploy/04_AutomationConsumer.ts index f034451..3a0b47c 100644 --- a/packages/hardhat/deploy/04_AutomationConsumer.ts +++ b/packages/hardhat/deploy/04_AutomationConsumer.ts @@ -5,16 +5,13 @@ import { networkConfig } from "../helper-hardhat-config"; // import LinkTokenABI from "@chainlink/contracts/abi/v0.8/LinkToken.json"; // import { approveAndTransfer } from "../scripts/approveAndTransfer"; -/** 1. Deploys the "AutomationConsumer" contract - * 2. Send LINK to AutomationConsumer contract - * 3. Register upkeep for AutomationConsumer contract - * +/** Deploys the AutomationConsumer contract * @param hre HardhatRuntimeEnvironment object. + * */ const deployAutomationConsumer: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { const { deployer } = await hre.getNamedAccounts(); const { deploy, log } = hre.deployments; - // const { ethers } = hre; log("------------------------------------"); const chainId = await hre.ethers.provider.getNetwork().then(network => network.chainId); @@ -40,7 +37,7 @@ const deployAutomationConsumer: DeployFunction = async function (hre: HardhatRun */ // if (linkTokenAddress) { - // const [signer] = await ethers.getSigners(); + // const [signer] = await ethers.getSigners(); // const AutomationConsumer = await ethers.getContract("AutomationConsumer", signer); // const fundAmount = "5"; diff --git a/packages/hardhat/tasks/get-token-balance.ts b/packages/hardhat/tasks/get-token-balance.ts index aacf670..3fe8abd 100644 --- a/packages/hardhat/tasks/get-token-balance.ts +++ b/packages/hardhat/tasks/get-token-balance.ts @@ -11,14 +11,15 @@ import { HardhatRuntimeEnvironment } from "hardhat/types"; */ export async function getTokenBalance(hre: HardhatRuntimeEnvironment, accountAddress: string, tokenAddress: string) { - const { ethers } = hre; - const { provider } = ethers; + if (hre.network.name !== "sepolia") { + throw new Error("This script is only configured for sepolia network"); + } - const tokenContract = new ethers.Contract(tokenAddress, ERC20_ABI, provider); + const tokenContract = new hre.ethers.Contract(tokenAddress, ERC20_ABI, hre.ethers.provider); const decimals = await tokenContract.decimals(); const symbol = await tokenContract.symbol(); const rawBalance = await tokenContract.balanceOf(accountAddress); - const formattedBalance = ethers.utils.formatUnits(rawBalance, decimals); + const formattedBalance = hre.ethers.utils.formatUnits(rawBalance, decimals); // prettier-ignore console.log((`Address: ${accountAddress} has ${formattedBalance} ${symbol}`)); diff --git a/packages/hardhat/tasks/send-link.ts b/packages/hardhat/tasks/send-link.ts index 237c5ca..a3addb5 100644 --- a/packages/hardhat/tasks/send-link.ts +++ b/packages/hardhat/tasks/send-link.ts @@ -4,10 +4,10 @@ import { HardhatRuntimeEnvironment } from "hardhat/types"; import { networkConfig } from "../helper-hardhat-config"; /** Fund a specified address with specified amount of LINK - * * @param recipientAddress who receives the LINK * @param amount human readable amount of LINK to send * + * @notice gas fees are boosted since this is only for sepolia network */ export async function sendLink(hre: HardhatRuntimeEnvironment, recipientAddress: string, amount: number) { @@ -24,9 +24,20 @@ export async function sendLink(hre: HardhatRuntimeEnvironment, recipientAddress: const decimals = await linkTokenContract.decimals(); const parsedAmount = hre.ethers.utils.parseUnits(amount.toString(), decimals); + // Boosting gas fees for speedy sepolia transactions + const { maxFeePerGas, maxPriorityFeePerGas } = await hre.ethers.provider.getFeeData(); + if (!maxFeePerGas || !maxPriorityFeePerGas) { + throw new Error("Failed to fetch gas fee data"); + } + const boost = hre.ethers.utils.parseUnits("2", "gwei"); + const boostedMaxFeePerGas = maxFeePerGas.add(boost); + const boostedMaxPriorityFeePerGas = maxPriorityFeePerGas.add(boost); + console.log("Sending transfer transaction..."); - // transfer(address to, uint256 amount) - const transferTx = await linkTokenContract.transfer(recipientAddress, parsedAmount); + const transferTx = await linkTokenContract.transfer(recipientAddress, parsedAmount, { + maxFeePerGas: boostedMaxFeePerGas, + maxPriorityFeePerGas: boostedMaxPriorityFeePerGas, + }); console.log("txHash", transferTx.hash); const transferTxReceipt = await transferTx.wait();