From 82d42d8bb97cb2f11baccc52df4a02c77c42c418 Mon Sep 17 00:00:00 2001 From: Anya <75732239+ArcAnya@users.noreply.github.com> Date: Fri, 10 Mar 2023 08:38:51 +0000 Subject: [PATCH 1/4] solving the pb with big numbers when decimal tiers --- services/ethers/OpenQClient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/ethers/OpenQClient.js b/services/ethers/OpenQClient.js index 0416a844d..a0a3c21c7 100755 --- a/services/ethers/OpenQClient.js +++ b/services/ethers/OpenQClient.js @@ -282,7 +282,7 @@ class OpenQClient { setPayoutScheduleFixed = async (library, _bountyId, _payoutSchedule, _payoutToken) => { const tierVolumesInWei = _payoutSchedule.map((tier) => { - const payoutVolumeInWei = tier * 10 ** _payoutToken.decimals; + const payoutVolumeInWei = ethers.utils.parseUnits(tier.toString(), _payoutToken.decimals); const payoutBigNumberVolumeInWei = ethers.BigNumber.from( payoutVolumeInWei.toLocaleString('fullwide', { useGrouping: false, From c985aa47183b3007848576844c8d2747a121eacc Mon Sep 17 00:00:00 2001 From: Anya <75732239+ArcAnya@users.noreply.github.com> Date: Fri, 10 Mar 2023 11:52:22 +0000 Subject: [PATCH 2/4] spread change regarding the decimals treatment with parseUnit --- components/FundBounty/FundPage/index.js | 4 ++- components/Stream/CreateStream.js | 4 ++- pages/batch.js | 4 ++- services/ethers/OpenQClient.js | 39 +++++++++++++++++++++---- services/utils/lib.js | 6 ++-- 5 files changed, 46 insertions(+), 11 deletions(-) diff --git a/components/FundBounty/FundPage/index.js b/components/FundBounty/FundPage/index.js index 12f794ecd..96977ed7a 100755 --- a/components/FundBounty/FundPage/index.js +++ b/components/FundBounty/FundPage/index.js @@ -112,7 +112,9 @@ const FundPage = () => { // Methods const hasEnoughAllowance = async () => { const allowanceBigNumber = await openQClient.allowance(library, account, token.address, bounty.bountyAddress); - const volumeInWei = volume * 10 ** token.decimals; + const volumeInWei = volume + ? ethers.utils.parseUnits(volume.toLocaleString('fullwide', { useGrouping: false }), token.decimals) + : 0; const bigNumberVolumeInWei = ethers.BigNumber.from(volumeInWei.toLocaleString('fullwide', { useGrouping: false })); const allowanceValue = allowanceBigNumber > 0 diff --git a/components/Stream/CreateStream.js b/components/Stream/CreateStream.js index f6708fa2b..058395a41 100755 --- a/components/Stream/CreateStream.js +++ b/components/Stream/CreateStream.js @@ -45,7 +45,9 @@ const CreateStream = () => { } } async function approveToken(volume, recipient, flowRate, type, token) { - const volumeInWei = volume * 10 ** token.decimals; + const volumeInWei = volume + ? ethers.utils.parseUnits(volume.toLocaleString('fullwide', { useGrouping: false }), token.decimals) + : 0; const bigNumberVolumeInWei = ethers.BigNumber.from(volumeInWei.toLocaleString('fullwide', { useGrouping: false })); const callerBalance = await openQClient.balanceOf(library, account, ethers.utils.getAddress(token.address)); if (callerBalance.lt(bigNumberVolumeInWei)) { diff --git a/pages/batch.js b/pages/batch.js index e535457a1..a524701d3 100644 --- a/pages/batch.js +++ b/pages/batch.js @@ -78,7 +78,9 @@ function Batch() { let decimals = parseInt(token.decimals) || 18; const newPayoutSchedule = payoutScheduleParsed.map((tierVolume) => { - let formattedVolume = tierVolume * 10 ** decimals; + let formattedVolume = tierVolume + ? ethers.utils.parseUnits(tierVolume.toLocaleString('fullwide', { useGrouping: false }), decimals) + : 0; return ethers.BigNumber.from(formattedVolume.toLocaleString('fullwide', { useGrouping: false })); }); diff --git a/services/ethers/OpenQClient.js b/services/ethers/OpenQClient.js index a0a3c21c7..ec105e728 100755 --- a/services/ethers/OpenQClient.js +++ b/services/ethers/OpenQClient.js @@ -86,7 +86,12 @@ class OpenQClient { const promise = new Promise(async (resolve, reject) => { let bountyInitOperation; let abiCoder = new ethers.utils.AbiCoder(); - const fundVolumeInWei = data.fundingTokenVolume * 10 ** data.fundingTokenAddress.decimals; + const fundVolumeInWei = data.fundingTokenVolume + ? ethers.utils.parseUnits( + data.fundingTokenVolume.toLocaleString('fullwide', { useGrouping: false }), + data.fundingTokenAddress.decimals + ) + : 0; const fundBigNumberVolumeInWei = ethers.BigNumber.from( fundVolumeInWei.toLocaleString('fullwide', { useGrouping: false }) ); @@ -115,7 +120,12 @@ class OpenQClient { break; case 1: { - const payoutVolumeInWei = data.payoutVolume * 10 ** data.payoutToken.decimals; + const payoutVolumeInWei = data.payoutVolume + ? ethers.utils.parseUnits( + data.payoutVolume.toLocaleString('fullwide', { useGrouping: false }), + data.payoutToken.decimals + ) + : 0; const payoutBigNumberVolumeInWei = ethers.BigNumber.from( payoutVolumeInWei.toLocaleString('fullwide', { useGrouping: false, @@ -175,7 +185,12 @@ class OpenQClient { case 3: { const tierVolumes = data.tiers.map((tier) => { - const payoutVolumeInWei = tier * 10 ** data.payoutToken.decimals; + const payoutVolumeInWei = tier + ? ethers.utils.parseUnits( + tier.toLocaleString('fullwide', { useGrouping: false }), + data.payoutToken.decimals + ) + : 0; const payoutBigNumberVolumeInWei = ethers.BigNumber.from( payoutVolumeInWei.toLocaleString('fullwide', { useGrouping: false, @@ -223,7 +238,12 @@ class OpenQClient { // setFunding inspired by fundBounty setFundingGoal = async (library, _bountyId, _fundingGoalToken, _fundingGoalVolume) => { const promise = new Promise(async (resolve, reject) => { - const volumeInWei = _fundingGoalVolume * 10 ** _fundingGoalToken.decimals; + const volumeInWei = _fundingGoalVolume + ? ethers.utils.parseUnits( + _fundingGoalVolume.toLocaleString('fullwide', { useGrouping: false }), + _fundingGoalToken.decimals + ) + : 0; const bigNumberVolumeInWei = ethers.BigNumber.from( volumeInWei.toLocaleString('fullwide', { useGrouping: false }) ); @@ -244,7 +264,12 @@ class OpenQClient { setPayout = async (library, _bountyId, _payoutToken, _payoutVolume) => { const promise = new Promise(async (resolve, reject) => { - const volumeInWei = _payoutVolume * 10 ** _payoutToken.decimals; + const volumeInWei = _payoutVolume + ? ethers.utils.parseUnits( + _payoutVolume.toLocaleString('fullwide', { useGrouping: false }), + _payoutToken.decimals + ) + : 0; const bigNumberVolumeInWei = ethers.BigNumber.from( volumeInWei.toLocaleString('fullwide', { useGrouping: false }) ); @@ -282,7 +307,9 @@ class OpenQClient { setPayoutScheduleFixed = async (library, _bountyId, _payoutSchedule, _payoutToken) => { const tierVolumesInWei = _payoutSchedule.map((tier) => { - const payoutVolumeInWei = ethers.utils.parseUnits(tier.toString(), _payoutToken.decimals); + const payoutVolumeInWei = tier + ? ethers.utils.parseUnits(tier.toLocaleString('fullwide', { useGrouping: false }), _payoutToken.decimals) + : 0; const payoutBigNumberVolumeInWei = ethers.BigNumber.from( payoutVolumeInWei.toLocaleString('fullwide', { useGrouping: false, diff --git a/services/utils/lib.js b/services/utils/lib.js index 65af2a00a..0cc680ac9 100644 --- a/services/utils/lib.js +++ b/services/utils/lib.js @@ -68,7 +68,9 @@ export const valueToDisplay = (value) => { export const getBigNumberVol = (volume, token) => { const volumeFloat = parseFloat(volume) || 0; - const volumeInWei = volumeFloat * 10 ** token.decimals; + const volumeInWei = volumeFloat + ? ethers.utils.parseUnits(volumeFloat.toLocaleString('fullwide', { useGrouping: false }), token.decimals) + : 0; return ethers.BigNumber.from(volumeInWei.toLocaleString('fullwide', { useGrouping: false })); }; @@ -218,7 +220,7 @@ export const isEveryValueNotNull = (obj) => { return kyc && w8Form && githubHasWallet && invoice; }; export const formatVolume = (tierVolume, token) => { - let bigNumberVolume = ethers.BigNumber.from(tierVolume.toString()); + let bigNumberVolume = ethers.BigNumber.from(tierVolume.toLocaleString('fullwide', { useGrouping: false })); let decimals = parseInt(token?.decimals) || 18; let formattedVolume = ethers.utils.formatUnits(bigNumberVolume, decimals); return formattedVolume; From 399358099af7169bd73dd370a1f9d2d635cdb9bb Mon Sep 17 00:00:00 2001 From: Anya <75732239+ArcAnya@users.noreply.github.com> Date: Mon, 3 Apr 2023 09:10:33 +0000 Subject: [PATCH 3/4] fixing issue on very small numbers due to parseFloat --- .../MintBountyModal/AddContestParams/SetTierValues/index.js | 2 +- services/utils/lib.js | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/components/MintBounty/MintBountyModal/AddContestParams/SetTierValues/index.js b/components/MintBounty/MintBountyModal/AddContestParams/SetTierValues/index.js index f3b6dbad7..b0b23f06d 100755 --- a/components/MintBounty/MintBountyModal/AddContestParams/SetTierValues/index.js +++ b/components/MintBounty/MintBountyModal/AddContestParams/SetTierValues/index.js @@ -33,7 +33,7 @@ const SetTierValues = ({ tierArr, initialVolumes, finalTierVolumes, setFinalTier function onFixedTierChange(e) { const newVolumes = [...finalTierVolumes]; - newVolumes[e.name] = parseFloat(e.target.value); + newVolumes[e.name] = e.target.value; setFixedTierVolumes(newVolumes); setFinalTierVolumes(newVolumes); } diff --git a/services/utils/lib.js b/services/utils/lib.js index ebf599320..3fdf9f853 100644 --- a/services/utils/lib.js +++ b/services/utils/lib.js @@ -76,9 +76,8 @@ export const valueToDisplay = (value) => { }; export const getBigNumberVol = (volume, token) => { - const volumeFloat = parseFloat(volume) || 0; - const volumeInWei = volumeFloat - ? ethers.utils.parseUnits(volumeFloat.toLocaleString('fullwide', { useGrouping: false }), token.decimals) + const volumeInWei = volume + ? ethers.utils.parseUnits(volume.toLocaleString('fullwide', { useGrouping: false }), token.decimals) : 0; return ethers.BigNumber.from(volumeInWei.toLocaleString('fullwide', { useGrouping: false })); From 5d343b6dde00039889c317677ddce87dd87acad9 Mon Sep 17 00:00:00 2001 From: Anya <75732239+ArcAnya@users.noreply.github.com> Date: Mon, 3 Apr 2023 11:59:52 +0000 Subject: [PATCH 4/4] accounting for NaN & empty --- .../AdminPage/SetTierAdminPage/SetTierAdminPage.test.js | 2 +- .../AddContestParams/SetTierValues/index.js | 2 +- services/utils/lib.js | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/components/AdminPage/SetTierAdminPage/SetTierAdminPage.test.js b/components/AdminPage/SetTierAdminPage/SetTierAdminPage.test.js index 0c203d038..e4d741d0b 100644 --- a/components/AdminPage/SetTierAdminPage/SetTierAdminPage.test.js +++ b/components/AdminPage/SetTierAdminPage/SetTierAdminPage.test.js @@ -73,6 +73,6 @@ describe('SetTierAdminPage', () => { expect(await screen.findByText(/^50 Matic/i)).toBeInTheDocument(); expect(await screen.findByText(/^30 Matic/i)).toBeInTheDocument(); expect(await screen.findByText(/^20 Matic/i)).toBeInTheDocument(); - expect(setTier).toBeCalledWith(bounty.bountyId, [50, 30, 20], Constants.maticAddress); + expect(setTier).toBeCalledWith(bounty.bountyId, ['50', '30', '20'], Constants.maticAddress); }); }); diff --git a/components/MintBounty/MintBountyModal/AddContestParams/SetTierValues/index.js b/components/MintBounty/MintBountyModal/AddContestParams/SetTierValues/index.js index b0b23f06d..5b4b9c2bf 100755 --- a/components/MintBounty/MintBountyModal/AddContestParams/SetTierValues/index.js +++ b/components/MintBounty/MintBountyModal/AddContestParams/SetTierValues/index.js @@ -33,7 +33,7 @@ const SetTierValues = ({ tierArr, initialVolumes, finalTierVolumes, setFinalTier function onFixedTierChange(e) { const newVolumes = [...finalTierVolumes]; - newVolumes[e.name] = e.target.value; + newVolumes[e.name] = isNaN(e.target.value) || e.target.value == '' ? 0 : e.target.value; setFixedTierVolumes(newVolumes); setFinalTierVolumes(newVolumes); } diff --git a/services/utils/lib.js b/services/utils/lib.js index 3fdf9f853..755d61de7 100644 --- a/services/utils/lib.js +++ b/services/utils/lib.js @@ -76,9 +76,10 @@ export const valueToDisplay = (value) => { }; export const getBigNumberVol = (volume, token) => { - const volumeInWei = volume - ? ethers.utils.parseUnits(volume.toLocaleString('fullwide', { useGrouping: false }), token.decimals) - : 0; + const volumeInWei = + isNaN(volume) || volume === '' + ? 0 + : ethers.utils.parseUnits(volume.toLocaleString('fullwide', { useGrouping: false }), token.decimals); return ethers.BigNumber.from(volumeInWei.toLocaleString('fullwide', { useGrouping: false })); };