generated from smartcontractkit/hardhat-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper-functions.js
87 lines (79 loc) · 3.4 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 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 isn't 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)
}
}
}
// https://github.com/smartcontractkit/chainlink/blob/dbabde12def11a803d995e482e5fc4287d9cfce4/contracts/test/test-helpers/helpers.ts#L93
const stripHexPrefix = (hex) => {
if (!ethers.utils.isHexString(hex)) {
throw Error(`Expected valid hex string, got: "${hex}"`)
}
return hex.replace("0x", "")
}
// https://github.com/smartcontractkit/chainlink/blob/dbabde12def11a803d995e482e5fc4287d9cfce4/contracts/test/test-helpers/helpers.ts#L21
const addHexPrefix = (hex) => {
return hex.startsWith("0x") ? hex : `0x${hex}`
}
// https://github.com/smartcontractkit/chainlink/blob/dbabde12def11a803d995e482e5fc4287d9cfce4/contracts/test/test-helpers/helpers.ts#L30
const numToBytes32 = (num) => {
const hexNum = ethers.utils.hexlify(num)
const strippedNum = stripHexPrefix(hexNum)
if (strippedNum.length > 32 * 2) {
throw Error(
"Cannot convert number to bytes32 format, value is greater than maximum bytes32 value"
)
}
return addHexPrefix(strippedNum.padStart(32 * 2, "0"))
}
module.exports = {
autoFundCheck,
verify,
numToBytes32,
}