Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/notifications update #333

Merged
merged 6 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/mail-scheduler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ConfigVariable, config } from '../utils/config'
import { uniqueId } from '../utils/crypto'
import { globalEm } from '../utils/globalEm'
import { createMailContent, executeMailDelivery } from './utils'
import { updateJoystreamPrice } from '../utils/joystreamPrice'

export async function getMaxAttempts(em: EntityManager): Promise<number> {
const maxAttempts = await config.get(ConfigVariable.EmailNotificationDeliveryMaxAttempts, em)
Expand All @@ -31,6 +32,7 @@ export async function mailsToDeliver(em: EntityManager): Promise<NotificationEma

export async function deliverEmails() {
const em = await globalEm
await updateJoystreamPrice()
const newEmailDeliveries = await mailsToDeliver(em)
const maxAttempts = await getMaxAttempts(em)
const appName = await config.get(ConfigVariable.AppName, em)
Expand Down
53 changes: 53 additions & 0 deletions src/utils/joystreamPrice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { createLogger } from '@subsquid/logger'
import BN from 'bn.js'

export let JOYSTREAM_USD_PRICE: number | null = null

const HAPI_TO_JOY_RATE = 10 ** 10
const HAPI_TO_JOY_RATE_BN = new BN(HAPI_TO_JOY_RATE)
const MAX_SAFE_NUMBER_BN = new BN(Number.MAX_SAFE_INTEGER)

const log = createLogger('price')

export const updateJoystreamPrice = async () => {
// we don't care if the request fails, app should continue to work w/o joy price
const data = await fetch('https://status.joystream.org/price').catch(() =>
log.error('Fetching JOYSTREAM price failed')
)
if (data) {
const json = await data.json()
JOYSTREAM_USD_PRICE = +json.price ?? 0
}
}

export const schedulePriceUpdate = async (): Promise<void> => {
while (true) {
await updateJoystreamPrice()
log.info(`Price refetched: ${JOYSTREAM_USD_PRICE}`)
await new Promise((resolve) => setTimeout(resolve, 1_000 * 60 * 5)) // 5mins
}
}

export const convertHapiToUSD = (hapis: BN | bigint, round = true) => {
if (!JOYSTREAM_USD_PRICE) return null
const tokens = hapiBnToTokenNumber(new BN(hapis.toString()))
const amount = tokens * JOYSTREAM_USD_PRICE
return round ? Math.round(amount) : amount
}

const hapiBnToTokenNumber = (bn: BN, roundUpToTwoDecimalPlaces?: boolean) => {
const wholeUnitsBn = bn.div(HAPI_TO_JOY_RATE_BN)
const fractionalUnitsBn = bn.mod(HAPI_TO_JOY_RATE_BN)

if (wholeUnitsBn.gt(MAX_SAFE_NUMBER_BN)) {
throw new Error('Trying to convert unsafe number from BN to number')
}

const wholeUnits = wholeUnitsBn.toNumber()
const fractionalHapiUnits = fractionalUnitsBn.toNumber()
const fractionalJoyUnits = fractionalHapiUnits / HAPI_TO_JOY_RATE
if (roundUpToTwoDecimalPlaces) {
return Math.ceil((wholeUnits + fractionalJoyUnits) * 100) / 100
}
return wholeUnits + fractionalJoyUnits
}
Loading
Loading