Skip to content

Commit

Permalink
Added merging of sub account types into one balance
Browse files Browse the repository at this point in the history
  • Loading branch information
skcxck committed Sep 3, 2021
1 parent f1c74ed commit 25704c3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
41 changes: 29 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ export const LunchMoneyKrakenConnection: LunchMoneyCryptoConnection<
const response = await kraken.getAccountBalances();
const validation = validateResponse(response) as KrakenValidation<Map<string, string>, KrakenWarnings>;

const balances: CryptoBalance[] = [];
const balances: Record<string, CryptoBalance> = {};

for (const [key, value] of Object.entries(validation.result)) {
// Ignoring (staking / margin / on hold) accounts for now
Object.entries(validation.result).forEach(function ([key, value]) {
if (key.includes('.')) {
continue;
const parts = key.split('.');
key = parts[0];
}

let type = 'crypto';
Expand All @@ -67,21 +67,26 @@ export const LunchMoneyKrakenConnection: LunchMoneyCryptoConnection<
cleaned = key.substr(1);
}

balances.push(<CryptoBalance>{
type: type,
raw: key,
asset: krakenToCommon(cleaned ?? key),
amount: value,
});
}
let balance = updateBalance(balances[cleaned ?? key], value);
if (!balance) {
balance = <CryptoBalance>{
type: type,
raw: key,
asset: krakenToCommon(cleaned ?? key),
amount: value,
};
}

balances[cleaned ?? key] = balance;
});

if (validation.warnings !== null) {
console.warn(`Received following warnings from Kraken: ${validation.warnings.join(', ')}`);
}

return {
providerName: 'kraken',
balances: balances,
balances: Object.values(balances),
};
},
};
Expand Down Expand Up @@ -128,3 +133,15 @@ export function krakenToCommon(ticker: string): string {

return mapping[ticker] ?? ticker;
}

/**
* If passed, increases crypto balance by given value
*/
export function updateBalance(balance: CryptoBalance | null, value: string): CryptoBalance | null {
if (balance == null) {
return null;
}

balance.amount = (Number.parseFloat(balance.amount) + Number.parseFloat(value)).toString();
return balance;
}
11 changes: 9 additions & 2 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,29 @@ describe('LunchMoneyKrakenConnection', () => {
'EUR.HOLD': '500.4838',
'EUR.M': '342.6940',
'ETH2.S': '0.1908877900',
ETH2: '0.1908877900',
},
});

const response = await LunchMoneyKrakenConnection.getBalances(config);

assert.strictEqual(response.providerName, 'kraken');
assert.strictEqual(response.balances.length, 2);
assert.strictEqual(response.balances.length, 3);

assert.strictEqual(response.balances[0].asset, 'EUR');
assert.strictEqual(response.balances[0].raw, 'ZEUR');
assert.strictEqual(response.balances[0].type, 'cash');
assert.strictEqual(response.balances[0].amount, '504861.8946');
assert.strictEqual(response.balances[0].amount, '505705.0724');

assert.strictEqual(response.balances[1].asset, 'BTC');
assert.strictEqual(response.balances[1].raw, 'XXBT');
assert.strictEqual(response.balances[1].type, 'crypto');
assert.strictEqual(response.balances[1].amount, '1011.1908877900');

assert.strictEqual(response.balances[2].asset, 'ETH2');
assert.strictEqual(response.balances[2].raw, 'ETH2');
assert.strictEqual(response.balances[2].type, 'crypto');
assert.strictEqual(response.balances[2].amount, '0.38177558');
});
it('Kraken responses with other status codes', async () => {
scope.post('/0/private/Balance').reply(500, {});
Expand Down

0 comments on commit 25704c3

Please sign in to comment.