Skip to content
This repository has been archived by the owner on Feb 25, 2019. It is now read-only.

Commit

Permalink
Add in example and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thodges-gh committed May 9, 2018
1 parent c45f423 commit 49c16d1
Show file tree
Hide file tree
Showing 13 changed files with 14,348 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
build/
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
# Chainlinked

Implementation of [How to make a Chainlinked contract](https://github.com/smartcontractkit/chainlink/wiki/How-to-make-a-Chainlinked-contract).

## Requirements

- NPM
- Truffle

## Installation

```bash
$ npm install
```

## Test

```bash
$ truffle test
```

## Deploy

Local development

```bash
$ truffle migrate --network development
```

Ropsten (requires unlocked & synced Ropsten node)

```bash
$ truffle migrate -f 4 --network ropsten --compile-all
```
23 changes: 23 additions & 0 deletions contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pragma solidity ^0.4.17;

contract Migrations {
address public owner;
uint public last_completed_migration;

modifier restricted() {
if (msg.sender == owner) _;
}

function Migrations() public {
owner = msg.sender;
}

function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}

function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}
51 changes: 51 additions & 0 deletions contracts/MyContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
pragma solidity ^0.4.23;

import "chainlink/solidity/contracts/Chainlinked.sol";
import "openzeppelin-solidity/contracts/ownership/Ownable.sol";

contract MyContract is Chainlinked, Ownable {
bytes32 internal requestId;
bytes32 internal jobId;
bytes32 public currentPrice;

event RequestFulfilled(
bytes32 indexed requestId,
bytes32 indexed price
);

function MyContract(address _link, address _oracle, bytes32 _jobId) public {
setLinkToken(_link);
setOracle(_oracle);
jobId = _jobId;
}

function requestEthereumPrice(string _currency) public onlyOwner {
ChainlinkLib.Run memory run = newRun(jobId, this, "fulfill(bytes32,bytes32)");
run.add("url", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD,EUR,JPY");
string[] memory path = new string[](1);
path[0] = _currency;
run.addStringArray("path", path);
requestId = chainlinkRequest(run, LINK(1));
}

function cancelRequest(uint256 _internalId)
public
onlyOwner
{
oracle.cancel(_internalId);
}

function fulfill(bytes32 _requestId, bytes32 _price)
public
onlyOracle
checkRequestId(_requestId)
{
emit RequestFulfilled(_requestId, _price);
currentPrice = _price;
}

modifier checkRequestId(bytes32 _requestId) {
require(requestId == _requestId);
_;
}
}
7 changes: 7 additions & 0 deletions migrations/1_initial_migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var Migrations = artifacts.require("./Migrations.sol");

module.exports = function(deployer) {
if (network == "test" || network == "development") {
deployer.deploy(Migrations);
}
};
8 changes: 8 additions & 0 deletions migrations/2_link_token_contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var LinkToken = artifacts.require("linktoken/contracts/LinkToken.sol");

module.exports = function(deployer, network) {
if (network == "test" || network == "development") {
deployer.deploy(LinkToken);
}
};

8 changes: 8 additions & 0 deletions migrations/3_oracle_contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var Oracle = artifacts.require("chainlink/solidity/contracts/Oracle.sol");
var LinkToken = artifacts.require("linktoken/contracts/LinkToken.sol");

module.exports = function(deployer, network) {
if (network == "test" || network == "development") {
deployer.deploy(Oracle, LinkToken.address);
}
};
11 changes: 11 additions & 0 deletions migrations/4_mycontract_migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var MyContract = artifacts.require("MyContract.sol");
var Oracle = artifacts.require("Oracle.sol");
var LinkToken = artifacts.require("LinkToken.sol");

module.exports = function(deployer, network) {
if (network == "ropsten") {
deployer.deploy(MyContract, "0x20fE562d797A42Dcb3399062AE9546cd06f63280", "0x5be84B6381d45579Ed04A887B8473F76699E0389", "0x3236363037363337303734333466333562623538613964623333656531383033");
} else {
deployer.deploy(MyContract, LinkToken.address, Oracle.address, "0x3463376237666662363662333434666261613634393935616638316533353561");
}
};
Loading

0 comments on commit 49c16d1

Please sign in to comment.