diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f5404bb --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/node_modules +/orbitdb +package-lock.json +.env + diff --git a/ABI.js b/ABI.js new file mode 100644 index 0000000..7c198ae --- /dev/null +++ b/ABI.js @@ -0,0 +1,437 @@ +const abi = [{ + "constant": false, + "inputs": [], + "name": "acceptOwnership", + "outputs": [], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": false, + "inputs": [{ + "name": "spender", + "type": "address" + }, + { + "name": "tokens", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [{ + "name": "success", + "type": "bool" + }], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": false, + "inputs": [{ + "name": "spender", + "type": "address" + }, + { + "name": "tokens", + "type": "uint256" + }, + { + "name": "data", + "type": "bytes" + } + ], + "name": "approveAndCall", + "outputs": [{ + "name": "success", + "type": "bool" + }], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": false, + "inputs": [{ + "name": "spender", + "type": "address" + }, + { + "name": "tokens", + "type": "uint256" + } + ], + "name": "decreaseApproval", + "outputs": [{ + "name": "success", + "type": "bool" + }], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": false, + "inputs": [], + "name": "getETH", + "outputs": [], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": false, + "inputs": [{ + "name": "spender", + "type": "address" + }, + { + "name": "tokens", + "type": "uint256" + } + ], + "name": "increaseApproval", + "outputs": [{ + "name": "success", + "type": "bool" + }], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": false, + "inputs": [{ + "name": "to", + "type": "address" + }, + { + "name": "tokens", + "type": "uint256" + } + ], + "name": "transfer", + "outputs": [{ + "name": "success", + "type": "bool" + }], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": false, + "inputs": [{ + "name": "tokenAddress", + "type": "address" + }, + { + "name": "tokens", + "type": "uint256" + } + ], + "name": "transferAnyERC20Token", + "outputs": [{ + "name": "success", + "type": "bool" + }], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": false, + "inputs": [{ + "name": "from", + "type": "address" + }, + { + "name": "to", + "type": "address" + }, + { + "name": "tokens", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [{ + "name": "success", + "type": "bool" + }], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": false, + "inputs": [{ + "name": "_newOwner", + "type": "address" + }], + "name": "transferOwnership", + "outputs": [], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": false, + "inputs": [{ + "name": "CCDBAddress", + "type": "string" + }], + "name": "updateCCDBAddress", + "outputs": [], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "payable": true, + "stateMutability": "payable", + "type": "fallback" + }, + { + "anonymous": false, + "inputs": [{ + "indexed": true, + "name": "_from", + "type": "address" + }, + { + "indexed": true, + "name": "_to", + "type": "address" + } + ], + "name": "OwnershipTransferred", + "type": "event" + }, + { + "anonymous": false, + "inputs": [{ + "indexed": true, + "name": "from", + "type": "address" + }, + { + "indexed": true, + "name": "to", + "type": "address" + }, + { + "indexed": false, + "name": "tokens", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + }, + { + "anonymous": false, + "inputs": [{ + "indexed": true, + "name": "tokenOwner", + "type": "address" + }, + { + "indexed": true, + "name": "spender", + "type": "address" + }, + { + "indexed": false, + "name": "tokens", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + }, + { + "constant": true, + "inputs": [], + "name": "_CCDBAddress", + "outputs": [{ + "name": "", + "type": "string" + }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "_totalSupply", + "outputs": [{ + "name": "", + "type": "uint256" + }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [{ + "name": "tokenOwner", + "type": "address" + }, + { + "name": "spender", + "type": "address" + } + ], + "name": "allowance", + "outputs": [{ + "name": "remaining", + "type": "uint256" + }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [{ + "name": "tokenOwner", + "type": "address" + }], + "name": "balanceOf", + "outputs": [{ + "name": "balance", + "type": "uint256" + }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "bonus1Ends", + "outputs": [{ + "name": "", + "type": "uint256" + }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "bonus2Ends", + "outputs": [{ + "name": "", + "type": "uint256" + }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "decimals", + "outputs": [{ + "name": "", + "type": "uint8" + }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "endDate", + "outputs": [{ + "name": "", + "type": "uint256" + }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "name", + "outputs": [{ + "name": "", + "type": "string" + }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "newOwner", + "outputs": [{ + "name": "", + "type": "address" + }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "owner", + "outputs": [{ + "name": "", + "type": "address" + }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "preICOEnds", + "outputs": [{ + "name": "", + "type": "uint256" + }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "symbol", + "outputs": [{ + "name": "", + "type": "string" + }], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "totalSupply", + "outputs": [{ + "name": "", + "type": "uint256" + }], + "payable": false, + "stateMutability": "view", + "type": "function" + } +] + + + +module.exports = abi \ No newline at end of file diff --git a/getEth.js b/getEth.js new file mode 100644 index 0000000..4ab1e88 --- /dev/null +++ b/getEth.js @@ -0,0 +1,31 @@ +const request = require('request-promise'); + +require('dotenv').config() + +// Source: https://apiv2.bitcoinaverage.com/ + +const getEth = async(dates) => { + const crypto = ['BTC', 'ETH', 'LTC'] + const normal = ['USD', 'GBP', 'CNY', 'EUR']; + res = {} + let opt = { + method: 'GET', + json: true, + headers: { + 'X-Testing': 'testing', + 'x-ba-key': process.env.APIKEY + }, + }; + for(element in crypto){ + opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${crypto[element]}USD?at=${Math.ceil(Date.now()/1000)-1}&resolution=minute` + res[crypto[element]] = (await request(opt))['average']; + } + for(element in normal){ + opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/ETH${normal[element]}?at=${Math.ceil(Date.now()/1000)-1}&resolution=minute` + res[normal[element]] = (await request(opt))['average']; + } + res.dates = dates; + return res; +}; + +module.exports = getEth; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..86aab13 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "scriptgraficas", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "ipfs-http-client": "^43.0.1", + "request-promise": "^4.2.5", + "web3": "^1.2.8", + "orbit-db": "^0.24.1", + "orbit-db-docstore": "^1.9.0", + "orbit-db-store": "^3.3.0", + "dotenv": "^8.2.0" + } +} diff --git a/scriptUpdateNode1.js b/scriptUpdateNode1.js new file mode 100644 index 0000000..2c91c8f --- /dev/null +++ b/scriptUpdateNode1.js @@ -0,0 +1,110 @@ +//Load Environment Variables +require('dotenv').config() + +//IPFS +const IpfsClient = require('ipfs-http-client'); +const OrbitDB = require('orbit-db'); +//Instance of IPFS locally in IPFS daemon +const node = IpfsClient('http://localhost:5001'); + +//Module for get prices +var getEth = require('./getEth'); + +//ABI from contract DECA ERC20 +const abi = require("./ABI.js"); + +//Web3 and IPC Provider +const Web3 = require('web3'); +const web3 = new Web3(new Web3.providers.WebsocketProvider(process.env.INFURAKEY)); + +//DECA Contract instance +const decaContract = new web3.eth.Contract(abi, '0xD60DC0805f44d10cAc6594f1a501c67929448957'); + + +async function index() { + //Open and load orbitdb databases + const orbitdb = await OrbitDB.createInstance(node) + const db = await orbitdb.docs('/orbitdb/zdpuAykPJ4qtBg2toS2vxr5eaPfGEBJmvGerM7V7x8qn5c8hW/decaCCDB', { + indexBy: 'CCID' + }); + await db.load(); + const db2 = await orbitdb.docs('decaPrices', { + indexBy: 'dates' + }); + await db2.load(); + const db3 = await orbitdb.docs('decaGeth', { + indexBy: 'dates' + }); + await db3.load(); + const db4 = await orbitdb.docs('decaCCT', { + indexBy: 'dates' + }); + await db4.load(); + //Flag to save the following data + let dates = 1592331780; + while (true) { + //Get Carbon Credits Data + const data = db.get(''); + //Compare flag and unixtime to add data + if (Math.round(new Date().getTime() / 1000) == dates) { + dates += 120; + //Get data from functions + try{ + let elemsDECAPrice = await getEth(dates); + console.log(elemsDECAPrice); + const hash1 = await db2.put(elemsDECAPrice); + console.log(hash1); + }catch{ + console.error("Error in DECA Prices"); + } + try{ + let elemsGeth = await getGethElems(dates); + console.log(elemsGeth); + const hash2 = await db3.put(elemsGeth); + console.log(hash2); + }catch{ + console.error("Error in GETH"); + } + try{ + let ccTotalEth = await getEthSum(data, dates); + console.log(ccTotalEth); + const hash3 = await db4.put(ccTotalEth); + console.log(hash3); + }catch{ + console.error("Error in CCDB") + } + } + } +} + +index(); + +//Function to save data from web3 and contract (ret TotalSupply, Ethereum, Date) +const getGethElems = async (dates) => { + let ret = {}; + var balanceEthereum = await web3.eth.getBalance('0xD60DC0805f44d10cAc6594f1a501c67929448957'); + var totalSupply = await decaContract.methods.totalSupply().call(); + totalSupply = web3.utils.fromWei(totalSupply, 'ether'); + balanceEthereum = web3.utils.fromWei(balanceEthereum, 'ether'); + ret.DTS = totalSupply; + ret.ETS = balanceEthereum; + ret.dates = dates; + return ret; +} + +//Function to save data from orbitDB CC (ret ccTotal, ccTS, dates) +const getEthSum = async (elemsCC, dates) => { + const ret = {}; + let sum = 0; + let sumCC = 0; + for (let key in elemsCC) { + sum += elemsCC[key]['conversionPrice']['ETH']; + } + for (let key in elemsCC) { + sumCC += 1; + } + ret.ccTotal = sumCC; + ret.ccTS = sum; + ret.dates = dates; + return ret; +} \ No newline at end of file diff --git a/scriptUpdateNode2.js b/scriptUpdateNode2.js new file mode 100644 index 0000000..9c53af0 --- /dev/null +++ b/scriptUpdateNode2.js @@ -0,0 +1,107 @@ +//Load Environment Variables +require('dotenv').config() + +//IPFS +const IpfsClient = require('ipfs-http-client'); +const OrbitDB = require('orbit-db'); +//Instance of IPFS locally in IPFS daemon +const node = IpfsClient('http://localhost:5001'); + +//Module for get prices +var getEth = require('./getEth'); + +//ABI from contract DECA ERC20 +const abi = require("./ABI.js"); + +//Web3 and IPC Provider +const Web3 = require('web3'); +const web3 = new Web3(new Web3.providers.WebsocketProvider(process.env.INFURAKEY)); + +//DECA Contract instance +const decaContract = new web3.eth.Contract(abi, '0xD60DC0805f44d10cAc6594f1a501c67929448957'); + +async function index() { + //Open and load orbitdb databases + const orbitdb = await OrbitDB.createInstance(node) + const db = await orbitdb.docs('/orbitdb/zdpuAykPJ4qtBg2toS2vxr5eaPfGEBJmvGerM7V7x8qn5c8hW/decaCCDB', { + indexBy: 'CCID' + }); + await db.load(); + const db2 = await orbitdb.docs('decaPrices', { + indexBy: 'dates' + }); + await db2.load(); + const db3 = await orbitdb.docs('decaGeth', { + indexBy: 'dates' + }); + await db3.load(); + const db4 = await orbitdb.docs('decaCCT', { + indexBy: 'dates' + }); + await db4.load(); + //Flag to save the following data + let dates = 1591980300; + while (true) { + //Get Carbon Credits Data + const data = db.get(''); + //Compare flag and unixtime to add data + if (Math.round(new Date().getTime() / 1000) == dates) { + dates += 300; + //Get data from functions + let elemsGeth = await getGethElems(dates); + let elemsDECAPrice = await getEth(dates); + let ccTotalEth = await getEthSum(data, dates); + //checks every 2 minutes to see if the data has already been inserted into the DBs if not + setTimeout(function () { + const query1 = db2.query((doc) => doc.dates == dates); + const query2 = db3.query((doc) => doc.dates == dates); + const query3 = db4.query((doc) => doc.dates == dates); + if(query1.length > 0){ + const hash1 = await db2.put(elemsDECAPrice); + console.log(hash1); + } + if(query2.length > 0){ + const hash2 = await db3.put(elemsGeth); + console.log(hash2); + } + if(query3.length > 0){ + const hash3 = await db4.put(ccTotalEth); + console.log(hash3); + } + + }, 20000); + } + } +} + +index(); + +//Function to save data from web3 and contract (ret TotalSupply, Ethereum, Date) +const getGethElems = async (dates) => { + let ret = {}; + var balanceEthereum = await web3.eth.getBalance('0xD60DC0805f44d10cAc6594f1a501c67929448957'); + var totalSupply = await decaContract.methods.totalSupply().call(); + totalSupply = web3.utils.fromWei(totalSupply, 'ether'); + balanceEthereum = web3.utils.fromWei(balanceEthereum, 'ether'); + ret.DTS = totalSupply; + ret.ETS = balanceEthereum; + ret.dates = dates; + return ret; +} + +//Function to save data from orbitDB CC (ret ccTotal, ccTS, dates) +const getEthSum = async (elemsCC, dates) => { + const ret = {}; + let sum = 0; + let sumCC = 0; + for (let key in elemsCC) { + sum += elemsCC[key]['conversionPrice']['ETH']; + } + for (let key in elemsCC) { + sumCC += 1; + } + ret.ccTotal = sumCC; + ret.ccTS = sum; + ret.dates = dates; + return ret; +} \ No newline at end of file