Skip to content

Commit

Permalink
Fixed incorrect withdrawal commissions calculation (Joystream#5116)
Browse files Browse the repository at this point in the history
* changed the commission calculation

* deduct full fee

* included balance ref, calculated safe value for transfer

---------

Co-authored-by: Artem <Artem Slugin>
  • Loading branch information
attemka authored and Artem committed Nov 21, 2023
1 parent ce6f1a4 commit 777abad
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
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

0 comments on commit 777abad

Please sign in to comment.