Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feature/tx-review
Browse files Browse the repository at this point in the history
  • Loading branch information
banklesss committed Sep 18, 2024
2 parents 22159a8 + 25bdc7a commit 11d8bb0
Show file tree
Hide file tree
Showing 38 changed files with 729 additions and 308 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Chain} from '@yoroi/types'
import _ from 'lodash'
import * as React from 'react'

import {useBalances} from '../../../../yoroi-wallets/hooks'
import {useBalances, useTransactionInfos} from '../../../../yoroi-wallets/hooks'
import {Amounts, Quantities} from '../../../../yoroi-wallets/utils/utils'
import {useSelectedWallet} from '../../../WalletManager/common/hooks/useSelectedWallet'
import {useWalletManager} from '../../../WalletManager/context/WalletManagerProvider'
Expand All @@ -14,19 +15,21 @@ import {SanchonetFaucetBanner} from './SanchonetFaucetBanner'

export const ShowBuyBanner = () => {
const {wallet} = useSelectedWallet()
const transactionInfos = useTransactionInfos({wallet})
const {
selected: {network},
} = useWalletManager()
const balances = useBalances(wallet)
const primaryAmount = Amounts.getAmount(balances, wallet.portfolioPrimaryTokenInfo.id)
const hasZeroPt = Quantities.isZero(primaryAmount.quantity)
const hasZeroTx = _.isEmpty(transactionInfos)

const showSmallBanner = useShowBuyBannerSmall()
const {resetShowBuyBannerSmall} = useResetShowBuyBannerSmall()

if (hasZeroPt && network === Chain.Network.Preprod) return <PreprodFaucetBanner />
if (hasZeroPt && network === Chain.Network.Sancho) return <SanchonetFaucetBanner />
if (hasZeroPt) return <BuyBannerBig />
if (hasZeroPt && hasZeroTx && network === Chain.Network.Preprod) return <PreprodFaucetBanner />
if (hasZeroPt && hasZeroTx && network === Chain.Network.Sancho) return <SanchonetFaucetBanner />
if (hasZeroPt && hasZeroTx) return <BuyBannerBig />
if (showSmallBanner) return <BuyBannerSmall onClose={resetShowBuyBannerSmall} />

return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import * as React from 'react'

import {governaceAfterBlock} from '../../../../kernel/config'
import {YoroiWallet} from '../../../../yoroi-wallets/cardano/types'
import {useStakingKey, useTipStatus} from '../../../../yoroi-wallets/hooks'
import {useStakingKey} from '../../../../yoroi-wallets/hooks'
import {CardanoMobile} from '../../../../yoroi-wallets/wallets'
import {useBestBlock} from '../../../WalletManager/common/hooks/useBestBlock'
import {useSelectedWallet} from '../../../WalletManager/common/hooks/useSelectedWallet'
import {GovernanceVote} from '../types'

Expand All @@ -30,7 +31,7 @@ export const mapStakingKeyStateToGovernanceAction = (state: StakingKeyState): Go
}

