Skip to content

Commit

Permalink
feat: enable collect fees for hey (#4963)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigint committed Jun 4, 2024
2 parents 5f7ab8b + b29bdef commit 70e2b11
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ const AmountConfig: FC<AmountConfigProps> = ({ setCollectType }) => {
amount: enabled
? null
: { currency: DEFAULT_COLLECT_TOKEN, value: '1' },
recipient: enabled ? null : currentProfile?.ownedBy.address,
recipients: enabled
? []
: [{ recipient: currentProfile?.ownedBy.address, split: 100 }],
type: enabled
? CollectOpenActionModuleType.SimpleCollectOpenActionModule
: collectModule.recipients?.length
? CollectOpenActionModuleType.MultirecipientFeeCollectOpenActionModule
: CollectOpenActionModuleType.SimpleCollectOpenActionModule
: CollectOpenActionModuleType.MultirecipientFeeCollectOpenActionModule
});
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { isAddress } from 'viem';
import AmountConfig from './AmountConfig';
import CollectLimitConfig from './CollectLimitConfig';
import FollowersConfig from './FollowersConfig';
import RecipientConfig from './RecipientConfig';
import ReferralConfig from './ReferralConfig';
import SplitConfig from './SplitConfig';
import TimeLimitConfig from './TimeLimitConfig';
Expand All @@ -29,17 +28,17 @@ const CollectForm: FC<CollectFormProps> = ({ setShowModal }) => {

const { SimpleCollectOpenActionModule } = CollectOpenActionModuleType;
const recipients = collectModule.recipients || [];
const { amount, recipient } = collectModule;
const splitTotal = recipients.reduce((acc, curr) => acc + curr.split, 0);
const hasInvalidRecipient = Boolean(amount?.value)
? !recipient || !isAddress(recipient)
: false;

const hasEmptyRecipients = recipients.some(
(recipient) => !recipient.recipient
);
const hasInvalidEthAddressInRecipients = recipients.some(
(recipient) => recipient.recipient && !isAddress(recipient.recipient)
);
const hasZeroSplits = recipients.some((recipient) => recipient.split === 0);
const hasImproperSplits = recipients.length > 1 && splitTotal !== 100;

const isRecipientsDuplicated = () => {
const recipientsSet = new Set(
recipients.map((recipient) => recipient.recipient)
Expand Down Expand Up @@ -81,7 +80,6 @@ const CollectForm: FC<CollectFormProps> = ({ setShowModal }) => {
{collectModule.amount?.value ? (
<>
<ReferralConfig setCollectType={setCollectType} />
<RecipientConfig setCollectType={setCollectType} />
<SplitConfig
isRecipientsDuplicated={isRecipientsDuplicated}
setCollectType={setCollectType}
Expand Down Expand Up @@ -116,10 +114,9 @@ const CollectForm: FC<CollectFormProps> = ({ setShowModal }) => {
disabled={
(parseFloat(collectModule.amount?.value as string) <= 0 &&
collectModule.type !== null) ||
splitTotal > 100 ||
hasImproperSplits ||
hasEmptyRecipients ||
recipients.length === 1 ||
hasInvalidRecipient ||
hasZeroSplits ||
hasInvalidEthAddressInRecipients ||
isRecipientsDuplicated()
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
} from '@heroicons/react/24/outline';
import { ADDRESS_PLACEHOLDER } from '@hey/data/constants';
import splitNumber from '@hey/helpers/splitNumber';
import { CollectOpenActionModuleType } from '@hey/lens';
import { Button, Input } from '@hey/ui';
import { useState } from 'react';
import { useCollectModuleStore } from 'src/store/non-persisted/publication/useCollectModuleStore';
import { useProfileStore } from 'src/store/persisted/useProfileStore';
import { isAddress } from 'viem';
Expand All @@ -29,9 +29,13 @@ const SplitConfig: FC<SplitConfigProps> = ({
const { currentProfile } = useProfileStore();
const { collectModule } = useCollectModuleStore((state) => state);

const currentAddress = currentProfile?.ownedBy.address || '';
const recipients = collectModule.recipients || [];
const hasRecipients = (recipients || []).length > 0;
const splitTotal = recipients?.reduce((acc, curr) => acc + curr.split, 0);
const [isToggleOn, setIsToggleOn] = useState(
recipients.length > 1 ||
(recipients.length === 1 && recipients[0].recipient !== currentAddress)
);
const splitTotal = recipients.reduce((acc, curr) => acc + curr.split, 0);

const splitEvenly = () => {
const equalSplits = splitNumber(100, recipients.length);
Expand Down Expand Up @@ -70,27 +74,35 @@ const SplitConfig: FC<SplitConfigProps> = ({
onChangeRecipientOrSplit(index, value, 'recipient');
};

const removeRecipient = (index: number) => {
const updatedRecipients = recipients.filter((_, i) => i !== index);
if (updatedRecipients.length === 0) {
setCollectType({
recipients: [{ recipient: currentAddress, split: 100 }]
});
setIsToggleOn(false);
} else {
setCollectType({ recipients: updatedRecipients });
}
};

const toggleSplit = () => {
setCollectType({
recipients: [{ recipient: currentAddress, split: 100 }]
});
setIsToggleOn(!isToggleOn);
};

return (
<div className="mt-5">
<ToggleWithHelper
description="Set multiple recipients for the collect fee"
heading="Split revenue"
icon={<UsersIcon className="size-5" />}
on={recipients.length > 0}
setOn={() => {
setCollectType({
recipients:
recipients.length > 0
? []
: [{ recipient: currentProfile?.ownedBy.address, split: 100 }],
type:
recipients.length > 0
? CollectOpenActionModuleType.SimpleCollectOpenActionModule
: CollectOpenActionModuleType.MultirecipientFeeCollectOpenActionModule
});
}}
on={isToggleOn}
setOn={toggleSplit}
/>
{hasRecipients ? (
{isToggleOn ? (
<div className="ml-8 mt-4 space-y-3">
<div className="space-y-2">
{recipients.map((recipient, index) => (
Expand Down Expand Up @@ -127,21 +139,14 @@ const SplitConfig: FC<SplitConfigProps> = ({
value={recipient.split}
/>
</div>
<button
onClick={() => {
setCollectType({
recipients: recipients.filter((_, i) => i !== index)
});
}}
type="button"
>
<button onClick={() => removeRecipient(index)} type="button">
<XCircleIcon className="size-5" />
</button>
</div>
))}
</div>
<div className="flex items-center justify-between">
{recipients.length >= 5 ? (
{recipients.length >= 4 ? (
<div />
) : (
<Button
Expand Down Expand Up @@ -171,6 +176,11 @@ const SplitConfig: FC<SplitConfigProps> = ({
Splits cannot exceed 100%. Total: <span>{splitTotal}</span>%
</div>
) : null}
{splitTotal < 100 ? (
<div className="text-sm font-bold text-red-500">
Splits cannot be less than 100%. Total: <span>{splitTotal}</span>%
</div>
) : null}
{isRecipientsDuplicated() ? (
<div className="text-sm font-bold text-red-500">
Duplicate recipient address found
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import type { Profile, RecipientDataOutput } from '@hey/lens';
import type { FC } from 'react';

import Slug from '@components/Shared/Slug';
import { POLYGONSCAN_URL } from '@hey/data/constants';
import {
APP_NAME,
POLYGONSCAN_URL,
REWARDS_ADDRESS
} from '@hey/data/constants';
import formatAddress from '@hey/helpers/formatAddress';
import getAvatar from '@hey/helpers/getAvatar';
import getProfile from '@hey/helpers/getProfile';
import getStampFyiURL from '@hey/helpers/getStampFyiURL';
import { useProfilesQuery } from '@hey/lens';
import { Image } from '@hey/ui';
import Link from 'next/link';

interface SplitsProps {
Expand Down Expand Up @@ -40,6 +45,21 @@ const Splits: FC<SplitsProps> = ({ recipients }) => {
const { recipient: address, split } = recipient;
const profile = getProfileByAddress(address) as Profile;

if (address === REWARDS_ADDRESS) {
return (
<div key={address}>
<div className="divider mb-2 mt-3" />
<div className="flex items-center justify-between text-sm">
<div className="ld-text-gray-500 flex w-full items-center space-x-2">
<img alt="Hey" className="size-4" src="/logo.png" />
<b>{APP_NAME} Fees</b>
</div>
<div className="font-bold">{split}%</div>
</div>
</div>
);
}

return (
<div
className="flex items-center justify-between text-sm"
Expand All @@ -53,7 +73,7 @@ const Splits: FC<SplitsProps> = ({ recipients }) => {
</>
) : (
<>
<img
<Image
alt="Avatar"
className="size-5 rounded-full border bg-gray-200 dark:border-gray-700"
src={profile ? getAvatar(profile) : getStampFyiURL(address)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import {
PuzzlePieceIcon,
UsersIcon
} from '@heroicons/react/24/outline';
import { POLYGONSCAN_URL } from '@hey/data/constants';
import {
APP_NAME,
POLYGONSCAN_URL,
REWARDS_ADDRESS
} from '@hey/data/constants';
import formatDate from '@hey/helpers/datetime/formatDate';
import formatAddress from '@hey/helpers/formatAddress';
import getProfile from '@hey/helpers/getProfile';
Expand Down Expand Up @@ -63,8 +67,17 @@ const CollectModule: FC<CollectModuleProps> = ({ openAction, publication }) => {
const usdPrice = collectModule?.amount?.asFiat?.value;
const currency = collectModule?.amount?.asset?.symbol;
const referralFee = collectModule?.referralFee;
const recipients =
(collectModule.__typename ===
'MultirecipientFeeCollectOpenActionSettings' &&
collectModule?.recipients) ||
[];
const recipientsWithoutFees = recipients?.filter(
(split) => split.recipient !== REWARDS_ADDRESS
);
const isMultirecipientFeeCollectModule =
collectModule.__typename === 'MultirecipientFeeCollectOpenActionSettings';
collectModule.__typename === 'MultirecipientFeeCollectOpenActionSettings' &&
recipientsWithoutFees.length > 1;
const percentageCollected = (countOpenActions / collectLimit) * 100;
const enabledTokens = allowedTokens?.map((t) => t.symbol);
const isTokenEnabled = enabledTokens?.includes(currency);
Expand Down Expand Up @@ -148,12 +161,18 @@ const CollectModule: FC<CollectModuleProps> = ({ openAction, publication }) => {
<HelpTooltip>
<div className="py-1">
<b>Collect Fees</b>
<div className="flex items-start space-x-10">
<div className="flex items-start justify-between space-x-10">
<div>Lens Protocol</div>
<b>
{(amount * 0.05).toFixed(2)} {currency} (5%)
</b>
</div>
<div className="flex items-start justify-between space-x-10">
<div>{APP_NAME}</div>
<b>
{(amount * 0.05).toFixed(2)} {currency} (5%)
</b>
</div>
</div>
</HelpTooltip>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const INITIAL_COLLECT_MODULE: CollectModuleType = {
collectLimit: null,
endsAt: null,
followerOnly: false,
recipient: null,
recipients: [],
referralFee: 0,
type: null
Expand Down
2 changes: 1 addition & 1 deletion packages/data/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const WMATIC_ADDRESS = IS_MAINNET
: '0x9c3c9283d3e44854697cd22d3faa240cfb032889';
export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
export const PERMIT_2_ADDRESS = '0x000000000022D473030F116dDEE9F6B43aC78BA3';
export const REWARDS_ADDRESS = '0x698386C93513d6D0C58f296633A7A3e529bd4026';
export const REWARDS_ADDRESS = '0x69696378FaEd01315c762e5122fFFBc7bab03570';
export const REWARDS_PROFILE_ID = '12'; // 0x0c
export const TEST_WALLET_ADDRESS = '0xb9C6e304545386E95d5c4ab183EE97A13555A49d';
export const TEST_PK =
Expand Down
Loading

1 comment on commit 70e2b11

@vercel
Copy link

@vercel vercel bot commented on 70e2b11 Jun 4, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

web – ./apps/web

web-heyxyz.vercel.app
hey.xyz
heyxyz.vercel.app
web-git-main-heyxyz.vercel.app

Please sign in to comment.