generated from smartcontractkit/hardhat-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper-functions.js
60 lines (56 loc) · 2.22 KB
/
helper-functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// we can't have these functions in our `helper-hardhat-config`
// since these use the hardhat library
// and it would be a circular dependency
const { run, network } = require("hardhat")
const { networkConfig } = require("./helper-hardhat-config")
const AUTO_FUND = process.env.AUTO_FUND || true
const autoFundCheck = async (contractAddr, networkName, linkTokenAddress, additionalMessage) => {
const chainId = network.config.chainId
console.log("Checking to see if contract can be auto-funded with LINK:")
const amount = networkConfig[chainId]["fundAmount"]
//check to see if user has enough LINK
const accounts = await ethers.getSigners()
const signer = accounts[0]
const LinkToken = await ethers.getContractFactory("LinkToken")
const linkTokenContract = new ethers.Contract(linkTokenAddress, LinkToken.interface, signer)
const balanceBN = await linkTokenContract.balanceOf(signer.address)
const balance = balanceBN.toString()
const contractBalanceBN = await linkTokenContract.balanceOf(contractAddr)
const contractBalance = await contractBalanceBN.toString()
if (balance > amount && amount > 0 && contractBalance < amount) {
//user has enough LINK to auto-fund
//and the contract isn't already funded
return true
} else {
//user doesn't have enough LINK, print a warning
console.log(
"Account doesn't have enough LINK to fund contracts, you're deploying to a network where auto funding isnt' done by default, the contract is already funded, or you set AUTO_FUND to false."
)
console.log(
`Please obtain LINK via the faucet at https://faucets.chain.link/${networkName} then run the following command to fund contract with LINK:`
)
console.log(
`yarn hardhat fund-link --contract ${contractAddr} --network ${networkName} ${additionalMessage}`
)
return false
}
}
const verify = async (contractAddress, args) => {
console.log("Verifying contract...")
try {
await run("verify:verify", {
address: contractAddress,
constructorArguments: args,
})
} catch (e) {
if (e.message.toLowerCase().includes("already verified")) {
console.log("Already verified!")
} else {
console.log(e)
}
}
}
module.exports = {
autoFundCheck,
verify,
}