Skip to content

Commit

Permalink
Merge pull request #1634 from ArcAnya/parseUnits
Browse files Browse the repository at this point in the history
Spread ethers.utils.parseUnits to all relevant places
  • Loading branch information
ArcAnya authored May 9, 2023
2 parents 6a1ec1d + 57071d6 commit c01eddf
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
4 changes: 3 additions & 1 deletion components/FundBounty/FundPage/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] = isNaN(e.target.value) || e.target.value == '' ? 0 : e.target.value;
setFixedTierVolumes(newVolumes);
setFinalTierVolumes(newVolumes);
}
Expand Down
4 changes: 3 additions & 1 deletion components/Stream/CreateStream.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 3 additions & 1 deletion lib/batchUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const convertPayoutScheduleToBigInt = (payoutSchedule, decimals) => {
const payoutScheduleParsed = payoutSchedule && JSON.parse(payoutSchedule);

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 }));
});

Expand Down
39 changes: 33 additions & 6 deletions services/ethers/OpenQClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,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 })
);
Expand Down Expand Up @@ -113,7 +118,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,
Expand Down Expand Up @@ -173,7 +183,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,
Expand Down Expand Up @@ -221,7 +236,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 })
);
Expand All @@ -242,7 +262,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 })
);
Expand Down Expand Up @@ -280,7 +305,9 @@ class OpenQClient {

setPayoutScheduleFixed = async (library, _bountyId, _payoutSchedule, _payoutToken) => {
const tierVolumesInWei = _payoutSchedule.map((tier) => {
const payoutVolumeInWei = tier * 10 ** _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,
Expand Down
6 changes: 4 additions & 2 deletions services/utils/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ export const valueToDisplay = (value) => {
};

export const getBigNumberVol = (volume, token) => {
const volumeFloat = parseFloat(volume) || 0;
const volumeInWei = volumeFloat * 10 ** token.decimals;
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 }));
};
Expand Down

0 comments on commit c01eddf

Please sign in to comment.