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

Fixed incorrect withdrawal commissions calculation #5116

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ export const MemberDropdown = forwardRef<HTMLDivElement, MemberDropdownProps>(
return new BN(selectedChannel?.channelStateBloatBond || 0)
}, [selectedChannel?.channelStateBloatBond])

const { accountBalance, lockedAccountBalance, totalBalance, debt, totalInvitationLock } =
useSubscribeAccountBalance()
const { accountBalance, lockedAccountBalance, totalBalance, totalInvitationLock } = useSubscribeAccountBalance()
const { accountBalance: channelBalance } =
useSubscribeAccountBalance(selectedChannel?.rewardAccount, {
channelStateBloatBond: memoizedChannelStateBloatBond,
Expand Down Expand Up @@ -129,14 +128,8 @@ export const MemberDropdown = forwardRef<HTMLDivElement, MemberDropdownProps>(
channelBalance={channelBalance}
totalBalance={totalBalance}
channelId={channelId}
accountDebt={debt}
/>
<SendFundsDialog
show={showSendDialog}
onExitClick={toggleSendDialog}
accountBalance={accountBalance}
accountDebt={debt}
/>
<SendFundsDialog show={showSendDialog} onExitClick={toggleSendDialog} accountBalance={accountBalance} />

<CSSTransition classNames={transitions.names.dropdown} in={isActive} timeout={0} mountOnEnter unmountOnExit>
<Container ref={mergeRefs([ref, containerRef])}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useApolloClient } from '@apollo/client'
import { BN_ZERO } from '@polkadot/util'
import debouncePromise from 'awesome-debounce-promise'
import BN from 'bn.js'
import { FC, useEffect, useRef, useState } from 'react'
Expand Down Expand Up @@ -27,7 +28,7 @@ import { DialogModal } from '@/components/_overlays/DialogModal'
import { atlasConfig } from '@/config'
import { hapiBnToTokenNumber, tokenNumberToHapiBn } from '@/joystream-lib/utils'
import { getMemberAvatar } from '@/providers/assets/assets.helpers'
import { useFee, useJoystream, useTokenPrice } from '@/providers/joystream'
import { useFee, useJoystream, useSubscribeAccountBalance, useTokenPrice } from '@/providers/joystream'
import { useTransaction } from '@/providers/transactions/transactions.hooks'
import { cVar } from '@/styles'
import { formatJoystreamAddress, isValidAddressPolkadotAddress } from '@/utils/address'
Expand All @@ -48,7 +49,6 @@ type SendFundsDialogProps = {
channelBalance?: BN
accountBalance?: BN
totalBalance?: BN
accountDebt?: BN
channelId?: string | null
show: boolean
}
Expand All @@ -58,7 +58,6 @@ export const SendFundsDialog: FC<SendFundsDialogProps> = ({
accountBalance = new BN(0),
channelBalance,
totalBalance = new BN(0),
accountDebt = new BN(0),
channelId,
activeMembership,
show,
Expand All @@ -79,6 +78,10 @@ export const SendFundsDialog: FC<SendFundsDialogProps> = ({
} = useForm<{ amount: number | null; account: string | null }>()
const convertedAmount = convertHapiToUSD(tokenNumberToHapiBn(watch('amount') || 0))
const account = watch('account') || ''
const { accountBalance: hookAccountBalance } = useSubscribeAccountBalance()
const balanceRef = useRef<BN>(BN_ZERO)
balanceRef.current = hookAccountBalance || BN_ZERO

const amountBN = tokenNumberToHapiBn(watch('amount') || 0)
const { fullFee: transferFee, loading: feeLoading } = useFee(
'sendFundsTx',
Expand Down Expand Up @@ -128,7 +131,7 @@ export const SendFundsDialog: FC<SendFundsDialogProps> = ({
)

const handleSendFunds = async () => {
const handler = await handleSubmit(async (data) => {
const handler = handleSubmit(async (data) => {
if (!joystream || !data.account || !data.amount || (isWithdrawalMode && (!activeMembership || !channelId))) {
SentryLogger.error('Failed to send funds', 'SendFundsDialog', {
isWithdrawalMode,
Expand All @@ -151,9 +154,11 @@ export const SendFundsDialog: FC<SendFundsDialogProps> = ({
} tokens have been sent over to ${shortenString(data.account, ADDRESS_CHARACTERS_LIMIT)} wallet address`,
},
txFactory: async (updateStatus) => {
const amount =
!isWithdrawalMode &&
amountBN.sub(amountBN.add(transferFee).gte(accountBalance) ? transferFee : new BN(0)).sub(accountDebt)
const safeTransferableValue = BN.min(amountBN, balanceRef.current)
const amount = safeTransferableValue.sub(
safeTransferableValue.add(transferFee).gt(balanceRef.current) ? transferFee : BN_ZERO
)

return (await joystream.extrinsics).sendFunds(
formatJoystreamAddress(data.account || ''),
amount.toString(),
Expand All @@ -174,12 +179,10 @@ export const SendFundsDialog: FC<SendFundsDialogProps> = ({
}
: undefined,
txFactory: async (updateStatus) => {
const fee = isOwnAccount ? withdrawFee : fullFee
const amount = amountBN.add(fee).gte(channelBalance) ? amountBN.sub(fee) : amountBN
return (await joystream.extrinsics).withdrawFromChannelBalance(
activeMembership!.id,
channelId as string,
amount.toString(),
amountBN.toString(),
proxyCallback(updateStatus)
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const PaymentsOverView = () => {
const [showClaimDialog, setShowClaimDialog] = useState<boolean>(false)
const { channel, loading } = useFullChannel(channelId || '')
const { availableAward, isAwardLoading } = useChannelPayout()
const { totalBalance, debt } = useSubscribeAccountBalance()
const { totalBalance } = useSubscribeAccountBalance()

const memoizedChannelStateBloatBond = useMemo(() => {
return new BN(channel?.channelStateBloatBond || 0)
Expand All @@ -43,7 +43,6 @@ export const PaymentsOverView = () => {
channelBalance={channelBalance}
channelId={channelId}
totalBalance={totalBalance}
accountDebt={debt}
/>
<ClaimChannelPaymentsDialog show={showClaimDialog} onExit={() => setShowClaimDialog(false)} />
<TilesWrapper>
Expand Down
Loading