Skip to content

Commit

Permalink
bump ajs and avalanche-sdks to latest version (#2054)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruijialin-avalabs authored Nov 5, 2024
1 parent f5528e1 commit ea1aaed
Show file tree
Hide file tree
Showing 39 changed files with 1,292 additions and 417 deletions.
3 changes: 2 additions & 1 deletion packages/core-mobile/app/contexts/BridgeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ function LocalBridgeProvider({
config &&
!TrackerSubscriptions.has(trackedTransaction.sourceTxHash) &&
avalancheProvider &&
ethereumProvider
ethereumProvider &&
bitcoinProvider
) {
// Start transaction tracking process (no need to await)
try {
Expand Down
68 changes: 52 additions & 16 deletions packages/core-mobile/app/hooks/networks/networkProviderHooks.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,74 @@
import {
Avalanche,
BitcoinProvider,
JsonRpcBatchInternal
} from '@avalabs/core-wallets-sdk'
import { useMemo } from 'react'
import { useEffect, useState } from 'react'
import {
getAvalancheEvmProvider,
getAvalancheXpProvider,
getBitcoinProvider,
getEthereumProvider
} from 'services/network/utils/providerUtils'
import Logger from 'utils/Logger'
import { useNetworks } from './useNetworks'

export function useEthereumProvider(): JsonRpcBatchInternal | undefined {
export function useEthereumProvider(
isTestnet?: boolean
): JsonRpcBatchInternal | undefined {
const { activeNetwork, networks } = useNetworks()
const _isTestnet = isTestnet ?? activeNetwork.isTestnet

return useMemo(
() => getEthereumProvider(networks, activeNetwork.isTestnet),
[networks, activeNetwork]
)
const [ethereumProvider, setEthereumProvider] =
useState<JsonRpcBatchInternal>()
useEffect(() => {
getEthereumProvider(networks, _isTestnet)
.then(setEthereumProvider)
.catch(Logger.error)
}, [networks, activeNetwork, _isTestnet])

return ethereumProvider
}

export function useBitcoinProvider(): BitcoinProvider {
export function useBitcoinProvider(
isTestnet?: boolean
): BitcoinProvider | undefined {
const { activeNetwork } = useNetworks()
const _isTestnet = isTestnet ?? activeNetwork.isTestnet
const [bitcoinProvider, setBitcoinProvider] = useState<BitcoinProvider>()
useEffect(() => {
getBitcoinProvider(_isTestnet).then(setBitcoinProvider).catch(Logger.error)
}, [activeNetwork, _isTestnet])

return useMemo(
() => getBitcoinProvider(activeNetwork.isTestnet),
[activeNetwork]
)
return bitcoinProvider
}

export function useAvalancheProvider(): JsonRpcBatchInternal | undefined {
export function useAvalancheProvider(
isTestnet?: boolean
): JsonRpcBatchInternal | undefined {
const { activeNetwork, networks } = useNetworks()
const _isTestnet = isTestnet ?? activeNetwork.isTestnet
const [avalancheProvider, setAvalancheProvider] =
useState<JsonRpcBatchInternal>()
useEffect(() => {
getAvalancheEvmProvider(networks, _isTestnet)
.then(setAvalancheProvider)
.catch(Logger.error)
}, [networks, activeNetwork, _isTestnet])
return avalancheProvider
}

return useMemo(
() => getAvalancheEvmProvider(networks, activeNetwork.isTestnet),
[networks, activeNetwork]
)
export function useAvalancheXpProvider(
isTestnet?: boolean
): Avalanche.JsonRpcProvider | undefined {
const { activeNetwork } = useNetworks()
const _isTestnet = isTestnet ?? activeNetwork.isTestnet
const [avalancheXpProvider, setAvalancheXpProvider] =
useState<Avalanche.JsonRpcProvider>()
useEffect(() => {
getAvalancheXpProvider(!!_isTestnet)
.then(setAvalancheXpProvider)
.catch(Logger.error)
}, [activeNetwork, _isTestnet])
return avalancheXpProvider
}
9 changes: 5 additions & 4 deletions packages/core-mobile/app/hooks/useCChainBaseFee.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { UseQueryResult, useQuery } from '@tanstack/react-query'
import { useSelector } from 'react-redux'
import NetworkService from 'services/network/NetworkService'
import { selectIsDeveloperMode } from 'store/settings/advanced'
import { TokenUnit } from '@avalabs/core-utils-sdk'
import useCChainNetwork from 'hooks/earn/useCChainNetwork'
import { useAvalancheXpProvider } from './networks/networkProviderHooks'

const REFETCH_INTERVAL = 10000 // 10 seconds

Expand All @@ -18,17 +18,18 @@ export const useCChainBaseFee = (): UseQueryResult<
Error
> => {
const isDeveloperMode = useSelector(selectIsDeveloperMode)
const avaxProvider = NetworkService.getAvalancheProviderXP(isDeveloperMode)
const cChainNetwork = useCChainNetwork()
const avaxProvider = useAvalancheXpProvider(isDeveloperMode)

return useQuery({
// no need to retry failed request as we are already doing interval fetching
retry: false,
refetchInterval: REFETCH_INTERVAL,
queryKey: ['cChainBaseFee', isDeveloperMode, cChainNetwork],
queryKey: ['cChainBaseFee', isDeveloperMode, cChainNetwork, avaxProvider],
queryFn: async () => {
if (!cChainNetwork || !avaxProvider) return undefined
const baseFeeWei = await avaxProvider.getApiC().getBaseFee()
if (!cChainNetwork) return undefined

return new TokenUnit(
baseFeeWei,
cChainNetwork.networkToken.decimals,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ const METHOD_NAME_TO_ICON: Record<
Transfer: undefined,
FillOrder: undefined,
Unwrap: undefined,
UNKNOWN: undefined
UNKNOWN: undefined,

ConvertSubnetTx: undefined,
RegisterSubnetValidatorTx: undefined,
SetSubnetValidatorWeightTx: undefined,
DisableSubnetValidatorTx: undefined,
IncreaseBalanceTx: undefined
}

export const PrimaryNetworkMethodIcon = ({
Expand Down
6 changes: 2 additions & 4 deletions packages/core-mobile/app/screens/nft/send/NftSend.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useMemo, useState } from 'react'
import React, { useEffect, useState } from 'react'
import { Image, ScrollView, StyleSheet, View } from 'react-native'
import { Text, Button } from '@avalabs/k2-mobile'
import { Space } from 'components/Space'
Expand All @@ -24,7 +24,6 @@ import AppNavigation from 'navigation/AppNavigation'
import { useNavigation, useRoute } from '@react-navigation/native'
import { NFTDetailsSendScreenProps } from 'navigation/types'
import useEVMSend from 'screens/send/hooks/useEVMSend'
import { getEvmProvider } from 'services/network/utils/providerUtils'
import { useSelector } from 'react-redux'
import { selectTokensWithBalance } from 'store/balance'
import { NetworkTokenWithBalance, TokenType } from '@avalabs/vm-module-types'
Expand Down Expand Up @@ -61,7 +60,6 @@ export default function NftSend({
isValidating
} = useSendContext()
const { activeNetwork } = useNetworks()
const provider = useMemo(() => getEvmProvider(activeNetwork), [activeNetwork])
const activeAccount = useSelector(selectActiveAccount)
const fromAddress = activeAccount?.addressC ?? ''
const tokens = useSelector(selectTokensWithBalance)
Expand All @@ -73,7 +71,7 @@ export default function NftSend({
const { send } = useEVMSend({
chainId: activeNetwork.chainId,
fromAddress,
provider,
network: activeNetwork,
maxFee,
nativeToken
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import React from 'react'
import { Text, TouchableOpacity, View } from '@avalabs/k2-mobile'
import {
AddressItem,
CurrencyItem,
DataItem,
DateItem,
DetailItemType,
DetailSection,
FundsRecipientItem,
LinkItem,
NodeIDItem,
TextItem
} from '@avalabs/vm-module-types'
import { Row } from 'components/Row'
Expand Down Expand Up @@ -63,14 +69,6 @@ export const DetailSectionView = ({
</Row>
)

const renderAddressValue = (address: string): JSX.Element => (
<TokenAddress address={address} />
)

const renderNodeIDValue = (nodeID: string): JSX.Element => (
<NodeID nodeID={nodeID} />
)

const renderDataValue = (data: string): JSX.Element => (
<TouchableOpacity hitSlop={12} onPress={() => onPressDataItem(data)}>
<Text variant="buttonSmall" sx={{ color: '$blueDark' }}>
Expand All @@ -79,10 +77,6 @@ export const DetailSectionView = ({
</TouchableOpacity>
)

const renderDateValue = (date: string): JSX.Element => (
<Text variant="buttonSmall">{getDateInMmmDdYyyyHhMmA(parseInt(date))}</Text>
)

const renderCurrencyValue = (
value: bigint,
decimals: number,
Expand All @@ -106,6 +100,32 @@ export const DetailSectionView = ({
)
}

const renderValue = (
item:
| AddressItem
| NodeIDItem
| CurrencyItem
| DataItem
| DateItem
| FundsRecipientItem
): JSX.Element => {
return item.type === DetailItemType.ADDRESS ? (
<TokenAddress address={item.value} />
) : item.type === DetailItemType.NODE_ID ? (
<NodeID nodeID={item.value} />
) : item.type === DetailItemType.DATA ? (
renderDataValue(item.value)
) : item.type === DetailItemType.DATE ? (
<Text variant="buttonSmall">
{getDateInMmmDdYyyyHhMmA(parseInt(item.value))}
</Text>
) : item.type === DetailItemType.CURRENCY ? (
renderCurrencyValue(item.value, item.maxDecimals, item.symbol)
) : (
renderCurrencyValue(item.amount, item.maxDecimals, item.symbol)
)
}

return (
<View>
{detailSection.title && (
Expand Down Expand Up @@ -143,19 +163,7 @@ export const DetailSectionView = ({
}}
key={index}>
<Text variant="caption">{item.label}</Text>
{item.type === DetailItemType.ADDRESS
? renderAddressValue(item.value)
: item.type === DetailItemType.NODE_ID
? renderNodeIDValue(item.value)
: item.type === DetailItemType.DATA
? renderDataValue(item.value)
: item.type === DetailItemType.DATE
? renderDateValue(item.value)
: renderCurrencyValue(
item.value,
item.maxDecimals,
item.symbol
)}
{renderValue(item)}
</Row>
)
})}
Expand Down
9 changes: 1 addition & 8 deletions packages/core-mobile/app/screens/send/components/SendAVM.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { useEffect, useMemo } from 'react'
import React, { useEffect } from 'react'
import { useSendContext } from 'contexts/SendContext'
import { Network } from '@avalabs/core-chains-sdk'
import { TokenWithBalanceAVM } from '@avalabs/vm-module-types'
import { useSelector } from 'react-redux'
import { selectActiveAccount } from 'store/account'
import NetworkService from 'services/network/NetworkService'
import { Contact, CorePrimaryAccount } from '@avalabs/types'
import { getAddressXP } from 'store/utils/account&contactGetters'
import useAVMSend from '../hooks/useAVMSend'
Expand Down Expand Up @@ -38,19 +37,13 @@ const SendAVM = ({
isSending,
maxFee
} = useSendContext()
const provider = useMemo(
() => NetworkService.getAvalancheProviderXP(!!network.isTestnet),
[network]
)

const activeAccount = useSelector(selectActiveAccount)

const fromAddress = activeAccount?.addressAVM ?? ''

const { send } = useAVMSend({
network,
fromAddress,
provider,
maxFee,
nativeToken,
account
Expand Down
9 changes: 2 additions & 7 deletions packages/core-mobile/app/screens/send/components/SendBTC.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useEffect, useMemo } from 'react'
import React, { useEffect } from 'react'
import { useSendContext } from 'contexts/SendContext'
import { Network } from '@avalabs/core-chains-sdk'
import { getBitcoinProvider } from 'services/network/utils/providerUtils'
import { TokenWithBalanceBTC } from '@avalabs/vm-module-types'
import { Contact, CorePrimaryAccount } from '@avalabs/types'
import useBTCSend from '../hooks/useBTCSend'
Expand Down Expand Up @@ -34,17 +33,13 @@ const SendBTC = ({
isSending,
maxFee
} = useSendContext()
const provider = useMemo(
() => getBitcoinProvider(network.isTestnet),
[network]
)

const fromAddress = account?.addressBTC ?? ''

const { send } = useBTCSend({
fromAddress,
isMainnet: !network.isTestnet,
provider,
network,
maxFee,
nativeToken
})
Expand Down
7 changes: 2 additions & 5 deletions packages/core-mobile/app/screens/send/components/SendEVM.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useMemo } from 'react'
import React from 'react'
import { useSendContext } from 'contexts/SendContext'
import { Network } from '@avalabs/core-chains-sdk'
import { getEvmProvider } from 'services/network/utils/providerUtils'
import { NetworkTokenWithBalance } from '@avalabs/vm-module-types'
import { Contact, CorePrimaryAccount } from '@avalabs/types'
import { getAddressProperty } from 'store/utils/account&contactGetters'
Expand Down Expand Up @@ -35,14 +34,12 @@ const SendEVM = ({
isSending,
maxFee
} = useSendContext()
const provider = useMemo(() => getEvmProvider(network), [network])

const fromAddress = account?.addressC ?? ''

const { send } = useEVMSend({
chainId: network.chainId,
fromAddress,
provider,
network,
maxFee,
nativeToken
})
Expand Down
9 changes: 1 addition & 8 deletions packages/core-mobile/app/screens/send/components/SendPVM.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { useEffect, useMemo } from 'react'
import React, { useEffect } from 'react'
import { useSendContext } from 'contexts/SendContext'
import { Network } from '@avalabs/core-chains-sdk'
import { TokenWithBalancePVM } from '@avalabs/vm-module-types'
import { useSelector } from 'react-redux'
import { selectActiveAccount } from 'store/account'
import NetworkService from 'services/network/NetworkService'
import { Contact, CorePrimaryAccount } from '@avalabs/types'
import { getAddressXP } from 'store/utils/account&contactGetters'
import usePVMSend from '../hooks/usePVMSend'
Expand Down Expand Up @@ -38,19 +37,13 @@ const SendPVM = ({
isSending,
maxFee
} = useSendContext()
const provider = useMemo(
() => NetworkService.getAvalancheProviderXP(!!network.isTestnet),
[network]
)

const activeAccount = useSelector(selectActiveAccount)

const fromAddress = activeAccount?.addressPVM ?? ''

const { send } = usePVMSend({
network,
fromAddress,
provider,
maxFee,
nativeToken,
account
Expand Down
Loading

0 comments on commit ea1aaed

Please sign in to comment.