Skip to content

Commit

Permalink
Merge branch 'master' into implement-offer-details-page
Browse files Browse the repository at this point in the history
  • Loading branch information
torztomasz committed Mar 27, 2023
2 parents 81e5534 + 4c88a37 commit 6a639d9
Show file tree
Hide file tree
Showing 83 changed files with 2,403 additions and 626 deletions.
52 changes: 46 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- master

jobs:
lint_build_test:
lint:
runs-on: ubuntu-latest
services:
postgres:
Expand All @@ -23,15 +23,55 @@ jobs:
ports:
- 5432:5432
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16.x
- run: yarn --frozen-lockfile
- run: yarn format
- run: yarn build
- run: yarn build:dependencies
- run: yarn lint
- run: yarn typecheck
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16.x
cache: "yarn"
- run: yarn --frozen-lockfile
- run: yarn format
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16.x
cache: "yarn"
- run: yarn --frozen-lockfile
- run: yarn build:dependencies
- run: yarn test
env:
TEST_DB_URL: postgresql://postgres:postgres@localhost:5432/postgres
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16.x
cache: "yarn"
- run: yarn --frozen-lockfile
- run: yarn build
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"typecheck": "wsrun -em typecheck",
"test": "wsrun -ecsm test",
"build": "wsrun -etm build",
"build:dependencies": "wsrun -p @explorer/{crypto,encoding,shared,state,types,testnet,frontend} -etm build",
"clean": "wsrun -ecsm clean",
"start": "cd packages/backend && yarn start"
},
Expand Down
11 changes: 11 additions & 0 deletions packages/backend/src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { createOldFrontendRouter } from './api/routers/OldFrontendRouter'
import { createStatusRouter } from './api/routers/StatusRouter'
import { Config } from './config'
import { AccountService } from './core/AccountService'
import { AssetDetailsService } from './core/AssetDetailsService'
import { AssetRegistrationCollector } from './core/collectors/AssetRegistrationCollector'
import { DepositWithTokenIdCollector } from './core/collectors/DepositWithTokenIdCollector'
import { PageCollector } from './core/collectors/PageCollector'
Expand Down Expand Up @@ -340,6 +341,10 @@ export class Application {
sentTransactionRepository
)
const userService = new UserService(userRegistrationEventRepository)
const assetDetailsService = new AssetDetailsService(
assetRepository,
config.starkex.tradingMode
)

