Skip to content

Commit

Permalink
feat: add aggregate SIP-10 balances to home screen, ref LEA-1726
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-watters committed Jan 17, 2025
1 parent b89614b commit 0a4f184
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export function AccountBalance({ accountIndex, fingerprint, ...textProps }: Acco
const { totalBalance } = useAccountBalance({ fingerprint, accountIndex });
// TODO: handle balance loading & error states
if (totalBalance.state !== 'success') return;
return <Balance balance={totalBalance.value} variant="label01" {...textProps} />;
return <Balance balance={totalBalance.value} variant="label01" {...textProps} />;
}
121 changes: 121 additions & 0 deletions packages/services/src/balances/sip10-balances.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { createMoney } from '@leather.io/utils';

import { Sip10AddressBalance, Sip10AssetBalance } from './sip10-balances.service';
import { getAggregateSip10Balances, sumBalances } from './sip10-balances.utils';

const mockBaseCryptoAssetBalance = {
totalBalance: createMoney(100, 'USD'),
inboundBalance: createMoney(10, 'USD'),
outboundBalance: createMoney(5, 'USD'),
pendingBalance: createMoney(105, 'USD'),
availableBalance: createMoney(95, 'USD'),
};

const mockBaseCryptoAssetBalance2 = {
totalBalance: createMoney(50, 'USD'),
inboundBalance: createMoney(5, 'USD'),
outboundBalance: createMoney(2, 'USD'),
pendingBalance: createMoney(53, 'USD'),
availableBalance: createMoney(48, 'USD'),
};

const mockSip10AssetBalance: Sip10AssetBalance = {
asset: {
category: 'fungible',
name: 'Test Token',
symbol: 'TEST',
decimals: 6,
contractId: 'SP000...TEST',
chain: 'stacks',
protocol: 'sip10',
canTransfer: true,
imageCanonicalUri: '',
hasMemo: false,
},
sip10: mockBaseCryptoAssetBalance,
usd: mockBaseCryptoAssetBalance,
};

const mockAddressBalances: Sip10AddressBalance[] = [
{
address: 'SP000...TEST',
sip10s: [
{
...mockSip10AssetBalance,
},
],
usd: mockBaseCryptoAssetBalance,
},
{
address: 'SP001...TEST',
sip10s: [
{
...mockSip10AssetBalance,
sip10: {
...mockBaseCryptoAssetBalance2,
},
},
],
usd: mockBaseCryptoAssetBalance,
},
];

describe('sumBalances', () => {
it('should correctly sum two balances', () => {
const initialBalance = mockBaseCryptoAssetBalance;
const accumulatedBalance = {
...mockBaseCryptoAssetBalance2,
};

const result = sumBalances({ initialBalance, accumulatedBalance });

expect(result.totalBalance.amount.toNumber()).toBe(150);
expect(result.inboundBalance.amount.toNumber()).toBe(15);
expect(result.outboundBalance.amount.toNumber()).toBe(7);
expect(result.pendingBalance.amount.toNumber()).toBe(158);
expect(result.availableBalance.amount.toNumber()).toBe(143);
});
});

describe('getAggregateSip10Balances', () => {
it('should aggregate balances from multiple addresses', () => {
const result = getAggregateSip10Balances(mockAddressBalances);

expect(result.length).toBe(1);
expect(result[0].asset.symbol).toBe('TEST');
expect(result[0].sip10.totalBalance.amount.toNumber()).toBe(200);
expect(result[0].usd.totalBalance.amount.toNumber()).toBe(200);
});

it('should handle empty address balances', () => {
const result = getAggregateSip10Balances([]);
expect(result).toEqual([]);
});

it('should handle multiple different tokens', () => {
const otherToken = {
...mockSip10AssetBalance,
asset: {
...mockSip10AssetBalance.asset,
symbol: 'OTHER',
},
};

const addressBalances = [
{
sip10s: [mockSip10AssetBalance, otherToken],
},
{
sip10s: [mockSip10AssetBalance],
},
];

const result = getAggregateSip10Balances(addressBalances);

expect(result.length).toBe(2);
expect(result[0].asset.symbol).toBe('TEST');
expect(result[1].asset.symbol).toBe('OTHER');
expect(result[0].sip10.totalBalance.amount.toNumber()).toBe(200);
expect(result[1].sip10.totalBalance.amount.toNumber()).toBe(100);
});
});
2 changes: 1 addition & 1 deletion packages/services/src/balances/sip10-balances.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface SumBaseCryptoAssetBalance {
accumulatedBalance: BaseCryptoAssetBalance;
}

function sumBalances({
export function sumBalances({
initialBalance,
accumulatedBalance,
}: SumBaseCryptoAssetBalance): BaseCryptoAssetBalance {
Expand Down

0 comments on commit 0a4f184

Please sign in to comment.