|
1 | 1 | import { BadRequestException, Inject, OnModuleInit } from '@nestjs/common'; |
2 | 2 | import BigNumber from 'bignumber.js'; |
3 | 3 | import { |
| 4 | + Balance, |
4 | 5 | Balances, |
5 | 6 | ConstructorArgs, |
6 | 7 | Dictionary, |
@@ -67,24 +68,30 @@ export abstract class ExchangeService extends PricingProvider implements OnModul |
67 | 68 | return this.exchange.name; |
68 | 69 | } |
69 | 70 |
|
70 | | - async getBalances(): Promise<Balances> { |
| 71 | + async getRawBalances(): Promise<Balances> { |
71 | 72 | return this.callApi((e) => e.fetchBalance()); |
72 | 73 | } |
73 | 74 |
|
74 | | - async getTotalBalances(): Promise<Dictionary<number>> { |
75 | | - const balances = await this.getBalances().then((b) => b.total); |
| 75 | + async getBalances(): Promise<{ total: Dictionary<number>; available: Dictionary<number> }> { |
| 76 | + const balances = await this.getRawBalances(); |
76 | 77 |
|
77 | | - const totalBalances = {}; |
| 78 | + return { |
| 79 | + total: this.aggregateBalances(balances.total), |
| 80 | + available: this.aggregateBalances(balances.free), |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + private aggregateBalances(balances: Balance): Dictionary<number> { |
| 85 | + const result: Dictionary<number> = {}; |
78 | 86 | for (const [asset, amount] of Object.entries(balances)) { |
79 | 87 | const [base, suffix] = asset.split('.'); |
80 | | - if (!suffix || suffix === 'F') totalBalances[base] = (totalBalances[base] ?? 0) + amount; |
| 88 | + if (!suffix || suffix === 'F') result[base] = (result[base] ?? 0) + (amount as number); |
81 | 89 | } |
82 | | - |
83 | | - return totalBalances; |
| 90 | + return result; |
84 | 91 | } |
85 | 92 |
|
86 | 93 | async getAvailableBalance(currency: string): Promise<number> { |
87 | | - return this.getBalances().then((b) => b.free[currency] ?? 0); |
| 94 | + return this.getRawBalances().then((b) => b.free[currency] ?? 0); |
88 | 95 | } |
89 | 96 |
|
90 | 97 | async getPrice(from: string, to: string): Promise<Price> { |
|
0 commit comments