Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPereira committed Nov 2, 2023
1 parent 5556349 commit 31f5ef7
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 37 deletions.
27 changes: 8 additions & 19 deletions packages/hardhat/contracts/AutomationConsumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
*/
Expand All @@ -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 {
Expand All @@ -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:
Expand All @@ -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"
);
Expand Down Expand Up @@ -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)));
}
}
4 changes: 1 addition & 3 deletions packages/hardhat/deploy/01_AggregatorV3Consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion packages/hardhat/deploy/02_FeedRegistryConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";

/** Deploy FeedRegistryConsumer contract
*
* @param hre HardhatRuntimeEnvironment object.
*/

Expand Down
4 changes: 3 additions & 1 deletion packages/hardhat/deploy/03_VRFConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 3 additions & 6 deletions packages/hardhat/deploy/04_AutomationConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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";
Expand Down
9 changes: 5 additions & 4 deletions packages/hardhat/tasks/get-token-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`));
Expand Down
17 changes: 14 additions & 3 deletions packages/hardhat/tasks/send-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();

Expand Down

0 comments on commit 31f5ef7

Please sign in to comment.