Skip to content

Commit

Permalink
refactor TransactionHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
torztomasz committed Mar 27, 2023
1 parent 6a639d9 commit 79415b1
Show file tree
Hide file tree
Showing 8 changed files with 766 additions and 433 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { renderOfferAndForcedTradePage } from '@explorer/frontend'
import { EthereumAddress, Timestamp } from '@explorer/types'

import { CollateralAsset } from '../../config/starkex/StarkexConfig'
import { TransactionHistory } from '../../core/TransactionHistory'
import {
Accepted,
ForcedTradeOfferRecord,
Expand Down Expand Up @@ -41,6 +42,7 @@ export class ForcedTradeOfferController {
}

const offer = await this.offerRepository.findById(id)

if (!offer) {
return { type: 'not found', content: 'Offer not found.' }
}
Expand Down Expand Up @@ -93,6 +95,9 @@ export class ForcedTradeOfferController {
positionId: userPositionId,
}
: undefined
const transactionHistory = new TransactionHistory({
forcedTradeOffer: offer,
})

const content = renderOfferAndForcedTradePage({
user: {
Expand All @@ -108,11 +113,8 @@ export class ForcedTradeOfferController {
collateralAmount: offer.collateralAmount,
syntheticAsset: { hashOrId: offer.syntheticAssetId },
syntheticAmount: offer.syntheticAmount,
//TODO: error will be resolved after https://github.com/l2beat/starkex-explorer/pull/340 merged
expirationTimestamp: offer.accepted
? Timestamp.fromHours(offer.accepted.submissionExpirationTime)
: Timestamp(0),
history: [{ timestamp: offer.createdAt, status: 'CREATED' }],
expirationTimestamp: offer.accepted?.submissionExpirationTime,
history: transactionHistory.getForcedTradeTransactionHistory(),
acceptForm: user && getAcceptForm(offer, user),
cancelForm: user && getCancelForm(offer, user),
finalizeForm: user && getFinalizeForm(offer, user, this.perpetualAddress),
Expand Down
48 changes: 23 additions & 25 deletions packages/backend/src/api/controllers/TransactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { assertUnreachable, UserDetails } from '@explorer/shared'
import { Hash256 } from '@explorer/types'

import { CollateralAsset } from '../../config/starkex/StarkexConfig'
import { TransactionHistory } from '../../core/TransactionHistory'
import { UserService } from '../../core/UserService'
import { AssetRepository } from '../../peripherals/database/AssetRepository'
import {
Expand All @@ -24,11 +25,6 @@ import {
} from '../../peripherals/database/transactions/UserTransactionRepository'
import { UserRegistrationEventRepository } from '../../peripherals/database/UserRegistrationEventRepository'
import { ControllerResult } from './ControllerResult'
import {
buildForcedTradeTransactionHistory,
buildForcedTransactionHistory,
buildRegularTransactionHistory,
} from './utils/buildTransactionHistory'

export class TransactionController {
constructor(
Expand Down Expand Up @@ -93,10 +89,11 @@ export class TransactionController {
await this.userRegistrationEventRepository.findByStarkKey(
userTransaction.data.starkKey
)
const history = buildForcedTransactionHistory({
sentTransaction,
userTransaction,
const transactionHistory = new TransactionHistory({
sentTransaction: sentTransaction,
userTransaction: userTransaction,
})

return renderPerpetualForcedWithdrawalPage({
user,
transactionHash: userTransaction.transactionHash,
Expand All @@ -107,7 +104,7 @@ export class TransactionController {
asset: { hashOrId: this.collateralAsset.assetId },
amount: userTransaction.data.quantizedAmount,
positionId: userTransaction.data.positionId.toString(),
history,
history: transactionHistory.getForcedTransactionHistory(),
stateUpdateId: userTransaction.included?.stateUpdateId,
})
}
Expand All @@ -116,7 +113,7 @@ export class TransactionController {
await this.userRegistrationEventRepository.findByStarkKey(
userTransaction.data.starkKey
)
const history = buildForcedTransactionHistory({
const transactionHistory = new TransactionHistory({
sentTransaction,
userTransaction,
})
Expand All @@ -128,7 +125,7 @@ export class TransactionController {
ethereumAddress: txUser?.ethAddress,
},
vaultId: userTransaction.data.vaultId.toString(),
history,
history: transactionHistory.getForcedTransactionHistory(),
stateUpdateId: userTransaction.included?.stateUpdateId,
})
}
Expand Down Expand Up @@ -157,7 +154,7 @@ export class TransactionController {
}
: undefined

const history = buildForcedTradeTransactionHistory({
const transactionHistory = new TransactionHistory({
forcedTradeOffer,
sentTransaction,
userTransaction,
Expand All @@ -177,14 +174,14 @@ export class TransactionController {
expirationTimestamp: forcedTradeOffer?.accepted
? forcedTradeOffer.accepted.submissionExpirationTime
: undefined,
history,
history: transactionHistory.getForcedTradeTransactionHistory(),
stateUpdateId: userTransaction.included?.stateUpdateId,
})
}
case 'Withdraw':
case 'WithdrawWithTokenId':
case 'MintWithdraw': {
const history = buildRegularTransactionHistory({
const transactionHistory = new TransactionHistory({
sentTransaction,
userTransaction,
})
Expand Down Expand Up @@ -217,7 +214,7 @@ export class TransactionController {
},
asset: { hashOrId: assetHash },
amount: userTransaction.data.quantizedAmount,
history,
history: transactionHistory.getRegularTransactionHistory(),
stateUpdateId: userTransaction.included?.stateUpdateId,
})
}
Expand Down Expand Up @@ -256,7 +253,7 @@ export class TransactionController {
positionId: sentTransaction.data.positionIdB.toString(),
}
: undefined
const history = buildForcedTradeTransactionHistory({
const transactionHistory = new TransactionHistory({
forcedTradeOffer,
sentTransaction,
})
Expand All @@ -272,7 +269,7 @@ export class TransactionController {
syntheticAsset: { hashOrId: sentTransaction.data.syntheticAssetId },
syntheticAmount: sentTransaction.data.syntheticAmount,
expirationTimestamp: sentTransaction.data.submissionExpirationTime,
history,
history: transactionHistory.getForcedTradeTransactionHistory(),
})
}
case 'ForcedWithdrawal': {
Expand All @@ -285,7 +282,7 @@ export class TransactionController {
await this.userRegistrationEventRepository.findByStarkKey(
sentTransaction.data.starkKey
)
const history = buildForcedTransactionHistory({ sentTransaction })
const transactionHistory = new TransactionHistory({ sentTransaction })

return renderPerpetualForcedWithdrawalPage({
user,
Expand All @@ -297,12 +294,12 @@ export class TransactionController {
asset: { hashOrId: this.collateralAsset.assetId },
amount: sentTransaction.data.quantizedAmount,
positionId: sentTransaction.data.positionId.toString(),
history,
history: transactionHistory.getForcedTransactionHistory(),
})
}

case 'Withdraw': {
const history = buildRegularTransactionHistory({
const transactionHistory = new TransactionHistory({
sentTransaction,
})
const data = sentTransaction.data
Expand All @@ -323,14 +320,11 @@ export class TransactionController {
ethereumAddress: recipient?.ethAddress,
},
asset: { hashOrId: assetHash },
history,
history: transactionHistory.getRegularTransactionHistory(),
})
}

case 'WithdrawWithTokenId': {
const history = buildRegularTransactionHistory({
sentTransaction,
})
const asset =
await this.assetRepository.findDetailsByAssetTypeAndTokenId(
sentTransaction.data.assetType,
Expand All @@ -346,6 +340,10 @@ export class TransactionController {
sentTransaction.data.starkKey
)

const transactionHistory = new TransactionHistory({
sentTransaction,
})

return renderRegularWithdrawalPage({
user,
transactionHash: sentTransaction.transactionHash,
Expand All @@ -354,7 +352,7 @@ export class TransactionController {
ethereumAddress: recipient?.ethAddress,
},
asset: { hashOrId: asset.assetHash },
history,
history: transactionHistory.getRegularTransactionHistory(),
})
}

Expand Down
Loading

0 comments on commit 79415b1

Please sign in to comment.