Skip to content

Commit

Permalink
Merge pull request #41 from mysteriumnetwork/js-math
Browse files Browse the repository at this point in the history
Fix price calcs
  • Loading branch information
tadaskay authored Apr 23, 2020
2 parents 3d7cca7 + 59f034e commit cced234
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mysterium-vpn-js",
"version": "3.0.0",
"version": "3.0.1",
"description": "Javascript SDK for Mysterium node",
"keywords": [
"mysterium",
Expand Down
18 changes: 10 additions & 8 deletions src/payment/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,26 @@ export interface PaymentMethod {
}
}

export const pricePerMinute = (pm: PaymentMethod): Money => {
if (!pm.rate.perSeconds) {
return { amount: 0, currency: pm.price.currency }
export const pricePerMinute = (pm?: PaymentMethod): Money => {
if (!pm || !pm.rate.perSeconds) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return { amount: 0, currency: pm!.price.currency }
}
return {
amount: (60 / pm.rate.perSeconds) * pm.price.amount,
amount: Math.round((60 / pm.rate.perSeconds) * pm.price.amount),
currency: pm.price.currency,
}
}

const bytesInGiB = 1024 * 1024 * 1024

export const pricePerGiB = (pm: PaymentMethod): Money => {
if (!pm.rate.perBytes) {
return { amount: 0, currency: pm.price.currency }
export const pricePerGiB = (pm?: PaymentMethod): Money => {
if (!pm || !pm.rate.perBytes) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return { amount: 0, currency: pm!.price.currency }
}
return {
amount: (bytesInGiB / pm.rate.perBytes) * pm.price.amount,
amount: Math.round((bytesInGiB / pm.rate.perBytes) * pm.price.amount),
currency: pm.price.currency,
}
}

0 comments on commit cced234

Please sign in to comment.