Skip to content

Commit

Permalink
fix/cbBTC one donation
Browse files Browse the repository at this point in the history
  • Loading branch information
kkatusic committed Feb 3, 2025
1 parent 9de7dcb commit be02a7c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
15 changes: 9 additions & 6 deletions src/components/views/donate/OneTime/DonateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ import { ChainType } from '@/types/config';
import { IProject, IWalletAddress } from '@/apollo/types/types';
import { useCreateSolanaDonation } from '@/hooks/useCreateSolanaDonation';
import { useTokenPrice } from '@/hooks/useTokenPrice';
import { calcDonationShare } from '@/components/views/donate/common/helpers';
import {
calcDonationShare,
calcDonationShareFor8Decimals,
} from '@/components/views/donate/common/helpers';
import { Spinner } from '@/components/Spinner';
import { FETCH_GIVETH_PROJECT_BY_ID } from '@/apollo/gql/gqlProjects';
import createGoogleTagEventPurchase from '@/helpers/googleAnalytics';
Expand Down Expand Up @@ -155,11 +158,11 @@ const DonateModal: FC<IDonateModalProps> = props => {
);
};

const { projectDonation, givethDonation } = calcDonationShare(
amount,
donationToGiveth,
token.decimals,
);
const { projectDonation, givethDonation } =
token.decimals === 8
? calcDonationShareFor8Decimals(amount, donationToGiveth)
: calcDonationShare(amount, donationToGiveth, token.decimals);

const projectDonationPrice = tokenPrice && tokenPrice * projectDonation;
const givethDonationPrice = tokenPrice && givethDonation * tokenPrice;

Expand Down
36 changes: 24 additions & 12 deletions src/components/views/donate/OneTime/OneTimeDonationCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { truncateToDecimalPlaces } from '@/lib/helpers';
import { IProjectAcceptedToken } from '@/apollo/types/gqlTypes';
import {
calcDonationShare,
calcDonationShareFor8Decimals,
prepareTokenList,
} from '@/components/views/donate/common/helpers';
import { DonateWrongNetwork } from '@/components/modals/DonateWrongNetwork';
Expand Down Expand Up @@ -346,11 +347,14 @@ const CryptoDonation: FC<{
const {
givethDonation: givethDonationAmount,
projectDonation: projectDonationAmount,
} = calcDonationShare(
amount,
donationToGiveth,
selectedOneTimeToken?.decimals ?? 18,
);
} =
selectedOneTimeToken?.decimals === 8
? calcDonationShareFor8Decimals(amount, donationToGiveth)
: calcDonationShare(
amount,
donationToGiveth,
selectedOneTimeToken?.decimals ?? 18,
);

const decimals = selectedOneTimeToken?.decimals || 18;
const donationUsdValue =
Expand Down Expand Up @@ -507,13 +511,21 @@ const CryptoDonation: FC<{
})}
:{' '}
{selectedOneTimeToken
? truncateToDecimalPlaces(
formatUnits(
selectedTokenBalance,
tokenDecimals,
),
tokenDecimals / 3,
)
? tokenDecimals === 8
? truncateToDecimalPlaces(
formatUnits(
selectedTokenBalance,
tokenDecimals,
),
18 / 3,
)
: truncateToDecimalPlaces(
formatUnits(
selectedTokenBalance,
tokenDecimals,
),
tokenDecimals / 3,
)
: 0.0}
</GLinkStyled>
<IconWrapper
Expand Down
19 changes: 18 additions & 1 deletion src/components/views/donate/common/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parseUnits } from 'viem';
import { IProjectAcceptedToken } from '@/apollo/types/gqlTypes';
import { MAX_TOKEN_ORDER } from '@/lib/constants/tokens';
import { EDonationFailedType } from '@/components/modals/FailedDonation';
import { formatCrypto } from '@/helpers/number';
import { formatCrypto, formatCryptoFor8Decimals } from '@/helpers/number';

export const prepareTokenList = (tokens: IProjectAcceptedToken[]) => {
const _tokens = [...tokens];
Expand Down Expand Up @@ -66,3 +66,20 @@ export const calcDonationShare = (
givethDonation: formatCrypto(givethDonation, decimals),
};
};

export const calcDonationShareFor8Decimals = (
totalDonation: bigint,
givethDonationPercent: number,
): {
projectDonation: number;
givethDonation: number;
} => {
let givethDonation = (totalDonation * BigInt(givethDonationPercent)) / 100n;

let projectDonation = totalDonation - givethDonation;

return {
projectDonation: formatCryptoFor8Decimals(projectDonation),
givethDonation: formatCryptoFor8Decimals(givethDonation),
};
};
4 changes: 4 additions & 0 deletions src/helpers/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export const formatCrypto = (amount: bigint, decimals: number) => {
return truncateToDecimalPlaces(formatUnits(amount, decimals), decimals / 3);
};

export const formatCryptoFor8Decimals = (amount: bigint) => {
return truncateToDecimalPlaces(formatUnits(amount, 8), 6);
};

export const formatEthHelper = (
amount: BigNumber.Value,
decimals: number = config.TOKEN_PRECISION,
Expand Down

0 comments on commit be02a7c

Please sign in to comment.