Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add formatter #39

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run format
31 changes: 16 additions & 15 deletions config/userInput.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
{
"start": "2021-01-01",
"end": "2021-01-31",
"currency": "CHF",
"priceData": "true",
"exportOutput": "true",
"addresses": [
"start": "2021-01-01",
"end": "2021-01-31",
"currency": "CHF",
"priceData": "true",
"exportOutput": "true",
"addresses": [
{
"name": "Account 1",
"address":"G1rrUNQSk7CjjEmLSGcpNu72tVtyzbWdUvgmSer9eBitXWf",
"startBalance": 10000,
"network": "Kusama"
"name": "Account 1",
"address": "G1rrUNQSk7CjjEmLSGcpNu72tVtyzbWdUvgmSer9eBitXWf",
"startBalance": 10000,
"network": "Kusama"
},

{
"name":"Account 2",
"address": "15fTw39Ju2jJiHeGe1fJ5DtgugUauy9tr2HZuiRNFwqnGQ1Q",
"startBalance": 20000,
"network": "Polkadot"
"name": "Account 2",
"address": "15fTw39Ju2jJiHeGe1fJ5DtgugUauy9tr2HZuiRNFwqnGQ1Q",
"startBalance": 20000,
"network": "Polkadot"
}
]}
]
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
"dependencies": {
"coingecko-api": "^1.0.10",
"curlrequest": "^1.0.1",
"mathjs": "^8.0.1"
"husky": "^7.0.4",
"mathjs": "^8.0.1",
"prettier": "^2.5.1"
},
"scripts": {
"format": "prettier -w ./config ./src",
"prepare": "husky install",
"start": "node src/index.js"
}
}
219 changes: 117 additions & 102 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -1,123 +1,138 @@
import CoinGecko from 'coingecko-api';
import { ceil, round } from 'mathjs';
import { transformDDMMYYYtoUnix } from './utils.js';



