Skip to content

Commit

Permalink
🧇 Unlock revenue share (#4906)
Browse files Browse the repository at this point in the history
* Bump types package

* Create a transaction for exiting revenue split

* Add proxy
  • Loading branch information
WRadoslaw committed Mar 14, 2024
1 parent c2319d8 commit 9ee4725
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactElement } from 'react'
import { ReactElement, useCallback } from 'react'

import { SvgActionCalendar, SvgJoyTokenMonochrome16 } from '@/assets/icons'
import { Avatar } from '@/components/Avatar'
Expand All @@ -8,35 +8,61 @@ import { NumberFormat } from '@/components/NumberFormat'
import { Text } from '@/components/Text'
import { Button } from '@/components/_buttons/Button'
import { InfoBox, Wrapper } from '@/components/_crt/RevenueShareWidget/RevenueShareWidget.styles'
import { useJoystream } from '@/providers/joystream'
import { useSnackbar } from '@/providers/snackbars'
import { useTransaction } from '@/providers/transactions/transactions.hooks'
import { useUser } from '@/providers/user/user.hooks'
import { formatDateTime } from '@/utils/time'

export type RevenueShareWidgetProps = {
tokenId: string
tokenName: string
userShare: number
userTokens: number
shareEndDate: Date
onAction?: () => void
onClaimShare?: () => void
status: 'active' | 'upcoming' | 'locked' | 'unlocked'
}
export const RevenueShareWidget = ({
userShare,
userTokens,
tokenName,
onAction,
onClaimShare,
shareEndDate,
status,
tokenId,
}: RevenueShareWidgetProps) => {
const { joystream, proxyCallback } = useJoystream()
const handleTransaction = useTransaction()
const { memberId } = useUser()
const { displaySnackbar } = useSnackbar()
const unlockStake = useCallback(async () => {
if (!joystream || !memberId) {
return
}
handleTransaction({
txFactory: async (updateStatus) =>
(await joystream.extrinsics).exitRevenueSplit(tokenId, memberId, proxyCallback(updateStatus)),
onTxSync: async (data) => {
displaySnackbar({
title: `${data.amount} $${tokenName} unlocked`,
iconType: 'success',
})
},
})
}, [joystream, memberId, handleTransaction, tokenId, proxyCallback, displaySnackbar, tokenName])

const actionNode = () => {
switch (status) {
case 'active':
return (
<Button fullWidth onClick={onAction}>
<Button fullWidth onClick={onClaimShare}>
Claim your share
</Button>
)
case 'unlocked':
return (
<Button fullWidth onClick={onAction}>
<Button fullWidth onClick={unlockStake}>
Unlock tokens
</Button>
)
Expand Down
8 changes: 5 additions & 3 deletions packages/atlas/src/joystream-lib/extrinsics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
ChannelInputBuckets,
ChannelInputMetadata,
CommentReaction,
ExitRevenueSplitResult,
ExtrinsicResult,
ExtrinsicStatus,
ExtrinsicStatusCallbackFn,
Expand Down Expand Up @@ -1017,14 +1018,15 @@ export class JoystreamLibExtrinsics {
return this.api.tx.projectToken.exitRevenueSplit(parseInt(tokenId), parseInt(memberId))
}

exitRevenueSplit: PublicExtrinsic<typeof this.exitRevenueSplitTx, ExtrinsicResult> = async (
exitRevenueSplit: PublicExtrinsic<typeof this.exitRevenueSplitTx, ExitRevenueSplitResult> = async (
tokenId,
memberId,
cb
) => {
const tx = await this.exitRevenueSplitTx(tokenId, memberId)
const { block } = await this.sendExtrinsic(tx, cb)
return { block }
const { block, getEventData } = await this.sendExtrinsic(tx, cb)
const amount = getEventData('projectToken', 'RevenueSplitLeft')[2]
return { block, amount: amount.toString() }
}

participateInSplitTx = async (tokenId: TokenId, memberId: MemberId, amount: StringifiedNumber) => {
Expand Down
3 changes: 3 additions & 0 deletions packages/atlas/src/joystream-lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ export type VideoExtrinsicResult = ExtrinsicResult<{
export type MemberExtrinsicResult = ExtrinsicResult<{ memberId: MemberId }>
export type NftExtrinsicResult = ExtrinsicResult
export type MetaprotcolExtrinsicResult = ExtrinsicResult<{ metaprotocol: true; transactionHash: string }>
export type ExitRevenueSplitResult = {
amount: string
} & ExtrinsicResult

type TxMethodsFromClass<T> = T extends `${infer _}Tx` ? T : never

Expand Down

0 comments on commit 9ee4725

Please sign in to comment.