-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNestModal.tsx
More file actions
140 lines (127 loc) · 4.45 KB
/
NestModal.tsx
File metadata and controls
140 lines (127 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { useState } from "react";
import { useParams } from "react-router-dom";
import { useAccountsContext } from "../accounts/AccountsContext";
import { Account, SignerTypeEnum } from "../accounts/types";
import { Modal } from "../components/Modal";
import { useSdkContext } from "../sdk/SdkContext";
import { Address } from "@unique-nft/utils";
import { useUniqueNFTFactory } from "../hooks/useUniqueNFTFactory";
import styled from "styled-components";
import { Button, ButtonWrapper, Loading } from "./UnnestModal";
import { switchNetwork } from "../utils/swithChain";
import { getCollection } from "../utils/getCollection";
type NestTModalProps = {
isVisible: boolean;
account?: Account;
onClose(): void;
};
export const ContentWrapper = styled.div`
display: flex;
flex-direction: column;
gap: 10px;
`;
export const NestTModal = ({ isVisible, onClose }: NestTModalProps) => {
const { selectedAccount, magic, providerWeb3Auth } = useAccountsContext();
const { sdk } = useSdkContext();
const { tokenId, collectionId } = useParams<{
tokenId: string;
collectionId: string;
}>();
const [tokenParentId, setTokenParentId] = useState<string>("");
const [collectionParentId, setCollectionParentId] = useState<string>("");
const [isLoading, setIsLoading] = useState(false);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const { getUniqueNFTFactory } = useUniqueNFTFactory(collectionId);
const onSign = async () => {
if (!sdk || !selectedAccount || !collectionId || !tokenId) {
setErrorMessage("All fields must be filled out.");
return;
}
setIsLoading(true);
setErrorMessage(null);
try {
if (selectedAccount.signerType === SignerTypeEnum.Ethereum) {
await switchNetwork();
const collection = await getUniqueNFTFactory();
if (!collection) {
throw new Error("Failed to initialize the collection helper.");
}
await transferToken(collection);
} else if (selectedAccount.signerType === SignerTypeEnum.Magiclink) {
if (!magic) throw Error('No Magic')
const collection = await getCollection(magic.rpcProvider, collectionId)
await transferToken(collection);
} else if (selectedAccount.signerType === SignerTypeEnum.Web3Auth) {
if (!providerWeb3Auth) throw Error('No Web3Auth provider')
const collection = await getCollection(providerWeb3Auth, collectionId)
await transferToken(collection);
} else {
await sdk?.token.nest({
parent: {
collectionId: +collectionParentId,
tokenId: +tokenParentId,
},
nested: {
collectionId: +collectionId,
tokenId: +tokenId,
},
});
}
setIsLoading(false);
window.location.reload();
} catch (error) {
console.error("Transfer failed:", error);
setErrorMessage("An error occurred");
setIsLoading(false);
}
};
const transferToken = async (collection: any) => {
if (!selectedAccount || !tokenId) return;
const newParentTokenAddress = Address.nesting.idsToAddress(
+collectionParentId,
+tokenParentId
);
const fromCross = Address.extract.ethCrossAccountId(selectedAccount.address);
const tx = await collection.transferFrom(
fromCross.eth,
newParentTokenAddress,
+tokenId
);
await tx.wait();
};
return (
<Modal isVisible={isVisible} onClose={onClose} isFlexible={true}>
<ContentWrapper>
<h3>Nest token</h3>
<div className="form-item">
<input
type="text"
placeholder="Enter parent collection Id"
value={collectionParentId}
onChange={(e) => setCollectionParentId(e.target.value)}
/>
</div>
<div className="form-item">
<input
type="text"
placeholder="Enter parent token Id"
value={tokenParentId}
onChange={(e) => setTokenParentId(e.target.value)}
/>
</div>
{errorMessage && (
<div className="form-item">
<div className="error-message">{errorMessage}</div>
</div>
)}
{isLoading && <Loading>Transferring...</Loading>}
<ButtonWrapper>
<Button onClick={onSign} disabled={isLoading}>
Submit
</Button>
<Button onClick={onClose}>Cancel</Button>
</ButtonWrapper>
</ContentWrapper>
</Modal>
);
};