-
Notifications
You must be signed in to change notification settings - Fork 5
/
hardhat.config.js
282 lines (245 loc) · 20.4 KB
/
hardhat.config.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//const { network } = require('hardhat');
/**
* @type import('hardhat/config').HardhatUserConfig
*/
require("@nomiclabs/hardhat-waffle")
require("@nomiclabs/hardhat-ethers")
require("@nomiclabs/hardhat-web3")
require("@nomiclabs/hardhat-truffle5")
require("@nomiclabs/hardhat-etherscan");
require("hardhat-deploy")
require('dotenv').config()
require("./tasks/faucet");
task("accounts", "Prints the list of accounts", async () => {
const accounts = await ethers.getSigners()
for (const account of accounts) {
console.log(account.address)
}
})
task("balance", "Prints an account's balance")
.addParam("account", "The account's address")
.setAction(async taskArgs => {
const account = web3.utils.toChecksumAddress(taskArgs.account)
const balance = await web3.eth.getBalance(account)
console.log(web3.utils.fromWei(balance, "ether"), "ETH")
})
module.exports = {}
task("fund-link", "Funds a contract with LINK")
.addParam("contract", "The address of the contract that requires LINK")
.addOptionalParam("linkAddress", "Set the LINK token address")
.setAction(async taskArgs => {
const contractAddr = taskArgs.contract
const networkId = network.name
console.log("Funding contract ", contractAddr, " on network ", networkId)
const LINK_TOKEN_ABI = [{ "inputs": [{ "internalType": "address", "name": "recipient", "type": "address" }, { "internalType": "uint256", "name": "amount", "type": "uint256" }], "name": "transfer", "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], "stateMutability": "nonpayable", "type": "function" }]
//set the LINK token contract address according to the environment
switch (networkId) {
case 'mainnet':
linkContractAddr = '0x514910771af9ca656af840dff83e8264ecf986ca'
break
case 'kovan':
linkContractAddr = '0xa36085F69e2889c224210F603D836748e7dC0088'
break
case 'rinkeby':
linkContractAddr = '0x01BE23585060835E02B77ef475b0Cc51aA1e0709'
break
case 'goerli':
linkContractAddr = '0x326c977e6efc84e512bb9c30f76e30c160ed06fb'
break
default: //default to kovan
linkContractAddr = '0xa36085F69e2889c224210F603D836748e7dC0088'
}
console.log(linkContractAddr)
//Fund with 1 LINK token
const amount = web3.utils.toHex(1e18)
//Get signer information
const accounts = await hre.ethers.getSigners()
const signer = accounts[0]
//Create connection to LINK token contract and initiate the transfer
const linkTokenContract = new ethers.Contract(linkContractAddr, LINK_TOKEN_ABI, signer)
var result = await linkTokenContract.transfer(contractAddr, amount).then(function (transaction) {
console.log('Contract ', contractAddr, ' funded with 1 LINK. Transaction Hash: ', transaction.hash)
})
})
task("request-data", "Calls an API Consumer Contract to request external data")
.addParam("contract", "The address of the API Consumer contract that you want to call")
.addOptionalParam("oracle", "Oracle contract address", '0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e')
.addOptionalParam("jobId", "Job Id of the job you wish to use", "29fa9aa13bf1468788b7cc4a500a45b8")
.addOptionalParam("payment", "Payment in LINK tokens required", '1000000000000000000')
.addOptionalParam("url", "URL to access", 'https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD')
.addOptionalParam("path", "JSON path to traverse", 'USD')
.addOptionalParam("times", "Multiplier if using an integer", '100')
.setAction(async taskArgs => {
const contractAddr = taskArgs.contract
const networkId = network.name
console.log("Calling API Consumer contract ", contractAddr, " on network ", networkId)
const API_CONSUMER_ABI = [{ "inputs": [{ "internalType": "address", "name": "_link", "type": "address" }], "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "bytes32", "name": "id", "type": "bytes32" }], "name": "ChainlinkCancelled", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "bytes32", "name": "id", "type": "bytes32" }], "name": "ChainlinkFulfilled", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "bytes32", "name": "id", "type": "bytes32" }], "name": "ChainlinkRequested", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "address", "name": "previousOwner", "type": "address" }, { "indexed": true, "internalType": "address", "name": "newOwner", "type": "address" }], "name": "OwnershipTransferred", "type": "event" }, { "inputs": [{ "internalType": "bytes32", "name": "_requestId", "type": "bytes32" }, { "internalType": "uint256", "name": "_payment", "type": "uint256" }, { "internalType": "bytes4", "name": "_callbackFunctionId", "type": "bytes4" }, { "internalType": "uint256", "name": "_expiration", "type": "uint256" }], "name": "cancelRequest", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "_oracle", "type": "address" }, { "internalType": "bytes32", "name": "_jobId", "type": "bytes32" }, { "internalType": "uint256", "name": "_payment", "type": "uint256" }, { "internalType": "string", "name": "_url", "type": "string" }, { "internalType": "string", "name": "_path", "type": "string" }, { "internalType": "int256", "name": "_times", "type": "int256" }], "name": "createRequestTo", "outputs": [{ "internalType": "bytes32", "name": "requestId", "type": "bytes32" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "data", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "_requestId", "type": "bytes32" }, { "internalType": "uint256", "name": "_data", "type": "uint256" }], "name": "fulfill", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "getChainlinkToken", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "isOwner", "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "owner", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "newOwner", "type": "address" }], "name": "transferOwnership", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "withdrawLink", "outputs": [], "stateMutability": "nonpayable", "type": "function" }]
//Get signer information
const accounts = await hre.ethers.getSigners()
const signer = accounts[0]
//Create connection to API Consumer Contract and call the createRequestTo function
const apiConsumerContract = new ethers.Contract(contractAddr, API_CONSUMER_ABI, signer)
var result = await apiConsumerContract.createRequestTo(taskArgs.oracle,
ethers.utils.toUtf8Bytes(taskArgs.jobId),
taskArgs.payment,
taskArgs.url,
taskArgs.path,
taskArgs.times).then(function (transaction) {
console.log('Contract ', contractAddr, ' external data request successfully called. Transaction Hash: ', transaction.hash)
console.log("Run the following to read the returned result:")
console.log("npx hardhat read-data --contract ", contractAddr)
})
})
task("read-data", "Calls an API Consumer Contract to read data obtained from an external API")
.addParam("contract", "The address of the API Consumer contract that you want to call")
.setAction(async taskArgs => {
const contractAddr = taskArgs.contract
const networkId = network.name
console.log("Reading data from API Consumer contract ", contractAddr, " on network ", networkId)
const API_CONSUMER_ABI = [{ "inputs": [{ "internalType": "address", "name": "_link", "type": "address" }], "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "bytes32", "name": "id", "type": "bytes32" }], "name": "ChainlinkCancelled", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "bytes32", "name": "id", "type": "bytes32" }], "name": "ChainlinkFulfilled", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "bytes32", "name": "id", "type": "bytes32" }], "name": "ChainlinkRequested", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "address", "name": "previousOwner", "type": "address" }, { "indexed": true, "internalType": "address", "name": "newOwner", "type": "address" }], "name": "OwnershipTransferred", "type": "event" }, { "inputs": [{ "internalType": "bytes32", "name": "_requestId", "type": "bytes32" }, { "internalType": "uint256", "name": "_payment", "type": "uint256" }, { "internalType": "bytes4", "name": "_callbackFunctionId", "type": "bytes4" }, { "internalType": "uint256", "name": "_expiration", "type": "uint256" }], "name": "cancelRequest", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "_oracle", "type": "address" }, { "internalType": "bytes32", "name": "_jobId", "type": "bytes32" }, { "internalType": "uint256", "name": "_payment", "type": "uint256" }, { "internalType": "string", "name": "_url", "type": "string" }, { "internalType": "string", "name": "_path", "type": "string" }, { "internalType": "int256", "name": "_times", "type": "int256" }], "name": "createRequestTo", "outputs": [{ "internalType": "bytes32", "name": "requestId", "type": "bytes32" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "data", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "_requestId", "type": "bytes32" }, { "internalType": "uint256", "name": "_data", "type": "uint256" }], "name": "fulfill", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "getChainlinkToken", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "isOwner", "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "owner", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "newOwner", "type": "address" }], "name": "transferOwnership", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "withdrawLink", "outputs": [], "stateMutability": "nonpayable", "type": "function" }]
//Get signer information
const accounts = await hre.ethers.getSigners()
const signer = accounts[0]
//Create connection to API Consumer Contract and call the createRequestTo function
const apiConsumerContract = new ethers.Contract(contractAddr, API_CONSUMER_ABI, signer)
var result = await apiConsumerContract.data().then(function (data) {
console.log('Data is: ', web3.utils.hexToNumber(data._hex))
})
})
task("request-random-number", "Requests a random number for a Chainlink VRF enabled smart contract")
.addParam("contract", "The address of the API Consumer contract that you want to call")
.addParam("seed", "The seed to be used in the requst for randomness", 777, types.int)
.setAction(async taskArgs => {
const contractAddr = taskArgs.contract
const seed = taskArgs.seed
const networkId = network.name
console.log("Requesting a random number using VRF consumer contract ", contractAddr, " on network ", networkId)
const RANDOM_NUMBER_CONSUMER_ABI = [{ "inputs": [{ "internalType": "address", "name": "_vrfCoordinator", "type": "address" }, { "internalType": "address", "name": "_link", "type": "address" }, { "internalType": "bytes32", "name": "_keyHash", "type": "bytes32" }, { "internalType": "uint256", "name": "_fee", "type": "uint256" }], "stateMutability": "nonpayable", "type": "constructor" }, { "inputs": [{ "internalType": "uint256", "name": "userProvidedSeed", "type": "uint256" }], "name": "getRandomNumber", "outputs": [{ "internalType": "bytes32", "name": "requestId", "type": "bytes32" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }], "name": "nonces", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "randomResult", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "requestId", "type": "bytes32" }, { "internalType": "uint256", "name": "randomness", "type": "uint256" }], "name": "rawFulfillRandomness", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "_keyHash", "type": "bytes32" }, { "internalType": "uint256", "name": "_fee", "type": "uint256" }, { "internalType": "uint256", "name": "_seed", "type": "uint256" }], "name": "requestRandomness", "outputs": [{ "internalType": "bytes32", "name": "requestId", "type": "bytes32" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "withdrawLink", "outputs": [], "stateMutability": "nonpayable", "type": "function" }]
//Get signer information
const accounts = await hre.ethers.getSigners()
const signer = accounts[0]
//Create connection to VRF Contract and call the getRandomNumber function
const vrfConsumerContract = new ethers.Contract(contractAddr, RANDOM_NUMBER_CONSUMER_ABI, signer)
var result = await vrfConsumerContract.getRandomNumber(seed).then(function (transaction) {
console.log('Contract ', contractAddr, ' external data request successfully called. Transaction Hash: ', transaction.hash)
console.log("Run the following to read the returned random number:")
console.log("npx hardhat read-random-number --contract ", contractAddr)
})
})
task("read-random-number", "Reads the random number returned to a contract by Chainlink VRF")
.addParam("contract", "The address of the VRF contract that you want to read")
.setAction(async taskArgs => {
const contractAddr = taskArgs.contract
const networkId = network.name
console.log("Reading data from VRF contract ", contractAddr, " on network ", networkId)
const RANDOM_NUMBER_CONSUMER_ABI = [{ "inputs": [{ "internalType": "address", "name": "_vrfCoordinator", "type": "address" }, { "internalType": "address", "name": "_link", "type": "address" }, { "internalType": "bytes32", "name": "_keyHash", "type": "bytes32" }, { "internalType": "uint256", "name": "_fee", "type": "uint256" }], "stateMutability": "nonpayable", "type": "constructor" }, { "inputs": [{ "internalType": "uint256", "name": "userProvidedSeed", "type": "uint256" }], "name": "getRandomNumber", "outputs": [{ "internalType": "bytes32", "name": "requestId", "type": "bytes32" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }], "name": "nonces", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "randomResult", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "requestId", "type": "bytes32" }, { "internalType": "uint256", "name": "randomness", "type": "uint256" }], "name": "rawFulfillRandomness", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "_keyHash", "type": "bytes32" }, { "internalType": "uint256", "name": "_fee", "type": "uint256" }, { "internalType": "uint256", "name": "_seed", "type": "uint256" }], "name": "requestRandomness", "outputs": [{ "internalType": "bytes32", "name": "requestId", "type": "bytes32" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "withdrawLink", "outputs": [], "stateMutability": "nonpayable", "type": "function" }]
//Get signer information
const accounts = await hre.ethers.getSigners()
const signer = accounts[0]
//Create connection to API Consumer Contract and call the createRequestTo function
const vrfConsumerContract = new ethers.Contract(contractAddr, RANDOM_NUMBER_CONSUMER_ABI, signer)
var result = await vrfConsumerContract.randomResult().then(function (data) {
console.log('Random Number is: ', web3.utils.hexToNumberString(data._hex))
})
})
task("read-price-feed", "Gets the latest price from a Chainlink Price Feed")
.addParam("contract", "The address of the Price Feed consumer contract that you want to read")
.setAction(async taskArgs => {
const contractAddr = taskArgs.contract
const networkId = network.name
console.log("Reading data from Price Feed consumer contract ", contractAddr, " on network ", networkId)
const PRICE_FEED_CONSUMER_ABI = [{ "inputs": [{ "internalType": "address", "name": "_priceFeed", "type": "address" }], "stateMutability": "nonpayable", "type": "constructor" }, { "inputs": [], "name": "getLatestPrice", "outputs": [{ "internalType": "int256", "name": "", "type": "int256" }], "stateMutability": "view", "type": "function" }]
//Get signer information
const accounts = await hre.ethers.getSigners()
const signer = accounts[0]
//Create connection to API Consumer Contract and call the createRequestTo function
const priceFeedConsumerContract = new ethers.Contract(contractAddr, PRICE_FEED_CONSUMER_ABI, signer)
var result = await priceFeedConsumerContract.getLatestPrice().then(function (data) {
console.log('Current price is: ', web3.utils.hexToNumber(data._hex))
})
})
task(
"blockNumber",
"Prints the current block number",
async (_, { ethers }) => {
await ethers.provider.getBlockNumber().then((blockNumber) => {
console.log("Current block number: " + blockNumber)
})
}
)
const mainnetRpcUrl = process.env.ALCHEMY_MAINNET_RPC_URL || "https://eth-mainnet.alchemyapi.io/v2/your-api-key"
const kovanRpcUrl = process.env.KOVAN_RPC_URL || "https://eth-kovan.alchemyapi.io/v2/your-api-key"
const privateKey = process.env.PRIVATE_KEY || "your private key"
module.exports = {
defaultNetwork: "hardhat",
// defaultNetwork: "kovan",
networks: {
hardhat: {
accounts: {
mnemonic: process.env.MNEMONIC,
initialIndex: 1 // cause chainlink hackathon account is the second one
},
chainId: 1337
},
rinkeby: {
url: process.env.RINKEBY_RPC_URL,
accounts: {
mnemonic: process.env.MNEMONIC,
initialIndex: 1 // cause chainlink hackathon account is the second one
},
chainId: 4,
gas: 4612388 // Gas limit used for deploys
},
kovan: {
url: process.env.KOVAN_RPC_URL,
accounts: {
mnemonic: process.env.MNEMONIC,
initialIndex: 1 // cause chainlink hackathon account is the second one
},
chainId: 42
},
matic: {
url: "https://rpc-mumbai.maticvigil.com",
accounts: {
mnemonic: process.env.MNEMONIC,
initialIndex: 1 // cause chainlink hackathon account is the second one
},
chainId: 137
},
maticTestnet:{
url: "https://rpc-mumbai.matic.today",
accounts: {
mnemonic: process.env.MNEMONIC,
initialIndex: 1 // cause chainlink hackathon account is the second one
},
chainId: 80001
}
},
etherscan: {
// Your API key for Etherscan
// Obtain one at https://etherscan.io/
apiKey: process.env.ETHERSCAN_API
},
namedAccounts: {
deployer: {
default: 0, // here this will by default take the first account as deployer
1: 0 // similarly on mainnet it will take the first account as deployer. Note though that depending on how hardhat network are configured, the account 0 on one network can be different than on another
},
feeCollector: {
default: 1
}
},
solidity: {
compilers: [
{
version: "0.6.6",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
]
}
}