-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
517 additions
and
190 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
101 changes: 101 additions & 0 deletions
101
packages/services/src/balances/bitcoin-balances.service.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { BtcCryptoAssetBalance } from '@leather.io/models'; | ||
import { | ||
baseCurrencyAmountInQuote, | ||
createBtcCryptoAssetBalance, | ||
createMoney, | ||
isDefined, | ||
sumNumbers, | ||
} from '@leather.io/utils'; | ||
|
||
import { LeatherApiClient, LeatherApiUtxo } from '../infrastructure/api/leather/leather-api.client'; | ||
import { MarketDataService } from '../market-data/market-data.service'; | ||
import { hasUneconomicalBalance, removeExistingUtxos } from './bitcoin-balances.utils'; | ||
|
||
export interface BtcBalance { | ||
balanceBtc: BtcCryptoAssetBalance; | ||
balanceUsd: BtcCryptoAssetBalance; | ||
} | ||
|
||
export interface BitcoinBalancesService { | ||
getBtcBalance(descriptors: string[], signal?: AbortSignal): Promise<BtcBalance>; | ||
} | ||
|
||
export function createBitcoinBalancesService( | ||
leatherApiClient: LeatherApiClient, | ||
marketDataService: MarketDataService | ||
): BitcoinBalancesService { | ||
/** | ||
* Retrieves total BTC balance for listed descriptors. Includes all sub-balances (inbound, outbound, protected, uneconomical). | ||
* | ||
* @returns {BtcBalance} BTC balance denominanted in both in BTC and fiat (USD). | ||
*/ | ||
async function getBtcBalance(descriptors: string[], signal?: AbortSignal) { | ||
const utxoPromises = descriptors.map(descriptor => | ||
leatherApiClient.fetchUtxos(descriptor, signal) | ||
); | ||
const totalUtxos = (await Promise.all(utxoPromises)).flat().filter(isDefined); | ||
const totalBalanceBtc = createMoney( | ||
sumNumbers(totalUtxos.map(utxo => Number(utxo.value))).toNumber(), | ||
'BTC' | ||
); | ||
// TODO: fetch inbound utxos | ||
const inboundUtxos = await getInboundUtxos(descriptors); | ||
const inboundBalanceBtc = createMoney( | ||
sumNumbers(inboundUtxos.map(utxo => Number(utxo.value))).toNumber(), | ||
'BTC' | ||
); | ||
// TODO: fetch outbound utxos | ||
const outboundUtxos = await getOutboundUtxos(descriptors); | ||
const outboundBalanceBtc = createMoney( | ||
sumNumbers(outboundUtxos.map(utxo => Number(utxo.value))).toNumber(), | ||
'BTC' | ||
); | ||
// TODO: fetch protected utxos | ||
const protectedUtxos = await getProtectedUtxos(descriptors); | ||
const protectedBalanceBtc = createMoney( | ||
sumNumbers(protectedUtxos.map(utxo => Number(utxo.value))).toNumber(), | ||
'BTC' | ||
); | ||
const uneconomicalUtxos = removeExistingUtxos(totalUtxos, protectedUtxos).filter( | ||
hasUneconomicalBalance | ||
); | ||
const uneconomicalBalanceBtc = createMoney( | ||
sumNumbers(uneconomicalUtxos.map(utxo => Number(utxo.value))).toNumber(), | ||
'BTC' | ||
); | ||
|
||
const btcMarketData = await marketDataService.getBtcMarketData(signal); | ||
return { | ||
balanceBtc: createBtcCryptoAssetBalance( | ||
totalBalanceBtc, | ||
inboundBalanceBtc, | ||
outboundBalanceBtc, | ||
protectedBalanceBtc, | ||
uneconomicalBalanceBtc | ||
), | ||
balanceUsd: createBtcCryptoAssetBalance( | ||
baseCurrencyAmountInQuote(totalBalanceBtc, btcMarketData), | ||
baseCurrencyAmountInQuote(inboundBalanceBtc, btcMarketData), | ||
baseCurrencyAmountInQuote(outboundBalanceBtc, btcMarketData), | ||
baseCurrencyAmountInQuote(protectedBalanceBtc, btcMarketData), | ||
baseCurrencyAmountInQuote(uneconomicalBalanceBtc, btcMarketData) | ||
), | ||
}; | ||
} | ||
|
||
async function getInboundUtxos(_descriptors: string[]): Promise<LeatherApiUtxo[]> { | ||
return []; | ||
} | ||
|
||
async function getOutboundUtxos(_descriptors: string[]): Promise<LeatherApiUtxo[]> { | ||
return []; | ||
} | ||
|
||
async function getProtectedUtxos(_descriptors: string[]): Promise<LeatherApiUtxo[]> { | ||
return []; | ||
} | ||
|
||
return { | ||
getBtcBalance, | ||
}; | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { LeatherApiUtxo } from '../infrastructure/api/leather/leather-api.client'; | ||
|
||
export const uneconomicalSatThreshold = 10000; | ||
|
||
export function hasUneconomicalBalance(utxo: LeatherApiUtxo) { | ||
return Number(utxo.value) < uneconomicalSatThreshold; | ||
} | ||
|
||
export function removeExistingUtxos( | ||
candidateUtxos: LeatherApiUtxo[], | ||
existingUtxos: LeatherApiUtxo[] | ||
) { | ||
const existingUtxoIds = new Set(existingUtxos.map(utxo => `${utxo.txid}:${utxo.vout}`)); | ||
|
||
return candidateUtxos.filter(utxo => !existingUtxoIds.has(`${utxo.txid}:${utxo.vout}`)); | ||
} | ||
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
Oops, something went wrong.