Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't render escape page if process already started #532

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/backend/src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,8 @@ export class Application {
stateUpdater,
stateUpdateRepository,
config.starkex.contracts.perpetual,
config.starkex.contracts.escapeVerifier
config.starkex.contracts.escapeVerifier,
userTransactionRepository
)

const transactionValidator = new TransactionValidator(ethereumClient)
Expand Down
27 changes: 25 additions & 2 deletions packages/backend/src/api/controllers/EscapeHatchController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ import {
} from '@explorer/shared'
import { MerkleProof, PositionLeaf } from '@explorer/state'
import { EthereumAddress } from '@explorer/types'
import isEmpty from 'lodash/isEmpty'

import { FreezeCheckService } from '../../core/FreezeCheckService'
import { PageContextService } from '../../core/PageContextService'
import { StateUpdater } from '../../core/StateUpdater'
import { StateUpdateRepository } from '../../peripherals/database/StateUpdateRepository'
import { UserTransactionRecord } from '../../peripherals/database/transactions/UserTransactionRepository'
import {
UserTransactionRecord,
UserTransactionRepository,
} from '../../peripherals/database/transactions/UserTransactionRepository'
import { calculatePositionValue } from '../../utils/calculatePositionValue'
import { ControllerResult } from './ControllerResult'
import { getPerpetualEscapables } from './getEscapableAssets'
import { serializeMerkleProofForEscape } from './serializeMerkleProofForEscape'

export class EscapeHatchController {
Expand All @@ -27,7 +32,8 @@ export class EscapeHatchController {
private readonly stateUpdater: StateUpdater,
private readonly stateUpdateRepository: StateUpdateRepository,
private readonly starkExAddress: EthereumAddress,
private readonly escapeVerifierAddress: EthereumAddress
private readonly escapeVerifierAddress: EthereumAddress,
private readonly userTransactionRepository: UserTransactionRepository
) {}

async getFreezeRequestActionPage(
Expand Down Expand Up @@ -159,13 +165,30 @@ export class EscapeHatchController {
const serializedState = encodeStateAsInt256Array(
latestStateUpdate.perpetualState
)

if (context.tradingMode === 'perpetual') {
const escapableMap = await getPerpetualEscapables(
this.userTransactionRepository,
merkleProof.starkKey,
context.collateralAsset
)
// If escape process has started on perpetuals, don't allow to start it again
if (!isEmpty(escapableMap)) {
return {
type: 'not found',
message: 'Escape process has already started',
}
}
}

const positionValues =
context.tradingMode === 'perpetual'
? calculatePositionValue(
merkleProof as MerkleProof<PositionLeaf>,
latestStateUpdate.perpetualState
)
: undefined

let content: string
switch (context.tradingMode) {
case 'perpetual':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { z } from 'zod'

import { assetToInfo } from '../../../utils/assets'
import { formatWithDecimals } from '../../../utils/formatting/formatAmount'
import { InfoIcon } from '../../assets/icons/InfoIcon'
import { AssetWithLogo } from '../../components/AssetWithLogo'
import { Button } from '../../components/Button'
import { Card } from '../../components/Card'
Expand All @@ -18,6 +19,7 @@ import { OrderedList } from '../../components/OrderedList'
import { ContentWrapper } from '../../components/page/ContentWrapper'
import { Page } from '../../components/page/Page'
import { TermsOfServiceAck } from '../../components/TermsOfServiceAck'
import { TooltipWrapper } from '../../components/Tooltip'
import { reactToHtml } from '../../reactToHtml'
import { PerformUserActionsPanel } from '../user/components/PerformUserActionsPanel'
import { ForcedActionCard } from './components/ForcedActionCard'
Expand Down Expand Up @@ -134,8 +136,11 @@ function EscapeHatchActionPage(props: Props) {
<ForcedActionCard>
<div className="flex gap-2">
<div className="flex flex-1 flex-col gap-2">
<span className="text-sm font-medium text-zinc-500">
<span className="flex items-center gap-1 text-sm font-medium text-zinc-500">
Estimated value
<TooltipWrapper content="Exact value of the withdrawal will be calculated by the StarkEx smart contracts">
<InfoIcon className="h-3.5 w-3.5" />
</TooltipWrapper>
</span>
<span className="break-all text-xl font-semibold">
{formatWithDecimals(props.positionValue, 2)}
Expand Down
Loading