export const useIsGovernanceFeatureEnabled = (wallet: YoroiWallet) => {
const {bestBlock} = useTipStatus({wallet, options: {suspense: true}})
const bestBlock = useBestBlock({options: {suspense: true}})
return bestBlock.height >= governaceAfterBlock[wallet.networkManager.network]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ import {useModal} from '../../../../components/Modal/ModalContext'
import {Text} from '../../../../components/Text'
import {isEmptyString} from '../../../../kernel/utils'
import {MultiToken} from '../../../../yoroi-wallets/cardano/MultiToken'
import {CardanoTypes, YoroiWallet} from '../../../../yoroi-wallets/cardano/types'
import {useTipStatus, useTransactionInfos} from '../../../../yoroi-wallets/hooks'
import {CardanoTypes} from '../../../../yoroi-wallets/cardano/types'
import {useTransactionInfos} from '../../../../yoroi-wallets/hooks'
import {TransactionInfo} from '../../../../yoroi-wallets/types/other'
import {formatDateAndTime, formatTokenWithSymbol} from '../../../../yoroi-wallets/utils/format'
import {asQuantity} from '../../../../yoroi-wallets/utils/utils'
import {usePrivacyMode} from '../../../Settings/PrivacyMode/PrivacyMode'
import {useBestBlock} from '../../../WalletManager/common/hooks/useBestBlock'
import {useSelectedWallet} from '../../../WalletManager/common/hooks/useSelectedWallet'
import {useWalletManager} from '../../../WalletManager/context/WalletManagerProvider'
import {messages, useStrings} from '../../common/strings'
Expand Down Expand Up @@ -173,7 +174,7 @@ export const TxDetails = () => {
</View>

<Boundary loading={{size: 'small'}}>
<Confirmations transaction={transaction} wallet={wallet} />
<Confirmations transaction={transaction} />
</Boundary>

<Label>{strings.transactionId}</Label>
Expand Down Expand Up @@ -201,18 +202,17 @@ export const TxDetails = () => {
)
}

const Confirmations = ({transaction, wallet}: {transaction: TransactionInfo; wallet: YoroiWallet}) => {
const Confirmations = ({transaction}: {transaction: TransactionInfo}) => {
const strings = useStrings()
const tipStatus = useTipStatus({
wallet,
const bestBlock = useBestBlock({
options: {
refetchInterval: 5000,
refetchInterval: 5_000,
},
})

return (
<Text secondary>
{strings.confirmations(transaction.blockNumber === 0 ? 0 : tipStatus.bestBlock.height - transaction.blockNumber)}
{strings.confirmations(transaction.blockNumber === 0 ? 0 : bestBlock.height - transaction.blockNumber)}
</Text>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {Chain} from '@yoroi/types'
import {useQuery, UseQueryOptions} from 'react-query'

import {useSelectedNetwork} from './useSelectedNetwork'

export const useBestBlock = ({options}: {options?: UseQueryOptions<Chain.Cardano.BestBlock, Error>}) => {
const {networkManager, network} = useSelectedNetwork()
const query = useQuery<Chain.Cardano.BestBlock, Error>({
suspense: true,
staleTime: 10_000,
retry: 3,
retryDelay: 1_000,
queryKey: [network, 'tipStatus'],
queryFn: () => networkManager.api.bestBlock(),
...options,
})

if (!query.data) throw new Error('Failed to retrive tipStatus')

return query.data
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {CardanoApi} from '@yoroi/api'
import {mountAsyncStorage, mountMMKVStorage, observableStorageMaker} from '@yoroi/common'
import {explorerManager} from '@yoroi/explorers'
import {createPrimaryTokenInfo} from '@yoroi/portfolio'
import {Chain, Network} from '@yoroi/types'
import {Api, Chain, Network} from '@yoroi/types'
import {freeze} from 'immer'

import {logger} from '../../../kernel/logger/logger'
Expand Down Expand Up @@ -128,22 +128,25 @@ export const networkConfigs: Readonly<Record<Chain.SupportedNetworks, Readonly<N

export function buildNetworkManagers({
tokenManagers,
apiMaker = CardanoApi.cardanoApiMaker,
}: {
tokenManagers: NetworkTokenManagers
apiMaker?: ({network}: {network: Chain.SupportedNetworks}) => Api.Cardano.Api
}): Readonly<Record<Chain.SupportedNetworks, Network.Manager>> {
const managers = Object.entries(networkConfigs).reduce<Record<Chain.SupportedNetworks, Network.Manager>>(
(networkManagers, [network, config]) => {
const tokenManager = tokenManagers[network as Chain.SupportedNetworks]
const networkRootStorage = mountMMKVStorage({path: `/`, id: `${network}.manager.v1`})
const rootStorage = observableStorageMaker(networkRootStorage)
const legacyRootStorage = observableStorageMaker(mountAsyncStorage({path: `/legacy/${network}/v1/`}))
const {getProtocolParams} = CardanoApi.cardanoApiMaker({network: config.network})
const {getProtocolParams, getBestBlock} = apiMaker({network: config.network})
const api = {
protocolParams: () =>
getProtocolParams().catch((error) => {
logger.error(`networkManager: ${network} protocolParams has failed, using hardcoded`, {error})
return Promise.resolve(protocolParamsPlaceholder)
}),
bestBlock: getBestBlock,
}

const info = dateToEpochInfo(config.eras)
Expand Down
13 changes: 11 additions & 2 deletions apps/wallet-mobile/src/legacy/Dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import {useTheme} from '@yoroi/theme'
import BigNumber from 'bignumber.js'
import React from 'react'
import {defineMessages, useIntl} from 'react-intl'
import {ActivityIndicator, RefreshControl, ScrollView, StyleSheet, View, ViewProps} from 'react-native'
import {
ActivityIndicator,
RefreshControl,
ScrollView,
StyleSheet,
useWindowDimensions,
View,
ViewProps,
} from 'react-native'
import {SafeAreaView} from 'react-native-safe-area-context'

import {Banner} from '../../components/Banner/Banner'
Expand Down Expand Up @@ -44,6 +52,7 @@ export const Dashboard = () => {
const {isLoading: isSyncing, sync} = useSync(wallet)
const isOnline = useIsOnline(wallet)
const {openModal, closeModal} = useModal()
const {height: windowHeight} = useWindowDimensions()

const balances = useBalances(wallet)
const primaryAmount = Amounts.getAmount(balances, wallet.portfolioPrimaryTokenInfo.id)
Expand Down Expand Up @@ -71,7 +80,7 @@ export const Dashboard = () => {
openModal(
'',
<WithdrawStakingRewards wallet={wallet} onSuccess={() => resetToTxHistory()} onCancel={() => closeModal()} />,
450,
windowHeight * 0.8,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import type {
FundInfoResponse,
PoolInfoRequest,
RawUtxo,
TipStatusResponse,
Transaction,
TxStatusRequest,
TxStatusResponse,
Expand Down Expand Up @@ -1086,10 +1085,6 @@ export const makeCardanoWallet = (networkManager: Network.Manager, implementatio
return legacyApi.fetchTxStatus(request, networkManager.legacyApiBaseUrl)
}

async fetchTipStatus(): Promise<TipStatusResponse> {
return legacyApi.getTipStatus(networkManager.legacyApiBaseUrl)
}

private isInitialized = false

private subscriptions: Array<WalletSubscription> = []
Expand Down
3 changes: 0 additions & 3 deletions apps/wallet-mobile/src/yoroi-wallets/cardano/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {WalletEncryptedStorage} from '../../kernel/storage/EncryptedStorage'
import type {
FundInfoResponse,
RawUtxo,
TipStatusResponse,
TransactionInfo,
TxStatusRequest,
TxStatusResponse,
Expand Down Expand Up @@ -155,7 +154,6 @@ export interface YoroiWallet {
saveMemo(txId: string, memo: string): Promise<void>
get transactions(): Record<string, TransactionInfo>
get confirmationCounts(): Record<string, null | number>
fetchTipStatus(): Promise<TipStatusResponse>
fetchTxStatus(request: TxStatusRequest): Promise<TxStatusResponse>

// Utxos
Expand Down Expand Up @@ -232,7 +230,6 @@ const yoroiWalletKeys: Array<keyof YoroiWallet> = [
// Balances, TxDetails
'transactions',
'confirmationCounts',
'fetchTipStatus',
'fetchTxStatus',

// Other
Expand Down
26 changes: 1 addition & 25 deletions apps/wallet-mobile/src/yoroi-wallets/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {logger} from '../../kernel/logger/logger'
import {deriveAddressFromXPub} from '../cardano/account-manager/derive-address-from-xpub'
import {getSpendingKey, getStakingKey} from '../cardano/addressInfo/addressInfo'
import {WalletEvent, YoroiWallet} from '../cardano/types'
import {TipStatusResponse, TRANSACTION_DIRECTION, TRANSACTION_STATUS, TxSubmissionStatus} from '../types/other'
import {TRANSACTION_DIRECTION, TRANSACTION_STATUS, TxSubmissionStatus} from '../types/other'
import {YoroiSignedTx, YoroiUnsignedTx} from '../types/yoroi'
import {delay} from '../utils/timeUtils'
import {Utxos} from '../utils/utils'
Expand Down Expand Up @@ -562,30 +562,6 @@ const fetchTxStatus = async (
}
}

// TODO: tipStatus is a network responsability
export const useTipStatus = ({
wallet,
options,
}: {
wallet: YoroiWallet
options?: UseQueryOptions<TipStatusResponse, Error>
}) => {
const {network} = useSelectedNetwork()
const query = useQuery<TipStatusResponse, Error>({
suspense: true,
staleTime: 10000,
retry: 3,
retryDelay: 1000,
queryKey: [network, 'tipStatus'],
queryFn: () => wallet.fetchTipStatus(),
...options,
})

if (!query.data) throw new Error('Failed to retrive tipStatus')

return query.data
}

export const useBalances = (wallet: YoroiWallet): Balance.Amounts => {
const utxos = useUtxos(wallet)

Expand Down
19 changes: 0 additions & 19 deletions apps/wallet-mobile/src/yoroi-wallets/mocks/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,25 +188,6 @@ const wallet: YoroiWallet = {
action('fetchTxStatus')(...args)
return {}
},
fetchTipStatus: async (...args: unknown[]) => {
action('fetchTipStatus')(...args)
return Promise.resolve({
bestBlock: {
epoch: 210,
slot: 76027,
globalSlot: 60426427,
hash: '2cf5a471a0c58cbc22534a0d437fbd91576ef10b98eea7ead5887e28f7a4fed8',
height: 3617708,
},
safeBlock: {
epoch: 210,
slot: 75415,
globalSlot: 60425815,
hash: 'ca18a2b607411dd18fbb2c1c0e653ec8a6a3f794f46ce050b4a07cf8ba4ab916',
height: 3617698,
},
})
},
submitTransaction: () => {
throw new Error('Not implemented: submitTransaction')
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"defaultMessage": "!!!Go to Staking Center",
"file": "src/legacy/Dashboard/Dashboard.tsx",
"start": {
"line": 229,
"line": 238,
"column": 23,
"index": 7486
"index": 7594
},
"end": {
"line": 232,
"line": 241,
"column": 3,
"index": 7619
"index": 7727
}
}
]
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
"peerDependencies": {
"@yoroi/common": "^1.5.4",
"axios": "^1.5.0",
"immer": "^10.0.3",
"react": ">= 16.8.0 <= 19.0.0",
"react-query": "^3.39.3",
"zod": "^3.22.1"
Expand Down
9 changes: 9 additions & 0 deletions packages/api/src/cardano/api/best-block.mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Chain} from '@yoroi/types'

export const bestBlockMockResponse: Chain.Cardano.BestBlock = {
epoch: 510,
slot: 130081,
globalSlot: 135086881,
hash: 'ab0093eb78bcb0146355741388632eb50c69407df8fa32de85e5f198d725e8f4',
height: 10850697,
}
Loading

0 comments on commit 11d8bb0

Please sign in to comment.