added resolve_current_btc_price_in_fiat fn
This commit is contained in:
parent
bb7d29aa71
commit
e613a94a73
@ -10534,39 +10534,50 @@
|
|||||||
if (localbitcoinplusplus.master_configurations.validTradingAmount.includes(btc_buy_price)) {
|
if (localbitcoinplusplus.master_configurations.validTradingAmount.includes(btc_buy_price)) {
|
||||||
if(!localbitcoinplusplus.master_configurations.tradableAsset2.includes(currency)) return false;
|
if(!localbitcoinplusplus.master_configurations.tradableAsset2.includes(currency)) return false;
|
||||||
let current_btc_price = localbitcoinplusplus.trade.prototype.get_current_btc_price_in_fiat(currency);
|
let current_btc_price = localbitcoinplusplus.trade.prototype.get_current_btc_price_in_fiat(currency);
|
||||||
if (current_btc_price > 0) {
|
if (typeof current_btc_price=="object" && current_btc_price.rate > 0) {
|
||||||
return parseFloat(btc_buy_price / current_btc_price).toFixed(8);
|
return parseFloat(btc_buy_price / current_btc_price.rate).toFixed(8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
throw new Error("Failed to calculate BTC equivalent of cash.");
|
||||||
},
|
},
|
||||||
get_current_btc_price_in_fiat(currency_code) {
|
get_current_btc_price_in_fiat(currency_code) {
|
||||||
return eval(`localbitcoinplusplus.trade.current_btc_price_in_${currency_code}`);
|
return localbitcoinplusplus.trade[`current_btc_price_in_${currency_code}`];
|
||||||
},
|
},
|
||||||
set_current_btc_price_in_fiat(currency_code) {
|
async resolve_current_btc_price_in_fiat(currency_code) {
|
||||||
let url = `https://api.coindesk.com/v1/bpi/currentprice/${currency_code}.json`;
|
let today = + new Date();
|
||||||
helper_functions.ajaxGet(url, function (res) {
|
let last_update_of_fiat_price_obj = localbitcoinplusplus.trade.prototype.get_current_btc_price_in_fiat(currency_code);
|
||||||
if (typeof res == "string" && res.length > 0) {
|
if(typeof last_update_of_fiat_price_obj!=="object"
|
||||||
try {
|
|| (today-last_update_of_fiat_price_obj.timestamp>3600000)) {
|
||||||
let res_object = JSON.parse(res);
|
last_update_of_fiat_price_obj = await localbitcoinplusplus.trade.prototype.set_current_btc_price_in_fiat(currency_code);
|
||||||
return Object.defineProperty(localbitcoinplusplus.trade,
|
return last_update_of_fiat_price_obj;
|
||||||
`current_btc_price_in_${currency_code}`, {
|
} else {
|
||||||
value: {rate:eval(`res_object.bpi.${currency_code}.rate_float`),
|
return last_update_of_fiat_price_obj;
|
||||||
timestamp: + new Date()},
|
}
|
||||||
writable: false,
|
},
|
||||||
configurable: true,
|
async set_current_btc_price_in_fiat(currency_code) {
|
||||||
enumerable: true
|
const url = `https://api.coindesk.com/v1/bpi/currentprice/${currency_code}.json`;
|
||||||
});
|
const res = await helper_functions.ajaxGet(url);
|
||||||
} catch (error) {
|
if (typeof res == "object" && typeof res.bpi =="object") {
|
||||||
console.error(error);
|
try {
|
||||||
return false;
|
let new_price = res.bpi[`${currency_code}`].rate_float;
|
||||||
}
|
await Object.defineProperty(localbitcoinplusplus.trade,
|
||||||
|
`current_btc_price_in_${currency_code}`, {
|
||||||
|
value: {rate:new_price,
|
||||||
|
timestamp: + new Date()},
|
||||||
|
writable: true,
|
||||||
|
configurable: false,
|
||||||
|
enumerable: true
|
||||||
|
});
|
||||||
|
return localbitcoinplusplus.trade[`current_btc_price_in_${currency_code}`];
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
},
|
},
|
||||||
sendTransaction(utxo_addr, utxo_addr_wif, receiver_address, receiving_amount, receiving_amount_currency="USD", change_adress, callback) {
|
sendTransaction(utxo_addr, utxo_addr_wif, receiver_address, receiving_amount, receiving_amount_currency="USD", change_adress, callback) {
|
||||||
let url = `${localbitcoinplusplus.flocha}/api/addr/${utxo_addr}/utxo`;
|
let url = `${localbitcoinplusplus.flocha}/api/addr/${utxo_addr}/utxo`;
|
||||||
helper_functions.ajaxGet(url, function (res) {
|
helper_functions.ajaxGet(url).then(res=>{
|
||||||
|
|
||||||
if (res.length > 0) {
|
if (res.length > 0) {
|
||||||
try {
|
try {
|
||||||
@ -10929,41 +10940,36 @@
|
|||||||
<script>
|
<script>
|
||||||
let helper_functions = {
|
let helper_functions = {
|
||||||
// AJAX Get
|
// AJAX Get
|
||||||
ajaxGet: function (url, callback) {
|
ajaxGet: async function (url) {
|
||||||
try {
|
try {
|
||||||
var xhr = new XMLHttpRequest();
|
//await the response of the fetch call
|
||||||
xhr.open('GET', url);
|
let response = await fetch(url);
|
||||||
xhr.onload = function () {
|
//proceed once the first promise is resolved.
|
||||||
if (xhr.status === 200) {
|
let data = await response.json()
|
||||||
callback(xhr.responseText);
|
//proceed only when the second promise is resolved
|
||||||
} else {
|
return data;
|
||||||
throw new Error(`Request to ${url} failed: ${xhr.status}`);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
xhr.send();
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(error);
|
throw new Error(error);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
//AJAX Post
|
//AJAX Post
|
||||||
ajaxPost: function (url, data, callback) {
|
ajaxPost: function (url = ``, data = {}) {
|
||||||
xhttp = new XMLHttpRequest();
|
return fetch(url, {
|
||||||
|
method: "POST", // *GET, POST, PUT, DELETE, etc.
|
||||||
xhttp.onreadystatechange = function() {
|
mode: "cors", // no-cors, cors, *same-origin
|
||||||
if (this.readyState == 4 && this.status == 200) {
|
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
|
||||||
console.log(this.responseText);
|
credentials: "same-origin", // include, *same-origin, omit
|
||||||
callback(this.responseText);
|
headers: {
|
||||||
} else {
|
"Content-Type": "application/json",
|
||||||
callback(false);
|
// "Content-Type": "application/x-www-form-urlencoded",
|
||||||
throw new Error(`Request to ${url} failed: ${xhttp.status}`);
|
},
|
||||||
}
|
redirect: "follow", // manual, *follow, error
|
||||||
};
|
referrer: "no-referrer", // no-referrer, *client
|
||||||
|
body: JSON.stringify(data), // body data type must match "Content-Type" header
|
||||||
xhttp.open('POST', url, true);
|
})
|
||||||
xhttp.setRequestHeader('Content-Type', 'application/json');
|
.then(response => response.json()); // parses response to JSON
|
||||||
xhttp.send(JSON.stringify(data));
|
},
|
||||||
},
|
|
||||||
|
|
||||||
// Create unique id
|
// Create unique id
|
||||||
unique_id: function () {
|
unique_id: function () {
|
||||||
@ -11216,7 +11222,7 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error('');
|
throw new Error(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -12553,7 +12559,7 @@
|
|||||||
try {
|
try {
|
||||||
//let url = `https://blockchain.info/q/addressbalance/${BTCAddress}?confirmations=6`;
|
//let url = `https://blockchain.info/q/addressbalance/${BTCAddress}?confirmations=6`;
|
||||||
let url = `https://testnet.flocha.in/api/addr/${trader_deposits.btc_address}/balance`;
|
let url = `https://testnet.flocha.in/api/addr/${trader_deposits.btc_address}/balance`;
|
||||||
helper_functions.ajaxGet(url, function (balance) {
|
helper_functions.ajaxGet(url).then(balance=> {
|
||||||
if (!isNaN(balance) && parseFloat(balance) > 0) {
|
if (!isNaN(balance) && parseFloat(balance) > 0) {
|
||||||
balance = parseFloat(balance);
|
balance = parseFloat(balance);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user