Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass prepared transaction to the SendConfirmation screen with navigate #102

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
optimize according to the review
  • Loading branch information
sviderock committed Feb 26, 2025
commit 0004125e45ac42d5fdc5d0c4e93683482db7970d
6 changes: 3 additions & 3 deletions packages/@divvi/mobile/src/navigator/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { KeylessBackupFlow, KeylessBackupOrigin } from 'src/keylessBackup/types'
import { Screens } from 'src/navigator/Screens'
import { Nft } from 'src/nfts/types'
import { EarnPosition } from 'src/positions/types'
import type { PreparedTransactionsResult } from 'src/public'
import { Recipient } from 'src/recipients/recipient'
import { QrCode, TransactionDataInput } from 'src/send/types'
import type { UsePrepareSendTransactions } from 'src/send/usePrepareSendTransactions'
import { AssetTabType } from 'src/tokens/types'
import { NetworkId, TokenTransaction, TokenTransfer } from 'src/transactions/types'
import { Countries } from 'src/utils/Countries'
Expand All @@ -34,14 +34,14 @@ interface SendConfirmationFromExternalParams {
origin: SendOrigin
transactionData: TransactionDataInput
isFromScan: boolean
prepareTransactions?: never
prepareTransactionsResult?: never
}

interface SendConfirmationParams {
origin: SendOrigin
transactionData: TransactionDataInput
isFromScan: boolean
prepareTransactions: UsePrepareSendTransactions
prepareTransactionsResult: PreparedTransactionsResult
}

type SendEnterAmountParams = {
Expand Down
9 changes: 3 additions & 6 deletions packages/@divvi/mobile/src/send/SendConfirmation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import { StackParamList } from 'src/navigator/types'
import { RootState } from 'src/redux/reducers'
import SendConfirmation from 'src/send/SendConfirmation'
import { sendPayment } from 'src/send/actions'
import {
usePrepareSendTransactions,
type UsePrepareSendTransactions,
} from 'src/send/usePrepareSendTransactions'
import { usePrepareSendTransactions } from 'src/send/usePrepareSendTransactions'
import { PreparedTransactionsPossible } from 'src/viem/prepareTransactions'
import { getSerializablePreparedTransaction } from 'src/viem/preparedTransactionSerialization'
import { RecursivePartial, createMockStore, getMockStackScreenProps } from 'test/utils'
Expand Down Expand Up @@ -46,7 +43,7 @@ const mockScreenProps = getMockStackScreenProps(Screens.SendConfirmation, {
},
origin: SendOrigin.AppSendFlow,
isFromScan: false,
prepareTransactions: undefined as never,
prepareTransactionsResult: undefined as never,
})

const mockFeeCurrencies = [
Expand Down Expand Up @@ -76,7 +73,7 @@ type ScreenProps = NativeStackScreenProps<
>

describe('SendConfirmation', () => {
let mockUsePrepareSendTransactionsOutput: UsePrepareSendTransactions
let mockUsePrepareSendTransactionsOutput: ReturnType<typeof usePrepareSendTransactions>

beforeEach(() => {
jest.clearAllMocks()
Expand Down
10 changes: 8 additions & 2 deletions packages/@divvi/mobile/src/send/SendConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default function SendConfirmation(props: Props) {
refreshPreparedTransactions,
clearPreparedTransactions,
prepareTransactionLoading,
} = usePrepareSendTransactions(props.route.params.prepareTransactions)
} = usePrepareSendTransactions(props.route.params.prepareTransactionsResult)

const fromExternal = props.route.name === Screens.SendConfirmationFromExternal
const tokenInfo = useTokenInfo(tokenId)
Expand All @@ -88,9 +88,15 @@ export default function SendConfirmation(props: Props) {
)

useEffect(() => {
// do not refresh if prepared transactions is available in the route params
if (fromExternal) {
return
}

if (!walletAddress || !tokenInfo) {
return // should never happen
}

clearPreparedTransactions()
const debouncedRefreshTransactions = setTimeout(() => {
return refreshPreparedTransactions({
Expand All @@ -102,7 +108,7 @@ export default function SendConfirmation(props: Props) {
})
}, DEBOUNCE_TIME_MS)
return () => clearTimeout(debouncedRefreshTransactions)
}, [tokenInfo, tokenAmount, recipient, walletAddress, feeCurrencies])
}, [tokenInfo, tokenAmount, recipient, walletAddress, feeCurrencies, fromExternal])

const disableSend =
isSending || !prepareTransactionsResult || prepareTransactionsResult.type !== 'possible'
Expand Down
2 changes: 1 addition & 1 deletion packages/@divvi/mobile/src/send/SendEnterAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function SendEnterAmount({ route }: Props) {
navigate(Screens.SendConfirmation, {
origin,
isFromScan,
prepareTransactions,
prepareTransactionsResult,
transactionData: {
tokenId: token.tokenId,
recipient,
Expand Down
33 changes: 11 additions & 22 deletions packages/@divvi/mobile/src/send/usePrepareSendTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,12 @@ export async function prepareSendTransactionsCallback({
/**
* Hook to prepare transactions for sending crypto.
*/

export type UsePrepareSendTransactions = {
prepareTransactionsResult: PreparedTransactionsResult | undefined
refreshPreparedTransactions: (
props: PrepareSendTransactionsCallbackProps
) => Promise<PreparedTransactionsResult | undefined>
clearPreparedTransactions: () => void
prepareTransactionError: Error | undefined
prepareTransactionLoading: boolean
}

export function usePrepareSendTransactions(
existingPrepareTranscation?: UsePrepareSendTransactions
existingPrepareTransactionResult?: PreparedTransactionsResult
) {
const prepareTransactions = useAsyncCallback(
(props: PrepareSendTransactionsCallbackProps) => {
if (existingPrepareTranscation) return
if (existingPrepareTransactionResult) return
return prepareSendTransactionsCallback(props)
},
{
Expand All @@ -82,13 +71,13 @@ export function usePrepareSendTransactions(
}
)

return (
existingPrepareTranscation ?? {
prepareTransactionsResult: prepareTransactions.result,
refreshPreparedTransactions: prepareTransactions.execute,
clearPreparedTransactions: prepareTransactions.reset,
prepareTransactionError: prepareTransactions.error,
prepareTransactionLoading: prepareTransactions.loading,
}
)
return {
prepareTransactionsResult: existingPrepareTransactionResult ?? prepareTransactions.result,
refreshPreparedTransactions: prepareTransactions.execute,
clearPreparedTransactions: prepareTransactions.reset,
prepareTransactionError: prepareTransactions.error,
prepareTransactionLoading: existingPrepareTransactionResult
? false
: prepareTransactions.loading,
}
}
Loading