const userTransactionMigrator = new UserTransactionMigrator(
database,
Expand Down Expand Up @@ -446,6 +451,7 @@ export class Application {
)
const homeController = new HomeController(
userService,
assetDetailsService,
assetRepository,
userTransactionRepository,
preprocessedStateDetailsRepository,
Expand All @@ -454,16 +460,19 @@ export class Application {
)
const userController = new UserController(
userService,
assetDetailsService,
preprocessedAssetHistoryRepository,
sentTransactionRepository,
userTransactionRepository,
userRegistrationEventRepository,
assetRepository,
config.starkex.tradingMode,
config.starkex.contracts.perpetual,
collateralAsset
)
const stateUpdateController = new StateUpdateController(
userService,
assetDetailsService,
stateUpdateRepository,
assetRepository,
userTransactionRepository,
Expand All @@ -474,8 +483,10 @@ export class Application {
const transactionController = new TransactionController(
userService,
sentTransactionRepository,
forcedTradeOfferRepository,
userTransactionRepository,
userRegistrationEventRepository,
assetRepository,
collateralAsset
)
const merkleProofController = new MerkleProofController(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class ForcedTransactionController {
}

const [offer, finalize] = await Promise.all([
this.offersRepository.findByHash(transactionHash),
this.offersRepository.findByTransactionHash(transactionHash),
this.findFinalizeTransaction(transaction),
])
const offerHistory = offer ? toForcedTradeOfferHistory(offer) : []
Expand Down
23 changes: 8 additions & 15 deletions packages/backend/src/api/controllers/HomeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
import { TradingMode, UserDetails } from '@explorer/shared'

import { CollateralAsset } from '../../config/starkex/StarkexConfig'
import { AssetDetailsService } from '../../core/AssetDetailsService'
import { UserService } from '../../core/UserService'
import { PaginationOptions } from '../../model/PaginationOptions'
import { AssetRepository } from '../../peripherals/database/AssetRepository'
import { PreprocessedStateDetailsRepository } from '../../peripherals/database/PreprocessedStateDetailsRepository'
import { UserTransactionData } from '../../peripherals/database/transactions/UserTransaction'
import { UserTransactionRepository } from '../../peripherals/database/transactions/UserTransactionRepository'
import { ControllerResult } from './ControllerResult'
import { getAssetHashToAssetDetailsMap } from './getAssetDetailsMap'
import { userTransactionToEntry } from './userTransactionToEntry'

const FORCED_TRANSACTION_TYPES: UserTransactionData['type'][] = [
Expand All @@ -25,6 +25,7 @@ const FORCED_TRANSACTION_TYPES: UserTransactionData['type'][] = [
export class HomeController {
constructor(
private readonly userService: UserService,
private readonly assetDetailsService: AssetDetailsService,
private readonly assetRepository: AssetRepository,
private readonly userTransactionRepository: UserTransactionRepository,
private readonly preprocessedStateDetailsRepository: PreprocessedStateDetailsRepository,
Expand Down Expand Up @@ -57,13 +58,9 @@ export class HomeController {
this.userTransactionRepository.countAll(FORCED_TRANSACTION_TYPES),
])

const assetDetailsMap = await getAssetHashToAssetDetailsMap(
this.tradingMode,
this.assetRepository,
{
userTransactions: forcedUserTransactions,
}
)
const assetDetailsMap = await this.assetDetailsService.getAssetDetailsMap({
userTransactions: forcedUserTransactions,
})

const transactions = forcedUserTransactions.map((t) =>
userTransactionToEntry(t, this.collateralAsset, assetDetailsMap)
Expand Down Expand Up @@ -130,13 +127,9 @@ export class HomeController {
this.userTransactionRepository.countAll(FORCED_TRANSACTION_TYPES),
])

const assetDetailsMap = await getAssetHashToAssetDetailsMap(
this.tradingMode,
this.assetRepository,
{
userTransactions: forcedUserTransactions,
}
)
const assetDetailsMap = await this.assetDetailsService.getAssetDetailsMap({
userTransactions: forcedUserTransactions,
})

const transactions = forcedUserTransactions.map((t) =>
userTransactionToEntry(t, this.collateralAsset, assetDetailsMap)
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/api/controllers/PositionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ function toTransactionHistory(
for (const sentTransaction of sentTransactions) {
if (
sentTransaction.data.type === 'Withdraw' ||
sentTransaction.data.type === 'WithdrawWithTokenId' ||
userTransactions.some(
(x) => x.transactionHash === sentTransaction.transactionHash
)
Expand Down
46 changes: 19 additions & 27 deletions packages/backend/src/api/controllers/StateUpdateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import {
renderStateUpdatePage,
renderStateUpdateTransactionsPage,
} from '@explorer/frontend'
import { AssetDetails, TradingMode, UserDetails } from '@explorer/shared'
import { TradingMode, UserDetails } from '@explorer/shared'
import { AssetHash, AssetId } from '@explorer/types'

import { CollateralAsset } from '../../config/starkex/StarkexConfig'
import { AssetDetailsMap } from '../../core/AssetDetailsMap'
import { AssetDetailsService } from '../../core/AssetDetailsService'
import { UserService } from '../../core/UserService'
import { PaginationOptions } from '../../model/PaginationOptions'
import { AssetRepository } from '../../peripherals/database/AssetRepository'
Expand All @@ -18,7 +20,6 @@ import { StateUpdateRepository } from '../../peripherals/database/StateUpdateRep
import { UserTransactionData } from '../../peripherals/database/transactions/UserTransaction'
import { UserTransactionRepository } from '../../peripherals/database/transactions/UserTransactionRepository'
import { ControllerResult } from './ControllerResult'
import { getAssetHashToAssetDetailsMap } from './getAssetDetailsMap'
import { userTransactionToEntry } from './userTransactionToEntry'
import { getAssetPriceUSDCents } from './utils/toPositionAssetEntries'

Expand All @@ -31,6 +32,7 @@ const FORCED_TRANSACTION_TYPES: UserTransactionData['type'][] = [
export class StateUpdateController {
constructor(
private readonly userService: UserService,
private readonly assetDetailsService: AssetDetailsService,
private readonly stateUpdateRepository: StateUpdateRepository,
private readonly assetRepository: AssetRepository,
private readonly userTransactionRepository: UserTransactionRepository,
Expand Down Expand Up @@ -86,14 +88,10 @@ export class StateUpdateController {
change: 0n,
}))

const assetDetailsMap = await getAssetHashToAssetDetailsMap(
this.tradingMode,
this.assetRepository,
{
assetHistory: balanceChanges,
userTransactions: forcedUserTransactions,
}
)
const assetDetailsMap = await this.assetDetailsService.getAssetDetailsMap({
assetHistory: balanceChanges,
userTransactions: forcedUserTransactions,
})

const transactions = forcedUserTransactions.map((t) =>
userTransactionToEntry(t, this.collateralAsset, assetDetailsMap)
Expand Down Expand Up @@ -142,13 +140,9 @@ export class StateUpdateController {
),
])

const assetDetailsMap = await getAssetHashToAssetDetailsMap(
this.tradingMode,
this.assetRepository,
{
assetHistory: balanceChanges,
}
)
const assetDetailsMap = await this.assetDetailsService.getAssetDetailsMap({
assetHistory: balanceChanges,
})

const balanceChangeEntries = toBalanceChangeEntries(
balanceChanges,
Expand Down Expand Up @@ -187,13 +181,9 @@ export class StateUpdateController {
]
)

const assetDetailsMap = await getAssetHashToAssetDetailsMap(
this.tradingMode,
this.assetRepository,
{
userTransactions: includedTransactions,
}
)
const assetDetailsMap = await this.assetDetailsService.getAssetDetailsMap({
userTransactions: includedTransactions,
})

const transactions = includedTransactions.map((t) =>
userTransactionToEntry(t, this.collateralAsset, assetDetailsMap)
Expand All @@ -212,14 +202,16 @@ export class StateUpdateController {
}

function toBalanceChangeEntries(
balanceChanges: PreprocessedAssetHistoryRecord<AssetHash | AssetId>[],
assetDetailsMap?: Record<string, AssetDetails>
balanceChanges: PreprocessedAssetHistoryRecord[],
assetDetailsMap?: AssetDetailsMap
) {
return balanceChanges.map((r) => ({
starkKey: r.starkKey,
asset: {
hashOrId: r.assetHashOrId,
assetDetails: assetDetailsMap?.[r.assetHashOrId.toString()],
assetDetails: AssetHash.check(r.assetHashOrId)
? assetDetailsMap?.getByAssetHash(r.assetHashOrId)
: undefined,
},
balance: r.balance,
change: r.balance - r.prevBalance,
Expand Down
Loading

0 comments on commit 6a639d9

Please sign in to comment.