Skip to content

Commit

Permalink
testnet: Integrate wallet locks and show error for full orderbook error
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Oct 20, 2023
1 parent 64a74e3 commit 9e02369
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React, { createContext, useContext, useState } from "react"
import React, { createContext, useContext, useEffect, useState } from "react"
import { useRenegade } from "@/contexts/Renegade/renegade-context"
import { Fade, Flex, Modal, ModalContent, ModalOverlay } from "@chakra-ui/react"

import { ErrorStep } from "@/components/steppers/order-stepper/steps/error-step"

import { ConfirmStep } from "./steps/confirm-step"
import { ExitStep } from "./steps/exit-step"
import { LoadingStep } from "./steps/loading-step"
Expand Down Expand Up @@ -41,6 +43,12 @@ const OrderStepperInner = () => {
>
{step === Step.EXIT && <ExitStep />}
</Fade>
<Fade
transition={{ enter: { duration: 0.25 } }}
in={step === Step.ERROR}
>
{step === Step.ERROR && <ErrorStep />}
</Fade>
</Flex>
</ModalContent>
</Modal>
Expand All @@ -59,13 +67,21 @@ export enum Step {
DEFAULT,
LOADING,
EXIT,
ERROR,
}

export enum ErrorType {
ORDERBOOK_FULL = "ORDERBOOK_FULL",
WALLET_LOCKED = "WALLET_LOCKED",
}

interface StepperContextType {
error?: ErrorType
midpoint: number
onBack: () => void
onClose: () => void
onNext: () => void
setError: (error: ErrorType) => void
setMidpoint: (midpoint: number) => void
setStep: (step: Step) => void
step: Step
Expand All @@ -76,6 +92,7 @@ const StepperContext = createContext<StepperContextType>({
onBack: () => {},
onClose: () => {},
onNext: () => {},
setError: () => {},
setMidpoint: () => {},
setStep: () => {},
step: Step.DEFAULT,
Expand All @@ -93,6 +110,7 @@ const StepperProvider = ({
const [step, setStep] = useState(Step.DEFAULT)
const [midpoint, setMidpoint] = useState(0)
const { setTask } = useRenegade()
const [error, setError] = useState<ErrorType>()

const handleNext = () => {
setStep(step + 1)
Expand All @@ -108,13 +126,19 @@ const StepperProvider = ({
onClose()
}

useEffect(() => {
if (error) setStep(Step.ERROR)
}, [error])

return (
<StepperContext.Provider
value={{
error,
midpoint,
onBack: handleBack,
onClose: handleClose,
onNext: handleNext,
setError,
setMidpoint,
setStep,
step,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import {
} from "@chakra-ui/react"
import { Exchange, PriceReport } from "@renegade-fi/renegade-js"

import { useStepper } from "../order-stepper"
import { ErrorType, useStepper } from "../order-stepper"

export function ConfirmStep() {
const { setMidpoint, onNext } = useStepper()
const { setMidpoint, onNext, setError } = useStepper()
const { getPriceData } = useExchange()
const { baseTicker, baseTokenAmount, direction, onPlaceOrder, quoteTicker } =
useOrder()
Expand Down Expand Up @@ -103,7 +103,21 @@ export function ConfirmStep() {
throw new Error("No current price report")
}
setMidpoint(currentPriceReport.midpointPrice || 0)
onPlaceOrder().then(() => onNext())
onPlaceOrder()
.then(() => onNext())
.catch((e) => {
if (
e.message ===
"RenegadeError: The maximum number of active, unmatched orders has been reached."
) {
setError(ErrorType.ORDERBOOK_FULL)
} else if (
e.message ===
"RenegadeError: The relayer returned a non-200 response. wallet update already in progress"
) {
setError(ErrorType.WALLET_LOCKED)
}
})
}}
>
<HStack spacing="4px">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Button, Flex, ModalBody, Text } from "@chakra-ui/react"

import { ErrorType, useStepper } from "../order-stepper"

export function ErrorStep() {
const { onClose, error } = useStepper()
const title = {
[ErrorType.ORDERBOOK_FULL]: "Orderbook full",
[ErrorType.WALLET_LOCKED]: "Wallet locked",
"": "",
}[error || ""]
const content = {
[ErrorType.ORDERBOOK_FULL]:
"You can only have 5 orders in the orderbook at a time. Please cancel an existing order to make room for this one.",
[ErrorType.WALLET_LOCKED]: "Please wait for the ongoing task to complete.",
"": "",
}[error || ""]

return (
<>
<ModalBody>
<Flex
alignItems="center"
justifyContent="center"
flexDirection="column"
gap="12"
textAlign="center"
>
<Text
color="white.50"
fontFamily="Favorit Extended"
fontSize="1.3em"
fontWeight="200"
>
{title}
</Text>
{/* TODO: Make this general error modal */}
<Text fontSize="0.9em">{content}</Text>
<Button
padding="20px"
color="white.80"
fontSize="1.2em"
fontWeight="200"
borderWidth="thin"
borderColor="white.40"
borderRadius="100px"
_hover={{
borderColor: "white.60",
color: "white",
}}
_focus={{
backgroundColor: "transparent",
}}
transition="0.15s"
backgroundColor="transparent"
onClick={onClose}
>
Close
</Button>
</Flex>
</ModalBody>
</>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ export function LoadingStep() {
const { onNext } = useStepper()
const { taskState, taskType } = useRenegade()
useEffect(() => {
if (
taskType === TaskType.PlaceOrder &&
taskState === TaskState.UpdatingValidityProofs
) {
if (taskType === TaskType.PlaceOrder && taskState === TaskState.Completed) {
onNext()
}
}, [onNext, taskState, taskType])
Expand Down
8 changes: 6 additions & 2 deletions testnet.renegade.fi/contexts/Order/order-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,13 @@ function OrderProvider({ children }: OrderProviderProps) {
type: "midpoint",
amount: BigInt(baseTokenAmount),
})
renegade.task
.placeOrder(accountId, order)
return renegade.task
.modifyOrPlaceOrder(accountId, order)
.then(([taskId]) => setTask(taskId, TaskType.PlaceOrder))
.catch((e) => {
console.log("🚀 ~ handlePlaceOrder ~ e:", e.message)
throw new Error(e)
})
}, [accountId, baseTicker, baseTokenAmount, direction, quoteTicker, setTask])

const handleSetDirection = useCallback((direction: Direction) => {
Expand Down
2 changes: 1 addition & 1 deletion testnet.renegade.fi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@react-hook/window-size": "3.0.7",
"@renegade-fi/renegade-js": "../../renegade-js",
"@renegade-fi/renegade-js": "^0.3.8",
"@t3-oss/env-nextjs": "^0.6.0",
"connectkit": "^1.4.0",
"dayjs": "^1.11.10",
Expand Down
8 changes: 5 additions & 3 deletions testnet.renegade.fi/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1478,9 +1478,9 @@
dependencies:
"@noble/hashes" "1.3.2"

"@noble/ed25519@https://github.com/renegade-fi/noble-ed25519#prehashed-context":
"@noble/ed25519@git+https://github.com/renegade-fi/noble-ed25519.git#prehashed-context":
version "1.7.1"
resolved "https://github.com/renegade-fi/noble-ed25519#dcd9e5541c527e23b07f09db09d3a2c32ff61b2c"
resolved "git+https://github.com/renegade-fi/noble-ed25519.git#dcd9e5541c527e23b07f09db09d3a2c32ff61b2c"

"@noble/[email protected]", "@noble/hashes@^1.3.0", "@noble/hashes@^1.3.1", "@noble/hashes@~1.3.0", "@noble/hashes@~1.3.2":
version "1.3.2"
Expand Down Expand Up @@ -1551,8 +1551,10 @@
"@react-hook/event" "^1.2.1"
"@react-hook/throttle" "^2.2.0"

"@renegade-fi/renegade-js@../../renegade-js":
"@renegade-fi/renegade-js@^0.3.8":
version "0.3.8"
resolved "https://registry.yarnpkg.com/@renegade-fi/renegade-js/-/renegade-js-0.3.8.tgz#1c0d88d5dfc7874b37c3e021ec394128be5ff0cb"
integrity sha512-RBJSj5cemkkwuBqup072BmBW26NY0OPBEj3m78JEa2K3hrDVqAVm9edVO+4U4BMDKc0BQoofL129h1n8UK2xJw==
dependencies:
"@noble/ed25519" "https://github.com/renegade-fi/noble-ed25519#prehashed-context"
"@noble/hashes" "^1.3.0"
Expand Down

0 comments on commit 9e02369

Please sign in to comment.