export async function addPriceData(obj){
let priceObject = await _getPriceObject(obj);
var prices;
var total_volume;


prices = _arrayToObject(priceObject.data.prices, "price");
total_volume = _arrayToObject(priceObject.data.total_volumes, "volume");

// set index to first day price were available to avoid looking for prices where are none
let i = _setIndex(obj);

for(i;i<obj.data.list.length;i++){

let tmp = transformDDMMYYYtoUnix(obj.data.list[i].day);
obj.data.list[i].price = round(prices.find(x => x.timestamp == tmp).price,2);
obj.data.list[i].volume = total_volume.find(x => x.timestamp == tmp).volume;
}
return obj;
import CoinGecko from "coingecko-api";
import { ceil, round } from "mathjs";
import { transformDDMMYYYtoUnix } from "./utils.js";

export async function addPriceData(obj) {
let priceObject = await _getPriceObject(obj);
var prices;
var total_volume;

prices = _arrayToObject(priceObject.data.prices, "price");
total_volume = _arrayToObject(priceObject.data.total_volumes, "volume");

// set index to first day price were available to avoid looking for prices where are none
let i = _setIndex(obj);

for (i; i < obj.data.list.length; i++) {
let tmp = transformDDMMYYYtoUnix(obj.data.list[i].day);
obj.data.list[i].price = round(
prices.find((x) => x.timestamp == tmp).price,
2
);
obj.data.list[i].volume = total_volume.find(
(x) => x.timestamp == tmp
).volume;
}
return obj;
}

async function _getPriceObject(obj){
const CoinGeckoClient = new CoinGecko();
var priceObject;

let start = transformDDMMYYYtoUnix(obj.data.list[0].day);
let end = transformDDMMYYYtoUnix(obj.data.list.slice(-1)[0].day);

// Avoid getting hourly or minute price data.
end = _checkDuration(start, end);

try{
priceObject = await CoinGeckoClient.coins.fetchMarketChartRange(obj.network, {
from: start,
to: end,
vs_currency: obj.currency,
});
} catch (e){
console.log('Error in parsing CoinGecko Data' + e);
}
return priceObject;
async function _getPriceObject(obj) {
const CoinGeckoClient = new CoinGecko();
var priceObject;

let start = transformDDMMYYYtoUnix(obj.data.list[0].day);
let end = transformDDMMYYYtoUnix(obj.data.list.slice(-1)[0].day);

// Avoid getting hourly or minute price data.
end = _checkDuration(start, end);

try {
priceObject = await CoinGeckoClient.coins.fetchMarketChartRange(
obj.network,
{
from: start,
to: end,
vs_currency: obj.currency,
}
);
} catch (e) {
console.log("Error in parsing CoinGecko Data" + e);
}
return priceObject;
}

/*
CoinGecko API returns a list of arrays without a key, value pair. This function creates an object from that list.
*/

function _arrayToObject(array, key){
let name = key;
let obj = [];
// populate the key
for(let i=0; i<array.length; i++){
obj[i] = {
"timestamp" : array[i][0] / 1000,
[name]: array[i][1],
}
}
return obj;
function _arrayToObject(array, key) {
let name = key;
let obj = [];
// populate the key
for (let i = 0; i < array.length; i++) {
obj[i] = {
timestamp: array[i][0] / 1000,
[name]: array[i][1],
};
}
return obj;
}

/*
This function checks if the user did input a time-period larger than 90 days. Minutely data will be provided for duration within 1 day and Hourly data will be used for duration between 1 day and 90 days. We are only interested in daily data, so we check if the duration is less than 90 days and then just increase it artificially (only the prices within the time-period of the user will be used later).
*/
*/

function _checkDuration(start, end){
var setEnd;
function _checkDuration(start, end) {
var setEnd;

let duration = (end - start) / 60 / 60 / 24;
let duration = (end - start) / 60 / 60 / 24;

if(duration < 90){
setEnd = start + 91 * 60 * 60 * 24;
} else {
setEnd = end;
}
return setEnd;
if (duration < 90) {
setEnd = start + 91 * 60 * 60 * 24;
} else {
setEnd = end;
}
return setEnd;
}

/*
This function checks when prices were available and sets the index correspondingly to avoid looking for prices when there were none available.
*/

function _setIndex(obj){
var index;

let network = obj.network;

if(network == 'polkadot'){
index = obj.data.list.findIndex(x => transformDDMMYYYtoUnix(x.day) > 1597708800);
}

if(network == 'kusama'){
index = obj.data.list.findIndex(x => transformDDMMYYYtoUnix(x.day) > 1568851200);
}

if(network == 'moonriver' ){
index = obj.data.list.findIndex(x => transformDDMMYYYtoUnix(x.day) > 1630022400);
}

if(network == 'moonbeam'){
index = obj.data.list.findIndex(x => transformDDMMYYYtoUnix(x.day) > 1641884400);
}

if(network == 'shiden'){
index = obj.data.list.findIndex(x => transformDDMMYYYtoUnix(x.day) > 1630303200);
}

if(network == 'astar'){
index = obj.data.list.findIndex(x => transformDDMMYYYtoUnix(x.day) > 1642402800);
}


if(index < 0){
index = 0;
}

return index;
}
function _setIndex(obj) {
var index;

let network = obj.network;

if (network == "polkadot") {
index = obj.data.list.findIndex(
(x) => transformDDMMYYYtoUnix(x.day) > 1597708800
);
}

if (network == "kusama") {
index = obj.data.list.findIndex(
(x) => transformDDMMYYYtoUnix(x.day) > 1568851200
);
}

if (network == "moonriver") {
index = obj.data.list.findIndex(
(x) => transformDDMMYYYtoUnix(x.day) > 1630022400
);
}

if (network == "moonbeam") {
index = obj.data.list.findIndex(
(x) => transformDDMMYYYtoUnix(x.day) > 1641884400
);
}

if (network == "shiden") {
index = obj.data.list.findIndex(
(x) => transformDDMMYYYtoUnix(x.day) > 1630303200
);
}

if (network == "astar") {
index = obj.data.list.findIndex(
(x) => transformDDMMYYYtoUnix(x.day) > 1642402800
);
}

if (index < 0) {
index = 0;
}

return index;
}
Loading