Skip to content

Commit

Permalink
Fix display values to use 6 decimals (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideSilva authored May 21, 2024
1 parent 346b496 commit 1abcd96
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 75 deletions.
38 changes: 21 additions & 17 deletions packages/contracts/test/contracts/token/SaleMaxTargetReached.d.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,45 +57,45 @@ contract SaleMaxTargetReachedTest is Test {
start = endRegistration + 1 * day + 1000;
end = start + 1 * day;

paymentToken = new MockERC20("usdc", "usdc", 18);
paymentToken = new MockERC20("usdc", "usdc", 6);
sale = new Sale(
address(paymentToken),
0.2 ether,
0.2 * 1e6,
start,
end,
2500000 ether,
500000 ether,
1000000 ether,
500000 * 1e6,
1000000 * 1e6,
startRegistration,
endRegistration
);

sale.setMerkleRoot(merkleRoot);
sale.setMinContribution(sale.paymentTokenToToken(100 ether));
sale.setMinContribution(sale.paymentTokenToToken(100 * 1e6));

paymentToken.mint(alice, 2000000 ether);
paymentToken.mint(bob, 2000000 ether);
paymentToken.mint(alice, 2000000 * 1e6);
paymentToken.mint(bob, 2000000 * 1e6);

vm.stopPrank();
}

function test_BuyRevertsWhenMaxTargetReached() public {
vm.startPrank(owner);
sale.setMaxTarget(1 ether);
sale.setMinContribution(1 ether);
sale.setMaxTarget(1 * 1e6);
sale.setMinContribution(1 * 1e6);
vm.stopPrank();

vm.warp(sale.start());

vm.startPrank(alice);
paymentToken.approve(address(sale), 2 ether);
sale.buy(sale.paymentTokenToToken(2 ether), merkleProofs[alice]);
paymentToken.approve(address(sale), 2 * 1e6);
sale.buy(sale.paymentTokenToToken(2 * 1e6), merkleProofs[alice]);
vm.stopPrank();

vm.startPrank(bob);
paymentToken.approve(address(sale), 1 ether);
paymentToken.approve(address(sale), 1 * 1e6);

uint256 amount = sale.paymentTokenToToken(1 ether);
uint256 amount = sale.paymentTokenToToken(1 * 1e6);

vm.expectRevert(Sale.MaxContributorsReached.selector);
sale.buy(amount, merkleProofs[bob]);
Expand All @@ -104,16 +104,20 @@ contract SaleMaxTargetReachedTest is Test {
}

function test_AllocationAfterMaxTargetReached() public {
vm.startPrank(owner);
sale.setMaxTarget(2000000 * 1e6);
sale.setMinContribution(1000000 * 1e6);

vm.warp(sale.start());

vm.startPrank(alice);
paymentToken.approve(address(sale), 2000000 ether);
sale.buy(sale.paymentTokenToToken(2000000 ether), merkleProofs[alice]);
paymentToken.approve(address(sale), 2000000 * 1e6);
sale.buy(sale.paymentTokenToToken(2000000 * 1e6), merkleProofs[alice]);
vm.stopPrank();

vm.startPrank(bob);
paymentToken.approve(address(sale), 2000000 ether);
sale.buy(sale.paymentTokenToToken(2000000 ether), merkleProofs[bob]);
paymentToken.approve(address(sale), 2000000 * 1e6);
sale.buy(sale.paymentTokenToToken(2000000 * 1e6), merkleProofs[bob]);
vm.stopPrank();

vm.warp(sale.end() + 1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,24 @@ contract SaleMinTargetNotReachedTest is Test {
start = endRegistration + 1 * DAY + 1000;
end = start + 1 * DAY;

paymentToken = new MockERC20("USDC", "USDC", 18);
paymentToken = new MockERC20("USDC", "USDC", 6);
sale = new Sale(
address(paymentToken),
0.2 ether,
0.2 * 1e6,
start,
end,
2500000 ether,
500000 ether,
1000000 ether,
500000 * 1e6,
1000000 * 1e6,
startRegistration,
endRegistration
);

sale.setMerkleRoot(merkleRoot);
sale.setMinContribution(sale.paymentTokenToToken(100 ether));
sale.setMinContribution(100 * 1e6);

paymentToken.mint(alice, 300000 ether);
paymentToken.mint(bob, 300000 ether);
paymentToken.mint(alice, 300000 * 1e6);
paymentToken.mint(bob, 300000 * 1e6);

vm.stopPrank();
}
Expand All @@ -85,35 +85,35 @@ contract SaleMinTargetNotReachedTest is Test {
vm.warp(sale.start());

vm.startPrank(alice);
paymentToken.approve(address(sale), 300000 ether);
sale.buy(sale.paymentTokenToToken(300000 ether), merkleProofs[alice]);
paymentToken.approve(address(sale), 300000 * 1e6);
sale.buy(sale.paymentTokenToToken(300000 * 1e6), merkleProofs[alice]);
vm.stopPrank();

vm.startPrank(bob);
paymentToken.approve(address(sale), 200000 ether);
sale.buy(sale.paymentTokenToToken(200000 ether), merkleProofs[bob]);
paymentToken.approve(address(sale), 200000 * 1e6);
sale.buy(sale.paymentTokenToToken(200000 * 1e6), merkleProofs[bob]);
vm.stopPrank();

require(paymentToken.balanceOf(address(sale)) == 500000 ether);
require(paymentToken.balanceOf(address(sale)) == 500000 * 1e6);

vm.startPrank(bob);
paymentToken.approve(address(sale), 100000 ether);
sale.buy(sale.paymentTokenToToken(100000 ether), merkleProofs[bob]);
paymentToken.approve(address(sale), 100000 * 1e6);
sale.buy(sale.paymentTokenToToken(100000 * 1e6), merkleProofs[bob]);
vm.stopPrank();

vm.warp(sale.end() + 1000);

require(
sale.totalUncappedAllocations() ==
sale.paymentTokenToToken(600000 ether)
sale.paymentTokenToToken(600000 * 1e6)
);
require(
sale.allocation(address(alice)) ==
((300000 ether / sale.currentTokenPrice()) * 1 ether)
(((300000 * 1e6) / sale.currentTokenPrice()) * 1 ether)
);
require(
sale.allocation(address(bob)) ==
((300000 ether / sale.currentTokenPrice()) * 1 ether)
(((300000 * 1e6) / sale.currentTokenPrice()) * 1 ether)
);
}

Expand All @@ -123,44 +123,47 @@ contract SaleMinTargetNotReachedTest is Test {
require(bytes32(sale.merkleRoot()) == bytes32(merkleRoot));

vm.startPrank(alice);
paymentToken.approve(address(sale), 200 ether);
sale.buy(sale.paymentTokenToToken(200 ether), merkleProofs[alice]);
paymentToken.approve(address(sale), 200 * 1e6);
sale.buy(sale.paymentTokenToToken(200 * 1e6), merkleProofs[alice]);
vm.stopPrank();

require(paymentToken.balanceOf(address(sale)) == 200 ether);
require(paymentToken.balanceOf(address(sale)) == 200 * 1e6);

vm.startPrank(bob);
paymentToken.approve(address(sale), 100 ether);
sale.buy(sale.paymentTokenToToken(100 ether), merkleProofs[bob]);
paymentToken.approve(address(sale), 100 * 1e6);
sale.buy(sale.paymentTokenToToken(100 * 1e6), merkleProofs[bob]);
vm.stopPrank();

require(
sale.totalUncappedAllocations() ==
sale.paymentTokenToToken(300 ether)
sale.paymentTokenToToken(300 * 1e6)
);
require(
sale.totalUncappedAllocations() <
sale.paymentTokenToToken(sale.minTarget())
);
require(sale.totalUncappedAllocations() < sale.minTarget());

vm.warp(sale.end() + 1000);

require(paymentToken.balanceOf(address(sale)) == 300 ether);
require(paymentToken.balanceOf(address(sale)) == 300 * 1e6);

vm.prank(owner);
sale.setIndividualCap(1000 ether);

require(sale.allocation(alice) == 0);
require(sale.refundAmount(alice) == 200 ether);
require(sale.refundAmount(alice) == 200 * 1e6);

vm.prank(alice);
sale.refund(alice);

require(paymentToken.balanceOf(address(sale)) == 100 ether);
require(paymentToken.balanceOf(address(sale)) == 100 * 1e6);

require(sale.allocation(bob) == 0);
require(sale.refundAmount(bob) == 100 ether);
require(sale.refundAmount(bob) == 100 * 1e6);

vm.prank(bob);
sale.refund(bob);

require(paymentToken.balanceOf(address(sale)) == 0 ether);
require(paymentToken.balanceOf(address(sale)) == 0 * 1e6);
}
}
8 changes: 4 additions & 4 deletions packages/web-app/app/_lib/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
useReadCtzndSaleStart,
useReadCtzndSaleEnd,
} from '@/wagmi.generated';
import { formatEther, parseEther } from 'viem';
import { formatEther, formatUnits, parseUnits } from 'viem';
import { sepolia } from 'viem/chains';

export const useKycCredential = () => {
Expand Down Expand Up @@ -209,7 +209,7 @@ export const useCtzndPaymentTokenAllowance = (userAddress: `0x${string}`) => {

export const useContributeToCtznd = () => {
const [amount, setAmount] = useState(0);
const amountInWei = useMemo(() => parseEther(amount.toString()), [amount]);
const amountInWei = useMemo(() => parseUnits(amount.toString(), 6), [amount]);
const { formattedValue: maxAmount } = usePaymentTokenBalance();
const { data: tokensToBuyInWei, error: tokenError } =
useReadCtzndSalePaymentTokenToToken({
Expand Down Expand Up @@ -272,8 +272,8 @@ export const useCtzndSaleCapStatus = () => {
const { data: minTarget } = useReadCtzndSaleMinTarget();

const investedValue = Number(totalInvested);
const maxValue = maxTarget ? Number(formatEther(maxTarget)) : undefined;
const minValue = minTarget ? Number(formatEther(minTarget)) : undefined;
const maxValue = maxTarget ? Number(formatUnits(maxTarget, 6)) : undefined;
const minValue = minTarget ? Number(formatUnits(minTarget, 6)) : undefined;

if (maxValue === undefined || minValue === undefined) {
return 'loading';
Expand Down
10 changes: 5 additions & 5 deletions packages/web-app/app/_lib/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
useReadCtzndSaleTotalUncappedAllocations,
useReadCtzndSaleUncappedAllocation,
} from '@/wagmi.generated';
import { formatEther, formatUnits } from 'viem';
import { formatEther, formatUnits, parseEther } from 'viem';
import { computeRisingTideCap } from '../_server/risingTide/risingtide';
import { appSignal } from '../app-signal';
import { useEffect } from 'react';
Expand Down Expand Up @@ -289,7 +289,7 @@ export const useTotalInvestedUsdcCtznd = () => {
});

const usdcValue =
ctzndTokensSold && tokensInvested ? formatEther(tokensInvested) : '0';
ctzndTokensSold && tokensInvested ? formatUnits(tokensInvested, 6) : '0';

return usdcValue;
};
Expand All @@ -302,7 +302,7 @@ export const useUserTotalInvestedUsdcCtznd = (address: `0x${string}`) => {
},
});
const { data: tokensInvested } = useReadCtzndSaleTokenToPaymentToken({
args: [ctzndTokensSold || 0n],
args: [parseEther(formatUnits(ctzndTokensSold || 0n, 6)) || 0n],
});

const usdcValue =
Expand All @@ -315,10 +315,10 @@ export const useCtzndMinContributionUsdc = () => {
const { data: min } = useReadCtzndSaleMinContribution();

const { data: minUsdc } = useReadCtzndSaleTokenToPaymentToken({
args: [min || 0n],
args: [parseEther(formatUnits(min || 0n, 6)) || 0n],
});

const usdcValue = min && minUsdc ? formatEther(minUsdc) : '0';
const usdcValue = min && minUsdc ? formatUnits(minUsdc, 6) : '0';

return usdcValue;
};
Expand Down
5 changes: 3 additions & 2 deletions packages/web-app/app/_server/sales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ctzndSaleAbi, ctzndSaleAddress } from '@/wagmi.generated';
import {
createWalletClient,
formatEther,
formatUnits,
getContract,
http,
publicActions,
Expand Down Expand Up @@ -83,8 +84,8 @@ export const saleDetails = async (): Promise<
maxTarget: contractResults[3],
start: contractResults[4] * 1000n,
end: contractResults[5] * 1000n,
minContribution: formatEther(contractResults[6]),
maxContribution: formatEther(contractResults[7]),
minContribution: formatUnits(contractResults[6], 6),
maxContribution: formatUnits(contractResults[7], 6),
totalTokensForSale: formatEther(contractResults[8]),
startRegistration: contractResults[9] * 1000n,
endRegistration: contractResults[10] * 1000n,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { useCtzndPaymentTokenAllowance, useEffectSafe } from '@/app/_lib/hooks';
import { Dialog } from '@headlessui/react';
import { formatEther } from 'viem';
import { formatEther, formatUnits } from 'viem';
import {
useBuyCtzndTokens,
useSetPaymentTokenAllowance,
Expand Down Expand Up @@ -218,7 +218,7 @@ export function ContributeDialog({
<div className="flex justify-between pb-6 text-sm">
<div className="uppercase text-mono-800">Current allowed value:</div>
<div className="text-mono-950">
{allowance ? formatEther(allowance) : 0} USDC
{allowance ? formatUnits(allowance, 6) : 0} USDC
</div>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions packages/web-app/app/_ui/my-projects/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { MyProjectSkeleton } from './my-project';
import { useReadCtzndSaleInvestorCount } from '@/wagmi.generated';
import { useAccount } from 'wagmi';
import { useUserTotalInvestedUsdcCtznd } from '@/app/_lib/queries';
import { formatEther } from 'viem';
import { formatUnits } from 'viem';

const ProjectRow = ({
logo,
Expand All @@ -26,8 +26,8 @@ const ProjectRow = ({
const totalContributions = contributions ? contributions.toString() : 0;
const totalUsdc = useUserTotalInvestedUsdcCtznd(address!);
const targetedRaise = usdRange(
BigInt(formatEther(minTarget)),
BigInt(formatEther(maxTarget)),
BigInt(formatUnits(minTarget, 6)),
BigInt(formatUnits(maxTarget, 6)),
);
return (
<Link
Expand Down
2 changes: 1 addition & 1 deletion packages/web-app/app/_ui/my-projects/my-project.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const Header = ({ project, logo, end }: TProjectSaleDetails) => {
<h3 className="text-sm text-mono-800">Rising Tide Mechanism</h3>
<div>
{saleCapStatus === 'above'
? 'ON (max. target reached'
? 'ON (max. target reached)'
: 'OFF (total contributed below max.)'}
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions packages/web-app/app/_ui/project/contribution/DataFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
useReadCtzndSaleMaxTarget,
useReadCtzndSaleTotalTokensForSale,
} from '@/wagmi.generated';
import { formatEther } from 'viem';
import { formatEther, formatUnits } from 'viem';
import { number } from '../../utils/intl-formaters/number';
import { useCtzndMinContributionUsdc } from '@/app/_lib/queries';
import { useTotalInvestedUsdcCtznd } from '@/app/_lib/queries';
Expand All @@ -14,7 +14,7 @@ const useMaxParticipants = () => {
const { data: maxTarget, isLoading: targetLoading } =
useReadCtzndSaleMaxTarget();
const min = useCtzndMinContributionUsdc();
const targetValue = maxTarget ? Number(formatEther(maxTarget)) : undefined;
const targetValue = maxTarget ? Number(formatUnits(maxTarget, 6)) : undefined;
const minValue = min ? Number(min) : undefined;

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/web-app/app/_ui/project/sale-status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
useReadCtzndSaleMaxTarget,
} from '@/wagmi.generated';
import { usdValue } from '../utils/intl-formaters/usd-value';
import { formatEther } from 'viem';
import { formatEther, formatUnits } from 'viem';
import clsx from 'clsx';
import { number } from '../utils/intl-formaters/number';
import { useTotalInvestedUsdcCtznd } from '@/app/_lib/queries';
Expand Down Expand Up @@ -186,7 +186,7 @@ const Info = () => {
export const SaleStatus = ({ hasGrant }: { hasGrant: boolean }) => {
const { data: maxTarget } = useReadCtzndSaleMaxTarget();
const totalCommitted = useTotalInvestedUsdcCtznd();
const maxValue = maxTarget ? Number(formatEther(maxTarget)) : 0;
const maxValue = maxTarget ? Number(formatUnits(maxTarget, 6)) : 0;
const { data: investorCount } = useReadCtzndSaleInvestorCount({
query: {
refetchInterval: 1000 * 10, // 10 seconds
Expand Down
Loading

0 comments on commit 1abcd96

Please sign in to comment.