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

Improve price tables #380

Merged
merged 7 commits into from
Apr 13, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,19 @@
"no-only-tests/no-only-tests": "error",
"no-restricted-imports": [
"error",
{ "paths": [".", "..", "../..", "../../.."], "patterns": ["**/old/**"] }
{
"paths": [
".",
"..",
"../..",
"../../..",
{
"name": "lodash",
"message": "Import [module] from lodash/[module] instead"
}
],
"patterns": ["**/old/**"]
}
],
"simple-import-sort/imports": "error",
"simple-import-sort/exports": "error",
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/core/AssetDetailsService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertUnreachable, TradingMode } from '@explorer/shared'
import { AssetHash } from '@explorer/types'
import { uniqBy } from 'lodash'
import uniqBy from 'lodash/uniqBy'

import { AssetRepository } from '../peripherals/database/AssetRepository'
import { PreprocessedAssetHistoryRecord } from '../peripherals/database/PreprocessedAssetHistoryRepository'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@explorer/types'
import { expect, mockObject } from 'earl'
import { BigNumber, providers } from 'ethers'
import range from 'lodash/range'

import { BlockRange } from '../../model'
import {
Expand Down Expand Up @@ -482,7 +483,7 @@ describe(UserTransactionCollector.name, () => {
it('can process multiple events', async () => {
const ethereumClient = mockObject<EthereumClient>({
async getLogsInRange() {
return Array.from({ length: 3 }).map((_, i) => {
return range(3).map((i) => {
const log = LogForcedWithdrawalRequest.encodeLog([
BigNumber.from(i),
BigNumber.from(i),
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/core/collectors/VerifierCollector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EthereumAddress } from '@explorer/types'
import { AbiCoder } from 'ethers/lib/utils'
import { partition } from 'lodash'
import partition from 'lodash/partition'

import { BlockRange } from '../../model/BlockRange'
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/model/BlockRange.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Hash256 } from '@explorer/types'
import assert from 'assert'
import { partition } from 'lodash'
import partition from 'lodash/partition'

import { BlockNumber } from '../peripherals/ethereum/types'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Hash256 } from '@explorer/types'
import { expect } from 'earl'
import { range } from 'lodash'
import range from 'lodash/range'

import { setupDatabaseTestSuite } from '../../test/database'
import { Logger } from '../../tools/Logger'
Expand Down Expand Up @@ -54,7 +54,7 @@ describe(BlockRepository.name, () => {
})

it('deletes all records after a block number', async () => {
const records: BlockRecord[] = Array.from({ length: 10 }).map((_, i) => ({
const records: BlockRecord[] = range(10).map((_, i) => ({
hash: Hash256.fake(),
number: i,
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
NodeOrLeaf,
} from '@explorer/state'
import { json, PedersenHash } from '@explorer/types'
import { partition } from 'lodash'
import partition from 'lodash/partition'

import { Logger } from '../../tools/Logger'
import { BaseRepository } from './shared/BaseRepository'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Hash256 } from '@explorer/types'
import { expect } from 'earl'
import range from 'lodash/range'

import { setupDatabaseTestSuite } from '../../test/database'
import { Logger } from '../../tools/Logger'
Expand Down Expand Up @@ -65,7 +66,7 @@ describe(PageMappingRepository.name, () => {
})

it('deletes all records after a block number', async () => {
const records = Array.from({ length: 10 }).map((_, i) =>
const records = range(10).map((_, i) =>
dummyPageMappingRecord({ blockNumber: i })
)
await repository.addMany(records)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Hash256 } from '@explorer/types'
import { expect } from 'earl'
import range from 'lodash/range'

import { setupDatabaseTestSuite } from '../../test/database'
import { Logger } from '../../tools/Logger'
Expand Down Expand Up @@ -53,7 +54,7 @@ describe(PageRepository.name, () => {
})

it('deletes all records after a block number', async () => {
const records = Array.from({ length: 10 }).map((_, i) => dummyPage(i))
const records = range(10).map((i) => dummyPage(i))
await repository.addMany(records)

await repository.deleteAfter(5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Timestamp,
} from '@explorer/types'
import { expect } from 'earl'
import range from 'lodash/range'

import { setupDatabaseTestSuite } from '../../test/database'
import { fakeInt, fakeTimestamp, fakeWithdrawal } from '../../test/fakes'
Expand Down Expand Up @@ -347,7 +348,7 @@ describe(StateUpdateRepository.name, () => {
stateTransitionHash: Hash256.fake(),
timestamp: Timestamp(blockNumber),
},
positions: Array.from({ length: blockNumber - 20_000 }).map((_, i) => ({
positions: range(blockNumber - 20_000).map((_, i) => ({
starkKey: StarkKey.fake(),
positionId: BigInt(blockNumber * 10 + i),
collateralBalance: 0n,
Expand Down Expand Up @@ -449,14 +450,12 @@ describe(StateUpdateRepository.name, () => {
stateTransitionHash: Hash256.fake(),
timestamp: Timestamp(blockNumber),
},
positions: Array.from({ length: blockNumber - 20_000 }).map(
(_, i) => ({
starkKey: StarkKey.fake(),
positionId: BigInt(blockNumber * 10 + i),
collateralBalance: 0n,
balances: [],
})
),
positions: range(blockNumber - 20_000).map((i) => ({
starkKey: StarkKey.fake(),
positionId: BigInt(blockNumber * 10 + i),
collateralBalance: 0n,
balances: [],
})),
prices: [],
transactionHashes: [],
})
Expand Down Expand Up @@ -522,7 +521,7 @@ describe(StateUpdateRepository.name, () => {
stateTransitionHash,
timestamp,
},
positions: Array.from({ length: 4 }).map((_, i) => ({
positions: range(4).map((i) => ({
starkKey: StarkKey.fake(`${blockNumber}${i}`),
positionId: BigInt(blockNumber * 10 + i),
collateralBalance: collateralBalance,
Expand All @@ -539,7 +538,7 @@ describe(StateUpdateRepository.name, () => {
blockNumber,
rootHash,
timestamp,
positions: Array.from({ length: 4 }).map((_, i) =>
positions: range(4).map((i) =>
expect.subset({
starkKey: StarkKey.fake(`${blockNumber}${i}`),
positionId: BigInt(blockNumber * 10 + i),
Expand All @@ -561,7 +560,7 @@ describe(StateUpdateRepository.name, () => {
stateTransitionHash: Hash256.fake(),
timestamp: Timestamp(blockNumber),
},
positions: Array.from({ length: blockNumber - 40_000 }).map((_, i) => ({
positions: range(blockNumber - 40_000).map((_, i) => ({
starkKey: StarkKey.fake(),
positionId: BigInt(blockNumber * 10 + i),
collateralBalance: 0n,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EthereumAddress, Hash256 } from '@explorer/types'
import { expect, mockFn, mockObject } from 'earl'
import { providers } from 'ethers'
import range from 'lodash/range'

import { BlockRange } from '../../model'
import { EthereumClient } from './EthereumClient'
Expand Down Expand Up @@ -81,7 +82,7 @@ describe(EthereumClient.name, () => {

it('works for a lot of hashes', async () => {
const blockRange = new BlockRange(
Array.from({ length: 500 }).map((v, i) => ({
range(500).map((i) => ({
number: 1000 + i,
hash: Hash256.fake(`${1000 + i}`),
}))
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"elliptic": "^6.5.4",
"hash.js": "^1.1.7",
"js-cookie": "^3.0.1",
"lodash": "^4.17.21",
"mersenne-twister": "^1.1.0",
"react": "^18.1.0",
"react-dom": "^18.1.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/frontend/src/preview/data/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Timestamp } from '@explorer/types'
import range from 'lodash/range'

export function repeat<T>(n: number, create: () => T) {
return Array.from({ length: n }).map(create)
return range(n).map(create)
}

export function randomTimestamp(): Timestamp {
Expand Down
13 changes: 7 additions & 6 deletions packages/frontend/src/preview/oldData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
StarkKey,
Timestamp,
} from '@explorer/types'
import range from 'lodash/range'

import {
ForcedTradeOfferDetailsProps,
Expand All @@ -27,7 +28,7 @@ import { TransactionFormProps } from '../view/old/transaction-form'
const ONE_HOUR = 60 * 60 * 1000

const createFakeTransactions = (count: number): ForcedTransactionEntry[] =>
Array.from({ length: count }).map((_, i) => {
range(count).map((_, i) => {
const assetId = randomChoice([
AssetId('LINK-7'),
AssetId('MKR-9'),
Expand All @@ -39,7 +40,7 @@ const createFakeTransactions = (count: number): ForcedTransactionEntry[] =>
const decimals = AssetId.decimals(assetId)
const digits = Math.floor(Math.random() * decimals + 6)
const randomDigit = () => randomChoice('0123456789'.split(''))
const amount = Array.from({ length: digits }).map(randomDigit).join('')
const amount = range(digits).map(randomDigit).join('')

return {
type,
Expand All @@ -61,7 +62,7 @@ function randomChoice<T>(items: readonly T[]) {
}

const createFakeOffers = (count: number): ForcedTradeOfferEntry[] =>
Array.from({ length: count }).map((_, i) => {
range(count).map((_, i) => {
const collateralAmount = BigInt(
Math.floor(Math.random() * 1000000 * (i + 1))
)
Expand Down Expand Up @@ -89,7 +90,7 @@ const createFakeOffers = (count: number): ForcedTradeOfferEntry[] =>

export const HOME_PROPS: HomeProps = {
account: undefined,
stateUpdates: Array.from({ length: 6 }).map((_, i) => ({
stateUpdates: range(6).map((_, i) => ({
id: i,
hash: PedersenHash.fake(),
forcedTransactionsCount: Math.floor(Math.random() * 3),
Expand All @@ -111,7 +112,7 @@ export const STATE_CHANGE_DETAILS_PROPS: StateUpdateDetailsProps = {
rootHash: PedersenHash.fake(),
blockNumber: Math.floor(Math.random() * 100),
timestamp: Timestamp.now(),
positions: Array.from({ length: 57 }).map((_, i) => ({
positions: range(57).map((_, i) => ({
starkKey: StarkKey.fake(),
positionId: BigInt(i + 1),
totalUSDCents: BigInt(Math.floor(Math.random() * 500_000_00)),
Expand Down Expand Up @@ -247,7 +248,7 @@ export const POSITION_AT_UPDATE_PROPS: PositionAtUpdateProps = {

export const STATE_CHANGES_INDEX_PROPS: StateUpdatesIndexProps = {
account: undefined,
stateUpdates: Array.from({ length: 10 }).map((_, i) => ({
stateUpdates: range(10).map((_, i) => ({
id: i,
hash: PedersenHash.fake(),
forcedTransactionsCount: Math.floor(Math.random() * 3),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import classNames from 'classnames'
import range from 'lodash/range'
import React from 'react'

import { Asset, assetToInfo } from '../../../../utils/assets'
Expand All @@ -16,20 +18,30 @@ export interface StateUpdatePriceEntry {
}

export function StateUpdatePricesTable(props: StateUpdatePricesTableProps) {
const tableCount = props.priceChanges.length == 1 ? 1 : 2

return (
<Table
columns={[{ header: 'Asset' }, { header: 'Price', numeric: true }]}
rows={props.priceChanges.map((transaction) => {
return {
cells: [
<AssetWithLogo
type="small"
assetInfo={assetToInfo(transaction.asset)}
/>,
formatWithDecimals(transaction.price, 2, { prefix: '$' }),
],
}
<div className={classNames('flex', tableCount === 2 && 'gap-12')}>
{range(tableCount).map((index) => {
return (
<Table
columns={[{ header: 'Asset' }, { header: 'Price', numeric: true }]}
rows={props.priceChanges
.filter((_, txIndex) => txIndex % tableCount === index)
.map((transaction) => {
return {
cells: [
<AssetWithLogo
type="small"
assetInfo={assetToInfo(transaction.asset)}
/>,
formatWithDecimals(transaction.price, 2, { prefix: '$' }),
],
}
})}
/>
)
})}
/>
</div>
)
}
2 changes: 1 addition & 1 deletion packages/state/src/MerkleNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pedersen } from '@explorer/crypto'
import { PedersenHash } from '@explorer/types'
import { partition } from 'lodash'
import partition from 'lodash/partition'

import { MerkleValue } from './MerkleValue'

Expand Down
2 changes: 1 addition & 1 deletion packages/state/src/calculateUpdatedPositions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OnChainData } from '@explorer/encoding'
import { AssetId, Timestamp } from '@explorer/types'
import { zip } from 'lodash'
import zip from 'lodash/zip'

import { MerkleTree } from './MerkleTree'
import { PositionLeaf } from './PositionLeaf'
Expand Down
3 changes: 2 additions & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
},
"dependencies": {
"@ethersproject/address": "^5.6.1",
"@ethersproject/bignumber": "^5.6.2"
"@ethersproject/bignumber": "^5.6.2",
"lodash": "^4.17.21"
},
"devDependencies": {}
}
4 changes: 3 additions & 1 deletion packages/types/src/fake.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import range from 'lodash/range'

const HEX_DIGITS = '0123456789abcdef'

export function fakeHexDigit() {
return HEX_DIGITS[Math.floor(Math.random() * HEX_DIGITS.length)]
}

export function fakeHexString(length: number) {
return Array.from({ length }).map(fakeHexDigit).join('')
return range(length).map(fakeHexDigit).join('')
}