Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix recurring donations #4876

Merged
merged 7 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/components/views/donate/Recurring/RecurringDonationCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,26 @@ export const RecurringDonationCard = () => {
if (selectedRecurringToken.token.isSuperToken) {
setAmount(balance.value || 0n);
}
if (selectedRecurringToken.token.decimals === 6) {
setAmount(0n);
setPerMonthAmount(0n);
}
}, [selectedRecurringToken, balance]);

const underlyingToken = selectedRecurringToken?.token.underlyingToken;

// Introduce a scaling factor to handle tokens with different decimals
const scaleFactor =
selectedRecurringToken?.token.decimals === 6 ? 10000n : 1n;

// total means project + giveth
const totalPerSec = perMonthAmount / ONE_MONTH_SECONDS;
const totalPerSec = perMonthAmount / (ONE_MONTH_SECONDS / scaleFactor);
const projectPerMonth =
(perMonthAmount * BigInt(100 - donationToGiveth)) / 100n;
const givethPerMonth = perMonthAmount - projectPerMonth;
const tokenBalance = balance?.value;
const tokenStream = tokenStreams[selectedRecurringToken?.token.id || ''];
const tokenStream =
tokenStreams[selectedRecurringToken?.token.id.toLowerCase() || ''];

const anchorContractAddress = useMemo(
() => findAnchorContractAddress(project.anchorContracts),
Expand All @@ -168,7 +177,8 @@ export const RecurringDonationCard = () => {
0n,
) || 0n;
const totalStreamPerSec = totalPerSec + otherStreamsPerSec;
const totalStreamPerMonth = totalStreamPerSec * ONE_MONTH_SECONDS;
const totalStreamPerMonth =
totalStreamPerSec * (ONE_MONTH_SECONDS / scaleFactor);
const streamRunOutInMonth =
totalStreamPerSec > 0 ? amount / totalStreamPerMonth : 0n;
const isTotalStreamExceed =
Expand Down Expand Up @@ -767,7 +777,9 @@ export const RecurringDonationCard = () => {
{showTopUpModal && selectedRecurringToken && (
<ModifySuperTokenModal
tokenStreams={
tokenStreams[selectedRecurringToken?.token.id || '']
tokenStreams[
selectedRecurringToken?.token.id.toLowerCase() || ''
]
}
setShowModal={setShowTopUpModal}
selectedToken={selectedRecurringToken?.token!}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export const Item: FC<IItemProps> = ({
{limitFraction(
formatUnits(
amount,
token.underlyingToken?.decimals || 18,
token.underlyingToken?.decimals ||
token.decimals ||
18,
),
)}
&nbsp;{token.symbol}
Expand All @@ -47,7 +49,9 @@ export const Item: FC<IItemProps> = ({
.multipliedBy(amount.toString())
.toFixed(0),
),
token.underlyingToken?.decimals || 18,
token.underlyingToken?.decimals ||
token.decimals ||
18,
),
2,
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Framework, type Operation } from '@superfluid-finance/sdk-core';
import { useAccount } from 'wagmi';
import { useIntl } from 'react-intl';
import { formatUnits } from 'viem';
import { ethers } from 'ethers';
import { Modal } from '@/components/modals/Modal';
import { useModalAnimation } from '@/hooks/useModalAnimation';
import { IModal } from '@/types/common';
Expand Down Expand Up @@ -227,10 +228,32 @@ const RecurringDonationInnerModal: FC<IRecurringDonationInnerModalProps> = ({

const operations: Operation[] = [];

let newAmount = amount;
let newPerMonthAmount = perMonthAmount;

// This is a special case with tokens that have 6 decimals
// We need to convert the amount to 18 decimals for the upgrade operation
// And also for the flow rate calculation
if (selectedRecurringToken.token.decimals === 6) {
const divisor = BigInt(
10 ** selectedRecurringToken.token.decimals,
);
const currentAmount = Number(amount) / Number(divisor);
newAmount = ethers.utils
.parseUnits(currentAmount.toString(), 18)
.toBigInt();

const currentPerMonth =
Number(perMonthAmount) / Number(divisor);
newPerMonthAmount = ethers.utils
.parseUnits(currentPerMonth.toString(), 18)
.toBigInt();
}
kkatusic marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +241 to +251
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here


// Upgrade the token to super token
if (!isUpdating && !selectedRecurringToken.token.isSuperToken) {
const upgradeOperation = await superToken.upgrade({
amount: amount.toString(),
amount: newAmount.toString(),
});

//Upgrading ETHx is a special case and can't be batched
Expand All @@ -245,8 +268,8 @@ const RecurringDonationInnerModal: FC<IRecurringDonationInnerModalProps> = ({
throw new Error('Project wallet address not found');
}

const _flowRate =
(perMonthAmount * BigInt(100 - donationToGiveth)) /
let _flowRate =
(newPerMonthAmount * BigInt(100 - donationToGiveth)) /
100n /
ONE_MONTH_SECONDS;

Expand Down Expand Up @@ -279,7 +302,7 @@ const RecurringDonationInnerModal: FC<IRecurringDonationInnerModalProps> = ({
}

const _newFlowRate =
(perMonthAmount * BigInt(donationToGiveth)) /
(newPerMonthAmount * BigInt(donationToGiveth)) /
100n /
ONE_MONTH_SECONDS;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const SelectTokenInnerModal: FC<ISelectTokenModalProps> = ({
setShowModal,
}) => {
const [tokens, setTokens] = useState<ISuperToken[]>([]);
const [underlyingTokens, setUnderlyingTokens] = useState<ISuperToken[]>([]);
kkatusic marked this conversation as resolved.
Show resolved Hide resolved
const [balances, setBalances] = useState<IBalances>({});

const { formatMessage } = useIntl();
Expand Down Expand Up @@ -84,11 +85,12 @@ const SelectTokenInnerModal: FC<ISelectTokenModalProps> = ({
return acc;
}, {} as IBalances);

const filteredTokens = superTokens.filter(
token => !(newBalances[token.symbol] > 0n),
);
const filteredTokens = superTokens.filter(token => {
return !(newBalances[token.underlyingToken.symbol] > 0n);
});

setTokens(filteredTokens);
setUnderlyingTokens(superTokens);
kkatusic marked this conversation as resolved.
Show resolved Hide resolved

// Update the state with the new balances
setBalances(newBalances);
Expand Down Expand Up @@ -127,12 +129,14 @@ const SelectTokenInnerModal: FC<ISelectTokenModalProps> = ({
</TitleSubheader>
{Object.keys(tokenStreams).map(tokenId => {
const token = superTokens.find(
token => token.id === tokenId,
token =>
token.id.toLowerCase() ===
tokenId.toLowerCase(),
) as IToken;
return token ? (
<StreamInfo
key={tokenId}
stream={tokenStreams[tokenId]}
stream={tokenStreams[tokenId.toLowerCase()]}
balance={balances[token.symbol]}
disable={
!balances[token.symbol] ||
Expand All @@ -150,7 +154,7 @@ const SelectTokenInnerModal: FC<ISelectTokenModalProps> = ({
) : null;
})}
{superTokens.map(token =>
tokenStreams[token.id] ||
tokenStreams[token.id.toLowerCase()] ||
balances[token.symbol] === 0n ? null : (
<TokenInfo
key={token.symbol}
Expand Down Expand Up @@ -185,33 +189,38 @@ const SelectTokenInnerModal: FC<ISelectTokenModalProps> = ({
id: 'label.superfluid_eligible_tokens_description',
})}
</TitleSubheader>
{tokens.length > 0 ? (
tokens.map(token => (
<TokenInfo
key={token.underlyingToken.symbol}
token={token.underlyingToken}
balance={
balances[token.underlyingToken.symbol]
}
disable={
balances[
token.underlyingToken.symbol
] === undefined ||
balances[
token.underlyingToken.symbol
] === 0n
}
onClick={() => {
setSelectedRecurringToken({
token: token.underlyingToken,
balance:
balances[
token.underlyingToken.symbol
],
});
setShowModal(false);
}}
/>
{underlyingTokens.length > 0 ? (
underlyingTokens.map(token => (
<>
<TokenInfo
key={token.underlyingToken?.symbol}
token={token.underlyingToken}
balance={
balances[
token.underlyingToken.symbol
]
}
disable={
balances[
token.underlyingToken.symbol
] === undefined ||
balances[
token.underlyingToken.symbol
] === 0n
}
onClick={() => {
setSelectedRecurringToken({
token: token.underlyingToken,
balance:
balances[
token.underlyingToken
.symbol
],
});
setShowModal(false);
}}
/>
</>
kkatusic marked this conversation as resolved.
Show resolved Hide resolved
))
) : (
<Caption>
Expand Down
45 changes: 30 additions & 15 deletions src/config/production.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -522,21 +522,36 @@ const config: EnvConfig = {
isSuperToken: true,
coingeckoId: 'dai',
},
// {
// underlyingToken: {
// decimals: 6,
// id: '0x7f5c764cbc14f9669b88837ca1490cca17c31607',
// name: 'USD Coin',
// symbol: 'USDC',
// coingeckoId: 'usd-coin',
// },
// decimals: 18,
// id: '0x8430f084b939208e2eded1584889c9a66b90562f',
// name: 'Super USD Coin',
// symbol: 'USDCx',
// isSuperToken: true,
// coingeckoId: 'usd-coin',
// },
{
underlyingToken: {
decimals: 6,
id: '0x7f5c764cbc14f9669b88837ca1490cca17c31607',
name: 'USD Coin',
symbol: 'USDC.e',
coingeckoId: 'usd-coin',
},
decimals: 18,
id: '0x8430f084b939208e2eded1584889c9a66b90562f',
name: 'Super USD Coin',
symbol: 'USDC.ex',
isSuperToken: true,
coingeckoId: 'usd-coin',
},
{
underlyingToken: {
decimals: 6,
id: '0x0b2c639c533813f4aa9d7837caf62653d097ff85',
name: 'USD Coin',
symbol: 'USDC',
coingeckoId: 'usd-coin',
},
decimals: 18,
id: '0x35Adeb0638EB192755B6E52544650603Fe65A006',
name: 'Super USD Coin',
symbol: 'USDCx',
isSuperToken: true,
coingeckoId: 'usd-coin',
},
{
underlyingToken: {
decimals: 18,
Expand Down
Loading