Skip to content

Commit

Permalink
did my best
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPereira committed Nov 2, 2023
1 parent 31f5ef7 commit 84c3283
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 55 deletions.
31 changes: 26 additions & 5 deletions packages/hardhat/contracts/AutomationConsumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ contract AutomationConsumer is AutomationCompatibleInterface, Ownable {
uint public s_upkeepID;

LinkTokenInterface public immutable i_link;
AutomationRegistrarInterface public immutable i_registrar;
AutomationRegistryBaseInterface public immutable i_registry;
AutomationRegistrarInterface public i_registrar;
AutomationRegistryBaseInterface public i_registry;

constructor(
LinkTokenInterface link,
Expand Down Expand Up @@ -106,9 +106,6 @@ contract AutomationConsumer is AutomationCompatibleInterface, Ownable {
/** This function allows for the registration of a new upkeep
* @param params required params for registering an upkeep
*
* DEBUG NOTES:
* - this contract successfully approves registrar to spend link
* - UNPREDICTABLE_GAS_LIMIT must be coming from i_registrar.registerUpkeep()
*/

function registerNewUpkeep(
Expand Down Expand Up @@ -162,7 +159,31 @@ contract AutomationConsumer is AutomationCompatibleInterface, Ownable {
emit IntervalUpdated(s_interval);
}

function updateRegistryAddress(
AutomationRegistryBaseInterface _registry
) public onlyOwner {
i_registry = _registry;
}

function updateRegistrarAddress(
AutomationRegistrarInterface _registrar
) public onlyOwner {
i_registrar = _registrar;
}

function withdrawLink() public onlyOwner {
require(
i_link.transfer(msg.sender, i_link.balanceOf(address(this))),
"Unable to withdraw LINK"
);
}

/** 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));
}
}
2 changes: 1 addition & 1 deletion packages/hardhat/deploy/03_VRFConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const deployVRFConsumer: DeployFunction = async function (hre: HardhatRuntimeEnv
if (+VRFConsumerLinkBalance === 0) {
const amount = 5;
console.log(`Funding VRFConsumer contract with ${amount} LINK`);
await sendLink(hre, VRFConsumer.address, amount);
await sendLink(VRFConsumer.address, amount, hre);
}
};

Expand Down
80 changes: 40 additions & 40 deletions packages/hardhat/deploy/04_AutomationConsumer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
import { networkConfig } from "../helper-hardhat-config";
import { sendLink } from "../tasks/send-link";
// import RegistrarABI from "@chainlink/contracts/abi/v0.8/AutomationRegistrar2_1.json";
// import LinkTokenABI from "@chainlink/contracts/abi/v0.8/LinkToken.json";
// import { approveAndTransfer } from "../scripts/approveAndTransfer";

/** 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;
Expand All @@ -27,54 +26,55 @@ const deployAutomationConsumer: DeployFunction = async function (hre: HardhatRun

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

/** FAILED ATTEMPT: programmatic registration through AutomationConsumer.registerNewUpkeep()
/** PROGRAMMATIC REGISTRATION FAILING!!!
*
* VERIFIED:
* - the AutomationConsumer contract does successfully approve registrar contract to spend LINK
*
* PROBLEM:
* - always fails with UNPREDICTABLE_GAS_LIMIT and I can't figure out why
* - always fails with UNPREDICTABLE_GAS_LIMIT but don't know why
*/

// if (linkTokenAddress) {
// const [signer] = await ethers.getSigners();
// const AutomationConsumer = await ethers.getContract("AutomationConsumer", signer);
const [signer] = await hre.ethers.getSigners();
const AutomationConsumer = await hre.ethers.getContract("AutomationConsumer", signer);
const upkeepID = await AutomationConsumer.s_upkeepID();

// if the automation consumer contract has not yet registered an upkeep subscription
if (upkeepID.toString() === "0") {
const fundAmount = 10;
const parsedFundAmount = hre.ethers.utils.parseUnits(fundAmount.toString(), 18);

// const fundAmount = "5";
if ((await AutomationConsumer.getLinkBalance()) < parsedFundAmount) {
await sendLink(AutomationConsumer.address, fundAmount, hre);
}

// await approveAndTransfer({
// tokenAddress: linkTokenAddress,
// tokenABI: LinkTokenABI,
// spenderAddress: AutomationConsumer.address,
// amount: fundAmount,
// });
// set up registration params: https://docs.chain.link/chainlink-automation/guides/register-upkeep-in-contract#register-the-upkeep
const registrationParams = {
name: "Programmatic Upkeep Registration",
encryptedEmail: "0x",
upkeepContract: AutomationConsumer.address,
gasLimit: "500000",
adminAddress: deployer,
triggerType: "0", // 0 for conditional upkeep
checkData: "0x",
triggerConfig: "0x",
offchainConfig: "0x",
amount: parsedFundAmount.toString(),
};

// // https://docs.chain.link/chainlink-automation/guides/register-upkeep-in-contract#register-the-upkeep
// const registrationParams = {
// name: "AutomationConsumer",
// encryptedEmail: "0x",
// upkeepContract: AutomationConsumer.address,
// gasLimit: "500000",
// adminAddress: deployer,
// triggerType: 0, // 0 for conditional upkeep
// checkData: "0x",
// triggerConfig: "0x",
// offchainConfig: "0x",
// amount: ethers.utils.parseUnits(fundAmount, 18),
// };
// console.log("registrationParams", registrationParams);
console.log("registrationParams", registrationParams);

// try {
// console.log("Registering upkeep...");
// const registerUpkeepTx = await AutomationConsumer.registerNewUpkeep(registrationParams);
// console.log("Register tx hash:", registerUpkeepTx.hash);
// await registerUpkeepTx.wait();
// console.log("Successfully registered upkeep with ID:", await AutomationConsumer.s_upkeepID());
// } catch (e) {
// console.log(e);
// console.log("Failed to register upkeep");
// }
// }
try {
console.log("Registering upkeep...");
const registerUpkeepTx = await AutomationConsumer.registerNewUpkeep(registrationParams);
console.log("Register tx hash:", registerUpkeepTx.hash);
await registerUpkeepTx.wait();
console.log("Successfully registered upkeep with ID:", await AutomationConsumer.s_upkeepID());
} catch (e) {
console.log(e);
console.log("Failed to register upkeep");
}
}
};

export default deployAutomationConsumer;
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat/tasks/send-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { networkConfig } from "../helper-hardhat-config";
* @notice gas fees are boosted since this is only for sepolia network
*/

export async function sendLink(hre: HardhatRuntimeEnvironment, recipientAddress: string, amount: number) {
export async function sendLink(recipientAddress: string, amount: number, hre: HardhatRuntimeEnvironment) {
if (hre.network.name !== "sepolia") {
throw new Error("This script is only configured for sepolia network");
}
Expand Down Expand Up @@ -52,5 +52,5 @@ task("send-link", "Send a specified amount of link to a specified address")
.addParam("recipient", "The address to send LINK token to")
.addParam("amount", "The human readable amount of LINK to send")
.setAction(async (taskArgs, hre) => {
await sendLink(hre, taskArgs.recipient, taskArgs.amount);
await sendLink(taskArgs.recipient, taskArgs.amount, hre);
});
2 changes: 1 addition & 1 deletion packages/nextjs/components/automation/Events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const Events = () => {
}
}, [events.length, eventsData, isLoadingEvents]);

console.log("eventsData", eventsData);
// console.log("eventsData", eventsData);

return (
<section className="grow bg-base-200 rounded-xl h-[350px]">
Expand Down
8 changes: 4 additions & 4 deletions packages/nextjs/components/automation/Showcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const Showcase = () => {
},
});

console.log("upkeepInfo", upkeepInfo);
// console.log("upkeepInfo", upkeepInfo);

return (
<div className="bg-base-100 rounded-xl mb-10 p-5 lg:p-10">
Expand All @@ -63,8 +63,8 @@ export const Showcase = () => {
<div>
<p className="text-xl">
Since smart contracts cannot initate transactions without the help of an externally owned account, a service
like chainlink automation is necessary for reliably executing transactions via time based or conditional
logic triggers.
like chainlink automation is required to reliably execute transactions via time based or conditional logic
triggers.
</p>

<p className="text-xl">
Expand Down Expand Up @@ -112,7 +112,7 @@ export const Showcase = () => {
<span className="font-bold mr-2">
{upkeepInfo?.balance ? parseFloat(formatEther(upkeepInfo.balance)).toFixed(2) : "0.0"} LINK
</span>
left in upkeep to fund automation
left in upkeep subscription
</div>
</div>
</div>
Expand Down
50 changes: 48 additions & 2 deletions packages/nextjs/generated/deployedContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const contracts = {
],
},
AutomationConsumer: {
address: "0x878758869763463082c27E996386A9b80Dc85323",
address: "0x388421109AD3eE7349Ead0CdF830284603d7dC79",
abi: [
{
inputs: [
Expand Down Expand Up @@ -182,7 +182,7 @@ const contracts = {
inputs: [
{
internalType: "bytes",
name: "checkData",
name: "",
type: "bytes",
},
],
Expand Down Expand Up @@ -215,6 +215,19 @@ const contracts = {
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [],
name: "getLinkBalance",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "getUpkeepInfo",
Expand Down Expand Up @@ -555,6 +568,39 @@ const contracts = {
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [
{
internalType: "contract AutomationRegistrarInterface",
name: "_registrar",
type: "address",
},
],
name: "updateRegistrarAddress",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [
{
internalType: "contract AutomationRegistryBaseInterface",
name: "_registry",
type: "address",
},
],
name: "updateRegistryAddress",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [],
name: "withdrawLink",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
],
},
VRFConsumer: {
Expand Down

0 comments on commit 84c3283

Please sign in to comment.