Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo committed Dec 17, 2024
1 parent d18f2de commit 2d639fb
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 62 deletions.
61 changes: 40 additions & 21 deletions apps/wallet-mobile/src/features/Swap/common/SwapProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import {usePortfolioBalances} from '../../Portfolio/common/hooks/usePortfolioBal
import {usePortfolioTokenInfos} from '../../Portfolio/common/hooks/usePortfolioTokenInfos'
import {useSelectedWallet} from '../../WalletManager/common/hooks/useSelectedWallet'
import {useNavigateTo} from './navigation'
import {useStrings} from './strings'

export const useSwap = () => React.useContext(SwapContext)

export const SwapProvider = ({children}: {children: React.ReactNode}) => {
const navigate = useNavigateTo()
const strings = useStrings()
const {wallet} = useSelectedWallet()
const network = wallet.networkManager.network
const _balances = usePortfolioBalances({wallet})
const balances = usePortfolioBalances({wallet})
const stakingKey = useStakingKey(wallet)
const address = wallet.externalAddresses[0]
const addressHex = useAddressHex(wallet)
Expand Down Expand Up @@ -65,6 +67,17 @@ export const SwapProvider = ({children}: {children: React.ReactNode}) => {
*/
const [state, dispatch] = React.useReducer(swapReducer, defaultState)

React.useEffect(() => {
const tokenAmount = balances.records.get(state.tokenInInput.tokenId ?? '.unknown')
const tokenBalance = Number(tokenAmount?.quantity ?? 0n) / 10 ** (tokenAmount?.info?.decimals ?? 0)
const hasEnoughBalance = tokenBalance >= Number(state.tokenInInput.value)
if (!hasEnoughBalance) {
dispatch({type: 'TokenInErrorChanged', value: strings.notEnoughBalance})
} else {
dispatch({type: 'TokenInErrorChanged', value: null})
}
}, [balances.records, state.tokenInInput.tokenId, state.tokenInInput.value, strings.notEnoughBalance])

React.useEffect(() => {
if (state.reqres === 'response') return

Expand All @@ -75,11 +88,16 @@ export const SwapProvider = ({children}: {children: React.ReactNode}) => {
slippage: state.slippageInput.value,
tokenIn: state.tokenInInput.tokenId,
tokenOut: state.tokenOutInput.tokenId,
amountIn: Number(state.tokenInInput.value),
// amountOut: Number(state.tokenOutInput:value),
...(state.lastInputTouched === 'in'
? {
amountIn: Number(state.tokenInInput.value),
...(state.orderType === 'limit' && {wantedPrice: Number(state.wantedPrice)}),
}
: {
amountOut: Number(state.tokenOutInput.value),
}),
blacklistedDexes: [],
dex: state.selectedDex.value,
// wantedPrice: Number(state.wantedPrice),
})
.then((response) => {
if (response.tag === 'left') {
Expand All @@ -95,14 +113,14 @@ export const SwapProvider = ({children}: {children: React.ReactNode}) => {

swapManager.api
.create({
slippage: state.slippageInput.value,
tokenIn: state.tokenInInput.tokenId,
tokenOut: state.tokenOutInput.tokenId,
amountIn: Number(state.tokenInInput.value),
// amountOut: Number(state.tokenOutInput:value),
...(state.orderType === 'limit'
? {wantedPrice: Number(state.wantedPrice)}
: {slippage: state.slippageInput.value}),
blacklistedDexes: [],
dex: state.selectedDex.value,
// wantedPrice: Number(state.wantedPrice),
})
.then((response) => {
if (response.tag === 'left') {
Expand All @@ -114,11 +132,13 @@ export const SwapProvider = ({children}: {children: React.ReactNode}) => {
})
}, [
navigate,
state.orderType,
state.selectedDex.value,
state.slippageInput.value,
state.tokenInInput.tokenId,
state.tokenInInput.value,
state.tokenOutInput.tokenId,
state.wantedPrice,
swapManager.api,
])

Expand Down Expand Up @@ -153,13 +173,13 @@ const swapReducer = (state: SwapState, action: SwapAction) => {
case SwapAction.TokenInInputTouched:
draft.tokenInInput.isTouched = true
draft.tokenInInput.value = ''
draft.tokenInInput.error = undefined
draft.tokenInInput.error = null

break
case SwapAction.TokenOutInputTouched:
draft.tokenOutInput.isTouched = true
draft.tokenOutInput.value = ''
draft.tokenOutInput.error = undefined
draft.tokenOutInput.error = null

break
case SwapAction.TokenInIdChanged:
Expand Down Expand Up @@ -193,19 +213,18 @@ const swapReducer = (state: SwapState, action: SwapAction) => {
break
case SwapAction.WantedPriceInputChanged:
draft.wantedPrice.value = action.value
draft.lastInputTouched = 'limit'

break
case SwapAction.SwitchTouched:
draft.tokenOutInput.isTouched = state.tokenInInput.isTouched
draft.tokenOutInput.tokenId = state.tokenInInput.tokenId
draft.tokenOutInput.value = ''
draft.tokenOutInput.error = undefined
draft.tokenOutInput.error = null

draft.tokenInInput.isTouched = state.tokenOutInput.isTouched
draft.tokenInInput.tokenId = state.tokenOutInput.tokenId
draft.tokenInInput.value = ''
draft.tokenInInput.error = undefined
draft.tokenInInput.error = null

break
case SwapAction.DexSelectorTouched:
Expand All @@ -218,8 +237,8 @@ const swapReducer = (state: SwapState, action: SwapAction) => {
draft.tokenInInput.value = ''
draft.tokenOutInput.value = ''

draft.tokenInInput.error = undefined
draft.tokenOutInput.error = undefined
draft.tokenInInput.error = null
draft.tokenOutInput.error = null

break
case SwapAction.ResetForm:
Expand Down Expand Up @@ -284,8 +303,8 @@ type SwapActionValueMap = {
TokenOutIdChanged: Portfolio.Token.Id
TokenInAmountChanged: string
TokenOutAmountChanged: string
TokenInErrorChanged: string
TokenOutErrorChanged: string
TokenInErrorChanged: string | null
TokenOutErrorChanged: string | null
WantedPriceInputChanged: string
SlippageInputChanged: number
SwitchTouched: undefined
Expand Down Expand Up @@ -313,14 +332,14 @@ const defaultState: SwapState = Object.freeze({
isTouched: true,
tokenId: primaryTokenId,
disabled: false,
error: undefined,
error: null,
value: '',
},
tokenOutInput: {
isTouched: false,
tokenId: undefined,
disabled: false,
error: undefined,
error: null,
value: '',
},
slippageInput: {
Expand All @@ -343,19 +362,19 @@ const defaultState: SwapState = Object.freeze({
type SwapState = {
reqres: 'request' | 'response'
orderType: 'market' | 'limit'
lastInputTouched: 'in' | 'out' | 'limit'
lastInputTouched: 'in' | 'out'
tokenInInput: {
isTouched: boolean
tokenId?: Portfolio.Token.Id
disabled: boolean
error: string | undefined
error: string | null
value: string
}
tokenOutInput: {
isTouched: boolean
tokenId?: Portfolio.Token.Id
disabled: boolean
error: string | undefined
error: string | null
value: string
}
slippageInput: {
Expand Down
32 changes: 16 additions & 16 deletions apps/wallet-mobile/translations/messages/src/AppNavigator.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,59 @@
"defaultMessage": "!!!Enter PIN",
"file": "src/AppNavigator.tsx",
"start": {
"line": 257,
"line": 254,
"column": 17,
"index": 10080
"index": 9784
},
"end": {
"line": 260,
"line": 257,
"column": 3,
"index": 10170
"index": 9874
}
},
{
"id": "components.initialization.custompinscreen.title",
"defaultMessage": "!!!Set PIN",
"file": "src/AppNavigator.tsx",
"start": {
"line": 261,
"line": 258,
"column": 18,
"index": 10190
"index": 9894
},
"end": {
"line": 264,
"line": 261,
"column": 3,
"index": 10288
"index": 9992
}
},
{
"id": "global.actions.dialogs.walletKeysInvalidated.title",
"defaultMessage": "!!!Auth with OS changes",
"file": "src/AppNavigator.tsx",
"start": {
"line": 265,
"line": 262,
"column": 25,
"index": 10315
"index": 10019
},
"end": {
"line": 268,
"line": 265,
"column": 3,
"index": 10429
"index": 10133
}
},
{
"id": "global.actions.dialogs.biometricsChange.message",
"defaultMessage": "!!!Auth with OS changed detected",
"file": "src/AppNavigator.tsx",
"start": {
"line": 269,
"line": 266,
"column": 27,
"index": 10458
"index": 10162
},
"end": {
"line": 272,
"line": 269,
"column": 3,
"index": 10579
"index": 10283
}
}
]
Loading

0 comments on commit 2d639fb

Please sign in to comment.