Skip to content

Commit

Permalink
Merge pull request #190 from EdgeApp/paul/fixNullNumbers
Browse files Browse the repository at this point in the history
Properly handle nulls in number type fields
  • Loading branch information
paullinator authored May 23, 2024
2 parents 2ad07f8 + 63d70e3 commit 7ffaaf2
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- added: Paybis support
- changed: Paginate caching engine to prevent timeouts
- changed: Create caching engine 'initialized' document entry for each app:partner pair
- fixed: Properly handle null values in 'number' typed fields

## 0.1.0

Expand Down
5 changes: 3 additions & 2 deletions src/bin/bogReporter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fetch from 'node-fetch'

import { config } from '../config'
import { safeParseFloat } from '../util'

const BITS_OF_GOLD_API_KEY = config.bog.apiKey

Expand Down Expand Up @@ -74,7 +75,7 @@ async function queryFiatRate(
throw new Error(`queryFiatRate failed with status code ${result.status}`)
}
const json = await result.json()
return parseFloat(json.exchangeRate)
return safeParseFloat(json.exchangeRate)
}

async function queryCryptoRate(
Expand All @@ -91,7 +92,7 @@ async function queryCryptoRate(
throw new Error(`queryCryptoRate failed with status code ${result.status}`)
}
const json = await result.json()
return parseFloat(json.exchangeRate)
return safeParseFloat(json.exchangeRate)
}

queryBog().catch(e => console.log(e))
12 changes: 7 additions & 5 deletions src/bin/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import nano from 'nano'

import { pagination } from '../dbutils'
import { DbTx, StandardTx } from '../types'
import { datelog } from '../util'
import { datelog, safeParseFloat } from '../util'

const config = js.readFileSync('./config.json')

