Skip to content

Commit

Permalink
Revert "Fix false non-zero balances"
Browse files Browse the repository at this point in the history
This reverts commit baf3fa9.
  • Loading branch information
kriscieslak authored and kriscieslak committed Mar 29, 2023
1 parent 97877fa commit bb98af3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 120 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file.

## [Unreleased]
- Revert "Fix false non-zero balances"

## [v3.0.4] 2023-02-24
- Fix deploy workflow
Expand Down
104 changes: 29 additions & 75 deletions src/opensearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ import {
} from "./model";
import {
findAll,
findFirst,
findOne,
getAll,
getByFieldQuery,
getByFieldsQuery,
getDocumentQuery,
getLatestQuery,
getMultiQuery,
Expand All @@ -28,7 +26,6 @@ import {
SortOption,
SortOptions,
SortOptionSince,
SortOrder,
} from "./query";

import {
Expand All @@ -38,8 +35,6 @@ import {
of,
orElse,
right,
bind,
bindTo,
TaskEither,
} from "fp-ts/lib/TaskEither";
import { fromNextString, Pagination, toNextString } from "./validation";
Expand Down Expand Up @@ -358,85 +353,44 @@ export const findBalanceByAddress =
(
address: string,
ordinal?: number
): TaskEither<ApplicationError, Result<Balance>> =>
ordinal
? findBalanceByAddressAndOrdinal(os)(address, ordinal)
: findLatestBalanceByAddress(os)(address);

const findBalanceByAddressAndOrdinal =
(os: Client) =>
(
address: string,
ordinal: number
): TaskEither<ApplicationError, Result<Balance>> =>
pipe(
findFirst<OpenSearchBalance>(
findOne<OpenSearchBalance>(
os.search(
getByFieldsQuery<OpenSearchBalance>(
getByFieldQuery<OpenSearchBalance, "address">(
OSIndex.Balances,
{ address, snapshotOrdinal: ordinal },
{ snapshotOrdinal: SortOrder.Desc },
{ size: 1 }
)
)
),
map((b) => {
const data = b
? {
balance: b.balance,
ordinal: b.snapshotOrdinal,
address: b.address,
"address",
address,
{
options: [
{
sortField: "snapshotOrdinal",
// To achieve (0, ordinal> we need to make (0, ordinal + 1)
...(ordinal !== undefined
? { searchSince: ordinal + 1 }
: {}),
searchDirection: SearchDirection.Before,
},
],
size: 1,
}
: { balance: 0, ordinal, address };

return {
data,
meta: {},
};
})
);

const findLatestBalanceByAddress =
(os: Client) =>
(address: string): TaskEither<ApplicationError, Result<Balance>> =>
pipe(
findFirst<OpenSearchBalance>(
os.search(
getByFieldsQuery<OpenSearchBalance>(
OSIndex.Balances,
{ address },
{ snapshotOrdinal: SortOrder.Desc },
{ size: 1 }
)
)
),
bindTo("balance"),
bind("snapshot", () =>
findOne<OpenSearchSnapshot>(
os.search(
getByFieldsQuery<OpenSearchSnapshot>(
OSIndex.Snapshots,
{},
{ ordinal: SortOrder.Desc },
{ size: 1 }
map(({ data: { snapshotOrdinal, balance, address }, meta }) => ({
data: { ordinal: snapshotOrdinal, balance, address },
meta,
})),
orElse((e: ApplicationError) => {
return e.statusCode === StatusCodes.NOT_FOUND
? pipe(
findSnapshot(os)("latest"),
map((s) => ({
data: { ordinal: s.data.ordinal, balance: 0, address },
meta: {},
}))
)
)
)
),
map(({ balance: b, snapshot: { data: s } }) => {
const data =
b && b.snapshotOrdinal >= s.ordinal - 1
? {
balance: b.balance,
ordinal: b.snapshotOrdinal,
address: b.address,
}
: { balance: 0, ordinal: s.ordinal, address };

return {
data,
meta: {},
};
: left(e);
})
);

Expand Down
45 changes: 0 additions & 45 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,6 @@ export function getByFieldQuery<T, K extends keyof T>(
};
}

export function getByFieldsQuery<T>(
index: string,
must: Partial<T> = {},
sort: { [K in keyof T]?: SortOrder } = {},
page: { size: number } = { size: maxSizeLimit }
): any {
return {
index,
body: {
size: page.size,
sort: Object.entries(sort).map(([field, value]) => ({
[field]: { order: value },
})),
query: {
bool: {
must: Object.entries(must).map(([field, value]) => ({
term: { [field]: value },
})),
},
},
},
};
}

export function getAll<T>(index: string, sort: SortOptions<T>): any {
return {
index,
Expand Down Expand Up @@ -178,27 +154,6 @@ export const findOne = <T>(
}))
);

export const findFirst = <T>(
search: TransportRequestPromise<ApiResponse>
): TaskEither<ApplicationError, T | undefined> =>
pipe(
tryCatch<ApplicationError, any>(
() =>
search.then((r) => {
return r.body.hits.hits;
}),
(err) =>
new ApplicationError(
"OpenSearch error",
[err as string],
StatusCodes.SERVER_ERROR
)
),
map((hits) => {
return hits[0]?._source as T;
})
);

export const findAll = <T>(
search: TransportRequestPromise<ApiResponse>
): TaskEither<ApplicationError, T[]> =>
Expand Down

0 comments on commit bb98af3

Please sign in to comment.