Skip to content

Commit

Permalink
Merge pull request #36 from smartcontractkit/harry/add-tasks-back
Browse files Browse the repository at this point in the history
added tasks back in to starter kit
  • Loading branch information
PatrickAlphaC authored Oct 19, 2021
2 parents 2f039eb + 235fbde commit 9e89acd
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
## Installation

### Kovan Ethereum Testnet
Set your `KOVAN_RPC_URL` [environment variable.](https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html). You can get one for free at [Infura's site.](https://infura.io/) You'll also need to set the variable `MNEMONIC` which is your mnemonic from your wallet, ie MetaMask. This is needed for deploying contracts to public networks. You can optionally set your `PRIVATE_KEY` instead with some changes to the `hardhat.config.js`.
Set your `KOVAN_RPC_URL` [environment variable.](https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html). You can get one for free at [Infura's site.](https://infura.io/) You'll also need to set the variable `PRIVATE_KEY`, which is your private key from your wallet, ie MetaMask. This is needed for deploying contracts to public networks. You can optionally set your `MNEMONIC` environment variable instead with some changes to the `hardhat.config.js`.

### Matic Mumbai Testnet
Set your `MUMBAI_RPC_URL` [environment variable](https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html). You can get one from the [Matic docs](https://docs.matic.network/docs/develop/network-details/network). You'll also need to set the variable `PRIVATE_KEY` which is your private key from your wallet, ie MetaMask. This is needed for deploying contracts to public networks. You can obtain testnet MATIC and LINK via the [MATIC Faucet](https://faucet.matic.network/)
Expand All @@ -34,15 +34,15 @@ Don't commit and push any changes to .env files that may contain sensitive infor
`.env` example:
```
KOVAN_RPC_URL='www.infura.io/asdfadsfafdadf'
MNEMONIC='cat dog frog...'
PRIVATE_KEY='abcdef'
MAINNET_RPC_URL="https://eth-mainnet.alchemyapi.io/v2/your-api-key"
MUMBAI_RPC_URL='https://rpc-mumbai.maticvigil.com'
POLYGON_MAINNET_RPC_URL='https://rpc-mainnet.maticvigil.com'
```
`bash` example
```
export KOVAN_RPC_URL='www.infura.io/asdfadsfafdadf'
export MNEMONIC='cat dog frog...'
export PRIVATE_KEY='abcdef'
export MAINNET_RPC_URL='https://eth-mainnet.alchemyapi.io/v2/your-api-key'
export MUMBAI_RPC_URL='https://rpc-mumbai.maticvigil.com'
export POLYGON_MAINNET_RPC_URL='https://rpc-mainnet.maticvigil.com'
Expand Down
17 changes: 8 additions & 9 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ require("@nomiclabs/hardhat-waffle")
require("@nomiclabs/hardhat-ethers")
require("@nomiclabs/hardhat-truffle5")
require("@nomiclabs/hardhat-etherscan")
require("@appliedblockchain/chainlink-plugins-api-consumer")
require("@appliedblockchain/chainlink-plugins-price-consumer")
require("@appliedblockchain/chainlink-plugins-random-number-consumer")
require("@appliedblockchain/chainlink-plugins-fund-link")
require("hardhat-deploy")
require("./tasks/accounts")
require("./tasks/balance")
require("./tasks/withdraw-link")
require("./tasks/block-number")
require("./tasks/price-consumer")

require("./tasks/random-number-consumer")
require("./tasks/price-consumer")
require("./tasks/api-consumer")
require("@appliedblockchain/chainlink-plugins-fund-link")

require('dotenv').config()

Expand All @@ -42,10 +41,10 @@ module.exports = {
},
kovan: {
url: KOVAN_RPC_URL,
// accounts: [PRIVATE_KEY],
accounts: {
mnemonic: MNEMONIC,
},
accounts: [PRIVATE_KEY],
//accounts: {
// mnemonic: MNEMONIC,
// },
saveDeployments: true,
},
rinkeby: {
Expand Down
2 changes: 2 additions & 0 deletions tasks/api-consumer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.readData = require("./read-data.js")
exports.requestData = require("./request-data.js")
26 changes: 26 additions & 0 deletions tasks/api-consumer/read-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 APIConsumer = await ethers.getContractFactory("APIConsumer")

//Get signer information
const accounts = await ethers.getSigners()
const signer = accounts[0]

//Create connection to API Consumer Contract and call the createRequestTo function
const apiConsumerContract = new ethers.Contract(contractAddr, APIConsumer.interface, signer)
let result = BigInt(await apiConsumerContract.volume()).toString()
console.log('Data is: ', result)
if (result == 0 && ['hardhat', 'localhost', 'ganache'].indexOf(network.name) == 0) {
console.log("You'll either need to wait another minute, or fix something!")
}
if (['hardhat', 'localhost', 'ganache'].indexOf(network.name) >= 0) {
console.log("You'll have to manually update the value since you're on a local chain!")
}
})

module.exports = {}
23 changes: 23 additions & 0 deletions tasks/api-consumer/request-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
let { networkConfig, getNetworkIdFromName } = require('../../helper-hardhat-config')

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")
.setAction(async taskArgs => {

const contractAddr = taskArgs.contract
let networkId = await getNetworkIdFromName(network.name)
console.log("Calling API Consumer contract ", contractAddr, " on network ", network.name)
const APIConsumer = await ethers.getContractFactory("APIConsumer")

//Get signer information
const accounts = await ethers.getSigners()
const signer = accounts[0]

//Create connection to API Consumer Contract and call the createRequestTo function
const apiConsumerContract = new ethers.Contract(contractAddr, APIConsumer.interface, signer)
var result = await apiConsumerContract.requestVolumeData()
console.log('Contract ', contractAddr, ' external data request successfully called. Transaction Hash: ', result.hash)
console.log("Run the following to read the returned result:")
console.log("npx hardhat read-data --contract " + contractAddr + " --network " + network.name)
})
module.exports = {}
1 change: 1 addition & 0 deletions tasks/price-consumer/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
exports.readPriceFeedEns = require("./read-price-feed-ens.js")
exports.readPriceFeed = require("./read-price-feed.js")
20 changes: 20 additions & 0 deletions tasks/price-consumer/read-price-feed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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

const PriceFeedConsumerContract = await ethers.getContractFactory("PriceConsumerV3")
console.log("Reading data from Price Feed consumer contract ", contractAddr, " on network ", networkId)

//Get signer information
const accounts = await ethers.getSigners()
const signer = accounts[0]
const priceFeedConsumerContract = await new ethers.Contract(contractAddr, PriceFeedConsumerContract.interface, signer)
await priceFeedConsumerContract.getLatestPrice().then((data) => {
console.log('Price is: ', BigInt(data).toString())
})
})

module.exports = {}
2 changes: 2 additions & 0 deletions tasks/random-number-consumer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.readRandomNumber = require("./read-random-number.js")
exports.requestRandomNumber = require("./request-random-number.js")
26 changes: 26 additions & 0 deletions tasks/random-number-consumer/read-random-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 RandomNumberConsumer = await ethers.getContractFactory("RandomNumberConsumer")

//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, RandomNumberConsumer.interface, signer)
let result = BigInt(await vrfConsumerContract.randomResult()).toString()
console.log('Random Number is: ', result)
if (result == 0 && ['hardhat', 'localhost', 'ganache'].indexOf(network.name) == 0) {
console.log("You'll either need to wait another minute, or fix something!")
}
if (['hardhat', 'localhost', 'ganache'].indexOf(network.name) >= 0) {
console.log("You'll have to manually update the value since you're on a local chain!")
}
})

module.exports = {}
22 changes: 22 additions & 0 deletions tasks/random-number-consumer/request-random-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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")
.setAction(async taskArgs => {

const contractAddr = taskArgs.contract
const networkId = network.name
console.log("Requesting a random number using VRF consumer contract ", contractAddr, " on network ", networkId)
const RandomNumberConsumer = await ethers.getContractFactory("RandomNumberConsumer")

//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, RandomNumberConsumer.interface, signer)
var result = await vrfConsumerContract.getRandomNumber()
console.log('Contract ', contractAddr, ' random number request successfully called. Transaction Hash: ', result.hash)
console.log("Run the following to read the returned random number:")
console.log("npx hardhat read-random-number --contract " + contractAddr + " --network " + network.name)
})

module.exports = {}

0 comments on commit 9e89acd

Please sign in to comment.