Skip to content

Commit

Permalink
when changing bottom mint refetch routes (#539)
Browse files Browse the repository at this point in the history
* when changing bottom mint refetch routes

* remove unneeded logic

* Make hooks run off the mint accs

* Fix trait_type crasher

* Fix trait_type crasher

---------

Co-authored-by: Chewing Glass <[email protected]>
  • Loading branch information
bryzettler and ChewingGlass authored Nov 10, 2023
1 parent 39cc5d0 commit c64bb30
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 115 deletions.
5 changes: 3 additions & 2 deletions src/features/collectables/HotspotCompressedListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ const HotspotListItem = ({
return undefined
}

return metadata.attributes.find((attr) => attr.trait_type === 'ecc_compact')
?.value
return metadata.attributes.find(
(attr) => attr?.trait_type === 'ecc_compact',
)?.value
}, [metadata])

const hasIotRewards = useMemo(
Expand Down
2 changes: 1 addition & 1 deletion src/features/collectables/HotspotDetailsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ const HotspotDetailsScreen = () => {
if (!collectable?.content?.metadata) return

const attribute = collectable?.content?.metadata.attributes?.find(
(a) => a.trait_type === 'ecc_compact',
(a) => a?.trait_type === 'ecc_compact',
)

if (!attribute?.value) return
Expand Down
254 changes: 142 additions & 112 deletions src/features/swaps/SwapScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ const SwapScreen = () => {
inputAmount,
)

const inputMintDecimals = useMint(inputMint)?.info?.decimals
const outputMintDecimals = useMint(outputMint)?.info?.decimals
const inputMintAcc = useMint(inputMint)?.info
const outputMintAcc = useMint(outputMint)?.info

const validInputMints = useMemo(() => {
if (isDevnet)
Expand Down Expand Up @@ -173,10 +173,10 @@ const SwapScreen = () => {
}, [])

const insufficientTokensToSwap = useMemo(() => {
if (inputAmount > 0 && typeof inputMintDecimals !== 'undefined') {
return inputMintBalance?.lt(toBN(inputAmount || 0, inputMintDecimals))
if (inputAmount > 0 && inputMintAcc) {
return inputMintBalance?.lt(toBN(inputAmount || 0, inputMintAcc.decimals))
}
}, [inputAmount, inputMintDecimals, inputMintBalance])
}, [inputAmount, inputMintBalance, inputMintAcc])

const showError = useMemo(() => {
if (hasRecipientError) return t('generic.notValidSolanaAddress')
Expand All @@ -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,14 @@ const SwapScreen = () => {
}

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

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

const tokenData = useMemo(() => {
Expand Down Expand Up @@ -448,92 +411,159 @@ const SwapScreen = () => {
const onTokenItemPressed = useCallback(
(youPay: boolean) => () => {
Keyboard.dismiss()
if (typeof inputMintDecimals !== undefined) {
setSelectorMode(youPay ? SelectorMode.youPay : SelectorMode.youReceive)
hntKeyboardRef.current?.show({
payer: currentAccount,
})
}
setSelectorMode(youPay ? SelectorMode.youPay : SelectorMode.youReceive)
hntKeyboardRef.current?.show({
payer: currentAccount,
})
},
[currentAccount, inputMintDecimals],
[currentAccount],
)

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 (outputMintAcc && inputMintAcc) {
const { address: input, decimals: inputDecimals } = inputMintAcc
const { address: output, decimals: outputDecimals } = outputMintAcc

if (!isDevnet && !output.equals(DC_MINT)) {
const route = await getRoute({
amount: balance.toNumber(),
inputMint: input.toBase58(),
outputMint: output.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)), outputDecimals),
)
}
if (input.equals(HNT_MINT) && output.equals(DC_MINT)) {
return setOutputAmount(
toNumber(
networkTokensToDc(toBN(balance, inputDecimals)) || new BN(0),
inputDecimals,
),
)
}
if (isDevnet) {
if (price && !input.equals(HNT_MINT)) {
return setOutputAmount(price)
}

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(),
slippageBps,
})
return setOutputAmount(0)
}

return isPay
? setOutputAmount(
toNumber(
new BN(Number(route?.outAmount || 0)),
outputMintDecimals,
),
)
: setInputAmount(
toNumber(
new BN(Number(route?.outAmount || 0)),
inputMintDecimals,
),
)
return setOutputAmount(0)
}
},
[
getRoute,
inputMintAcc,
isDevnet,
networkTokensToDc,
outputMintAcc,
price,
slippageBps,
],
)

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,
),
)
useEffect(() => {
// if changing outputMint ensure we get new routes
;(async () => {
if (
!isDevnet &&
inputMintAcc &&
outputMintAcc &&
typeof inputAmount !== 'undefined' &&
inputAmount > 0 &&
!outputMintAcc?.address.equals(DC_MINT)
) {
setRecipient('')
setRecipientOpen(false)
await getOutputAmount({
balance: toBN(inputAmount || 0, inputMintAcc.decimals),
})
}
})()
}, [
getOutputAmount,
inputAmount,
inputMintAcc,
outputMintAcc,
isDevnet,
setRecipient,
setRecipientOpen,
])

const getInputAmount = useCallback(
async ({ balance }: { balance: BN }) => {
if (outputMintAcc && inputMintAcc) {
const { address: input, decimals: inputDecimals } = inputMintAcc
const { address: output, decimals: outputDecimals } = outputMintAcc

if (!isDevnet && !output.equals(DC_MINT)) {
const route = await getRoute({
amount: balance.toNumber(),
inputMint: output.toBase58(),
outputMint: input.toBase58(),
slippageBps,
})

if (isDevnet) {
if (price && !inputMint.equals(HNT_MINT)) {
return isPay ? setOutputAmount(price) : setInputAmount(price)
return setInputAmount(
toNumber(new BN(Number(route?.outAmount || 0)), inputDecimals),
)
}
if (input.equals(HNT_MINT) && output.equals(DC_MINT)) {
return setInputAmount(
toNumber(
dcToNetworkTokens(toBN(balance, outputDecimals)) || new BN(0),
inputDecimals,
),
)
}
if (isDevnet) {
if (price && !input.equals(HNT_MINT)) {
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,
inputMint,
inputMintDecimals,
outputMint,
outputMintDecimals,
slippageBps,
getRoute,
inputMintAcc,
isDevnet,
outputMintAcc,
price,
slippageBps,
],
)

const onConfirmBalance = useCallback(
async ({ balance }: { balance: BN }) => {
if (inputMintAcc && outputMintAcc) {
const { decimals: inputDecimals } = inputMintAcc
const { decimals: outputDecimals } = outputMintAcc
const isPay = selectorMode === SelectorMode.youPay
const amount = toNumber(balance, isPay ? inputDecimals : outputDecimals)

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

Expand Down

0 comments on commit c64bb30

Please sign in to comment.