Last active
June 20, 2019 06:56
-
-
Save davesag/3567876481344419827e514bae78a02b to your computer and use it in GitHub Desktop.
Work out how much the coins you have in Independent Reserve are worth (in $AUD)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see https://github.com/davesag/ir-api | |
const ir = require('ir-api') | |
const WordTable = require('word-table') | |
// see https://www.independentreserve.com/api | |
// To generate an API Key, go to the Settings page, click "API Keys" and then click "generate". | |
// Select the level of access to grant the key and reenter your password to confirm the creation of the key. | |
// Ensure that you select the lowest level of access required for your usage, the recommended level being Read-only. | |
const apiKey = 'put-your-api-key-here' | |
const apiSecret = 'put-your-api-secret-here' | |
const { | |
getAccounts, | |
getFxRates, | |
getMarketSummary, | |
getValidPrimaryCurrencyCodes | |
} = ir(apiKey, apiSecret, { timeout: 100 }) | |
// utilities | |
const onlyWithBalance = ({ availableBalance }) => availableBalance > 0 | |
const fetchBaseData = async () => Promise.all([ | |
getValidPrimaryCurrencyCodes(), | |
getFxRates(), | |
getAccounts() | |
]) | |
const separateCryptoFromFiat = (allCrypto, coins) => { | |
const crypto = [] | |
const fiat = [] | |
coins.forEach(coin => { | |
if (allCrypto.includes(coin)) crypto.push(coin) | |
else fiat.push(coin) | |
}) | |
return { crypto, fiat } | |
} | |
const fxConversion = (fxRates, currencyCode, secondaryCurrencyCode, availableBalance) => { | |
if (currencyCode === secondaryCurrencyCode) return [1, availableBalance] | |
const { rate } = fxRates.find( | |
({ currencyCodeA, currencyCodeB }) => | |
currencyCodeA === secondaryCurrencyCode && | |
currencyCodeB === currencyCode | |
) | |
return [rate, availableBalance * rate] | |
} | |
// do the work | |
const run = async (secondaryCurrencyCode = 'Aud') => { | |
const [allowedCoins, fxRates, accounts] = await fetchBaseData() | |
const current = accounts.filter(onlyWithBalance) | |
const coins = current.map(({ currencyCode }) => currencyCode) | |
const { crypto, fiat } = separateCryptoFromFiat(allowedCoins, coins) | |
const marketSummaries = await Promise.all( | |
crypto.map(primaryCurrencyCode => | |
getMarketSummary({ primaryCurrencyCode, secondaryCurrencyCode })) | |
) | |
const rates = marketSummaries.reduce((acc, elem) => { | |
acc[elem.primaryCurrencyCode] = elem.dayAvgPrice | |
return acc | |
}, {}) | |
let sum = 0 | |
const header = ['Currency', 'Balance', 'AUD Price', 'AUD Value'] | |
const body = [ | |
...current.map(({ currencyCode, availableBalance }) => { | |
const [rate, value] = fiat.includes(currencyCode) | |
? fxConversion(fxRates, currencyCode, secondaryCurrencyCode, availableBalance) | |
: [rates[currencyCode], availableBalance * rates[currencyCode]] | |
sum += value | |
return [currencyCode, availableBalance, rate, value] | |
}), | |
['total', '', '', sum] | |
] | |
const wt = new WordTable(header, body) | |
console.log(wt.string()) | |
} | |
run().catch(error => { | |
console.error(error) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment