-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(swap): Add swap cancellation easy-confirmation support (#2734)
- Loading branch information
Showing
9 changed files
with
190 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
apps/wallet-mobile/src/features/Swap/common/ConfirmRawTx/ConfirmRawTx.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import {storiesOf} from '@storybook/react-native' | ||
import {mockSwapManager, mockSwapStateDefault, SwapProvider} from '@yoroi/swap' | ||
import React from 'react' | ||
import {StyleSheet, View} from 'react-native' | ||
|
||
import {SelectedWalletProvider} from '../../../../SelectedWallet' | ||
import {YoroiWallet} from '../../../../yoroi-wallets/cardano/types' | ||
import {mocks as walletMocks} from '../../../../yoroi-wallets/mocks' | ||
import {mocks} from '../mocks' | ||
import {SwapFormProvider} from '../SwapFormProvider' | ||
import {ConfirmRawTx} from './ConfirmRawTx' | ||
|
||
storiesOf('ConfirmRawTx', module) | ||
.addDecorator((story) => <View style={styles.container}>{story()}</View>) | ||
.add('ConfirmRawTxWithPassword', () => ( | ||
<Provider wallet={walletMocks.wallet}> | ||
<ConfirmRawTx /> | ||
</Provider> | ||
)) | ||
.add('ConfirmRawTxWithOs', () => ( | ||
<Provider wallet={{...walletMocks.wallet, isEasyConfirmationEnabled: true}}> | ||
<ConfirmRawTx /> | ||
</Provider> | ||
)) | ||
.add('ConfirmRawTxWithHw', () => ( | ||
<Provider wallet={{...walletMocks.wallet, isHW: true}}> | ||
<ConfirmRawTx /> | ||
</Provider> | ||
)) | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
padding: 16, | ||
}, | ||
}) | ||
|
||
const Provider = ({children, wallet}: {children: React.ReactNode; wallet: YoroiWallet}) => { | ||
return ( | ||
<SelectedWalletProvider wallet={wallet}> | ||
<SwapProvider | ||
initialState={{ | ||
...mockSwapStateDefault, | ||
unsignedTx: walletMocks.yoroiUnsignedTx, | ||
createOrder: {...mocks.confirmTx.createOrder}, | ||
}} | ||
swapManager={{ | ||
...mockSwapManager, | ||
}} | ||
> | ||
<SwapFormProvider>{children}</SwapFormProvider> | ||
</SwapProvider> | ||
</SelectedWalletProvider> | ||
) | ||
} |
20 changes: 20 additions & 0 deletions
20
apps/wallet-mobile/src/features/Swap/common/ConfirmRawTx/ConfirmRawTx.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react' | ||
|
||
import {useSelectedWallet} from '../../../../SelectedWallet' | ||
import {ConfirmRawTxWithHW} from './ConfirmRawTxWithHW' | ||
import {ConfirmRawTxWithOs} from './ConfirmRawTxWithOs' | ||
import {ConfirmRawTxWithPassword} from './ConfirmRawTxWithPassword' | ||
|
||
export const ConfirmRawTx = ({onConfirm}: {onConfirm?: (rootKey: string) => Promise<void>}) => { | ||
const wallet = useSelectedWallet() | ||
|
||
if (wallet.isHW) { | ||
return <ConfirmRawTxWithHW /> | ||
} | ||
|
||
if (wallet.isEasyConfirmationEnabled) { | ||
return <ConfirmRawTxWithOs onConfirm={onConfirm} /> | ||
} | ||
|
||
return <ConfirmRawTxWithPassword onConfirm={onConfirm} /> | ||
} |
4 changes: 4 additions & 0 deletions
4
apps/wallet-mobile/src/features/Swap/common/ConfirmRawTx/ConfirmRawTxWithHW.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const ConfirmRawTxWithHW = () => { | ||
// TODO: Needs to be implemented | ||
return null | ||
} |
47 changes: 47 additions & 0 deletions
47
apps/wallet-mobile/src/features/Swap/common/ConfirmRawTx/ConfirmRawTxWithOs.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React, {useEffect} from 'react' | ||
import {ActivityIndicator, StyleSheet, Text, View} from 'react-native' | ||
|
||
import {useSelectedWallet} from '../../../../SelectedWallet' | ||
import {COLORS} from '../../../../theme' | ||
import {useAuthOsWithEasyConfirmation} from '../../../../yoroi-wallets/auth' | ||
|
||
export const ConfirmRawTxWithOs = ({onConfirm}: {onConfirm?: (rootKey: string) => Promise<void>}) => { | ||
const wallet = useSelectedWallet() | ||
|
||
const {authWithOs, error} = useAuthOsWithEasyConfirmation( | ||
{id: wallet.id}, | ||
{onSuccess: (rootKey) => onConfirm?.(rootKey)}, | ||
) | ||
|
||
useEffect(() => { | ||
if (!wallet.isEasyConfirmationEnabled) return | ||
authWithOs() | ||
}, [wallet.isEasyConfirmationEnabled, authWithOs]) | ||
|
||
if (error) { | ||
return ( | ||
<View style={styles.center}> | ||
<Text style={styles.errorMessage} numberOfLines={3}> | ||
{error.message} | ||
</Text> | ||
</View> | ||
) | ||
} | ||
|
||
return ( | ||
<View style={styles.center}> | ||
<ActivityIndicator size="large" color="black" /> | ||
</View> | ||
) | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
center: { | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}, | ||
errorMessage: { | ||
color: COLORS.ERROR_TEXT_COLOR, | ||
textAlign: 'center', | ||
}, | ||
}) |
42 changes: 42 additions & 0 deletions
42
apps/wallet-mobile/src/features/Swap/common/ConfirmRawTx/ConfirmRawTxWithPassword.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React, {useState} from 'react' | ||
|
||
import {Spacer} from '../../../../components' | ||
import {useSelectedWallet} from '../../../../SelectedWallet' | ||
import {ConfirmWithSpendingPassword} from '../ConfirmWithSpendingPassword' | ||
|
||
export const ConfirmRawTxWithPassword = ({onConfirm}: {onConfirm?: (rootKey: string) => Promise<void>}) => { | ||
const wallet = useSelectedWallet() | ||
|
||
const handlePasswordConfirm = async (password: string) => { | ||
const rootKey = await wallet.encryptedStorage.rootKey.read(password) | ||
return onConfirm?.(rootKey) | ||
} | ||
|
||
return <PasswordInput onConfirm={handlePasswordConfirm} /> | ||
} | ||
|
||
const PasswordInput = ({onConfirm}: {onConfirm: (password: string) => Promise<void>}) => { | ||
const [error, setError] = useState<Error | null>(null) | ||
const [loading, setLoading] = useState(false) | ||
|
||
const onConfirmPress = async (password: string) => { | ||
setError(null) | ||
setLoading(true) | ||
try { | ||
await onConfirm(password) | ||
} catch (e: unknown) { | ||
if (e instanceof Error) { | ||
setError(e) | ||
} | ||
} | ||
setLoading(false) | ||
} | ||
|
||
return ( | ||
<> | ||
<ConfirmWithSpendingPassword onSubmit={onConfirmPress} isLoading={loading} error={error ?? undefined} /> | ||
|
||
<Spacer height={10} /> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters