Browse Source

Merge 5083e7a300 into 3b2d6c9bfb

pull/1/merge
Mayel de Borniol 5 years ago
committed by GitHub
parent
commit
5a38b2fe2c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 224 additions and 12 deletions
  1. +2
    -0
      .gitignore
  2. +11
    -2
      README.md
  3. +147
    -0
      extension/extension.js
  4. +2
    -0
      extension/jquery-3.3.1.min.js
  5. +22
    -0
      extension/manifest.json
  6. +20
    -4
      index.html
  7. +20
    -6
      index.js

+ 2
- 0
.gitignore

@ -1,3 +1,5 @@
node_modules
package-lock.json
data
*.bak
*.pem

+ 11
- 2
README.md

@ -1,5 +1,14 @@
# faircoin-calculator
Very simple Faircoin calculator.
Simple Faircoin calculator to show the historical value of FairCoin at any given date.
The price dataset is from http://api.fairplayground.info/rawdata/faircoin_prices.csv
You can use it on the web at http://mayel.space/faircoin-calculator/ or install the browser extension which displays the value of FairCoin at the time of each transaction when <a href="https://chain.fair.to/">browsing the blockchain.</a>
Extension available for <a href="https://addons.mozilla.org/en-US/firefox/addon/faircoin-calculator/">Firefox</a> and <a href="https://chrome.google.com/webstore/detail/faircoin-calculator/alnbjhldpppajfdoknbdpienjbiogmlp">Chrome</a>.
If you're happy with the FairCoin Calculator, you can donate at <a href="faircoin:fMGcrP9nAFic4356F4oHDDWVq8YvtJSXZL">fMGcrP9nAFic4356F4oHDDWVq8YvtJSXZL</a>
The price dataset is getted from http://api.fairplayground.info/rawdata/faircoin_prices.csv

+ 147
- 0
extension/extension.js

@ -0,0 +1,147 @@
var total_fair;
var total_euro;
var total_market;
$(function() {
ajax({
type: "GET",
url: "https://api.fairplayground.info/rawdata/faircoin_prices.csv",
}, processData);
function processData(data) {
if(data) priceData = csvToArray(data);
// $(".inner-cont").append("priceData: ");
// $(".inner-cont").append(priceData);
var heads = "<p style='text-align:center; background:#fff'>If you're happy with the FairCoin Calculator, you can donate at <a href='faircoin:fMGcrP9nAFic4356F4oHDDWVq8YvtJSXZL'>fMGcrP9nAFic4356F4oHDDWVq8YvtJSXZL</a>";
$("body").append(heads);
if (priceData) {
var heads = '<th>Official Rate</th><th>Official Value</th><th>Market Value</th>';
if($("thead tr").length){ // faircoin 2
$("thead tr").append(heads);
$('tbody').each(function() {
total_fair = total_euro = total_market = 0;
$(this).find('tr').each(function() {
var dateVar = $(this).find('td:first').text();
var d = new Date(dateVar);
var dateISO = d.toISOString().slice(0, 10);
var faircoins = parseFloat($(this).find('td:nth-child(3)').text());
// $(this).append('<td>'+faircoins+'</td>');
calculateEuros(dateISO, faircoins, this);
});
$(this).append('<tr><td><strong>Totals</strong></td><td></td><td>' + (Math.round(total_fair * 100) / 100) + ' FAIR</td><td></td><td>€ ' + (Math.round(total_euro * 100) / 100) + '</td><td>€ ' + (Math.round(total_market * 100) / 100) + '</td></tr>');
});
} else { // faircoin 1
$("tbody tr:first-child").append(heads);
$('tbody tr.direct').each(function() {
var dateVar = $(this).find('td:nth-child(3)').text();
var d = new Date(dateVar);
var dateISO = d.toISOString().slice(0, 10);
var faircoins = cleanUpCurrency($(this).find('td:nth-child(4)').text());
// $(this).append('<td>'+faircoins+'</td>');
calculateEuros(dateISO, faircoins, this);
});
}
}
};
function calculateEuros(date, faircoins, el) {
if (!faircoins) {
console.log("please, fill the faircoin input box");
return;
}
if (!date) {
console.log("no date");
return;
}
//let's find the date in the priceData
var selectedPriceData = [];
for (var i = 0; i < priceData.length; i++) {
if (priceData[i][0] == date) {
selectedPriceData = priceData[i];
}
}
if (selectedPriceData.length < 1) {
console.log("selected date not found in the dataset");
return;
}
// console.log(selectedPriceData);
var euros = faircoins * selectedPriceData[3];
var market = faircoins * selectedPriceData[2];
total_fair = total_fair+faircoins;
total_euro = total_euro+euros;
total_market = total_market+market;
$(el).append('<td>' + parseFloat(selectedPriceData[3]).toString() + '</td>');
$(el).append('<td>€ ' + (Math.round(euros * 100) / 100) + '</td>');
$(el).append('<td>€ ' + (Math.round(market * 100) / 100) + '</td>');
}
function ajax (options, callback) {
var xhr;
xhr = new XMLHttpRequest();
xhr.open(options.type, options.url, options.async || true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
return callback(xhr.responseText);
}
};
return xhr.send();
};
function csvToArray(csv) {
var a1 = csv.split("\n");
var r = [];
for (var i = 0; i < a1.length; i++) {
var a2 = a1[i].split(",");
r.push(a2);
}
return (r);
}
function cleanUpCurrency(s){
var expression = /\(.(.+)\)/;
//Check if it is in the proper format
if(s.match(expression)){
//It matched - strip out parentheses and append - at front
return parseFloat('-' + s.replace(/[\$\(\),]/g,''));
}
else{
return parseFloat(s);
}
}
});

