added mongo suport

This commit is contained in:
osmarperez 2020-06-30 17:04:32 -05:00
parent e5546f3cad
commit bbccb592f8
2 changed files with 76 additions and 45 deletions

View File

@ -6,6 +6,8 @@ const IpfsClient = require('ipfs-http-client');
const OrbitDB = require('orbit-db'); const OrbitDB = require('orbit-db');
//Instance of IPFS locally in IPFS daemon //Instance of IPFS locally in IPFS daemon
const node = IpfsClient('http://localhost:5001'); const node = IpfsClient('http://localhost:5001');
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://"+process.env.USERDB+":"+process.env.PASSDB+"@localhost:27017/"+process.env.DBNAME;
//Module for get prices //Module for get prices
var getEth = require('./getEth'); var getEth = require('./getEth');
@ -35,41 +37,59 @@ async function index() {
indexBy: 'dates' indexBy: 'dates'
}); });
await db3.load(); await db3.load();
const db4 = await orbitdb.docs('decaCCT', { const db4 = await orbitdb.docs('decaCCTS', {
indexBy: 'dates' indexBy: 'dates'
}); });
await db4.load(); await db4.load();
const dbm = await MongoClient.connect(url, { useUnifiedTopology: true }).catch(err => { console.log(err); });
//Flag to save the following data //Flag to save the following data
let dates = 1591980300; let dates = 1593553845;
while (true) { while (true) {
//Get Carbon Credits Data //Get Carbon Credits Data
const data = db.get(''); const data = db.get('');
//Compare flag and unixtime to add data //Compare flag and unixtime to add data
if (Math.round(new Date().getTime() / 1000) == dates) { if (Math.round(new Date().getTime() / 1000) == dates) {
dates += 300; var dbo = dbm.db("DECA");
//Get data from functions //Get data from functions
let elemsGeth = await getGethElems(dates); let elemsGeth = await getGethElems(dates);
let elemsDECAPrice = await getEth(dates); let elemsDECAPrice = await getEth(dates);
let ccTotalEth = await getEthSum(data, 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 //checks every 2 minutes to see if the data has already been inserted into the DBs if not
setTimeout(function () { setTimeout(async function () {
const query1 = db2.query((doc) => doc.dates == dates); const query1 = db2.query((doc) => doc.dates == dates);
const query2 = db3.query((doc) => doc.dates == dates); const query2 = db3.query((doc) => doc.dates == dates);
const query3 = db4.query((doc) => doc.dates == dates); const query3 = db4.query((doc) => doc.dates == dates);
dbo.collection("decaPrice").insertOne(elemsDECAPrice, function(err, res) {
if (err) throw err;
console.log("Insert decaPrice")
});
dbo.collection("decaGeth").insertOne(elemsGeth, function(err, res) {
if (err) throw err;
console.log("Insert decaGeth")
});
dbo.collection("decaCCTS").insertOne(ccTotalEth, function(err, res) {
if (err) throw err;
console.log("Insert decaCCTS")
});
if (query1.length > 0) { if (query1.length > 0) {
let elemsDECAPrice = await getEth(dates);
const hash1 = await db2.put(elemsDECAPrice); const hash1 = await db2.put(elemsDECAPrice);
console.log(hash1); console.log(hash1);
} }
if (query2.length > 0) { if (query2.length > 0) {
let elemsGeth = await getGethElems(dates);
const hash2 = await db3.put(elemsGeth); const hash2 = await db3.put(elemsGeth);
console.log(hash2); console.log(hash2);
} }
if (query3.length > 0) { if (query3.length > 0) {
let ccTotalEth = await getEthSum(data, dates);
const hash3 = await db4.put(ccTotalEth); const hash3 = await db4.put(ccTotalEth);
console.log(hash3); console.log(hash3);
} }
dates += 60;
}, 20000); }, 10000);
} }
} }
} }

View File

