Skip to content

Commit

Permalink
reafactor(wallet-mobile): calc locked - to resolve without api
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain committed May 4, 2024
1 parent ee37073 commit 6c1ef88
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import BigNumber from 'bignumber.js'

import {Address} from '../../types'
import {RawUtxo} from '../../types/other'
import {CardanoMobile} from '../../wallets'
import {COINS_PER_UTXO_BYTE} from '../constants/common'
import {cardanoValueFromRemoteFormat, normalizeToAddress} from '../utils'

export async function calcLockedDeposit(utxos: RawUtxo[], address: Address, coinsPerUtxoByteStr: string) {
const utxosWithAssets = utxos.filter((u) => u.assets.length > 0)
const addressPlaceholder =
'addr1qx8nuj8a7gy8kes4pedpfdscrlxr6p8gkzyzmhdmsf4209xssydveuc8xyx4zh27fwcmr62mraeezjwf24hzkyejwfmqmpfpy5'

const coinsPerUtxoByte = await CardanoMobile.BigNum.fromStr(coinsPerUtxoByteStr ?? COINS_PER_UTXO_BYTE)
export async function calcLockedDeposit({
rawUtxos,
address = addressPlaceholder,
coinsPerUtxoByteStr = COINS_PER_UTXO_BYTE,
}: {
rawUtxos: RawUtxo[]
address?: string
coinsPerUtxoByteStr?: string
}) {
const utxosWithAssets = rawUtxos.filter((u) => u.assets.length > 0)

const coinsPerUtxoByte = await CardanoMobile.BigNum.fromStr(coinsPerUtxoByteStr)
const dataCost = await CardanoMobile.DataCost.newCoinsPerByte(coinsPerUtxoByte)
const normalizedAddress = await normalizeToAddress(address)
if (normalizedAddress === undefined) throw new Error('calcLockedDeposit::Error not a valid address')
Expand Down
12 changes: 7 additions & 5 deletions apps/wallet-mobile/src/yoroi-wallets/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,18 @@ export const useLockedAmount = (
options?: UseQueryOptions<Balance.Quantity, Error, Balance.Quantity, [string, string, 'lockedAmount']>,
) => {
const {protocolParams} = useProtocolParams(wallet, {suspense: true})
const coinsPerUtxoByte = protocolParams?.coinsPerUtxoByte ?? ''
const coinsPerUtxoByte = protocolParams?.coinsPerUtxoByte

const query = useQuery({
...options,
suspense: true,
queryKey: [wallet.id, coinsPerUtxoByte, 'lockedAmount'],
queryKey: [wallet.id, coinsPerUtxoByte ?? '', 'lockedAmount'],
queryFn: () =>
calcLockedDeposit(wallet.utxos, wallet.receiveAddresses[0], coinsPerUtxoByte).then(
(amount) => amount.toString() as Balance.Quantity,
),
calcLockedDeposit({
rawUtxos: wallet.utxos,
address: wallet.receiveAddresses[0],
coinsPerUtxoByteStr: coinsPerUtxoByte,
}).then((amount) => amount.toString() as Balance.Quantity),
})

React.useEffect(() => {
Expand Down

0 comments on commit 6c1ef88

Please sign in to comment.