Skip to content

Commit

Permalink
All working
Browse files Browse the repository at this point in the history
  • Loading branch information
anupam-io committed Apr 9, 2021
1 parent 5dee3af commit 6716347
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 181 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
## Start the Chainlink Node
```
cd ~/.chainlink-kovan
docker run --name chainlink-kovan --network host -p 6688:6688 -v ~/.chainlink-kovan:/chainlink -it --env-file=.env smartcontract/chainlink:0.10.3 local n
Visit: http://localhost:6688/
Expand Down Expand Up @@ -84,4 +83,13 @@
## Deployed contracts

### [Oracle.sol](https://kovan.etherscan.io/address/0x63a7E202B1e0d76C576841fB91E6dB0D03D95a0F)
### [ATestnetConsumer.sol](https://kovan.etherscan.io/address/0x3D07b397734D638906db75859eb97949C9402f72)
### [ATestnetConsumer.sol](https://kovan.etherscan.io/address/0x3D07b397734D638906db75859eb97949C9402f72)

yarn run v1.22.10
$ truffle run verify Oracle APIConsumer --network kovan --license MIT
Verifying Oracle
Pass - Verified: https://kovan.etherscan.io/address/0x23fe945a86b022585401DB244Ba30aa44F2046b2#contracts
Verifying APIConsumer
Pass - Verified: https://kovan.etherscan.io/address/0x6f5374cf4800899a45A7dd6Cb135E428EF4b9278#contracts
Successfully verified 2 contract(s).
Done in 28.65s.
16 changes: 16 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require("express");

const app = express();

app.get("/code", (req, res) => {
res.send({ number: 321.213 });
});

port = 3000;

app.listen(port, () => {
console.log(`Server started at http://localhost:${port}`);
});


`http://localhost:3000/code`
66 changes: 66 additions & 0 deletions contracts/APIConsumer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

import "@chainlink/contracts/src/v0.6/Oracle.sol";
import "@chainlink/contracts/src/v0.6/ChainlinkClient.sol";
import "@chainlink/contracts/src/v0.6/vendor/Ownable.sol";

contract APIConsumer is ChainlinkClient, Ownable {
uint256 private constant ORACLE_PAYMENT = 1 * LINK;

uint256 public currentPrice;
int256 public changeDay;
bytes32 public lastMarket;

event RequestEthereumPriceFulfilled(
bytes32 indexed requestId,
uint256 indexed price
);

constructor() public Ownable() {
setPublicChainlinkToken();
}

function requestEthereumPrice(address _oracle, string memory _jobId)
public
onlyOwner
{
Chainlink.Request memory req =
buildChainlinkRequest(
stringToBytes32(_jobId),
address(this),
this.fulfillEthereumPrice.selector
);
req.add(
"get",
"https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD"
);
req.add("path", "USD");
req.addInt("times", 10000);
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}

function fulfillEthereumPrice(bytes32 _requestId, uint256 _price)
public
recordChainlinkFulfillment(_requestId)
{
emit RequestEthereumPriceFulfilled(_requestId, _price);
currentPrice = _price;
}

function stringToBytes32(string memory source)
private
pure
returns (bytes32 result)
{
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}

assembly {
// solhint-disable-line no-inline-assembly
result := mload(add(source, 32))
}
}
}
127 changes: 0 additions & 127 deletions contracts/ATestnetConsumer.sol

This file was deleted.

68 changes: 27 additions & 41 deletions job_specs/ethuint256.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,28 @@
{
"name": "",
"initiators": [
{
"type": "runlog",
"params": {
"address": "0x63a7e202b1e0d76c576841fb91e6db0d03d95a0f"
}
}
],
"tasks": [
{
"type": "httpget",
"confirmations": 0,
"params": {
"get": "https://bitstamp.net/api/ticker/"
}
},
{
"type": "jsonparse",
"params": {
"path": [
"last"
]
}
},
{
"type": "multiply",
"params": {
"times": 100
}
},
{
"type": "ethuint256"
},
{
"type": "ethtx"
}
],
"startAt": "2020-02-09T15:13:03Z"
}