@ -1,11 +1,12 @@
//Load Environment Variables //Load Environment Variables
require('dotenv').config() require('dotenv').config()
//IPFS //IPFS
const IpfsClient = require('ipfs-http-client'); const IpfsClient = require('ipfs-http-client');
const OrbitDB = require('orbit-db'); const OrbitDB = require('orbit-db');
//Instance of IPFS locally in IPFS daemon //Instance of IPFS locally in IPFS daemon
const node = IpfsClient('http://localhost:5001'); const node = IpfsClient('http://localhost:5001');
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://accountUser:Asd123456!@localhost:27017/DECA";
//Module for get prices //Module for get prices
var getEth = require('./getEth'); var getEth = require('./getEth');
@ -18,8 +19,7 @@ const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.WebsocketProvider(process.env.INFURAKEY)); const web3 = new Web3(new Web3.providers.WebsocketProvider(process.env.INFURAKEY));
//DECA Contract instance //DECA Contract instance
const decaContract = new web3.eth.Contract(abi, '0xD60DC0805f44d10cAc6594f1a501c67929448957'); const decaContract = new web3.eth.Contract(abi, '0xE453a11fC1925605638cC590897498375973D301');
async function index() { async function index() {
//Open and load orbitdb databases //Open and load orbitdb databases
@ -28,7 +28,7 @@ async function index() {
indexBy: 'CCID' indexBy: 'CCID'
}); });
await db.load(); await db.load();
const db2 = await orbitdb.docs('decaPrices', { const db2 = await orbitdb.docs('decaPrice', {
indexBy: 'dates' indexBy: 'dates'
}); });
await db2.load(); await db2.load();
@ -36,39 +36,50 @@ async function index() {
indexBy: 'dates' indexBy: 'dates'
}); });
await db3.load(); await db3.load();
const db4 = await orbitdb.docs('decaCCT', { const db4 = await orbitdb.docs('decaCCTS', {
indexBy: 'dates' indexBy: 'dates'
}); });
await db4.load(); await db4.load();
const dbm = await MongoClient.connect(url, { useUnifiedTopology: true })
.catch(err => { console.log(err); });
//Flag to save the following data //Flag to save the following data
let dates = 1592331780; let dates = 1593552592;
while (true) { while (true) {
//Get Carbon Credits Data
const data = db.get('');
//Compare flag and unixtime to add data //Compare flag and unixtime to add data
if (Math.round(new Date().getTime() / 1000) == dates) { if (Math.round(new Date().getTime() / 1000) == dates) {
dates += 120; dates += 30;
//Get data from functions //Get data from functions
var dbo = dbm.db("DECA");
try{ try{
let elemsDECAPrice = await getEth(dates); let elemsDECAPrice = await getEth(dates);
console.log(elemsDECAPrice);
const hash1 = await db2.put(elemsDECAPrice); const hash1 = await db2.put(elemsDECAPrice);
dbo.collection("decaPrice").insertOne(elemsDECAPrice, function(err, res) {
if (err) throw err;
console.log("Insert decaPrice")
});
console.log(hash1); console.log(hash1);
}catch{ }catch{
console.error("Error in DECA Prices"); console.error("Error in DECA Prices");
} }
try{ try{
let elemsGeth = await getGethElems(dates); let elemsGeth = await getGethElems(dates);
console.log(elemsGeth);
const hash2 = await db3.put(elemsGeth); const hash2 = await db3.put(elemsGeth);
dbo.collection("decaGeth").insertOne(elemsGeth, function(err, res) {
if (err) throw err;
console.log("Insert decaGeth")
});
console.log(hash2); console.log(hash2);
}catch{ }catch{
console.error("Error in GETH"); console.error("Error in GETH");
} }
try{ try{
const data = await db.get('');
let ccTotalEth = await getEthSum(data, dates); let ccTotalEth = await getEthSum(data, dates);
console.log(ccTotalEth);
const hash3 = await db4.put(ccTotalEth); const hash3 = await db4.put(ccTotalEth);
dbo.collection("decaCCTS").insertOne(ccTotalEth, function(err, res) {
if (err) throw err;
console.log("Insert decaCCTS")
});
console.log(hash3); console.log(hash3);
}catch{ }catch{
console.error("Error in CCDB") console.error("Error in CCDB")