-
-
Notifications
You must be signed in to change notification settings - Fork 34
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
Fix recurring donations #4876
Changes from all commits
de3da5f
2b943cf
7b1534a
0ce1b94
664c79a
fdb5f41
058091c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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; | ||
|
||
|
@@ -279,7 +302,7 @@ const RecurringDonationInnerModal: FC<IRecurringDonationInnerModalProps> = ({ | |
} | ||
|
||
const _newFlowRate = | ||
(perMonthAmount * BigInt(donationToGiveth)) / | ||
(newPerMonthAmount * BigInt(donationToGiveth)) / | ||
100n / | ||
ONE_MONTH_SECONDS; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { useState, type FC } from 'react'; | ||
import { useState, useMemo, type FC } from 'react'; | ||
import styled from 'styled-components'; | ||
import { P, brandColors, semanticColors } from '@giveth/ui-design-system'; | ||
import { formatUnits } from 'viem'; | ||
|
@@ -8,7 +8,7 @@ import { TokenIcon } from '@/components/views/donate/TokenIcon/TokenIcon'; | |
import { TableCell } from './ActiveStreamsSection'; | ||
import { ISuperfluidStream } from '@/types/superFluid'; | ||
import { ONE_MONTH_SECONDS } from '@/lib/constants/constants'; | ||
import { limitFraction } from '@/helpers/number'; | ||
import { limitFraction, formatDonation } from '@/helpers/number'; | ||
import config from '@/configuration'; | ||
import { countActiveStreams } from '@/helpers/donate'; | ||
import { findTokenByAddress } from '@/helpers/superfluid'; | ||
|
@@ -19,6 +19,13 @@ interface IStreamRowProps { | |
} | ||
|
||
export const StreamRow: FC<IStreamRowProps> = ({ tokenStream }) => { | ||
const superToken = useMemo( | ||
() => | ||
config.OPTIMISM_CONFIG.SUPER_FLUID_TOKENS.find( | ||
s => s.id === tokenStream[0].token.id, | ||
), | ||
[tokenStream], | ||
); | ||
const [showModifyModal, setShowModifyModal] = useState(false); | ||
const { address, chain } = useAccount(); | ||
const { switchChain } = useSwitchChain(); | ||
|
@@ -39,7 +46,7 @@ export const StreamRow: FC<IStreamRowProps> = ({ tokenStream }) => { | |
0n, | ||
); | ||
const monthlyFlowRate = totalFlowRate * ONE_MONTH_SECONDS; | ||
const { symbol, decimals } = tokenStream[0].token; | ||
// const { symbol } = tokenStream[0].token; | ||
const runOutMonth = | ||
monthlyFlowRate > 0 && balance?.value | ||
? balance?.value / monthlyFlowRate | ||
|
@@ -57,12 +64,12 @@ export const StreamRow: FC<IStreamRowProps> = ({ tokenStream }) => { | |
) | ||
: '0'} | ||
</P> | ||
<P>{symbol}</P> | ||
<P>{superToken?.symbol}</P> | ||
</TableCell> | ||
<TableCell> | ||
<P color={semanticColors.jade[500]}> | ||
{limitFraction( | ||
formatUnits(monthlyFlowRate || 0n, decimals), | ||
{formatDonation( | ||
limitFraction(formatUnits(BigInt(monthlyFlowRate), 18)), | ||
)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kkatusic found like 3 more instances where you format the units directly with 18, shouldn't this be related to the token decimals? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that was the problem in first way, we can't use donation amount or better say flow rate amount for token decimals on 6 base, because flow will fail. We need to use super token decimal base :( |
||
| ||
{underlyingSymbol} | ||
|
@@ -109,11 +116,11 @@ export const StreamRow: FC<IStreamRowProps> = ({ tokenStream }) => { | |
Deposit/Withdraw | ||
</ModifyButton> | ||
</TableCell> | ||
{showModifyModal && ( | ||
{showModifyModal && superToken && ( | ||
<ModifySuperTokenModal | ||
tokenStreams={tokenStream} | ||
setShowModal={setShowModifyModal} | ||
selectedToken={tokenStream[0].token} | ||
selectedToken={superToken} | ||
refreshBalance={refetch} | ||
/> | ||
)} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here