2020-06-17 18:54:29 +00:00
|
|
|
//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'];
|
2020-06-17 18:55:21 +00:00
|
|
|
sumCC +=1;
|
2020-06-17 18:54:29 +00:00
|
|
|
}
|
|
|
|
ret.ccTotal = sumCC;
|
|
|
|
ret.ccTS = sum;
|
|
|
|
ret.dates = dates;
|
|
|
|
return ret;
|
2020-06-17 18:55:21 +00:00
|
|
|
}
|