-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
86 lines (80 loc) · 3.05 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Utils functions
function getHowManyBtcCanIBuy(client, fiatCurrency, money, useSandboxMode, fakeBTCPrice) {
/*
Get number of BTCs that you would get based on that fiat amount
*/
if (useSandboxMode) {
return new Promise((resolve, reject) => {
resolve(money / fakeBTCPrice);
});
} else {
let currencyPair = 'BTC-' + fiatCurrency;
return new Promise((resolve, reject) => {
return client.getBuyPrice({ 'currencyPair': currencyPair }, function (err, obj) {
if (!err) {
btcPrice = parseFloat(obj.data.amount);
resolve(money / btcPrice);
}
});
});
}
}
function getTotalPriceOfBtcAmount(client, fiatCurrency, btcAmount, useSandboxMode, fakeBTCPrice) {
/*
Get number of Fiat that you would get based on that BTC amount
*/
if (useSandboxMode) {
return new Promise((resolve, reject) => {
resolve(btcAmount * fakeBTCPrice)
})
} else {
let currencyPair = 'BTC-' + fiatCurrency
return new Promise((resolve, reject)=> {
return client.getBuyPrice({'currencyPair': currencyPair, function (err, obj) {
if (!err) {
btcPrice = parseFloat(obj.data.amount)
}
}})
})
}
}
function refreshRealData(
/*
Refresh the User balance as It could have been changed in the exchange
*/
client,
fiatCurrency,
userBalanceBtcElement,
userBalanceFiatElement,
currentBtcPriceElement,
estimatedAmountElement,
currentBalanceBtc,
currentBalanceFiat
) {
return new Promise((resolve, reject) => {
return client.getAccounts({}, function (err, accounts) {
accounts.forEach(function(acct) {
if (acct.type === 'fiat' && acct.currency === fiatCurrency) {
// This code will update your Fiat balance using the real data from Coinbase
currentBalanceFiat = parseFloat(acct.balance.amount)
userBalanceFiatElement.innerHTML = 'Balance Fiat: ' + currentBalanceFiat + ' ' + fiatCurrency;
// This code will update the displayed BTC price using real data from Coinbase
getTotalPriceOfBtcAmount(client, fiatCurrency, 1, false, null).then(function(price) {
currentBtcPriceElement.innerHTML = '(Price: ' + price + ' ' + fiatCurrency + ')'
})
estimatedAmountElement.innerHTML = '';
} else if (acct.type === 'wallet' && acct.currency === 'BTC') {
// This code will update your BTC balance using the real data from Coinbase
currentBalanceBtc = parseFloat(acct.balance.amount)
userBalanceBtcElement.innerHTML = 'Balance BTC: ' + currentBalanceBtc + ' ' + acct.balance.currency;
}
resolve()
})
})
})
}
module.exports = {
getHowManyBtcCanIBuy,
getTotalPriceOfBtcAmount,
refreshRealData
}