Skip to content

Commit

Permalink
wallet method to get balance in fiat
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan committed Jul 17, 2018
1 parent d5abf5a commit 7fadb07
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
36 changes: 35 additions & 1 deletion src/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class Wallet {
* }
*/
async getExchangeRates(coins_array, fiat = "usd"){
let coins = coins_array || Object.keys(this.getCoins());
let coins = coins_array || Object.keys(this.getCoins());
let rates = {};
let promiseArray = {};

Expand All @@ -219,6 +219,40 @@ class Wallet {
}
// console.log(`Exchange rates: ${JSON.stringify(rates, null, 4)}`)
return rates;
}
/**
* Calculate Balance of coins after exchange rate conversion
* @param {array} [coins_array=this.getCoins()] - An array of coins you want to get exchange rates for. If none are given, an array of all available coins will be used.
* @param {string} [fiat="usd"] - The fiat currency you wish to check against. If none is given, "usd" is defaulted.
* @return {Object} exchange_rates
* @example
* let wallet = new Wallet(...)
* wallet.getFiatBalances(["flo", "bitcoin", "litecoin"], "usd")
*
* //returns
* {
* "flo": expect.any(Number) || "error",
* "bitcoin": expect.any(Number) || "error",
* "litecoin": expect.any(Number) || "error"
* }
*/
async getFiatBalances(coins_array, fiat = "usd"){
let xrBalances = {}, balances = {}, xrates = {};
try {
balances = await this.getCoinBalances(coins_array);
xrates = await this.getExchangeRates(coins_array, fiat);
} catch (err) {console.log("Error trying to fetch balances/xrates: ", err)}

for (let coinB in balances) {
for (let coinX in xrates) {
if (coinB === coinX) {
if (!isNaN(balances[coinB]) && !isNaN(xrates[coinX])) {
xrBalances[coinB] = balances[coinB] * xrates[coinX]
}
}
}
}
return xrBalances
}
/**
* Init Wallet from BIP39 Mnemonic
Expand Down
12 changes: 11 additions & 1 deletion test/Wallet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ test('Wallet can return all coins', () => {
test('Wallet getCoinBalances', async () => {
var w = new Wallet("siren comic spy donkey unknown license asset lens proud bus exhaust section", {discover: false})
let balances = await w.getCoinBalances()

// console.log(balances)
expect(balances).toHaveProperty("flo");
expect(balances).toHaveProperty("bitcoin");
expect(balances).toHaveProperty("litecoin");
Expand All @@ -87,6 +87,16 @@ test('Wallet getExchangeRates', async () => {

}, 100000)

test('Wallet getFiatBalances', async () => {
var w = new Wallet("siren comic spy donkey unknown license asset lens proud bus exhaust section", {discover: false})
let fb = await w.getFiatBalances()
// console.log(fb)
expect(fb).toHaveProperty("flo");
expect(fb).toHaveProperty("bitcoin");
expect(fb).toHaveProperty("litecoin");

}, 100000)

// test('Wallet sendPayment', (done) => {
// var w = new Wallet('00000000000000000000000000000000', {
// discover: false,
Expand Down

0 comments on commit 7fadb07

Please sign in to comment.