-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix stack size exceeded error and add missing dep (for yarn install) (#…
…394)
- Loading branch information
Showing
8 changed files
with
106 additions
and
63 deletions.
There are no files selected for viewing
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
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
46 changes: 46 additions & 0 deletions
46
packages/widget-v2/src/pages/SwapPage/useUpdateAmountWhenRouteChanges.ts
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,46 @@ | ||
import { useEffect } from "react"; | ||
import { useAtom } from "jotai"; | ||
import { | ||
swapDirectionAtom, | ||
sourceAssetAtom, | ||
destinationAssetAtom, | ||
} from "@/state/swapPage"; | ||
import { convertTokenAmountToHumanReadableAmount } from "@/utils/crypto"; | ||
import { limitDecimalsDisplayed } from "@/utils/number"; | ||
import { skipRouteAtom } from "@/state/route"; | ||
|
||
export const useUpdateAmountWhenRouteChanges = () => { | ||
const [route] = useAtom(skipRouteAtom); | ||
const [direction] = useAtom(swapDirectionAtom); | ||
const [sourceAsset, setSourceAsset] = useAtom(sourceAssetAtom); | ||
const [destinationAsset, setDestinationAsset] = useAtom(destinationAssetAtom); | ||
|
||
useEffect(() => { | ||
if (!route.data || !sourceAsset || !destinationAsset) return; | ||
|
||
const swapInAmount = convertTokenAmountToHumanReadableAmount( | ||
route.data.amountOut, | ||
destinationAsset.decimals | ||
); | ||
const swapOutAmount = convertTokenAmountToHumanReadableAmount( | ||
route.data.amountIn, | ||
sourceAsset.decimals | ||
); | ||
|
||
const swapInAmountChanged = swapInAmount !== destinationAsset.amount; | ||
const swapOutAmountChanged = swapOutAmount !== sourceAsset.amount; | ||
|
||
if (direction === "swap-in" && swapInAmountChanged) { | ||
setDestinationAsset((old) => ({ | ||
...old, | ||
amount: limitDecimalsDisplayed(swapInAmount), | ||
})); | ||
} else if (direction === "swap-out" && swapOutAmountChanged) { | ||
setSourceAsset((old) => ({ | ||
...old, | ||
amount: limitDecimalsDisplayed(swapOutAmount), | ||
})); | ||
} | ||
}, [route.data, sourceAsset, destinationAsset, direction, setSourceAsset, setDestinationAsset]); | ||
|
||
}; |
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