Skip to content

Commit f3f1a47

Browse files
committed
refactor: clean code
1 parent 7f22868 commit f3f1a47

2 files changed

Lines changed: 11 additions & 14 deletions

File tree

src/handlers/tokenInstanceHandlers.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,19 @@ multiMapper("tokenInstance:Transfer", async ({ event, context }) => {
8989
accountAddress,
9090
} as const;
9191

92-
const existingPosition = (await TokenInstancePositionService.get(
92+
let positionWasInitialized = false;
93+
const position = (await TokenInstancePositionService.getOrInit(
9394
context,
94-
positionQuery
95-
)) as TokenInstancePositionService | null;
96-
const position = (existingPosition ??
97-
((await TokenInstancePositionService.getOrInit(
98-
context,
99-
positionQuery,
100-
event,
101-
async (tokenInstancePosition) =>
102-
await initialisePosition(context, event, tokenAddress, tokenInstancePosition)
103-
)) as TokenInstancePositionService)) as TokenInstancePositionService;
95+
positionQuery,
96+
event,
97+
async (tokenInstancePosition) => {
98+
positionWasInitialized = true;
99+
await initialisePosition(context, event, tokenAddress, tokenInstancePosition);
100+
}
101+
)) as TokenInstancePositionService;
104102

105103
const positionData = position.read();
106-
const positionAlreadyExisted = existingPosition !== null;
104+
const positionAlreadyExisted = !positionWasInitialized;
107105
const currentBalance = positionData.balance ?? 0n;
108106

109107
if (!positionAlreadyExisted && !isIncrease) {

src/helpers/investorPositionCheckpoint.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ export function computeInvestorPositionCheckpoint(
6363
} = input;
6464

6565
const periodEarnings = computePeriodEarnings(balanceBefore, tokenPrice, tokenPriceAtLastChange);
66-
const cumulativeEarningsAfter =
67-
cumulativeEarningsBefore + (periodEarnings === null ? 0n : periodEarnings);
66+
const cumulativeEarningsAfter = cumulativeEarningsBefore + (periodEarnings ?? 0n);
6867

6968
if (isIncrease) {
7069
const costBasisAfter = costBasisBefore + amount * tokenPrice;

0 commit comments

Comments
 (0)