Skip to content

Commit

Permalink
move stuff around
Browse files Browse the repository at this point in the history
  • Loading branch information
tyleroooo committed Dec 13, 2024
1 parent 2c7563a commit dc2500a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/abacus-ts/calculators/blockRewards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IndexerHistoricalBlockTradingReward } from '@/types/indexer/indexerApiGen';
import { keyBy, maxBy } from 'lodash';

import { MustBigNumber } from '@/lib/numbers';

import { Loadable } from '../lib/loadable';
import { mapLoadableData } from '../lib/mapLoadable';
import { mergeObjects } from '../lib/mergeObjects';

function calculateBlockRewards(

Check failure on line 10 in src/abacus-ts/calculators/blockRewards.ts

View workflow job for this annotation

GitHub Actions / lint

'calculateBlockRewards' is defined but never used
liveTransfers: Loadable<IndexerHistoricalBlockTradingReward[]>,
restTransfers: Loadable<IndexerHistoricalBlockTradingReward[]>
) {
const getRewardsById = (data: Loadable<IndexerHistoricalBlockTradingReward[]>) =>
mapLoadableData(data, (d) => keyBy(d, (reward) => reward.createdAtHeight));
return mergeObjects(
getRewardsById(liveTransfers).data ?? {},
getRewardsById(restTransfers).data ?? {},
(first, second) => maxBy([first, second], (f) => MustBigNumber(f.createdAtHeight).toNumber())!
);
}
21 changes: 21 additions & 0 deletions src/abacus-ts/calculators/fills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IndexerCompositeFillObject } from '@/types/indexer/indexerManual';
import { keyBy, maxBy } from 'lodash';

import { MustBigNumber } from '@/lib/numbers';

import { Loadable } from '../lib/loadable';
import { mapLoadableData } from '../lib/mapLoadable';
import { mergeObjects } from '../lib/mergeObjects';

function calculateFills(

Check failure on line 10 in src/abacus-ts/calculators/fills.ts

View workflow job for this annotation

GitHub Actions / lint

'calculateFills' is defined but never used
liveFills: Loadable<IndexerCompositeFillObject[]>,
restFills: Loadable<IndexerCompositeFillObject[]>
) {
const getFillsById = (data: Loadable<IndexerCompositeFillObject[]>) =>
mapLoadableData(data, (d) => keyBy(d, (fill) => fill.id ?? ''));
return mergeObjects(
getFillsById(liveFills).data ?? {},
getFillsById(restFills).data ?? {},
(first, second) => maxBy([first, second], (f) => MustBigNumber(f.createdAtHeight).toNumber())!
);
}
21 changes: 21 additions & 0 deletions src/abacus-ts/calculators/transfers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IndexerTransferResponseObject } from '@/types/indexer/indexerApiGen';
import { keyBy, maxBy } from 'lodash';

import { MustBigNumber } from '@/lib/numbers';

import { Loadable } from '../lib/loadable';
import { mapLoadableData } from '../lib/mapLoadable';
import { mergeObjects } from '../lib/mergeObjects';

function calculateTransfers(

Check failure on line 10 in src/abacus-ts/calculators/transfers.ts

View workflow job for this annotation

GitHub Actions / lint

'calculateTransfers' is defined but never used
liveTransfers: Loadable<IndexerTransferResponseObject[]>,
restTransfers: Loadable<IndexerTransferResponseObject[]>
) {
const getTransfersById = (data: Loadable<IndexerTransferResponseObject[]>) =>
mapLoadableData(data, (d) => keyBy(d, (transfer) => transfer.id));
return mergeObjects(
getTransfersById(liveTransfers).data ?? {},
getTransfersById(restTransfers).data ?? {},
(first, second) => maxBy([first, second], (f) => MustBigNumber(f.createdAtHeight).toNumber())!
);
}
8 changes: 8 additions & 0 deletions src/abacus-ts/lib/mapLoadable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Loadable } from './loadable';

export function mapLoadableData<T, R>(load: Loadable<T>, map: (obj: T) => R): Loadable<R> {
return {
...load,
data: load.data != null ? map(load.data) : undefined,
} as Loadable<R>;
}
21 changes: 21 additions & 0 deletions src/abacus-ts/lib/mergeObjects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type SimpleMap<T> = { [key: string]: T };
export function mergeObjects<T>(one: SimpleMap<T>, two: SimpleMap<T>, merge: (a: T, b: T) => T) {
const finalObj: SimpleMap<T> = {};

[...Object.keys(one), ...Object.keys(two)].forEach((key) => {
if (finalObj[key] != null) {
return;
}
const obj = one[key];
const otherObj = two[key];
if (obj != null && otherObj != null) {
finalObj[key] = merge(obj, otherObj);
} else if (obj == null && otherObj == null) {
// do nothing
} else {
// we know one of them is non-null
finalObj[key] = (obj ?? otherObj)!;
}
});
return finalObj;
}

0 comments on commit dc2500a

Please sign in to comment.