"name": "LastJob003",
"initiators": [{
"type": "RunLog",
"params": {
"address": "0x23fe945a86b022585401DB244Ba30aa44F2046b2"
}
}],
"tasks": [{
"type": "HTTPGet"
},
{
"type": "JSONParse"
},
{
"type": "Multiply"
},
{
"type": "EthUint256"
},
{
"type": "EthTx"
}
],
"startAt": "2020-02-09T15:13:03Z",
"endAt": null,
"minPayment": "1000000000000000000"
}
8 changes: 8 additions & 0 deletions main_copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { default: axios } = require("axios");

async function main() {
url = `http://localhost:3000/code`;
res = await axios.get(url);
console.log(res.data.number);
}
main();
6 changes: 3 additions & 3 deletions migrations/3_testnet_consumer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const ATestnetConsumer = artifacts.require("ATestnetConsumer");
const APIConsumer = artifacts.require("APIConsumer");

module.exports = function (deployer) {
deployer.deploy(ATestnetConsumer);
};
deployer.deploy(APIConsumer);
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
"scripts": {
"compile": "truffle compile",
"migrate": "truffle migrate --reset --network kovan",
"verify": "truffle run verify Oracle ATestnetConsumer --network kovan --license MIT",
"verify": "truffle run verify Oracle APIConsumer --network kovan --license MIT",
"check": "node main.js"
},
"dependencies": {
"@chainlink/contracts": "^0.1.7",
"@truffle/hdwallet-provider": "^1.2.6",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"pg": "^8.5.1",
"truffle-plugin-verify": "^0.5.7",
"web3": "^1.3.5"
Expand Down
6 changes: 3 additions & 3 deletions scripts/1_fund_link.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Oracle = artifacts.require("Oracle");
const ATestnetConsumer = artifacts.require("ATestnetConsumer");
const APIConsumer = artifacts.require("APIConsumer");
const Link = require("@chainlink/contracts/abi/v0.6/LinkTokenInterface.json");
require("dotenv").config();

Expand All @@ -11,14 +11,14 @@ module.exports = async function (callback) {

const acc = await web3.eth.getAccounts();

const amount = (10 ** 2).toString();
const amount = (5 * 10 ** 18).toString();
tx = await link.methods
.transfer((await Oracle.deployed()).address, amount)
.send({ from: acc[0] });
console.log("gasUsed: ", tx.gasUsed);

tx = await link.methods
.transfer((await ATestnetConsumer.deployed()).address, amount)
.transfer((await APIConsumer.deployed()).address, amount)
.send({ from: acc[0] });
console.log("gasUsed: ", tx.gasUsed);

Expand Down
4 changes: 2 additions & 2 deletions scripts/3_request.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const Oracle = artifacts.require("Oracle");
const ATestnetConsumer = artifacts.require("ATestnetConsumer");
const APIConsumer = artifacts.require("APIConsumer");
require("dotenv").config();

module.exports = async function (callback) {
const con = await ATestnetConsumer.deployed();
const con = await APIConsumer.deployed();

try {
tx = await con.requestEthereumPrice(
Expand Down
2 changes: 1 addition & 1 deletion truffle-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {
providerOrUrl: rpc_url,
}),
network_id: 42, // Any network (default: none)
skipDryRun: false, // Skip dry run before migrations? (default: false for public nets )
skipDryRun: true, // Skip dry run before migrations? (default: false for public nets )
},
// Useful for testing. The `development` name is special - truffle uses it by default
// if it's defined here and no other network is specified at the command line.
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1947,7 +1947,7 @@ evp_bytestokey@^1.0.0, evp_bytestokey@^1.0.3:
md5.js "^1.3.4"
safe-buffer "^5.1.1"

express@^4.14.0:
express@^4.14.0, express@^4.17.1:
version "4.17.1"
resolved "https://registry.yarnpkg.com/express/-/express-4.17.1.tgz#4491fc38605cf51f8629d39c2b5d026f98a4c134"
integrity sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==
Expand Down

0 comments on commit 6716347

Please sign in to comment.