Feature finished
This commit is contained in:
parent
9fe97b99cb
commit
50a77fdeb0
|
@ -0,0 +1,3 @@
|
|||
APIKEY=
|
||||
API_MODE=
|
||||
# Available modes: dev, startup, grow
|
|
@ -1,4 +1,5 @@
|
|||
let moment = require('moment');
|
||||
const moment = require('moment');
|
||||
const request = require('request-promise');
|
||||
require('dotenv').config();
|
||||
|
||||
// Source: https://apiv2.bitcoinaverage.com/
|
||||
|
@ -6,33 +7,27 @@ require('dotenv').config();
|
|||
const divisaTraceback = async(timestamp, divisa, amount) => {
|
||||
let crypto = ['BTC', 'ETH', 'LTC']
|
||||
let normal = ['USD', 'GBP', 'CNY', 'EUR'];
|
||||
try {
|
||||
if((Date.now() - 7776000000) <= (timestamp * 1000)) {
|
||||
if(crypto.includes(divisa) || normal.includes(divisa)) {
|
||||
if(amount >= 0) {
|
||||
let divisaTraceback = {};
|
||||
let resolution = await getResolution(timestamp);
|
||||
if(amount > 0) {
|
||||
let divisaTraceback = {};
|
||||
let resolution = await getResolution(timestamp);
|
||||
if(crypto.includes(divisa)) {
|
||||
divisaTraceback[crypto.splice(crypto.indexOf(divisa), 1)[0]] = Number(amount);
|
||||
Object.assign(divisaTraceback, await requestIteration(normal, divisa, true, timestamp, amount, resolution));
|
||||
Object.assign(divisaTraceback, await requestIteration(crypto, 'USD', false, timestamp, divisaTraceback['USD'], resolution));
|
||||
}
|
||||
else if (normal.includes(divisa)) {
|
||||
divisaTraceback[normal.splice(normal.indexOf(divisa), 1)[0]] = Number(amount);
|
||||
Object.assign(divisaTraceback, await requestIteration(crypto, divisa, false, timestamp, amount, resolution));
|
||||
Object.assign(divisaTraceback, await requestIteration(normal, 'BTC', true, timestamp, divisaTraceback['BTC'], resolution));
|
||||
} else throw new Error('Currency target invalid');
|
||||
|
||||
if(crypto.includes(divisa)) {
|
||||
divisaTraceback[crypto.splice(crypto.indexOf(divisa), 1)[0]] = Number(amount);
|
||||
Object.assign(divisaTraceback, await requestIteration(normal, divisa, true, timestamp, amount, resolution));
|
||||
Object.assign(divisaTraceback, await requestIteration(crypto, 'USD', false, timestamp, divisaTraceback['USD'], resolution));
|
||||
}
|
||||
else {
|
||||
divisaTraceback[normal.splice(normal.indexOf(divisa), 1)[0]] = Number(amount);
|
||||
Object.assign(divisaTraceback, await requestIteration(crypto, divisa, false, timestamp, amount, resolution));
|
||||
Object.assign(divisaTraceback, await requestIteration(normal, 'BTC', true, timestamp, divisaTraceback['BTC'], resolution));
|
||||
}
|
||||
return(divisaTraceback);
|
||||
} else return('The amount must be 1 or higher');
|
||||
} else return('Currency target invalid');
|
||||
} else return('Date out of range')
|
||||
} catch(e) {
|
||||
throw 'error';
|
||||
}
|
||||
console.log(` Resolution: ${resolution}`);
|
||||
return(divisaTraceback);
|
||||
} else throw new Error('The amount must be 0 or higher');
|
||||
};
|
||||
|
||||
const requestIteration = async(set, divisa, flag, timestamp, amount, resolution) => {
|
||||
timestamp = timestamp.format('X');
|
||||
let opt = {
|
||||
method: 'GET',
|
||||
json: true,
|
||||
|
@ -44,12 +39,14 @@ const requestIteration = async(set, divisa, flag, timestamp, amount, resolution)
|
|||
for (element in set) {
|
||||
if(flag) {
|
||||
opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${divisa}${set[element]}?at=${timestamp}&resolution=${resolution}`;
|
||||
let { average } = await request(opt);
|
||||
let { average, time } = await request(opt);
|
||||
if(res.time === undefined) res.time = moment.utc(time, 'YYYY-MM-DD HH:mm:ss').format('DD-MM-YYYY HH:mm Z');
|
||||
res[set[element]] = average * amount;
|
||||
}
|
||||
else {
|
||||
opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${set[element]}${divisa}?at=${timestamp}&resolution=${resolution}`;
|
||||
let { average } = await request(opt);
|
||||
let { average, time } = await request(opt);
|
||||
if(res.time === undefined) res.time = moment.utc(time, 'YYYY-MM-DD HH:mm:ss').format('DD-MM-YYYY HH:mm Z');
|
||||
res[set[element]] = amount / average;
|
||||
}
|
||||
}
|
||||
|
@ -58,17 +55,13 @@ const requestIteration = async(set, divisa, flag, timestamp, amount, resolution)
|
|||
|
||||
const getResolution = async(timestamp) => {
|
||||
let resolution = 'day';
|
||||
let momentTime = moment(timestamp);
|
||||
if(process.env.API_MODE == 'dev') {
|
||||
if(momentTime >= moment().subtract(2, 'hours')) resolution = 'minute';
|
||||
else if(momentTime >= moment().subtract(10, 'days')) resolution = 'hour';
|
||||
|
||||
} else if(process.env.API_MODE == 'startup' || process.env.API_MODE == 'grow') {
|
||||
if(momentTime >= moment().subtract(24, 'hours')) resolution = 'minute';
|
||||
else if(momentTime >= moment().subtract(31, 'days')) resolution = 'hour';
|
||||
|
||||
} else throw new Error('Bad config');
|
||||
|
||||
if(process.env.APIMODE == 'dev') {
|
||||
if(timestamp >= moment.utc().subtract(2, 'hours')) resolution = 'minute';
|
||||
else if(timestamp >= moment().subtract(10, 'days')) resolution = 'hour';
|
||||
} else if(process.env.APIMODE == 'startup' || process.env.APIMODE == 'grow') {
|
||||
if(timestamp >= moment.utc().subtract(24, 'hours')) resolution = 'minute';
|
||||
else if(timestamp >= moment.utc().subtract(31, 'days')) resolution = 'hour';
|
||||
} else throw new Error('Bad config at .env file');
|
||||
return resolution;
|
||||
}
|
||||
|
||||
|
|
19
interfaz.js
19
interfaz.js
|
@ -91,7 +91,7 @@ const main = async () => {
|
|||
}
|
||||
while (true) {
|
||||
await init();
|
||||
//console.clear();
|
||||
console.clear();
|
||||
console.log('Select one to modify the field.\n');
|
||||
let carbonCreditMenu = await generateData(carbonCredit, 0);
|
||||
let { insertMenu } = await inquirer.prompt([{
|
||||
|
@ -113,7 +113,7 @@ const main = async () => {
|
|||
if (await validateInsertion(carbonCredit)) {
|
||||
try{
|
||||
carbonCredit.conversionPrice = await divisaTraceback(
|
||||
moment.utc(carbonCredit.cancelDate + ' +0000', 'DD-MM-YYYY HH:mm Z').unix(),
|
||||
moment.utc(carbonCredit.cancelDate, 'DD-MM-YYYY HH:mm Z'),
|
||||
carbonCredit.cancelPrice.divisa,
|
||||
carbonCredit.cancelPrice.qty
|
||||
);
|
||||
|
@ -148,10 +148,10 @@ const main = async () => {
|
|||
} else {
|
||||
console.clear();
|
||||
console.log('Insertion aborted');
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
} catch(e) {
|
||||
console.log(e.message);
|
||||
console.log(`Error at divisaTraceback: ${e.message}`);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
@ -187,11 +187,12 @@ const main = async () => {
|
|||
if (input == '') console.log('Input mustn\'t be empty');
|
||||
else if (insertMenu == 'issueDate' || insertMenu == 'cancelDate' || insertMenu == 'ccVintageStart' || insertMenu == 'ccVintageEnd') {
|
||||
const date = moment.utc(input + ' +0000', 'DD-MM-YYYY HH:mm');
|
||||
if(date.isValid() && moment() >= date) {
|
||||
console.log(date);
|
||||
if(date.isValid() && moment.utc() >= date) {
|
||||
if(insertMenu != 'cancelDate') carbonCredit[insertMenu] = date.format('DD-MM-YYYY HH:mm Z');
|
||||
else if((date >= moment().subtract(3, 'months') && process.env.API_MODE == 'dev') ||
|
||||
(date >= moment().subtract(1, 'year') && process.env.API_MODE == 'startup') ||
|
||||
(date >= moment().subtract(7, 'year') && process.env.API_MODE == 'grow')) {
|
||||
else if((date >= moment.utc().subtract(3, 'months') && process.env.APIMODE == 'dev') ||
|
||||
(date >= moment.utc().subtract(1, 'year') && process.env.APIMODE == 'startup') ||
|
||||
(date >= moment.utc().subtract(7, 'year') && process.env.APIMODE == 'grow')) {
|
||||
carbonCredit[insertMenu] = date.format('DD-MM-YYYY HH:mm Z');
|
||||
} else console.error(' Date is too old to get pricing data');
|
||||
} else console.error(' Date must follow next syntax: dd-mm-yyyy hh-mm and can\'t be in the future');
|
||||
|
@ -529,7 +530,7 @@ const generateData = async (data, n) => {
|
|||
menu.push({ name: element, disabled: ' ' });
|
||||
menu = menu.concat(await generateData(data[element], n + 1));
|
||||
} else {
|
||||
let space = ' '.repeat(20 - element.length);
|
||||
let space = ' '.repeat(25 - element.length);
|
||||
space = space.substring(0, space.length - tab.length);
|
||||
menu.push(`${tab}${element}${space}${data[element]}`);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue