Skip to content

Commit

Permalink
refactor vrf deployment script and add useful tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPereira committed Nov 2, 2023
1 parent c87a5b4 commit 5556349
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/hardhat/deploy/01_AggregatorV3Consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { DeployFunction } from "hardhat-deploy/types";
import { developmentChains, networkConfig } from "../helper-hardhat-config";
import { network } from "hardhat";

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

const deployAggregatorV3Consumer: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployer } = await hre.getNamedAccounts();
const { deploy, log } = hre.deployments;
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat/deploy/02_FeedRegistryConsumer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";

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

const deployRegistryFeedConsumer: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployer } = await hre.getNamedAccounts();
const { deploy, log } = hre.deployments;
Expand Down
30 changes: 11 additions & 19 deletions packages/hardhat/deploy/03_VRFConsumer.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
import { networkConfig } from "../helper-hardhat-config";
import { approveAndTransfer } from "../scripts/approveAndTransfer";
import LinkTokenABI from "@chainlink/contracts/abi/v0.8/LinkToken.json";
import { getTokenBalance, sendLink } from "../tasks";

/** 1. Deploy the "VRFConsumer" contract
* 2. Send 5 LINK to VRFConsumer contract
*
* @param hre HardhatRuntimeEnvironment object.
/** Deploy VRFConsumer contract
* also funds contract with LINK if its a fresh deployment
*/

const deployVRFConsumer: 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 @@ -27,18 +24,13 @@ const deployVRFConsumer: DeployFunction = async function (hre: HardhatRuntimeEnv
autoMine: true,
});

if (linkTokenAddress) {
const vrfConsumer = await ethers.getContract("VRFConsumer", deployer);
const vrfConsumerBalance = await vrfConsumer.getLinkBalance();
const minBalance = ethers.utils.parseUnits("1", 18);

if (vrfConsumerBalance < minBalance)
await approveAndTransfer({
tokenAddress: linkTokenAddress,
tokenABI: LinkTokenABI,
spenderAddress: VRFConsumer.address,
amount: "5",
});
// check the LINK balance
const VRFConsumerLinkBalance = await getTokenBalance(hre, VRFConsumer.address, linkTokenAddress);
// fund with LINK if the balance is 0 LINK (i.e. fresh deployment)
if (+VRFConsumerLinkBalance === 0) {
const amount = 5;
console.log(`Funding VRFConsumer contract with ${amount} LINK`);
await sendLink(hre, VRFConsumer.address, amount);
}
};

Expand Down
2 changes: 2 additions & 0 deletions packages/hardhat/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import "hardhat-deploy";
import "@matterlabs/hardhat-zksync-solc";
import "@matterlabs/hardhat-zksync-verify";

import "./tasks";

// If not set, it uses ours Alchemy's default API key.
// You can get your own at https://dashboard.alchemyapi.io
const providerApiKey = process.env.ALCHEMY_API_KEY || "oKxs-03sij-U_N0iOlrSsZFr29-IqbuF";
Expand Down
16 changes: 11 additions & 5 deletions packages/hardhat/helper-hardhat-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ interface NetworkConfigEntryTypes {
LINK_USD: string;
};
VRFConsumer: {
VRFV2WrapperAddress?: string;
linkTokenAddress?: string;
VRFV2WrapperAddress: string;
linkTokenAddress: string;
};
AutomationConsumer: {
registrarAddress?: string;
linkTokenAddress?: string;
registryAddress?: string;
registrarAddress: string;
linkTokenAddress: string;
registryAddress: string;
};
tokenAddress: {
LINK: string;
};
}

Expand All @@ -34,6 +37,9 @@ const networkConfig: { [key: number]: NetworkConfigEntryTypes } = {
ETH_USD: "0x694AA1769357215DE4FAC081bf1f309aDC325306",
LINK_USD: "0xc59E3633BAAC79493d908e63626716e204A45EdF",
},
tokenAddress: {
LINK: "0x779877A7B0D9E8603169DdbD7836e478b4624789",
},
},
};

Expand Down
34 changes: 34 additions & 0 deletions packages/hardhat/tasks/get-token-balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// import chalk from "chalk";
import { task } from "hardhat/config";
import ERC20_ABI from "@chainlink/contracts/abi/v0.8/ERC20.json";
import { HardhatRuntimeEnvironment } from "hardhat/types";

/** Get token balance for a given address
* @param accountAddress the address to check
* @param tokenAddress the token address
*
* @returns the token balance in human readable format
*/

export async function getTokenBalance(hre: HardhatRuntimeEnvironment, accountAddress: string, tokenAddress: string) {
const { ethers } = hre;
const { provider } = ethers;

const tokenContract = new ethers.Contract(tokenAddress, ERC20_ABI, provider);
const decimals = await tokenContract.decimals();
const symbol = await tokenContract.symbol();
const rawBalance = await tokenContract.balanceOf(accountAddress);
const formattedBalance = ethers.utils.formatUnits(rawBalance, decimals);

// prettier-ignore
console.log((`Address: ${accountAddress} has ${formattedBalance} ${symbol}`));

return formattedBalance;
}

task("get-token-balance", "Gets the token balance for a given address")
.addParam("account", "The account's address")
.addParam("token", "The token's contract address")
.setAction(async (taskArgs, hre) => {
await getTokenBalance(hre, taskArgs.account, taskArgs.token);
});
2 changes: 2 additions & 0 deletions packages/hardhat/tasks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./send-link";
export * from "./get-token-balance";
45 changes: 45 additions & 0 deletions packages/hardhat/tasks/send-link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { task } from "hardhat/config";
import ERC20_ABI from "@chainlink/contracts/abi/v0.8/ERC20.json";
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
*
*/

export async function sendLink(hre: HardhatRuntimeEnvironment, recipientAddress: string, amount: number) {
if (hre.network.name !== "sepolia") {
throw new Error("This script is only configured for sepolia network");
}

const chainId = await hre.ethers.provider.getNetwork().then(network => network.chainId);
const linkTokenAddress = networkConfig[chainId].tokenAddress.LINK;
const [signer] = await hre.ethers.getSigners();

console.log("Fetching LINK token contract data...");
const linkTokenContract = new hre.ethers.Contract(linkTokenAddress, ERC20_ABI, signer);
const decimals = await linkTokenContract.decimals();
const parsedAmount = hre.ethers.utils.parseUnits(amount.toString(), decimals);

console.log("Sending transfer transaction...");
// transfer(address to, uint256 amount)
const transferTx = await linkTokenContract.transfer(recipientAddress, parsedAmount);
console.log("txHash", transferTx.hash);
const transferTxReceipt = await transferTx.wait();

if (transferTxReceipt.status !== 1) {
throw new Error("Transfer transaction failed");
}

console.log(`Sent ${amount} LINK to ${recipientAddress}`);
}

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);
});

0 comments on commit 5556349

Please sign in to comment.