From b65ad1057c69d4de9988a486250774b8e42df8bd Mon Sep 17 00:00:00 2001 From: silverpill Date: Sun, 18 Sep 2022 12:38:41 +0000 Subject: [PATCH] Refactor monero subscription component Using computed properties and atomic units. --- src/api/subscriptions-common.ts | 27 ++++++++++++++ src/api/subscriptions-ethereum.ts | 31 +++------------- src/api/subscriptions-monero.ts | 26 +++++++++++-- src/components/SubscriptionMonero.vue | 37 +++++++++++-------- .../SubscriptionSettingsEthereum.vue | 2 +- src/components/SubscriptionSettingsMonero.vue | 1 + 6 files changed, 78 insertions(+), 46 deletions(-) diff --git a/src/api/subscriptions-common.ts b/src/api/subscriptions-common.ts index b9babcd..7a8e15c 100644 --- a/src/api/subscriptions-common.ts +++ b/src/api/subscriptions-common.ts @@ -1,7 +1,34 @@ +import { BigNumber, FixedNumber } from "@ethersproject/bignumber" + import { BACKEND_URL } from "@/constants" +import { floatToBigNumber, roundBigNumber } from "@/utils/numbers" import { http } from "./common" import { Profile, User } from "./users" +const SECONDS_IN_DAY = 3600 * 24 +const SECONDS_IN_MONTH = SECONDS_IN_DAY * 30 + +export function getPricePerSec( + pricePerMonth: number, + tokenDecimals: number, +): BigNumber { + const pricePerMonthInt = floatToBigNumber(pricePerMonth, tokenDecimals) + return pricePerMonthInt.div(SECONDS_IN_MONTH) +} + +export function getPricePerMonth( + pricePerSec: BigNumber, +): BigNumber { + return roundBigNumber(pricePerSec.mul(SECONDS_IN_MONTH), 4) +} + +export function formatAmount( + value: BigNumber, + tokenDecimals: number, +): FixedNumber { + return FixedNumber.fromValue(value, tokenDecimals) +} + export interface SubscriptionOption { type: string; price: number | null; diff --git a/src/api/subscriptions-ethereum.ts b/src/api/subscriptions-ethereum.ts index c268b9c..a5cde65 100644 --- a/src/api/subscriptions-ethereum.ts +++ b/src/api/subscriptions-ethereum.ts @@ -7,12 +7,14 @@ import { ethereumAddressMatch, EthereumSignature } from "@/utils/ethereum" import { floatToBigNumber, roundBigNumber } from "@/utils/numbers" import { http } from "./common" import { Contracts, getContract } from "./contracts" -import { registerSubscriptionOption } from "./subscriptions-common" +import { + formatAmount, + getPricePerMonth, + getPricePerSec, + registerSubscriptionOption, +} from "./subscriptions-common" import { Profile, User } from "./users" -const SECONDS_IN_DAY = 3600 * 24 -const SECONDS_IN_MONTH = SECONDS_IN_DAY * 30 - export interface SubscriptionToken { address: string; symbol: string; @@ -33,27 +35,6 @@ export async function getSubscriptionToken( } } -export function getPricePerSec( - pricePerMonth: number, - tokenDecimals: number, -): BigNumber { - const pricePerMonthInt = floatToBigNumber(pricePerMonth, tokenDecimals) - return pricePerMonthInt.div(SECONDS_IN_MONTH) -} - -export function getPricePerMonth( - pricePerSec: BigNumber, -): BigNumber { - return roundBigNumber(pricePerSec.mul(SECONDS_IN_MONTH), 4) -} - -export function formatAmount( - value: BigNumber, - tokenDecimals: number, -): FixedNumber { - return FixedNumber.fromValue(value, tokenDecimals) -} - export async function getSubscriptionAuthorization( authToken: string, pricePerSec: BigNumber, diff --git a/src/api/subscriptions-monero.ts b/src/api/subscriptions-monero.ts index 360f994..747c2c0 100644 --- a/src/api/subscriptions-monero.ts +++ b/src/api/subscriptions-monero.ts @@ -2,22 +2,40 @@ import { BigNumber } from "@ethersproject/bignumber" import { BACKEND_URL } from "@/constants" import { http } from "./common" -import { registerSubscriptionOption } from "./subscriptions-common" import { formatAmount, getPricePerMonth as _getPricePerMonth, getPricePerSec as _getPricePerSec, -} from "./subscriptions-ethereum" + registerSubscriptionOption, +} from "./subscriptions-common" import { Profile, User } from "./users" +const MONERO_DECIMALS = 12 + +export function formatXmrAmount(value: number | BigNumber): number { + if (typeof value === "number") { + value = BigNumber.from(value) + } + return formatAmount(value, MONERO_DECIMALS).toUnsafeFloat() +} + export function getPricePerSec(pricePerMonth: number): number { - return _getPricePerSec(pricePerMonth, 12).toNumber() + return _getPricePerSec(pricePerMonth, MONERO_DECIMALS).toNumber() } export function getPricePerMonth(pricePerSec: number): number { const pricePerSecInt = BigNumber.from(pricePerSec) const pricePerMonthInt = _getPricePerMonth(pricePerSecInt) - return formatAmount(pricePerMonthInt, 12).toUnsafeFloat() + return formatXmrAmount(pricePerMonthInt) +} + +export function getPaymentAmount( + pricePerSec: number, + durationMonths: number, +): number { + const pricePerSecInt = BigNumber.from(pricePerSec) + const pricePerMonthInt = _getPricePerMonth(pricePerSecInt) + return Math.round(pricePerMonthInt.toNumber() * durationMonths) } export async function registerMoneroSubscriptionOption( diff --git a/src/components/SubscriptionMonero.vue b/src/components/SubscriptionMonero.vue index 71977e9..fba4299 100644 --- a/src/components/SubscriptionMonero.vue +++ b/src/components/SubscriptionMonero.vue @@ -63,7 +63,7 @@
-
{{ getPaymentAmount() }} XMR
+
{{ formatXmrAmount(paymentAmount) }} XMR