Skip to content

Commit

Permalink
feat(rwa): set infinite values for compliance rules (#2770)
Browse files Browse the repository at this point in the history
  • Loading branch information
sstraatemans authored Jan 7, 2025
1 parent a93fed7 commit c23f8a7
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 46 deletions.
2 changes: 2 additions & 0 deletions .changeset/witty-birds-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
6 changes: 3 additions & 3 deletions packages/apps/rwa-demo/src/app/(app)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ const Home = () => {
{asset && (
<>
<ComplianceRule
value={`${asset.maxSupply < 0 ? 0 : asset.maxSupply} tokens`}
value={`${asset.maxSupply < 0 ? 'no limit' : asset.maxSupply} tokens`}
label="Supply limit"
/>
<ComplianceRule
value={`${asset.maxBalance < 0 ? 0 : asset.maxBalance} tokens`}
value={`${asset.maxBalance < 0 ? 'no limit' : asset.maxBalance} tokens`}
label="Max balance"
/>
<ComplianceRule
value={`${asset.maxInvestors < 0 ? 0 : asset.maxInvestors} (${asset.investorCount}) investors`}
value={`${asset.maxInvestors < 0 ? 'no limit' : asset.maxInvestors} (${asset.investorCount}) investors`}
label="Max Investors"
/>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Exact, Scalars } from '@/__generated__/sdk';
import { useEventSubscriptionSubscription } from '@/__generated__/sdk';
import type { IWalletAccount } from '@/components/AccountProvider/AccountType';
import {
INFINITE_COMPLIANCE,
LOCALSTORAGE_ASSETS_KEY,
LOCALSTORAGE_ASSETS_SELECTED_KEY,
} from '@/constants';
Expand Down Expand Up @@ -132,10 +133,10 @@ export const AssetProvider: FC<PropsWithChildren> = ({ children }) => {
}) => {
const asset: IAsset = {
uuid: crypto.randomUUID(),
supply: -1,
maxSupply: -1,
maxBalance: -1,
maxInvestors: -1,
supply: INFINITE_COMPLIANCE,
maxSupply: INFINITE_COMPLIANCE,
maxBalance: INFINITE_COMPLIANCE,
maxInvestors: INFINITE_COMPLIANCE,
investorCount: 0,
contractName,
namespace,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { INFINITE_COMPLIANCE } from '@/constants';
import { useAccount } from '@/hooks/account';
import { useAsset } from '@/hooks/asset';
import { useDistributeTokens } from '@/hooks/distributeTokens';
Expand All @@ -15,7 +16,7 @@ import { cloneElement, useEffect, useRef, useState } from 'react';
import { Controller, useForm } from 'react-hook-form';
import { AssetPausedMessage } from '../AssetPausedMessage/AssetPausedMessage';
import { InvestorFrozenMessage } from '../InvestorFrozenMessage/InvestorFrozenMessage';
import { NoComplianceMessage } from '../NoComplianceMessage/NoComplianceMessage';
import { MaxSupplyMessage } from '../MaxSupplyMessage/MaxSupplyMessage';
import { SendTransactionAnimation } from '../SendTransactionAnimation/SendTransactionAnimation';
import type { ITransaction } from '../TransactionsProvider/TransactionsProvider';

Expand Down Expand Up @@ -85,7 +86,11 @@ export const DistributionForm: FC<IProps> = ({
return message;
};

const maxAmount = (asset?.maxBalance ?? 0) - balance;
let maxAmount = INFINITE_COMPLIANCE;
if ((asset?.maxBalance ?? 0) >= 0) {
maxAmount = (asset?.maxBalance ?? 0) - balance;
}

return (
<>
{isRightAsideExpanded && isOpen && (
Expand All @@ -99,15 +104,17 @@ export const DistributionForm: FC<IProps> = ({
rules={{
required: true,
min: 0,
max: maxAmount,
max: maxAmount >= 0 ? maxAmount : undefined,
}}
render={({ field }) => (
<TextField
type="number"
label="Amount"
{...field}
errorMessage={errors.amount?.message}
description={`max amount: ${maxAmount < 0 ? 0 : maxAmount} `}
description={
maxAmount >= 0 ? `max amount: ${maxAmount}` : ''
}
/>
)}
/>
Expand All @@ -117,7 +124,7 @@ export const DistributionForm: FC<IProps> = ({
<>
<InvestorFrozenMessage investorAccount={investorAccount} />
<AssetPausedMessage />
<NoComplianceMessage />
<MaxSupplyMessage />
</>
}
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { INFINITE_COMPLIANCE } from '@/constants';
import { useAsset } from '@/hooks/asset';
import { MonoWarning } from '@kadena/kode-icons';
import { token } from '@kadena/kode-ui/styles';
Expand All @@ -12,7 +13,7 @@ export const MaxInvestorBalanceCheck: FC<IProps> = ({ balance }) => {

if (!asset) return null;

if (asset?.maxBalance < balance)
if (asset?.maxBalance < balance && asset?.maxBalance > INFINITE_COMPLIANCE)
return (
<MonoWarning
style={{ color: token('color.icon.semantic.warning.default') }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ export const InvestorBatchForm: FC<IProps> = ({ onClose, trigger }) => {
<Button onPress={handleOnClose} variant="transparent">
Cancel
</Button>
<Button type="submit">Add investors</Button>
<Button isDisabled={accounts.length === 0} type="submit">
Add investors
</Button>
</RightAsideFooter>
</form>
</RightAside>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { INFINITE_COMPLIANCE } from '@/constants';
import { useAsset } from '@/hooks/asset';
import { Notification } from '@kadena/kode-ui';
import type { FC } from 'react';

export const MaxSupplyMessage: FC = () => {
const { asset } = useAsset();

if (!asset) return;
const isMaxReached =
asset.maxSupply <= asset.supply && asset.maxSupply > INFINITE_COMPLIANCE;
if (!isMaxReached) return;

return (
<Notification intent="info" role="status">
The max Supply for this contract is reached ({asset.maxSupply} tokens)
</Notification>
);
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { INFINITE_COMPLIANCE } from '@/constants';
import { useAsset } from '@/hooks/asset';
import { useSetCompliance } from '@/hooks/setCompliance';
import type { ISetComplianceProps } from '@/services/setCompliance';
Expand Down Expand Up @@ -72,7 +73,7 @@ export const SetComplianceForm: FC<IProps> = ({ onClose, trigger }) => {
<Controller
name="maxBalance"
control={control}
rules={{ required: true, min: 0 }}
rules={{ required: true, min: INFINITE_COMPLIANCE }}
render={({ field }) => (
<TextField type="number" label="Max Balance" {...field} />
)}
Expand All @@ -81,15 +82,15 @@ export const SetComplianceForm: FC<IProps> = ({ onClose, trigger }) => {
<Controller
name="maxSupply"
control={control}
rules={{ required: true, min: 0 }}
rules={{ required: true, min: INFINITE_COMPLIANCE }}
render={({ field }) => (
<TextField type="number" label="Max Supply" {...field} />
)}
/>
<Controller
name="maxInvestors"
control={control}
rules={{ required: true, min: 0 }}
rules={{ required: true, min: INFINITE_COMPLIANCE }}
render={({ field }) => (
<TextField
type="number"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { INFINITE_COMPLIANCE } from '@/constants';
import { useAsset } from '@/hooks/asset';
import { MonoWarning } from '@kadena/kode-icons';
import { Stack } from '@kadena/kode-ui';
Expand All @@ -9,7 +10,7 @@ export const SupplyCountContractDetails: FC = () => {

if (!asset) return null;

if (asset.supply > asset.maxSupply)
if (asset.supply > asset.maxSupply && asset.maxSupply > INFINITE_COMPLIANCE)
return (
<Stack alignItems="center" gap="xs">
<MonoWarning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const FormatDeleteInvestor = () => {
investorAccount,
});

console.log(notAllowedReason);

const handleDelete = async () => {
await submit({ investor: investorAccount });
};
Expand All @@ -34,7 +36,7 @@ export const FormatDeleteInvestor = () => {
/>
}
>
Are you sure you want to delete this agent?
Are you sure you want to delete this investor?
</Confirmation>
);
};
Expand Down
1 change: 1 addition & 0 deletions packages/apps/rwa-demo/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const ACCOUNT_COOKIE_NAME = 'rwa';
export const LOCALSTORAGE_ASSETS_KEY = 'assets';
export const LOCALSTORAGE_ASSETS_SELECTED_KEY = 'selected_asset';
export const INFINITE_COMPLIANCE = -1;

// todo: this is temporary for devnet
export const ADMIN = {
Expand Down
6 changes: 5 additions & 1 deletion packages/apps/rwa-demo/src/hooks/deleteInvestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import { useNotifications } from '@kadena/kode-ui/patterns';
import { useEffect, useState } from 'react';
import { useAccount } from './account';
import { useAsset } from './asset';
import { useGetInvestorBalance } from './getInvestorBalance';
import { useTransactions } from './transactions';

export const useDeleteInvestor = ({
investorAccount,
}: {
investorAccount?: string;
}) => {
const { account, sign, accountRoles, isMounted, balance } = useAccount();
useGetInvestorBalance;
const { account, sign, accountRoles, isMounted } = useAccount();
const { data: balance } = useGetInvestorBalance({ investorAccount });
const { paused } = useAsset();
const { addTransaction, isActiveAccountChangeTx } = useTransactions();
const { addNotification } = useNotifications();
Expand Down Expand Up @@ -61,6 +64,7 @@ export const useDeleteInvestor = ({
accountRoles.isWhitelistManager() &&
balance !== undefined &&
balance <= 0;

setIsAllowed(result);
if (!result) {
setNotAllowedReason(
Expand Down
9 changes: 6 additions & 3 deletions packages/apps/rwa-demo/src/hooks/distributeTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const useDistributeTokens = ({
investorAccount: string;
}) => {
const { frozen } = useFreeze({ investorAccount });
const { paused } = useAsset();
const { paused, asset } = useAsset();

const { account, sign, accountRoles, isMounted } = useAccount();
const { addTransaction, isActiveAccountChangeTx } = useTransactions();
Expand Down Expand Up @@ -50,12 +50,14 @@ export const useDistributeTokens = ({
};

useEffect(() => {
if (!isMounted) return;
if (!isMounted || !asset) return;
setIsAllowed(
!frozen &&
!paused &&
accountRoles.isSupplyModifier() &&
!isActiveAccountChangeTx,
!isActiveAccountChangeTx &&
((asset.maxSupply >= 0 && asset.supply < asset.maxSupply) ||
asset.maxSupply < 0),
);
}, [
frozen,
Expand All @@ -64,6 +66,7 @@ export const useDistributeTokens = ({
isMounted,
accountRoles,
isActiveAccountChangeTx,
asset,
]);

return { submit, isAllowed };
Expand Down
9 changes: 5 additions & 4 deletions packages/apps/rwa-demo/src/services/getComplianceRules.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { INFINITE_COMPLIANCE } from '@/constants';
import { getClient, getNetwork } from '@/utils/client';
import { getAsset } from '@/utils/getAsset';
import { Pact } from '@kadena/client';
Expand Down Expand Up @@ -27,7 +28,7 @@ export const getMaxBalance = async (): Promise<number> => {

const data = (result as any).data as any;

return result.status === 'success' ? data : -1;
return result.status === 'success' ? data : INFINITE_COMPLIANCE;
};

export const getMaxSupply = async (): Promise<number> => {
Expand All @@ -48,7 +49,7 @@ export const getMaxSupply = async (): Promise<number> => {

const data = (result as any).data as any;

return result.status === 'success' ? data : -1;
return result.status === 'success' ? data : INFINITE_COMPLIANCE;
};

export const getInvestorCount = async (): Promise<number> => {
Expand All @@ -69,7 +70,7 @@ export const getInvestorCount = async (): Promise<number> => {

const data = (result as any).data as any;

return result.status === 'success' ? data : -1;
return result.status === 'success' ? data : INFINITE_COMPLIANCE;
};

export const getMaxInvestors = async (): Promise<number> => {
Expand All @@ -90,7 +91,7 @@ export const getMaxInvestors = async (): Promise<number> => {

const data = (result as any).data as any;

return result.status === 'success' ? data : -1;
return result.status === 'success' ? data : INFINITE_COMPLIANCE;
};

export const getComplianceRules = async (): Promise<IComplianceProps> => {
Expand Down

0 comments on commit c23f8a7

Please sign in to comment.