Skip to content

Commit 858b89e

Browse files
committed
refactor: implement useReadTransferRestrictions hook for transfer restrictions
- Replaced direct contract calls in TransferRestrictionsLabel and ListForSaleButton components with the new useReadTransferRestrictions hook for improved code reusability and clarity. - Updated components to handle transfer restrictions using the new hook, streamlining the logic and enhancing maintainability.
1 parent cca9a96 commit 858b89e

File tree

3 files changed

+56
-60
lines changed

3 files changed

+56
-60
lines changed

components/hypercert/transfer-restrictions-label.tsx

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
PopoverTrigger,
1313
} from "@/components/ui/popover";
1414
import { useState } from "react";
15+
import { useReadTransferRestrictions } from "@/hooks/use-read-transfer-restrictions";
1516

1617
export default function TransferRestrictionsLabel({
1718
hypercertId,
@@ -21,39 +22,8 @@ export default function TransferRestrictionsLabel({
2122
showSeparator?: boolean;
2223
}) {
2324
const [isOpen, setIsOpen] = useState(false);
24-
const { contractAddress, id } = parseClaimOrFractionId(hypercertId);
25-
const { data: transferRestrictions } = useReadContract({
26-
abi: [
27-
{
28-
inputs: [{ internalType: "uint256", name: "tokenID", type: "uint256" }],
29-
name: "readTransferRestriction",
30-
outputs: [
31-
{
32-
internalType: "string",
33-
name: "",
34-
type: "string",
35-
},
36-
],
37-
stateMutability: "view",
38-
type: "function",
39-
},
40-
],
41-
address: getAddress(contractAddress || ""),
42-
functionName: "readTransferRestriction",
43-
args: [id],
44-
query: {
45-
enabled: !!contractAddress && !!id,
46-
select: (data) => {
47-
if (data === "AllowAll") {
48-
return TransferRestrictions.AllowAll;
49-
} else if (data === "DisallowAll") {
50-
return TransferRestrictions.DisallowAll;
51-
} else if (data === "FromCreatorOnly") {
52-
return TransferRestrictions.FromCreatorOnly;
53-
}
54-
},
55-
},
56-
});
25+
const transferRestrictions = useReadTransferRestrictions(hypercertId);
26+
5727
if (transferRestrictions === undefined) return null;
5828
return (
5929
<>

components/marketplace/list-for-sale-button.tsx

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { useState } from "react";
1818
import { getAddress } from "viem";
1919

2020
import { isChainIdSupported } from "@/lib/isChainIdSupported";
21+
import { useReadTransferRestrictions } from "@/hooks/use-read-transfer-restrictions";
22+
import { TransferRestrictions } from "@hypercerts-org/sdk";
2123

2224
export function ListForSaleButton({
2325
hypercert,
@@ -34,29 +36,9 @@ export function ListForSaleButton({
3436
const { chain_id: chainId, contract_address: contractAddress } =
3537
hypercert.contract || {};
3638

37-
const { data: transferRestrictions } = useReadContract({
38-
abi: [
39-
{
40-
inputs: [{ internalType: "uint256", name: "tokenID", type: "uint256" }],
41-
name: "readTransferRestriction",
42-
outputs: [
43-
{
44-
internalType: "string",
45-
name: "",
46-
type: "string",
47-
},
48-
],
49-
stateMutability: "view",
50-
type: "function",
51-
},
52-
],
53-
address: getAddress(contractAddress || ""),
54-
functionName: "readTransferRestriction",
55-
args: [tokenId!],
56-
query: {
57-
enabled: !!contractAddress && !!tokenId,
58-
},
59-
});
39+
const transferRestrictions = useReadTransferRestrictions(
40+
hypercert.hypercert_id || "",
41+
);
6042

6143
const [isOpen, setIsOpen] = useState(false);
6244

@@ -87,8 +69,8 @@ export function ListForSaleButton({
8769
!client ||
8870
!client.isClaimOrFractionOnConnectedChain(hypercertId) ||
8971
!fractionsOwnedByUser.length ||
90-
transferRestrictions === "DisallowAll" ||
91-
(transferRestrictions === "FromCreatorOnly" &&
72+
transferRestrictions === TransferRestrictions.DisallowAll ||
73+
(transferRestrictions === TransferRestrictions.FromCreatorOnly &&
9274
address?.toLowerCase() !== hypercert.creator_address?.toLowerCase());
9375

9476
const getToolTipMessage = () => {
@@ -114,11 +96,11 @@ export function ListForSaleButton({
11496
return "You do not own any fractions of this hypercert";
11597
}
11698

117-
if (transferRestrictions === "DisallowAll") {
99+
if (transferRestrictions === TransferRestrictions.DisallowAll) {
118100
return "Secondary sales are not allowed for this hypercert";
119101
}
120102

121-
if (transferRestrictions === "FromCreatorOnly") {
103+
if (transferRestrictions === TransferRestrictions.FromCreatorOnly) {
122104
return "Only the creator can sell this hypercert";
123105
}
124106

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
parseClaimOrFractionId,
3+
TransferRestrictions,
4+
} from "@hypercerts-org/sdk";
5+
import { getAddress } from "viem";
6+
import { useReadContract } from "wagmi";
7+
8+
export const useReadTransferRestrictions = (hypercertId: string) => {
9+
const { contractAddress, id } = parseClaimOrFractionId(hypercertId);
10+
const { data: transferRestrictions } = useReadContract({
11+
abi: [
12+
{
13+
inputs: [{ internalType: "uint256", name: "tokenID", type: "uint256" }],
14+
name: "readTransferRestriction",
15+
outputs: [
16+
{
17+
internalType: "string",
18+
name: "",
19+
type: "string",
20+
},
21+
],
22+
stateMutability: "view",
23+
type: "function",
24+
},
25+
],
26+
address: getAddress(contractAddress || ""),
27+
functionName: "readTransferRestriction",
28+
args: [id],
29+
query: {
30+
enabled: !!contractAddress && !!id,
31+
select: (data) => {
32+
if (data === "AllowAll") {
33+
return TransferRestrictions.AllowAll;
34+
} else if (data === "DisallowAll") {
35+
return TransferRestrictions.DisallowAll;
36+
} else if (data === "FromCreatorOnly") {
37+
return TransferRestrictions.FromCreatorOnly;
38+
}
39+
},
40+
},
41+
});
42+
43+
return transferRestrictions;
44+
};

0 commit comments

Comments
 (0)