Skip to content
This repository has been archived by the owner on Aug 15, 2023. It is now read-only.

Commit

Permalink
feat: start the dealer price service in parallel (#70)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastien Verreault <sebver@pm.me>
  • Loading branch information
sebastienverreault and Sebastien Verreault authored Feb 21, 2022
1 parent 7c393fc commit 19b2886
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ services:
- graphql
ports:
- 3333:3333
- 50055:50055
expose:
- 3333
- 50055
networks:
- monitoring
environment:
Expand Down
2 changes: 2 additions & 0 deletions src/app/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Dealer } from "../Dealer"
import { exporter } from "../servers/exporter/exporter"
import nodePgMigrate from "node-pg-migrate"
import { RunnerOption } from "node-pg-migrate/dist/types"
import { priceService } from "../servers/price"

const logger = baseLogger.child({ module: "cron" })

Expand Down Expand Up @@ -32,6 +33,7 @@ const scheduler = async () => {

Promise.all([
exporter().catch((err) => logger.error(err)),
priceService().catch((err) => logger.error(err)),
scheduler().catch((err) => logger.error(err)),
nodePgMigrate(options).catch((err) => logger.error(err)),
])
61 changes: 33 additions & 28 deletions src/servers/price/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ import {
import { yamlConfig } from "../../config"
const fees = yamlConfig.fees

export const main = async () => {
//
// Get and update data
//
const logger = baseLogger.child({ module: "price-service" })

const updateMarketData = async () => {
await loop()
}

Expand All @@ -58,7 +57,7 @@ function getCentsFromSatsForImmediateBuy(
const response = new GetCentsFromSatsForImmediateBuyResponse()

const amountInSats = call.request.getAmountInSatoshis()
baseLogger.info(
logger.info(
{ amountInSats },
"Received a GetCentsFromSatsForImmediateBuy({amountInSats}) call",
)
Expand All @@ -70,7 +69,7 @@ function getCentsFromSatsForImmediateBuy(
const currentFee = 1 - (fees.BASE_FEE + fees.IMMEDIATE_CONVERSION_SPREAD)
const amountInCents = usd2cents(lastBidInUsdPerBtc * amountInBtc * currentFee)

baseLogger.info(
logger.info(
{ amountInSats, amountInCents },
"Responding to GetCentsFromSatsForImmediateBuy({amountInSats}) call with {amountInCents}",
)
Expand All @@ -89,7 +88,7 @@ function getCentsFromSatsForImmediateSell(
const response = new GetCentsFromSatsForImmediateSellResponse()

const amountInSats = call.request.getAmountInSatoshis()
baseLogger.info(
logger.info(
{ amountInSats },
"Received a GetCentsFromSatsForImmediateSell({amountInSats}) call",
)
Expand All @@ -101,7 +100,7 @@ function getCentsFromSatsForImmediateSell(
const currentFee = 1 + (fees.BASE_FEE + fees.IMMEDIATE_CONVERSION_SPREAD)
const amountInCents = usd2cents(lastAskInUsdPerBtc * amountInBtc * currentFee)

baseLogger.info(
logger.info(
{ amountInSats, amountInCents },
"Responding to GetCentsFromSatsForImmediateSell({amountInSats}) call with {amountInCents}",
)
Expand All @@ -121,7 +120,7 @@ function getCentsFromSatsForFutureBuy(

const amountInSats = call.request.getAmountInSatoshis()
const timeInSec = call.request.getTimeInSeconds()
baseLogger.info(
logger.info(
{ amountInSats, timeInSec },
"Received a GetCentsFromSatsForFutureBuy({amountInSats}, {timeInSec}) call",
)
Expand All @@ -134,7 +133,7 @@ function getCentsFromSatsForFutureBuy(
const currentFee = 1 - (fees.BASE_FEE + fees.DELAYED_CONVERSION_SPREAD)
const amountInCents = usd2cents(lastBidInUsdPerBtc * amountInBtc * currentFee)

baseLogger.info(
logger.info(
{ amountInSats, timeInSeconds, amountInCents },
"Responding to GetCentsFromSatsForFutureBuy({amountInSats}, {timeInSeconds}) call with {amountInCents}",
)
Expand All @@ -154,7 +153,7 @@ function getCentsFromSatsForFutureSell(

const amountInSats = call.request.getAmountInSatoshis()
const timeInSec = call.request.getTimeInSeconds()
baseLogger.info(
logger.info(
{ amountInSats, timeInSec },
"Received a GetCentsFromSatsForFutureSell({amountInSats}, {timeInSec}) call",
)
Expand All @@ -167,7 +166,7 @@ function getCentsFromSatsForFutureSell(
const currentFee = 1 + (fees.BASE_FEE + fees.DELAYED_CONVERSION_SPREAD)
const amountInCents = usd2cents(lastAskInUsdPerBtc * amountInBtc * currentFee)

baseLogger.info(
logger.info(
{ amountInSats, timeInSeconds, amountInCents },
"Responding to GetCentsFromSatsForFutureSell({amountInSats}, {timeInSeconds}) call with {amountInCents}",
)
Expand All @@ -186,7 +185,7 @@ function getSatsFromCentsForImmediateBuy(
const response = new GetSatsFromCentsForImmediateBuyResponse()

const amountInCents = call.request.getAmountInCents()
baseLogger.info(
logger.info(
{ amountInCents },
"Received a GetSatsFromCentsForImmediateBuy({amountInCents}) call",
)
Expand All @@ -198,7 +197,7 @@ function getSatsFromCentsForImmediateBuy(
const currentFee = 1 + (fees.BASE_FEE + fees.IMMEDIATE_CONVERSION_SPREAD)
const amountInSatoshis = btc2sat(amountInUsd / (lastAskInUsdPerBtc * currentFee))

baseLogger.info(
logger.info(
{ amountInCents, amountInSatoshis },
"Responding to GetSatsFromCentsForImmediateBuy({amountInCents}) call with {amountInSatoshis}",
)
Expand All @@ -217,7 +216,7 @@ function getSatsFromCentsForImmediateSell(
const response = new GetSatsFromCentsForImmediateSellResponse()

const amountInCents = call.request.getAmountInCents()
baseLogger.info(
logger.info(
{ amountInCents },
"Received a GetSatsFromCentsForImmediateSell({amountInCents}) call",
)
Expand All @@ -229,7 +228,7 @@ function getSatsFromCentsForImmediateSell(
const currentFee = 1 - (fees.BASE_FEE + fees.IMMEDIATE_CONVERSION_SPREAD)
const amountInSatoshis = btc2sat(amountInUsd / (lastBidInUsdPerBtc * currentFee))

baseLogger.info(
logger.info(
{ amountInCents, amountInSatoshis },
"Responding to GetSatsFromCentsForImmediateSell({amountInCents}) call with {amountInSatoshis}",
)
Expand All @@ -249,7 +248,7 @@ function getSatsFromCentsForFutureBuy(

const amountInCents = call.request.getAmountInCents()
const timeInSec = call.request.getTimeInSeconds()
baseLogger.info(
logger.info(
{ amountInCents, timeInSec },
"Received a GetSatsFromCentsForFutureBuy({amountInCents}, {timeInSec}) call",
)
Expand All @@ -262,7 +261,7 @@ function getSatsFromCentsForFutureBuy(
const currentFee = 1 + (fees.BASE_FEE + fees.DELAYED_CONVERSION_SPREAD)
const amountInSatoshis = btc2sat(amountInUsd / (lastAskInUsdPerBtc * currentFee))

baseLogger.info(
logger.info(
{ amountInCents, timeInSeconds, amountInSatoshis },
"Responding to GetSatsFromCentsForFutureBuy({amountInCents}, {timeInSeconds}) call with {amountInSatoshis}",
)
Expand All @@ -282,7 +281,7 @@ function getSatsFromCentsForFutureSell(

const amountInCents = call.request.getAmountInCents()
const timeInSec = call.request.getTimeInSeconds()
baseLogger.info(
logger.info(
{ amountInCents, timeInSec },
"Received a GetSatsFromCentsForFutureSell({amountInCents}, {timeInSec}) call",
)
Expand All @@ -295,7 +294,7 @@ function getSatsFromCentsForFutureSell(
const currentFee = 1 - (fees.BASE_FEE + fees.DELAYED_CONVERSION_SPREAD)
const amountInSatoshis = btc2sat(amountInUsd / (lastBidInUsdPerBtc * currentFee))

baseLogger.info(
logger.info(
{ amountInCents, timeInSeconds, amountInSatoshis },
"Responding to GetSatsFromCentsForFutureSell({amountInCents}, {timeInSeconds}) call with {amountInSatoshis}",
)
Expand All @@ -312,7 +311,7 @@ function getCentsPerSatsExchangeMidRate(
callback: sendUnaryData<GetCentsPerSatsExchangeMidRateResponse>,
) {
const response = new GetCentsPerSatsExchangeMidRateResponse()
baseLogger.info(
logger.info(
{ lastAsk: lastAskInUsdPerBtc, lastBid: lastBidInUsdPerBtc },
"Received a GetCentsPerSatsExchangeMidRate() call",
)
Expand Down Expand Up @@ -344,10 +343,16 @@ function getServer() {
return server
}

const serverPort = process.env.PRICE_SERVER_PORT ?? "50055"
const routeServer = getServer()
routeServer.bindAsync(`0.0.0.0:${serverPort}`, ServerCredentials.createInsecure(), () => {
console.info(`Price server running on port ${serverPort}`)
main()
routeServer.start()
})
export const priceService = async () => {
const serverPort = process.env.PRICE_SERVER_PORT ?? "50055"
const routeServer = getServer()
routeServer.bindAsync(
`0.0.0.0:${serverPort}`,
ServerCredentials.createInsecure(),
() => {
logger.info(`Price Service running on port ${serverPort}`)
updateMarketData()
routeServer.start()
},
)
}
2 changes: 1 addition & 1 deletion src/servers/price/price_fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { sleep } from "../../utils"

dotenv.config()

const logger = baseLogger.child({ module: "exporter" })
const logger = baseLogger.child({ module: "price-service" })
const dealer = new Dealer(logger)

export let lastBidInUsdPerBtc: number
Expand Down

0 comments on commit 19b2886

Please sign in to comment.