Skip to content

Commit

Permalink
CP-9066: Fix x/p chain send issue (#1476)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruijialin-avalabs authored Aug 13, 2024
1 parent 1a825e4 commit a785a07
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 151 deletions.
28 changes: 23 additions & 5 deletions packages/core-mobile/app/components/UniversalTokenSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import { formatLargeCurrency } from 'utils/Utils'
import { Amount } from 'types'
import { Text } from '@avalabs/k2-mobile'
import { TokenWithBalance } from '@avalabs/vm-module-types'
import {
isTokenWithBalanceAVM,
isTokenWithBalancePVM
} from '@avalabs/avalanche-module'

interface Props {
selectedToken?: TokenWithBalance
Expand Down Expand Up @@ -92,17 +96,31 @@ const UniversalTokenSelector: FC<Props> = ({
[onAmountChange]
)

const getAvailableBalance = useCallback(() => {
if (selectedToken === undefined) {
return
}

if (
isTokenWithBalancePVM(selectedToken) ||
isTokenWithBalanceAVM(selectedToken)
) {
return `Balance ${selectedToken.availableDisplayValue || '0'} ${
selectedToken.symbol
}`
}
return `Balance ${selectedToken?.balanceDisplayValue || '0'} ${
selectedToken.symbol
}`
}, [selectedToken])

return (
<View style={{ marginHorizontal: 16 }}>
<Row style={{ justifyContent: 'space-between', alignItems: 'center' }}>
<AvaText.Heading3 textStyle={{ marginBottom: 4 }}>
{label ?? 'Token'}
</AvaText.Heading3>
<AvaText.Body2>
{selectedToken &&
selectedToken?.balanceDisplayValue &&
`Balance ${selectedToken.balanceDisplayValue} ${selectedToken.symbol}`}
</AvaText.Body2>
<AvaText.Body2>{getAvailableBalance()}</AvaText.Body2>
</Row>
<Row
style={{
Expand Down
4 changes: 2 additions & 2 deletions packages/core-mobile/app/contexts/SendNFTContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useSelector } from 'react-redux'
import { selectActiveAccount } from 'store/account'
import { NFTItem } from 'store/nft'
import { selectSelectedCurrency } from 'store/settings/currency'
import { selectNativeTokenBalanceForNetworkAndAccount } from 'store/balance/slice'
import { selectAvailableNativeTokenBalanceForNetworkAndAccount } from 'store/balance/slice'
import { SendState } from 'services/send/types'
import sendService from 'services/send/SendService'
import { InteractionManager } from 'react-native'
Expand Down Expand Up @@ -50,7 +50,7 @@ export const SendNFTContextProvider = ({
const activeAccount = useSelector(selectActiveAccount)
const selectedCurrency = useSelector(selectSelectedCurrency)
const nativeTokenBalance = useSelector((state: RootState) =>
selectNativeTokenBalanceForNetworkAndAccount(
selectAvailableNativeTokenBalanceForNetworkAndAccount(
state,
activeNetwork.chainId,
activeAccount?.index
Expand Down
4 changes: 2 additions & 2 deletions packages/core-mobile/app/contexts/SendTokenContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, {
useState
} from 'react'
import { InteractionManager } from 'react-native'
import { selectNativeTokenBalanceForNetworkAndAccount } from 'store/balance/slice'
import { selectAvailableNativeTokenBalanceForNetworkAndAccount } from 'store/balance/slice'
import { useSelector } from 'react-redux'
import { selectActiveAccount } from 'store/account'
import sendService from 'services/send/SendService'
Expand Down Expand Up @@ -60,7 +60,7 @@ export const SendTokenContextProvider = ({
const activeAccount = useSelector(selectActiveAccount)
const selectedCurrency = useSelector(selectSelectedCurrency)
const nativeTokenBalance = useSelector((state: RootState) =>
selectNativeTokenBalanceForNetworkAndAccount(
selectAvailableNativeTokenBalanceForNetworkAndAccount(
state,
activeNetwork.chainId,
activeAccount?.index
Expand Down
5 changes: 3 additions & 2 deletions packages/core-mobile/app/screens/bridge/Bridge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import { Tooltip } from 'components/Tooltip'
import { DOCS_BRIDGE_FAQS } from 'resources/Constants'
import { selectSelectedCurrency } from 'store/settings/currency/slice'
import { useNetworks } from 'hooks/networks/useNetworks'
import { selectNativeTokenBalanceForNetworkAndAccount } from 'store/balance/slice'
import { selectAvailableNativeTokenBalanceForNetworkAndAccount } from 'store/balance/slice'
import { RootState } from 'store'
import { selectActiveAccount } from 'store/account/slice'
import { Audios, audioFeedback } from 'utils/AudioFeedback'
Expand Down Expand Up @@ -116,7 +116,7 @@ const Bridge: FC = () => {
const [isPending, setIsPending] = useState(false)
const tokenInfoData = useTokenInfoContext()
const nativeTokenBalance = useSelector((state: RootState) =>
selectNativeTokenBalanceForNetworkAndAccount(
selectAvailableNativeTokenBalanceForNetworkAndAccount(
state,
activeNetwork.chainId,
activeAccount?.index
Expand Down Expand Up @@ -483,6 +483,7 @@ const Bridge: FC = () => {
style={{
top: 22
}}
alignment="flex-end"
prompt={
<Text variant="buttonMedium" style={styles.tokenSelectorText}>
Select Network
Expand Down
13 changes: 11 additions & 2 deletions packages/core-mobile/app/services/send/SendServiceAVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import { AvalancheTxParams } from 'store/rpc/handlers/avalanche_sendTransaction/
import { GAS_LIMIT_FOR_XP_CHAIN } from 'consts/fees'
import { getInternalExternalAddrs } from 'services/send/utils'
import { stripChainAddress } from 'store/account/utils'
import {
isTokenWithBalanceAVM,
isTokenWithBalancePVM
} from '@avalabs/avalanche-module'

export class SendServiceAVM {
constructor(private activeNetwork: Network) {}
Expand All @@ -37,8 +41,13 @@ export class SendServiceAVM {
const sendFee = defaultMaxFeePerGas
? BigInt(gasLimit) * defaultMaxFeePerGas
: undefined
let maxAmount = token.balance - (sendFee || 0n)
maxAmount = maxAmount > 0n ? maxAmount : 0n

const availableBalance =
isTokenWithBalanceAVM(token) || isTokenWithBalancePVM(token)
? token.available
: token.balance
let maxAmount = availableBalance && availableBalance - (sendFee || 0n)
maxAmount = maxAmount && maxAmount > 0n ? maxAmount : 0n

const newState: SendState = {
...sendState,
Expand Down
12 changes: 10 additions & 2 deletions packages/core-mobile/app/services/send/SendServicePVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import { AvalancheTxParams } from 'store/rpc/handlers/avalanche_sendTransaction/
import { GAS_LIMIT_FOR_XP_CHAIN } from 'consts/fees'
import { stripChainAddress } from 'store/account/utils'
import { TokenType } from '@avalabs/vm-module-types'
import {
isTokenWithBalanceAVM,
isTokenWithBalancePVM
} from '@avalabs/avalanche-module'

export class SendServicePVM {
constructor(private activeNetwork: Network) {}
Expand All @@ -37,8 +41,12 @@ export class SendServicePVM {
const sendFee = defaultMaxFeePerGas
? BigInt(gasLimit) * defaultMaxFeePerGas
: undefined
let maxAmount = token.balance - (sendFee || 0n)
maxAmount = maxAmount > 0n ? maxAmount : 0n
const availableBalance =
isTokenWithBalanceAVM(token) || isTokenWithBalancePVM(token)
? token.available
: token.balance
let maxAmount = availableBalance && availableBalance - (sendFee || 0n)
maxAmount = maxAmount && maxAmount > 0n ? maxAmount : 0n

const newState: SendState = {
...sendState,
Expand Down
36 changes: 25 additions & 11 deletions packages/core-mobile/app/store/balance/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { selectActiveNetwork, selectIsTestnet } from 'store/network'
import { Network } from '@avalabs/core-chains-sdk'
import { selectIsDeveloperMode } from 'store/settings/advanced'
import { TokenType } from '@avalabs/vm-module-types'
import {
isTokenWithBalanceAVM,
isTokenWithBalancePVM
} from '@avalabs/avalanche-module'
import {
Balance,
Balances,
Expand Down Expand Up @@ -224,20 +228,30 @@ const _selectBalanceKeyForNetworkAndAccount = (
return getKey(chainId, accountIndex)
}

export const selectNativeTokenBalanceForNetworkAndAccount = createSelector(
[_selectAllBalances, _selectBalanceKeyForNetworkAndAccount],
(allBalances, key): bigint => {
if (key === undefined) return 0n
export const selectAvailableNativeTokenBalanceForNetworkAndAccount =
createSelector(
[_selectAllBalances, _selectBalanceKeyForNetworkAndAccount],
(allBalances, key): bigint => {
if (key === undefined) return 0n

const balanceForNetworkAndAccount = allBalances[key]
const balanceForNetworkAndAccount = allBalances[key]

const nativeToken = Object.values(
balanceForNetworkAndAccount?.tokens ?? []
)?.find(token => token.type === TokenType.NATIVE)
const nativeToken = Object.values(
balanceForNetworkAndAccount?.tokens ?? []
)?.find(token => {
return token.type === TokenType.NATIVE
})

return nativeToken?.balance ?? 0n
}
)
if (
nativeToken &&
(isTokenWithBalancePVM(nativeToken) ||
isTokenWithBalanceAVM(nativeToken))
) {
return nativeToken.available ?? 0n
}
return nativeToken?.balance ?? 0n
}
)

// actions
export const { setStatus, setBalances } = balanceSlice.actions
Expand Down
8 changes: 4 additions & 4 deletions packages/core-mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@
"gen:glacierApi": "npx openapi-zod-client 'https://glacier-api-dev.avax.network/api-json' -o './app/utils/network/glacierApi.client.ts'"
},
"dependencies": {
"@avalabs/avalanche-module": "0.1.5",
"@avalabs/avalanche-module": "0.1.6",
"@avalabs/avalanchejs": "4.0.5",
"@avalabs/bitcoin-module": "0.1.5",
"@avalabs/bitcoin-module": "0.1.6",
"@avalabs/bridge-unified": "2.1.0",
"@avalabs/core-bridge-sdk": "3.1.0-alpha.0",
"@avalabs/core-chains-sdk": "3.1.0-alpha.0",
"@avalabs/core-coingecko-sdk": "3.1.0-alpha.0",
"@avalabs/core-utils-sdk": "3.1.0-alpha.0",
"@avalabs/core-wallets-sdk": "3.1.0-alpha.0",
"@avalabs/evm-module": "0.1.5",
"@avalabs/evm-module": "0.1.6",
"@avalabs/glacier-sdk": "3.1.0-alpha.0",
"@avalabs/k2-mobile": "workspace:*",
"@avalabs/types": "3.1.0-alpha.0",
"@avalabs/vm-module-types": "0.1.5",
"@avalabs/vm-module-types": "0.1.6",
"@blockaid/client": "0.10.0",
"@coinbase/cbpay-js": "2.2.1",
"@cubist-labs/cubesigner-sdk": "0.3.28",
Expand Down
Loading

0 comments on commit a785a07

Please sign in to comment.