-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sip10s to account total balance
- Loading branch information
1 parent
94fa068
commit 5fbcb4b
Showing
3 changed files
with
73 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import type { Money } from '@leather.io/models'; | ||
import { | ||
baseCurrencyAmountInQuote, | ||
createMoney, | ||
isDefined, | ||
isMoneyGreaterThanZero, | ||
sumMoney, | ||
} from '@leather.io/utils'; | ||
|
||
import { useSip10FiatMarketData } from '../use-calculate-sip10-fiat-value'; | ||
import { useCombinedFilteredSip10Tokens } from '../use-filtered-sip10-tokens'; | ||
import { type AssetFilter, useManageTokens } from '../use-manage-tokens'; | ||
|
||
interface UseSip10ManagedTokensBalanceArgs { | ||
stxAddress: string; | ||
assetFilter?: AssetFilter; | ||
} | ||
|
||
export function useSip10ManagedTokensBalance({ | ||
stxAddress, | ||
assetFilter = 'all', | ||
}: UseSip10ManagedTokensBalanceArgs) { | ||
const { getTokenMarketData } = useSip10FiatMarketData(); | ||
const { allTokens = [], supportedTokens } = useCombinedFilteredSip10Tokens({ | ||
address: stxAddress, | ||
filter: 'all', | ||
}); | ||
const { filterTokens } = useManageTokens(); | ||
const preEnabledTokensIds = supportedTokens.map(t => t.info.contractId); | ||
const managedTokens = filterTokens({ | ||
tokens: allTokens, | ||
filter: assetFilter, | ||
getTokenId: t => t.info.contractId, | ||
preEnabledTokensIds, | ||
}); | ||
|
||
const balance = useMemo(() => { | ||
const baseBalance: Money = createMoney(0, 'USD'); | ||
return sumMoney([ | ||
baseBalance, | ||
...managedTokens | ||
.map(token => { | ||
const tokenMarketData = getTokenMarketData( | ||
token.info.contractId, | ||
token.balance.availableBalance.symbol | ||
); | ||
if ( | ||
!tokenMarketData || | ||
!token.balance.availableBalance || | ||
!isMoneyGreaterThanZero(tokenMarketData.price) | ||
) | ||
return; | ||
return baseCurrencyAmountInQuote(token.balance.availableBalance, tokenMarketData); | ||
}) | ||
.filter(isDefined), | ||
]); | ||
}, [managedTokens, getTokenMarketData]); | ||
|
||
return balance; | ||
} |
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