Skip to content

Commit

Permalink
chore: use correct proto
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Feb 29, 2024
1 parent e08c196 commit d1b39ba
Show file tree
Hide file tree
Showing 6 changed files with 1,032 additions and 72 deletions.
8 changes: 2 additions & 6 deletions history/src/services/notifications/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ export const NotificationsService = (): INotificationsService => {
const priceChanged = async (
args: PriceChangedArgs,
): Promise<void | NotificationsServiceError> => {
const priceChangedEvent = createPriceChangedEvent(args)

try {
const req = new HandleNotificationEventRequest()
const event = new NotificationEvent()

// TODO: Create price changed event and configure it

const priceChangedEvent = createPriceChangedEvent(args)
event.setPrice(priceChangedEvent)
req.setEvent(event)

await grpcClient.handleNotificationEvent(req)
} catch (err) {
if (err instanceof Error) {
Expand Down
50 changes: 19 additions & 31 deletions history/src/services/notifications/price-changed-event.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@
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: number
}
import { Money, PriceChangeDirection, PriceChanged } from "./proto/notifications_pb"

export const createPriceChangedEvent = ({
range,
initialPrice,
finalPrice,
}: PriceChangedArgs): PriceChangedEvent => {
const priceChange = calculatePriceChangeBips(initialPrice.price, finalPrice.price)
}: PriceChangedArgs) => {
const priceChange = calculatePriceChangePercentage(initialPrice.price, finalPrice.price)

return {
currentPriceInUsd: finalPrice.price.toFixed(2),
priceChangeDirection: priceChange.changeDirection,
priceChangeInBips: priceChange.bipAmount,
timeRange: range,
timestamp: finalPrice.timestamp,
}
const priceOfOneBitcoin = new Money()
priceOfOneBitcoin.setMinorUnits(usdMajorUnitToMinorUnit(finalPrice.price))
priceOfOneBitcoin.setCurrencyCode("USD")

const priceChangedEvent = new PriceChanged()
priceChangedEvent.setPriceOfOneBitcoin(priceOfOneBitcoin)
priceChangedEvent.setDirection(priceChange.direction)
priceChangedEvent.setPriceChangePercentage(priceChange.percentage)
return priceChangedEvent
}

const calculatePriceChangeBips = (initialPrice: Price, finalPrice: Price) => {
const bipAmount = Math.round(
((finalPrice - initialPrice) / initialPrice) * 10000,
).toString()
const usdMajorUnitToMinorUnit = (usd: number) => usd * 100

const calculatePriceChangePercentage = (initialPrice: Price, finalPrice: Price) => {
const percentage = ((finalPrice - initialPrice) / initialPrice) * 100

const changeDirection =
finalPrice > initialPrice ? PriceDirection.Increase : PriceDirection.Decrease
return { bipAmount, changeDirection }
const direction =
finalPrice > initialPrice ? PriceChangeDirection.UP : PriceChangeDirection.DOWN
return { percentage, direction }
}
35 changes: 35 additions & 0 deletions history/src/services/notifications/proto/notifications.proto
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ message NotificationEvent {
IdentityVerificationApproved identity_verification_approved = 3;
IdentityVerificationDeclined identity_verification_declined = 4;
IdentityVerificationReviewStarted identity_verification_review_started = 5;
TransactionInfo transaction = 6;
PriceChanged price = 7;
}
}

Expand Down Expand Up @@ -199,3 +201,36 @@ message IdentityVerificationDeclined {
message IdentityVerificationReviewStarted {
string user_id = 1;
}

message TransactionInfo {
string user_id = 1;
TransactionType type = 2;
Money settlement_amount = 3;
optional Money display_amount = 4;
}

enum TransactionType {
INTRA_LEDGER_RECEIPT = 0;
INTRA_LEDGER_PAYMENT= 1;
ONCHAIN_RECEIPT = 2;
ONCHAIN_RECEIPT_PENDING = 3;
ONCHAIN_PAYMENT = 4;
LIGHTNING_RECEIPT = 5;
LIGHTNING_PAYMENT = 6;
}

message Money {
string currency_code = 1;
uint64 minor_units = 2;
}

enum PriceChangeDirection {
UP = 0;
DOWN = 1;
}

message PriceChanged {
Money price_of_one_bitcoin = 1;
PriceChangeDirection direction = 2;
double price_change_percentage = 3;
}
116 changes: 116 additions & 0 deletions history/src/services/notifications/proto/notifications_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,16 @@ export class NotificationEvent extends jspb.Message {
getIdentityVerificationReviewStarted(): IdentityVerificationReviewStarted | undefined;
setIdentityVerificationReviewStarted(value?: IdentityVerificationReviewStarted): NotificationEvent;

hasTransaction(): boolean;
clearTransaction(): void;
getTransaction(): TransactionInfo | undefined;
setTransaction(value?: TransactionInfo): NotificationEvent;

hasPrice(): boolean;
clearPrice(): void;
getPrice(): PriceChanged | undefined;
setPrice(value?: PriceChanged): NotificationEvent;

getDataCase(): NotificationEvent.DataCase;

serializeBinary(): Uint8Array;
Expand All @@ -649,6 +659,8 @@ export namespace NotificationEvent {
identityVerificationApproved?: IdentityVerificationApproved.AsObject,
identityVerificationDeclined?: IdentityVerificationDeclined.AsObject,
identityVerificationReviewStarted?: IdentityVerificationReviewStarted.AsObject,
transaction?: TransactionInfo.AsObject,
price?: PriceChanged.AsObject,
}

export enum DataCase {
Expand All @@ -658,6 +670,8 @@ export namespace NotificationEvent {
IDENTITY_VERIFICATION_APPROVED = 3,
IDENTITY_VERIFICATION_DECLINED = 4,
IDENTITY_VERIFICATION_REVIEW_STARTED = 5,
TRANSACTION = 6,
PRICE = 7,
}

}
Expand Down Expand Up @@ -783,6 +797,93 @@ export namespace IdentityVerificationReviewStarted {
}
}

export class TransactionInfo extends jspb.Message {
getUserId(): string;
setUserId(value: string): TransactionInfo;
getType(): TransactionType;
setType(value: TransactionType): TransactionInfo;

hasSettlementAmount(): boolean;
clearSettlementAmount(): void;
getSettlementAmount(): Money | undefined;
setSettlementAmount(value?: Money): TransactionInfo;

hasDisplayAmount(): boolean;
clearDisplayAmount(): void;
getDisplayAmount(): Money | undefined;
setDisplayAmount(value?: Money): TransactionInfo;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): TransactionInfo.AsObject;
static toObject(includeInstance: boolean, msg: TransactionInfo): TransactionInfo.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: TransactionInfo, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): TransactionInfo;
static deserializeBinaryFromReader(message: TransactionInfo, reader: jspb.BinaryReader): TransactionInfo;
}

export namespace TransactionInfo {
export type AsObject = {
userId: string,
type: TransactionType,
settlementAmount?: Money.AsObject,
displayAmount?: Money.AsObject,
}
}

export class Money extends jspb.Message {
getCurrencyCode(): string;
setCurrencyCode(value: string): Money;
getMinorUnits(): number;
setMinorUnits(value: number): Money;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): Money.AsObject;
static toObject(includeInstance: boolean, msg: Money): Money.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: Money, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): Money;
static deserializeBinaryFromReader(message: Money, reader: jspb.BinaryReader): Money;
}