+ 2
- 0
extension/jquery-3.3.1.min.js
File diff suppressed because it is too large
View File


+ 22
- 0
extension/manifest.json

@ -0,0 +1,22 @@
{
"manifest_version": 2,
"name": "FairCoin Calculator",
"version": "1.2",
"permissions": [
"webRequest",
"<all_urls>",
"*://api.fairplayground.info/*"
],
"content_scripts": [
{
"matches": [
"https://chain.fair.to/address*",
"https://chain.fair-coin.org/address/*"
],
"js": [ "jquery-3.3.1.min.js", "extension.js" ]
}
],
"gecko": {
"id": "faircoin-calculator@mayel.space"
}
}

+ 20
- 4
index.html

@ -12,7 +12,7 @@
<body>
<!-- github -->
<a target="_blank" href="https://github.com/arnaucode/faircoin-calculator"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub"></a>
<a target="_blank" href="https://github.com/mayel/faircoin-calculator"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub"></a>
<!-- /github -->
<div class="container">
@ -23,6 +23,9 @@
<div class="card-title">
<img src="img/faircoin-logo.png" class="img-fluid" />
</div>
<p>Display the historical value of FairCoin at any given date:</p>
<div class="row">
<div class="col-sm-12">
<input id="inputDate" type="date">
@ -31,26 +34,30 @@
<br>
<div class="row">
<div class="col-sm-5">
<input type="number" class="form-control" id="inputFaircoin" placeholder="faircoin">
<input type="number" class="form-control" id="inputFaircoin" placeholder="FairCoin">
</div>
<div class="col-sm-2">
FC
FAIR
</div>
<!--
<div class="col-sm-5">
<button onclick="calculateEuros()" type="button" class="btn btn-outline-primary">Calculate euros</button>
</div>
-->
</div>
<br>
<div class="row">
<div class="col-sm-5">
<input type="number" class="form-control" id="inputEuro" placeholder="euro">
<input type="number" class="form-control" id="inputEuro" placeholder="Euros">
</div>
<div class="col-sm-2">
</div>
<!--
<div class="col-sm-5">
<button onclick="calculateFaircoins()" type="button" class="btn btn-outline-primary">Calculate faircoins</button>
</div>
-->
</div>
<br>
<div class="row">
@ -59,10 +66,19 @@
</div>
</div>
</div>
<p> You can also use the browser extension which displays the historical value of FairCoin at the time of each transaction when <a href="https://chain.fair.to/">browsing the blockchain.</a> Available for <a href="https://addons.mozilla.org/en-US/firefox/addon/faircoin-calculator/">Firefox</a> and <a href="https://chrome.google.com/webstore/detail/faircoin-calculator/alnbjhldpppajfdoknbdpienjbiogmlp">Chrome</a>.
<p>If you're happy with the FairCoin Calculator, you can donate at <a href="faircoin:fMGcrP9nAFic4356F4oHDDWVq8YvtJSXZL">fMGcrP9nAFic4356F4oHDDWVq8YvtJSXZL</a>
</div>
</div>
</div>
<!-- bootstrap -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

+ 20
- 6
index.js

@ -6,13 +6,25 @@ var priceData = "";
//get the dataset
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://api.fairplayground.info/rawdata/faircoin_prices.csv",
url: "https://api.fairplayground.info/rawdata/faircoin_prices.csv",
dataType: "text",
success: function(data) {processData(data);}
success: processData
});
$('#inputFaircoin').on('input', calculateEuros);
$('#inputEuro').on('input', calculateFaircoins);
$('#inputDate').on('input', calculateEuros);
var strDate = new Date().toISOString().slice(0,10);
$('#inputDate').val(strDate);
});
function csvToArray(csv) {
var a1 = csv.split("\n");
var r = [];
@ -22,6 +34,7 @@ function csvToArray(csv) {
}
return(r);
}
function processData(data) {
priceData = csvToArray(data);
};
@ -53,9 +66,10 @@ function calculateEuros() {
var euros = faircoins * selectedPriceData[3];
document.getElementById("inputEuro").value= euros;
document.getElementById("result").innerHTML = faircoins + "FC, are " + euros + "€";
toastr.success(faircoins + "FC, are " + euros + "€");
document.getElementById("result").innerHTML = "The official rate was " + parseFloat(selectedPriceData[3]).toString() + " <p>" + faircoins + " FAIR were worth " + euros + " €";
toastr.success(faircoins + " FAIR were " + euros + " €");
}
function calculateFaircoins() {
document.getElementById("result").innerHTML="";
var euros = document.getElementById("inputEuro").value;
@ -83,6 +97,6 @@ function calculateFaircoins() {
var faircoins = euros / selectedPriceData[3];
document.getElementById("inputFaircoin").value= faircoins;
document.getElementById("result").innerHTML = euros + "€, are " + faircoins + "FC";
toastr.success(euros + "€, are " + faircoins + "FC");
document.getElementById("result").innerHTML = "The official rate was " + parseFloat(selectedPriceData[3]).toString() + " <p>" + euros + " € were worth " + faircoins + " FAIR";
toastr.success(euros + "€ were " + faircoins + " FAIR");
}

Loading…
Cancel
Save