Skip to content

Commit

Permalink
fix: no wallet balance service calls
Browse files Browse the repository at this point in the history
  • Loading branch information
alexp3y committed Jan 17, 2025
1 parent e6e1099 commit 20dccd5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 25 deletions.
11 changes: 9 additions & 2 deletions packages/services/src/balances/btc-balances.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { MarketDataService } from '../market-data/market-data.service';
import { BitcoinAccountIdentifier, BitcoinAccountServiceRequest } from '../shared/bitcoin.types';
import { UtxosService } from '../utxos/utxos.service';
import { sumUtxoValues } from '../utxos/utxos.utils';
import { btcCryptoAssetZeroBalanceBtc, btcCryptoAssetZeroBalanceUsd } from './constants';

export interface BtcAccountBalance {
account: BitcoinAccountIdentifier;
Expand Down Expand Up @@ -50,8 +51,14 @@ export function createBtcBalancesService(
balanceRequests.map(req => getBtcAccountBalance(req, signal))
);
return {
btc: aggregateBtcCryptoAssetBalances(accountBalances.map(bal => bal.btc)),
usd: aggregateBtcCryptoAssetBalances(accountBalances.map(bal => bal.usd)),
btc: aggregateBtcCryptoAssetBalances([
btcCryptoAssetZeroBalanceBtc,
...accountBalances.map(bal => bal.btc),
]),
usd: aggregateBtcCryptoAssetBalances([
btcCryptoAssetZeroBalanceUsd,
...accountBalances.map(bal => bal.usd),
]),
accountBalances,
};
}
Expand Down
11 changes: 11 additions & 0 deletions packages/services/src/balances/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {
createBtcCryptoAssetBalance,
createMoney,
createStxCryptoAssetBalance,
} from '@leather.io/utils';

export const btcCryptoAssetZeroBalanceBtc = createBtcCryptoAssetBalance(createMoney(0, 'BTC'));
export const btcCryptoAssetZeroBalanceUsd = createBtcCryptoAssetBalance(createMoney(0, 'USD'));
export const stxCryptoAssetZeroBalanceStx = createStxCryptoAssetBalance(createMoney(0, 'STX'));
export const stxCryptoAssetZeroBalanceUsd = createStxCryptoAssetBalance(createMoney(0, 'USD'));
export const baseCryptoAssetZeroBalanceUsd = createStxCryptoAssetBalance(createMoney(0, 'USD'));
6 changes: 5 additions & 1 deletion packages/services/src/balances/runes-balances.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { RuneAssetService } from '../assets/rune-asset.service';
import { BestInSlotApiClient } from '../infrastructure/api/best-in-slot/best-in-slot-api.client';
import { MarketDataService } from '../market-data/market-data.service';
import { BitcoinAccountIdentifier } from '../shared/bitcoin.types';
import { baseCryptoAssetZeroBalanceUsd } from './constants';
import { parseRunesOutputsBalances } from './runes-balances.utils';

export interface RuneAssetBalance {
Expand Down Expand Up @@ -58,7 +59,10 @@ export function createRunesBalancesService(
accounts.map(account => getRunesAccountBalance(account, signal))
);
return {
usd: aggregateBaseCryptoAssetBalances(accountBalances.map(b => b.usd)),
usd: aggregateBaseCryptoAssetBalances([
baseCryptoAssetZeroBalanceUsd,
...accountBalances.map(b => b.usd),
]),
accountBalances,
};
}
Expand Down
6 changes: 5 additions & 1 deletion packages/services/src/balances/sip10-balances.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { Sip10AssetService } from '../assets/sip10-asset.service';
import { HiroStacksApiClient } from '../infrastructure/api/hiro/hiro-stacks-api.client';
import { MarketDataService } from '../market-data/market-data.service';
import { baseCryptoAssetZeroBalanceUsd } from './constants';

export interface Sip10AssetBalance {
asset: Sip10CryptoAssetInfo;
Expand Down Expand Up @@ -47,7 +48,10 @@ export function createSip10BalancesService(
const addressBalances = await Promise.all(
addresses.map(address => getSip10AddressBalance(address, signal))
);
const totalUsdBalance = aggregateBaseCryptoAssetBalances(addressBalances.map(r => r.usd));
const totalUsdBalance = aggregateBaseCryptoAssetBalances([
baseCryptoAssetZeroBalanceUsd,
...addressBalances.map(r => r.usd),
]);
return {
usd: totalUsdBalance,
addressBalances,
Expand Down
11 changes: 9 additions & 2 deletions packages/services/src/balances/stx-balances.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '../infrastructure/api/hiro/hiro-stacks-api.utils';
import { MarketDataService } from '../market-data/market-data.service';
import { StacksTransactionsService } from '../transactions/stacks-transactions.service';
import { stxCryptoAssetZeroBalanceStx, stxCryptoAssetZeroBalanceUsd } from './constants';
import { calculateInboundStxBalance, calculateOutboundStxBalance } from './stx-balances.utils';

export interface StxAggregateBalance {
Expand Down Expand Up @@ -46,8 +47,14 @@ export function createStxBalancesService(
addresses.map(address => getStxAddressBalance(address, signal))
);
return {
stx: aggregateStxCryptoAssetBalances(addressBalances.map(res => res.stx)),
usd: aggregateStxCryptoAssetBalances(addressBalances.map(res => res.usd)),
stx: aggregateStxCryptoAssetBalances([
stxCryptoAssetZeroBalanceStx,
...addressBalances.map(res => res.stx),
]),
usd: aggregateStxCryptoAssetBalances([
stxCryptoAssetZeroBalanceUsd,
...addressBalances.map(res => res.usd),
]),
balances: addressBalances,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,29 @@ export function createBestInSlotApiClient(
params.append('offset', '0');
params.append('count', '2000');

return await cache.fetchWithCache(
[
'best-in-slot-inscriptions',
bitcoinNetworkModeToCoreNetworkMode(networkService.getConfig().chain.bitcoin.mode),
descriptor,
],
async () => {
const res = await limiter.add(
RateLimiterType.BestInSlot,
() =>
axios.get<BestInSlotApiResponse<BisInscription[]>>(
`${getBestInSlotBasePath(networkService.getConfig().chain.bitcoin.mode)}/wallet/inscriptions_xpub`,
{ params, signal }
),
{ signal }
return bitcoinNetworkModeToCoreNetworkMode(networkService.getConfig().chain.bitcoin.mode) !==
'mainnet'
? []
: await cache.fetchWithCache(
[
'best-in-slot-inscriptions',
bitcoinNetworkModeToCoreNetworkMode(networkService.getConfig().chain.bitcoin.mode),
descriptor,
],
async () => {
const res = await limiter.add(
RateLimiterType.BestInSlot,
() =>
axios.get<BestInSlotApiResponse<BisInscription[]>>(
`${getBestInSlotBasePath(networkService.getConfig().chain.bitcoin.mode)}/wallet/inscriptions_xpub`,
{ params, signal }
),
{ signal }
);
return z.array(bisInscriptionSchema).parse(res.data.data);
},
{ ttl: HttpCacheTimeMs.twoMinutes }
);
return z.array(bisInscriptionSchema).parse(res.data.data);
},
{ ttl: HttpCacheTimeMs.twoMinutes }
);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down

0 comments on commit 20dccd5

Please sign in to comment.