Expand Down Expand Up @@ -143,7 +143,9 @@ async function migration(): Promise<void> {
payoutTxid: cleanedShapeshiftTx.outputTXID,
payoutAddress: cleanedShapeshiftTx.outputAddress,
payoutCurrency: cleanedShapeshiftTx.outputCurrency,
payoutAmount: parseFloat(cleanedShapeshiftTx.outputAmount),
payoutAmount: safeParseFloat(
cleanedShapeshiftTx.outputAmount
),
timestamp: cleanedShapeshiftTx.timestamp,
isoDate: new Date(
cleanedShapeshiftTx.timestamp * 1000
Expand All @@ -161,7 +163,7 @@ async function migration(): Promise<void> {
const timestamp =
typeof cleanedOldTx.timestamp === 'number'
? cleanedOldTx.timestamp
: parseFloat(cleanedOldTx.timestamp)
: safeParseFloat(cleanedOldTx.timestamp)
const isoDate = new Date(timestamp * 1000).toISOString()
const depositAddress =
typeof cleanedOldTx.inputAddress === 'string' &&
Expand All @@ -175,11 +177,11 @@ async function migration(): Promise<void> {
const depositAmount =
typeof cleanedOldTx.inputAmount === 'number'
? cleanedOldTx.inputAmount
: parseFloat(cleanedOldTx.inputAmount)
: safeParseFloat(cleanedOldTx.inputAmount)
const payoutAmount =
typeof cleanedOldTx.outputAmount === 'number'
? cleanedOldTx.outputAmount
: parseFloat(cleanedOldTx.outputAmount)
: safeParseFloat(cleanedOldTx.outputAmount)
const newTx: StandardTx = {
orderId: cleanedOldTx.inputTXID,
depositTxid: undefined,
Expand Down
4 changes: 2 additions & 2 deletions src/bin/partnerTotals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fetch from 'node-fetch'
// import fetch from 'node-fetch'
import config from '../../config.json'
import { asDbTx, DbTx } from '../types'
import { datelog, standardizeNames } from '../util'
import { datelog, safeParseFloat, standardizeNames } from '../util'

const nanoDb = nano(config.couchDbFullpath)
const QUERY_LIMIT = 100
Expand Down Expand Up @@ -201,7 +201,7 @@ async function getExchangeRate(
datelog(
`Rate for ${currencyA} -> ${currencyB} ${date}: ${jsonObj.exchangeRate}`
)
return parseFloat(jsonObj.exchangeRate)
return safeParseFloat(jsonObj.exchangeRate)
} catch (e) {
datelog(
`Could not not get exchange rate for ${currencyA} and ${currencyB} at ${date}.`,
Expand Down
4 changes: 2 additions & 2 deletions src/partners/bitrefill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import fetch from 'node-fetch'

import { PartnerPlugin, PluginParams, PluginResult, StandardTx } from '../types'
import { datelog } from '../util'
import { datelog, safeParseFloat } from '../util'

const asBitrefillTx = asObject({
paymentReceived: asBoolean,
Expand Down Expand Up @@ -103,7 +103,7 @@ export async function queryBitrefill(
if (inputAmountStr == null) {
break
}
const inputAmountNum = parseFloat(
const inputAmountNum = safeParseFloat(
div(inputAmountStr, multipliers[inputCurrency], 8)
)
const ssTx: StandardTx = {
Expand Down
6 changes: 3 additions & 3 deletions src/partners/bity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { asArray, asObject, asString, asUnknown } from 'cleaners'
import fetch from 'node-fetch'

import { PartnerPlugin, PluginParams, PluginResult, StandardTx } from '../types'
import { datelog } from '../util'
import { datelog, safeParseFloat } from '../util'

const asBityTx = asObject({
id: asString,
Expand Down Expand Up @@ -123,11 +123,11 @@ export async function queryBity(
depositTxid: undefined,
depositAddress: undefined,
depositCurrency: tx.input.currency.toUpperCase(),
depositAmount: parseFloat(tx.input.amount),
depositAmount: safeParseFloat(tx.input.amount),
payoutTxid: undefined,
payoutAddress: undefined,
payoutCurrency: tx.output.currency.toUpperCase(),
payoutAmount: parseFloat(tx.output.amount),
payoutAmount: safeParseFloat(tx.output.amount),
timestamp: Date.parse(tx.timestamp_created.concat('Z')) / 1000,
isoDate: new Date(tx.timestamp_created.concat('Z')).toISOString(),
usdValue: -1,
Expand Down
11 changes: 8 additions & 3 deletions src/partners/changehero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import {
StandardTx,
Status
} from '../types'
import { datelog, retryFetch, smartIsoDateFromTimestamp } from '../util'
import {
datelog,
retryFetch,
safeParseFloat,
smartIsoDateFromTimestamp
} from '../util'

const asChangeHeroStatus = asMaybe(asValue('finished', 'expired'), 'other')

Expand Down Expand Up @@ -127,11 +132,11 @@ export async function queryChangeHero(
depositTxid: tx.payinHash,
depositAddress: tx.payinAddress,
depositCurrency: tx.currencyFrom.toUpperCase(),
depositAmount: parseFloat(tx.amountFrom),
depositAmount: safeParseFloat(tx.amountFrom),
payoutTxid: tx.payoutHash,
payoutAddress: tx.payoutAddress,
payoutCurrency: tx.currencyTo.toUpperCase(),
payoutAmount: parseFloat(tx.amountTo),
payoutAmount: safeParseFloat(tx.amountTo),
timestamp: tx.createdAt,
isoDate: smartIsoDateFromTimestamp(tx.createdAt).isoDate,
usdValue: -1,
Expand Down
6 changes: 3 additions & 3 deletions src/partners/changelly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Changelly from 'api-changelly/lib.js'
import { asArray, asNumber, asObject, asString, asUnknown } from 'cleaners'

import { PartnerPlugin, PluginParams, PluginResult, StandardTx } from '../types'
import { datelog } from '../util'
import { datelog, safeParseFloat } from '../util'

const asChangellyTx = asObject({
id: asString,
Expand Down Expand Up @@ -138,11 +138,11 @@ export async function queryChangelly(
depositTxid: tx.payinHash,
depositAddress: tx.payinAddress,
depositCurrency: tx.currencyFrom.toUpperCase(),
depositAmount: parseFloat(tx.amountFrom),
depositAmount: safeParseFloat(tx.amountFrom),
payoutTxid: tx.payoutHash,
payoutAddress: tx.payoutAddress,
payoutCurrency: tx.currencyTo.toUpperCase(),
payoutAmount: parseFloat(tx.amountTo),
payoutAmount: safeParseFloat(tx.amountTo),
timestamp: tx.createdAt,
isoDate: new Date(tx.createdAt * 1000).toISOString(),
usdValue: -1,
Expand Down
11 changes: 8 additions & 3 deletions src/partners/godex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import {
StandardTx,
Status
} from '../types'
import { datelog, retryFetch, smartIsoDateFromTimestamp } from '../util'
import {
datelog,
retryFetch,
safeParseFloat,
smartIsoDateFromTimestamp
} from '../util'

const asGodexPluginParams = asObject({
settings: asObject({
Expand Down Expand Up @@ -117,11 +122,11 @@ export async function queryGodex(
depositTxid: tx.hash_in,
depositAddress: tx.deposit,
depositCurrency: tx.coin_from.toUpperCase(),
depositAmount: parseFloat(tx.deposit_amount),
depositAmount: safeParseFloat(tx.deposit_amount),
payoutTxid: undefined,
payoutAddress: tx.withdrawal,
payoutCurrency: tx.coin_to.toUpperCase(),
payoutAmount: parseFloat(tx.withdrawal_amount),
payoutAmount: safeParseFloat(tx.withdrawal_amount),
timestamp,
isoDate,
usdValue: -1,
Expand Down
11 changes: 8 additions & 3 deletions src/partners/letsexchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import {
StandardTx,
Status
} from '../types'
import { datelog, retryFetch, smartIsoDateFromTimestamp } from '../util'
import {
datelog,
retryFetch,
safeParseFloat,
smartIsoDateFromTimestamp
} from '../util'

export const asLetsExchangePluginParams = asObject({
settings: asObject({
Expand Down Expand Up @@ -126,11 +131,11 @@ export async function queryLetsExchange(
depositTxid: tx.hash_in,
depositAddress: tx.deposit,
depositCurrency: tx.coin_from.toUpperCase(),
depositAmount: parseFloat(tx.deposit_amount),
depositAmount: safeParseFloat(tx.deposit_amount),
payoutTxid: undefined,
payoutAddress: tx.withdrawal,
payoutCurrency: tx.coin_to.toUpperCase(),
payoutAmount: parseFloat(tx.withdrawal_amount),
payoutAmount: safeParseFloat(tx.withdrawal_amount),
timestamp,
isoDate,
usdValue: -1,
Expand Down
4 changes: 2 additions & 2 deletions src/partners/shapeshift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { asArray, asNumber, asObject, asString, asUnknown } from 'cleaners'
import fetch from 'node-fetch'

import { PartnerPlugin, PluginParams, PluginResult, StandardTx } from '../types'
import { datelog } from '../util'
import { datelog, safeParseFloat } from '../util'

const asShapeshiftTx = asObject({
orderId: asString,
Expand Down Expand Up @@ -63,7 +63,7 @@ export async function queryShapeshift(
payoutTxid: tx.outputTXID,
payoutAddress: tx.outputAddress,
payoutCurrency: tx.outputCurrency,
payoutAmount: parseFloat(tx.outputAmount),
payoutAmount: safeParseFloat(tx.outputAmount),
timestamp: tx.timestamp,
isoDate: new Date(tx.timestamp * 1000).toISOString(),
usdValue: -1,
Expand Down
7 changes: 4 additions & 3 deletions src/partners/simplex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from 'cleaners'

import { PartnerPlugin, PluginParams, PluginResult, StandardTx } from '../types'
import { safeParseFloat } from '../util'

const asSimplexTx = asObject({
amount_usd: asString,
Expand Down Expand Up @@ -111,14 +112,14 @@ export async function querySimplex(
depositTxid: undefined,
depositAddress: undefined,
depositCurrency: tx.currency,
depositAmount: parseFloat(tx.fiat_total_amount),
depositAmount: safeParseFloat(tx.fiat_total_amount),
payoutTxid: undefined,
payoutAddress: undefined,
payoutCurrency: tx.crypto_currency,
payoutAmount: parseFloat(tx.amount_crypto),
payoutAmount: safeParseFloat(tx.amount_crypto),
timestamp,
isoDate: new Date(timestamp * 1000).toISOString(),
usdValue: parseFloat(tx.amount_usd),
usdValue: safeParseFloat(tx.amount_usd),
rawTx
}
ssFormatTxs.push(ssTx)
Expand Down
6 changes: 3 additions & 3 deletions src/partners/switchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { asArray, asObject, asString, asUnknown } from 'cleaners'
import fetch from 'node-fetch'

import { PartnerPlugin, PluginParams, PluginResult, StandardTx } from '../types'
import { datelog } from '../util'
import { datelog, safeParseFloat } from '../util'

const asSwitchainTx = asObject({
id: asString,
Expand Down Expand Up @@ -78,11 +78,11 @@ export async function querySwitchain(
depositTxid: tx.depositTxId,
depositAddress: tx.depositAddress,
depositCurrency: pair[0].toUpperCase(),
depositAmount: parseFloat(tx.amountFrom),
depositAmount: safeParseFloat(tx.amountFrom),
payoutTxid: tx.withdrawTxId,
payoutAddress: tx.withdrawAddress,
payoutCurrency: pair[1].toUpperCase(),
payoutAmount: parseFloat(tx.rate),
payoutAmount: safeParseFloat(tx.rate),
timestamp: timestamp / 1000,
isoDate: tx.createdAt,
usdValue: -1,
Expand Down
6 changes: 3 additions & 3 deletions src/partners/totle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fetch from 'node-fetch'
import Web3 from 'web3'

import { PartnerPlugin, PluginParams, PluginResult, StandardTx } from '../types'
import { datelog } from '../util'
import { datelog, safeParseFloat } from '../util'

const asCurrentBlockResult = asNumber

Expand Down Expand Up @@ -393,7 +393,7 @@ export async function queryTotle(
depositTxid: receipt.transactionHash,
depositAddress: receipt.from,
depositCurrency: sourceToken.symbol,
depositAmount: parseFloat(
depositAmount: safeParseFloat(
div(
sourceAmount.toString(),
(10 ** sourceToken.decimals).toString(),
Expand All @@ -404,7 +404,7 @@ export async function queryTotle(
payoutTxid: receipt.transactionHash,
payoutAddress: receipt.to,
payoutCurrency: destinationToken.symbol,
payoutAmount: parseFloat(
payoutAmount: safeParseFloat(
div(
destinationAmount.toString(),
(10 ** destinationToken.decimals).toString(),
Expand Down
7 changes: 4 additions & 3 deletions src/partners/wyre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { asNumber, asObject, asOptional, asString } from 'cleaners'
import fetch from 'node-fetch'

import { PartnerPlugin, PluginParams, PluginResult, StandardTx } from '../types'
import { safeParseFloat } from '../util'

const asWyreTx = asObject({
id: asString,
Expand Down Expand Up @@ -96,14 +97,14 @@ export async function queryWyre(
depositTxid: undefined,
depositAddress: undefined,
depositCurrency: tx.sourceCurrency,
depositAmount: parseFloat(tx.sourceAmount),
depositAmount: safeParseFloat(tx.sourceAmount),
payoutTxid: undefined,
payoutAddress: undefined,
payoutCurrency: tx.destCurrency,
payoutAmount: parseFloat(tx.destAmount),
payoutAmount: safeParseFloat(tx.destAmount),
timestamp: dateMs / 1000,
isoDate: date.toISOString(),
usdValue: parseFloat(tx.usdEquiv),
usdValue: safeParseFloat(tx.usdEquiv),
rawTx: txStr
}

Expand Down
4 changes: 2 additions & 2 deletions src/ratesEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
CurrencyCodeMappings,
DbTx
} from './types'
import { datelog, standardizeNames } from './util'
import { datelog, safeParseFloat, standardizeNames } from './util'

const nanoDb = nano(config.couchDbFullpath)
const QUERY_FREQ_MS = 15000
Expand Down Expand Up @@ -222,7 +222,7 @@ async function getExchangeRate(
datelog(
`Rate for ${currencyA} -> ${currencyB} ${date}: ${jsonObj.exchangeRate}`
)
return parseFloat(jsonObj.exchangeRate)
return safeParseFloat(jsonObj.exchangeRate)
} catch (e) {
datelog(
`Could not not get exchange rate for ${currencyA} and ${currencyB} at ${date}.`,
Expand Down
Loading

0 comments on commit 7ffaaf2

Please sign in to comment.