Skip to content

Commit

Permalink
feat: notify price changes
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Feb 27, 2024
1 parent 8bb472c commit 910bee3
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 18 deletions.
5 changes: 3 additions & 2 deletions history/src/app/history/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./get-price-history"
export * from "./update-price-history"
export * from "./get-price-history";

Check failure on line 1 in history/src/app/history/index.ts

View workflow job for this annotation

GitHub Actions / Check Code

Delete `;`
export * from "./update-price-history";

Check failure on line 2 in history/src/app/history/index.ts

View workflow job for this annotation

GitHub Actions / Check Code

Delete `;`
export * from "./notify-price-change";

Check failure on line 3 in history/src/app/history/index.ts

View workflow job for this annotation

GitHub Actions / Check Code

Delete `;`
10 changes: 7 additions & 3 deletions history/src/app/history/index.types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
type GetPriceHistoryArgs = {
currency: string
range?: string
}
currency: string;

Check failure on line 2 in history/src/app/history/index.types.d.ts

View workflow job for this annotation

GitHub Actions / Check Code

Delete `;`
range?: string;

Check failure on line 3 in history/src/app/history/index.types.d.ts

View workflow job for this annotation

GitHub Actions / Check Code

Delete `;`
};

Check failure on line 4 in history/src/app/history/index.types.d.ts

View workflow job for this annotation

GitHub Actions / Check Code

Delete `;`

type NotifyPriceChangeArgs = {
range?: PriceRange;

Check failure on line 7 in history/src/app/history/index.types.d.ts

View workflow job for this annotation

GitHub Actions / Check Code

Delete `;`
};

Check failure on line 8 in history/src/app/history/index.types.d.ts

View workflow job for this annotation

GitHub Actions / Check Code

Delete `;`
32 changes: 32 additions & 0 deletions history/src/app/history/notify-price-change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { defaultBaseCurrency } from "@config";

Check failure on line 1 in history/src/app/history/notify-price-change.ts

View workflow job for this annotation

GitHub Actions / Check Code

Delete `;`
import { PriceRange } from "@domain/price";

Check failure on line 2 in history/src/app/history/notify-price-change.ts

View workflow job for this annotation

GitHub Actions / Check Code

Delete `;`
import { checkedToCurrency } from "@domain/primitives";
import { PriceRepository } from "@services/database";
import { NotificationsService } from "@services/notifications";

export const notifyPriceChange = async ({ range }: NotifyPriceChangeArgs) => {
const quote = checkedToCurrency("USD");

if (quote instanceof Error) {
return quote;
}

const rangeToQuery = range || PriceRange.OneDay;
const prices = await PriceRepository().listPrices({
base: defaultBaseCurrency,
quote,
range: rangeToQuery,
});

if (prices instanceof Error) {
return prices;
}

const notificationsService = NotificationsService();

return notificationsService.priceChanged({
initialPrice: prices[0],
finalPrice: prices[prices.length - 1],
range: rangeToQuery,
});
};
10 changes: 5 additions & 5 deletions history/src/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { wrapAsyncToRunInSpan } from "@services/tracing"
import { wrapAsyncToRunInSpan } from "@services/tracing";

import * as HistoryMod from "./history"
import * as HistoryMod from "./history";

const allFunctions = {
History: { ...HistoryMod },
}
};

for (const subModule in allFunctions) {
for (const fn in allFunctions[subModule]) {
allFunctions[subModule][fn] = wrapAsyncToRunInSpan({
namespace: `app.${subModule.toLowerCase()}`,
fn: allFunctions[subModule][fn],
})
});
}
}

export const { History } = allFunctions
export const { History } = allFunctions;
6 changes: 6 additions & 0 deletions history/src/domain/notifications/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ErrorLevel, ServiceError } from "../errors";

export class NotificationsServiceError extends ServiceError {}
export class UnknownNotificationServiceError extends NotificationsServiceError {
level = ErrorLevel.Critical;
}
1 change: 1 addition & 0 deletions history/src/domain/notifications/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./errors";
13 changes: 13 additions & 0 deletions history/src/domain/notifications/index.types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type NotificationsServiceError = import("./errors").NotificationsServiceError;

interface INotificationsService {
priceChanged(
args: PriceChangedArgs
): Promise<void | NotificationsServiceError>;
}

type PriceChangedArgs = {
range: PriceRange;
initialPrice: Tick;
finalPrice: Tick;
};
17 changes: 9 additions & 8 deletions history/src/servers/history/cron.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import dotenv from "dotenv"
import dotenv from "dotenv";

import { History } from "@app"
import { History } from "@app";

import { closeDbConnections } from "@services/database"
import { closeDbConnections } from "@services/database";

dotenv.config()
dotenv.config();

const startServer = async () => {
await History.updatePriceHistory()
await closeDbConnections()
}
await History.updatePriceHistory();
await History.notifyPriceChange({});
await closeDbConnections();
};

if (require.main === module) {
startServer()
startServer();
}
12 changes: 12 additions & 0 deletions history/src/services/notifications/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const NotificationsService = (): INotificationsService => {
const priceChanged = async (
args: PriceChangedArgs
): Promise<void | NotificationsServiceError> => {
const priceChangedEvent = createPriceChangedEvent(args);

// Send event to notification service
};
return {
priceChanged,
};
};
22 changes: 22 additions & 0 deletions history/src/services/notifications/price-change-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const PriceDirection = {
Increase: "Increase",
Decrease: "Decrease",
} as const;

type PriceDirection = (typeof PriceDirection)[keyof typeof PriceDirection];

type PriceChangedEvent = {
currentPriceInUsd: string;
priceChangeDirection: PriceDirection;
priceChangeInBips: string;
timeRange: PriceRange;
timestamp: string;
};

const createPriceChangedEvent = ({
range,
initialPrice,
finalPrice,
}: PriceChangedArgs): PriceChangedEvent => {
throw new Error("Method not implemented.");
};

0 comments on commit 910bee3

Please sign in to comment.