Skip to content

Commit

Permalink
Don't render escape page if process already started
Browse files Browse the repository at this point in the history
  • Loading branch information
adamiak committed Oct 30, 2024
1 parent d2531c5 commit aa89389
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
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

0 comments on commit aa89389

Please sign in to comment.