From a2f8b90cdda00a753f2a62ab7fb5c36db5451809 Mon Sep 17 00:00:00 2001 From: butterscotch Date: Thu, 19 Oct 2023 16:23:50 +0300 Subject: [PATCH] comments: proposal how to format output to show fraction. --- src/helper/helper.js | 9 +++++++++ src/stores/sacrifice.js | 11 ++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/helper/helper.js b/src/helper/helper.js index 755e507..db7af15 100644 --- a/src/helper/helper.js +++ b/src/helper/helper.js @@ -29,6 +29,15 @@ export function formatNumber(number) { return formattedWithOptions; } +// input type bigint +export function formatBigIntDivision(nominator, denominator) { + const integer = (nominator / denominator).toString(); + // pick how much digits after point to show + const fraction = (nominator % denominator).toString().substring(0,10); + // output type string + return integer + '.' + fraction; +} + export function getAddressShortName(address) { // const address = "0x66422602B2f4885c57EE794174f98d9E4DDBBBC6"; const addressBegin = address.substring(0, 6); diff --git a/src/stores/sacrifice.js b/src/stores/sacrifice.js index 7ff0d0e..f574fe1 100644 --- a/src/stores/sacrifice.js +++ b/src/stores/sacrifice.js @@ -9,7 +9,7 @@ import { useRewardLockerStore } from "@/stores/rewardlocker"; import { useTokenStore } from "@/stores/tokens"; import { waitForTransaction, readContract, watchBlockNumber, multicall } from "@wagmi/core"; import { showSuccessToast } from "@/helper/toast.js"; -import { formatNumber } from "@/helper/helper.js"; +import { formatNumber, formatBigIntDivision } from "@/helper/helper.js"; export const useSacrificeStore = defineStore("sacrifice", { state: () => ({ @@ -64,16 +64,17 @@ export const useSacrificeStore = defineStore("sacrifice", { }); console.log("rewardData", rewardData); //output - const outputSacrifice = rewardData.reduce((x, y) => { - // CHECK: when you divide like this you are losing remainder, let's better work on formatShowNumber(BigInt: number) function - return x + BigInt(y.data) / 10n ** token.decimals; + let outputSacrifice = rewardData.reduce((x, y) => { + return x + BigInt(y.data); }, 0n); + outputSacrifice = formatBigIntDivision(outputSacrifice, 10n ** token.decimals); + console.log("outputSacrifice", outputSacrifice); token.allowance = await tokenStore.allowance(); this.sacrificeState = 4; showSuccessToast( - formatNumber(outputSacrifice), + outputSacrifice, "Success!", "You turned your meme into:", "You can claim once the current cycle is finished and your clan bonus was calculated."