Skip to content

Commit

Permalink
Merge pull request #1474 from smartcontractkit/chores/update-truffle-box
Browse files Browse the repository at this point in the history
Add scripts to our Truffle Box
  • Loading branch information
thodges-gh authored Jul 22, 2019
2 parents 200ca50 + d12d6d5 commit 2b37dfc
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 12 deletions.
63 changes: 61 additions & 2 deletions evm/box/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Chainlink Truffle Box

Implementation of [how to make a Chainlinked contract](https://docs.chain.link/docs/getting-started).
Implementation of a [Chainlink requesting contract](https://docs.chain.link/docs/create-a-chainlinked-project).

## Requirements

- NPM

## Installation

Package installation should have occurred for you during the Truffle Box setup. However, if you add dependencies, you'll need to add them to the project by running:

```bash
npm install
```
Expand Down Expand Up @@ -36,4 +38,61 @@ For deploying to live networks, Truffle will use `truffle-hdwallet-provider` for

```bash
npm run migrate:live
```
```

## Helper Scripts

There are 3 helper scripts provided with this box in the scripts directory:
- `fund-contract.js`
- `request-data.js`
- `read-contract.js`

They can be used by calling them from `npx truffle exec`, for example:

```bash
npx truffle exec scripts/fund-contract.js --network live
```

The CLI will output something similar to the following:

```
Using network 'live'.
Funding contract: 0x972DB80842Fdaf6015d80954949dBE0A1700705E
0xd81fcf7bfaf8660149041c823e843f0b2409137a1809a0319d26db9ceaeef650
Truffle v5.0.25 (core: 5.0.25)
Node v10.15.1
```

In the `request-data.js` script, example parameters are provided for you. You can change the oracle address, Job ID, and parameters based on the information available on [our documentation](https://docs.chain.link/docs/testnet-oracles).

```bash
npx truffle exec scripts/request-data.js --network live
```

This creates a request and will return the transaction ID, for example:

```
Using network 'live'.
Creating request on contract: 0x972DB80842Fdaf6015d80954949dBE0A1700705E
0x828f256109f22087b0804a4d1a5c25e8ce9e5ac4bbc777b5715f5f9e5b181a4b
Truffle v5.0.25 (core: 5.0.25)
Node v10.15.1
```

After creating a request on a live network, you will want to wait 3 blocks for the Chainlink node to respond. Then call the `read-contract.js` script to read the contract's state.

```bash
npx truffle exec scripts/read-contract.js --network live
```

Once the oracle has responded, you will receive a value similar to the one below:

```
Using network 'live'.
21568
Truffle v5.0.25 (core: 5.0.25)
Node v10.15.1
```
2 changes: 1 addition & 1 deletion evm/box/contracts/MyContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ contract MyContract is ChainlinkClient, Ownable {
uint256 _expiration
)
public
onlyOwner
onlyOwner
{
cancelChainlinkRequest(_requestId, _payment, _callbackFunctionId, _expiration);
}
Expand Down
14 changes: 7 additions & 7 deletions evm/box/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
"version": "0.6.0",
"description": "A Chainlink example in a Truffle box",
"scripts": {
"compile": "./node_modules/.bin/truffle compile",
"console:dev": "./node_modules/.bin/truffle console --network cldev",
"console:live": "./node_modules/.bin/truffle console --network live",
"migrate:dev": "./node_modules/.bin/truffle migrate --reset --network cldev",
"migrate:live": "./node_modules/.bin/truffle migrate --network live",
"test": "./node_modules/.bin/truffle test"
"compile": "npx truffle compile",
"console:dev": "npx truffle console --network cldev",
"console:live": "npx truffle console --network live",
"migrate:dev": "npx truffle migrate --reset --network cldev",
"migrate:live": "npx truffle migrate --network live",
"test": "npx truffle test"
},
"license": "MIT",
"dependencies": {
"chainlink": "0.6.5",
"chainlink-test-helpers": "^0.6.0-3",
"chainlink-test-helpers": "^0.6.0-5",
"openzeppelin-solidity": "^1.12.0"
},
"devDependencies": {
Expand Down
20 changes: 20 additions & 0 deletions evm/box/scripts/fund-contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let MyContract = artifacts.require('MyContract')
let LinkToken = artifacts.require('LinkToken')

/*
This script is meant to assist with funding the requesting
contract with LINK. It will send 1 LINK to the requesting
contract for ease-of-use. Any extra LINK present on the contract
can be retrieved by calling the withdrawLink() function.
*/

const payment = process.env.TRUFFLE_CL_BOX_PAYMENT || '1000000000000000000'

module.exports = async (callback) => {
let mc = await MyContract.deployed()
let tokenAddress = await mc.getChainlinkToken()
let token = await LinkToken.at(tokenAddress)
console.log('Funding contract:', mc.address)
let tx = await token.transfer(mc.address, payment)
callback(tx.tx)
}
12 changes: 12 additions & 0 deletions evm/box/scripts/read-contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let MyContract = artifacts.require('MyContract')

/*
This script makes it easy to read the data variable
of the requesting contract.
*/

module.exports = async (callback) => {
let mc = await MyContract.deployed()
let data = await mc.data.call()
callback(data)
}
28 changes: 28 additions & 0 deletions evm/box/scripts/request-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
let MyContract = artifacts.require('MyContract')

/*
This script allows for a Chainlink request to be created from
the requesting contract. Defaults to the Chainlink oracle address
on this page: https://docs.chain.link/docs/testnet-oracles
*/

const oracleAddress = process.env.TRUFFLE_CL_BOX_ORACLE_ADDRESS || '0xc99B3D447826532722E41bc36e644ba3479E4365'
const jobId = process.env.TRUFFLE_CL_BOX_JOB_ID || '9f0406209cf64acda32636018b33de11'
const payment = process.env.TRUFFLE_CL_BOX_PAYMENT || '1000000000000000000'
const url = process.env.TRUFFLE_CL_BOX_URL || 'https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD'
const path = process.env.TRUFFLE_CL_BOX_JSON_PATH || 'USD'
const times = process.env.TRUFFLE_CL_BOX_TIMES || '100'

module.exports = async (callback) => {
let mc = await MyContract.deployed()
console.log('Creating request on contract:', mc.address)
let tx = await mc.createRequestTo(
oracleAddress,
web3.utils.toHex(jobId),
payment,
url,
path,
times
)
callback(tx.tx)
}
4 changes: 2 additions & 2 deletions evm/box/test/MyContract_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ contract('MyContract', accounts => {
const stranger = accounts[2]
const consumer = accounts[3]

// These parameters are used to validate the data was recieved
// These parameters are used to validate the data was received
// on the deployed oracle contract. The Job ID only represents
// the type of data, but will not work on a public testnet.
// For the latest JobIDs, visit our docs here:
// https://docs.chain.link/v1.1/docs/addresses-and-job-ids
// https://docs.chain.link/docs/testnet-oracles
const jobId = web3.utils.toHex('4c7b7ffb66b344fbaa64995af81e355a')
const url =
'https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD,EUR,JPY'
Expand Down
1 change: 1 addition & 0 deletions truffle-box.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
".prettierrc",
".soliumignore",
".soliumrc.json",
"babel.config.js",
"chainlink-launcher-sgx.sh",
"chainlink-launcher.sh",
"core",
Expand Down

0 comments on commit 2b37dfc

Please sign in to comment.