export namespace Money {
export type AsObject = {
currencyCode: string,
minorUnits: number,
}
}

export class PriceChanged extends jspb.Message {

hasPriceOfOneBitcoin(): boolean;
clearPriceOfOneBitcoin(): void;
getPriceOfOneBitcoin(): Money | undefined;
setPriceOfOneBitcoin(value?: Money): PriceChanged;
getDirection(): PriceChangeDirection;
setDirection(value: PriceChangeDirection): PriceChanged;
getPriceChangePercentage(): number;
setPriceChangePercentage(value: number): PriceChanged;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): PriceChanged.AsObject;
static toObject(includeInstance: boolean, msg: PriceChanged): PriceChanged.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: PriceChanged, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): PriceChanged;
static deserializeBinaryFromReader(message: PriceChanged, reader: jspb.BinaryReader): PriceChanged;
}

export namespace PriceChanged {
export type AsObject = {
priceOfOneBitcoin?: Money.AsObject,
direction: PriceChangeDirection,
priceChangePercentage: number,
}
}

export enum NotificationChannel {
PUSH = 0,
}
Expand Down Expand Up @@ -812,3 +913,18 @@ export enum DeclinedReason {
DOCUMENTS_DO_NOT_MATCH = 4,
OTHER = 5,
}

export enum TransactionType {
INTRA_LEDGER_RECEIPT = 0,
INTRA_LEDGER_PAYMENT = 1,
ONCHAIN_RECEIPT = 2,
ONCHAIN_RECEIPT_PENDING = 3,
ONCHAIN_PAYMENT = 4,
LIGHTNING_RECEIPT = 5,
LIGHTNING_PAYMENT = 6,
}

export enum PriceChangeDirection {
UP = 0,
DOWN = 1,
}
Loading

0 comments on commit d1b39ba

Please sign in to comment.