Skip to content

Commit

Permalink
when changing bottom mint refetch routes
Browse files Browse the repository at this point in the history
  • Loading branch information
bryzettler committed Nov 9, 2023
1 parent 39cc5d0 commit 152e223
Showing 1 changed file with 119 additions and 88 deletions.
207 changes: 119 additions & 88 deletions src/features/swaps/SwapScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,17 @@ const SwapScreen = () => {
transactionError,
])

const refreshInputs = useCallback(async () => {
const refresh = useCallback(async () => {
setInputAmount(0)
setOutputAmount(0)
}, [])

const refresh = useCallback(async () => {
refreshInputs()
setInputMint(MOBILE_MINT)
setOutputMint(HNT_MINT)
setSolFee(SOL_TXN_FEE)
setRecipient('')
setRecipientOpen(false)
setSelectorMode(SelectorMode.youPay)
setNetworkError(undefined)
}, [refreshInputs])
}, [])

useAsync(async () => {
if (
Expand Down Expand Up @@ -263,38 +259,6 @@ const SwapScreen = () => {
}
}, [inputMint, outputMint, validOutputMints])

useEffect(() => {
// if changing outputMint ensure we get new routes
;(async () => {
if (
!isDevnet &&
typeof inputMintDecimals !== 'undefined' &&
typeof inputAmount !== 'undefined' &&
inputAmount > 0 &&
!outputMint.equals(DC_MINT)
) {
setRecipient('')
setRecipientOpen(false)
await getRoute({
amount: toBN(inputAmount || 0, inputMintDecimals).toNumber(),
inputMint: inputMint.toBase58(),
outputMint: outputMint.toBase58(),
slippageBps,
})
}
})()
}, [
isDevnet,
inputAmount,
outputMint,
inputMint,
slippageBps,
inputMintDecimals,
getRoute,
setRecipient,
setRecipientOpen,
])

const Header = useMemo(() => {
return (
<Box
Expand Down Expand Up @@ -402,15 +366,16 @@ const SwapScreen = () => {
}

if (selectorMode === SelectorMode.youReceive) {
refreshInputs()
setOutputMint(mint)
if (inputAmount > 0) {
}
}

if (selectorMode === SelectorMode.youPay && mint.equals(HNT_MINT)) {
setOutputMint(DC_MINT)
}
},
[refresh, refreshInputs, selectorMode],
[refresh, selectorMode, inputAmount],
)

const tokenData = useMemo(() => {
Expand Down Expand Up @@ -458,82 +423,148 @@ const SwapScreen = () => {
[currentAccount, inputMintDecimals],
)

const onConfirmBalance = useCallback(
const getOutputAmount = useCallback(
async ({ balance }: { balance: BN }) => {
if (typeof inputMintDecimals === 'undefined') return
if (typeof outputMintDecimals === 'undefined') return
const isPay = selectorMode === SelectorMode.youPay
const amount = toNumber(
balance,
isPay ? inputMintDecimals : outputMintDecimals,
)
if (!isDevnet && !outputMint.equals(DC_MINT)) {
const route = await getRoute({
amount: balance.toNumber(),
inputMint: inputMint.toBase58(),
outputMint: outputMint.toBase58(),
slippageBps,
})

// eslint-disable-next-line @typescript-eslint/no-unused-expressions
isPay ? setInputAmount(amount) : setOutputAmount(amount)
return setOutputAmount(
toNumber(new BN(Number(route?.outAmount || 0)), outputMintDecimals),
)
}
if (inputMint.equals(HNT_MINT) && outputMint.equals(DC_MINT)) {
return setOutputAmount(
toNumber(
networkTokensToDc(toBN(balance, inputMintDecimals)) || new BN(0),
inputMintDecimals,
),
)
}
if (isDevnet) {
if (price && !inputMint.equals(HNT_MINT)) {
return setOutputAmount(price)
}

return setOutputAmount(0)
}

return setOutputAmount(0)
},
[
getRoute,
inputMint,
inputMintDecimals,
isDevnet,
networkTokensToDc,
outputMint,
outputMintDecimals,
price,
slippageBps,
],
)

useEffect(() => {
// if changing outputMint ensure we get new routes
;(async () => {
if (
!isDevnet &&
typeof inputMintDecimals !== 'undefined' &&
typeof inputAmount !== 'undefined' &&
inputAmount > 0 &&
!outputMint.equals(DC_MINT)
) {
setRecipient('')
setRecipientOpen(false)
await getOutputAmount({
balance: toBN(inputAmount || 0, inputMintDecimals),
})
}
})()
}, [
getOutputAmount,
inputAmount,
inputMint,
inputMintDecimals,
isDevnet,
outputMint,
setRecipient,
setRecipientOpen,
])

const getInputAmount = useCallback(
async ({ balance }: { balance: BN }) => {
if (typeof inputMintDecimals === 'undefined') return
if (typeof outputMintDecimals === 'undefined') return
if (!isDevnet && !outputMint.equals(DC_MINT)) {
const route = await getRoute({
amount: balance.toNumber(),
inputMint: isPay ? inputMint.toBase58() : outputMint.toBase58(),
outputMint: isPay ? outputMint.toBase58() : inputMint.toBase58(),
inputMint: outputMint.toBase58(),
outputMint: inputMint.toBase58(),
slippageBps,
})

return isPay
? setOutputAmount(
toNumber(
new BN(Number(route?.outAmount || 0)),
outputMintDecimals,
),
)
: setInputAmount(
toNumber(
new BN(Number(route?.outAmount || 0)),
inputMintDecimals,
),
)
return setInputAmount(
toNumber(new BN(Number(route?.outAmount || 0)), inputMintDecimals),
)
}

if (inputMint.equals(HNT_MINT) && outputMint.equals(DC_MINT)) {
return isPay
? setOutputAmount(
toNumber(
networkTokensToDc(toBN(balance, inputMintDecimals)) ||
new BN(0),
inputMintDecimals,
),
)
: setInputAmount(
toNumber(
dcToNetworkTokens(toBN(balance, outputMintDecimals)) ||
new BN(0),
inputMintDecimals,
),
)
return setInputAmount(
toNumber(
dcToNetworkTokens(toBN(balance, outputMintDecimals)) || new BN(0),
inputMintDecimals,
),
)
}

if (isDevnet) {
if (price && !inputMint.equals(HNT_MINT)) {
return isPay ? setOutputAmount(price) : setInputAmount(price)
return setInputAmount(price)
}

return isPay ? setOutputAmount(0) : setInputAmount(0)
return setInputAmount(0)
}

return isPay ? setOutputAmount(0) : setInputAmount(0)
return setInputAmount(0)
},
[
selectorMode,
networkTokensToDc,
dcToNetworkTokens,
price,
getRoute,
inputMint,
inputMintDecimals,
isDevnet,
outputMint,
outputMintDecimals,
price,
slippageBps,
getRoute,
isDevnet,
],
)

const onConfirmBalance = useCallback(
async ({ balance }: { balance: BN }) => {
if (typeof inputMintDecimals === 'undefined') return
if (typeof outputMintDecimals === 'undefined') return
const isPay = selectorMode === SelectorMode.youPay
const amount = toNumber(
balance,
isPay ? inputMintDecimals : outputMintDecimals,
)

// eslint-disable-next-line @typescript-eslint/no-unused-expressions
isPay ? setInputAmount(amount) : setOutputAmount(amount)
await (isPay ? getOutputAmount({ balance }) : getInputAmount({ balance }))
},
[
selectorMode,
inputMintDecimals,
outputMintDecimals,
getInputAmount,
getOutputAmount,
],
)

Expand Down

0 comments on commit 152e223

Please sign in to comment.