-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTransferNFTModal.tsx
More file actions
146 lines (130 loc) · 4.44 KB
/
TransferNFTModal.tsx
File metadata and controls
146 lines (130 loc) · 4.44 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
141
142
143
144
145
146
import { ChangeEvent, useState } from "react";
import { useParams } from "react-router-dom";
import { ethers } from "ethers";
import { Address } from "@unique-nft/utils";
import { useAccountsContext } from "../accounts/AccountsContext";
import { Account, SignerTypeEnum } from "../accounts/types";
import { Modal } from "../components/Modal";
import { useUniqueNFTFactory } from "../hooks/useUniqueNFTFactory";
import { ContentWrapper } from "./NestModal";
import { Button, ButtonWrapper, Loading } from "./UnnestModal";
import { switchNetwork } from "../utils/swithChain";
import { getCollection } from "../utils/getCollection";
import { useSdkContext } from "../sdk/SdkContext";
type TransferNFTModalProps = {
isVisible: boolean;
account?: Account;
onClose(): void;
};
export const TransferNFTModal = ({
isVisible,
onClose,
}: TransferNFTModalProps) => {
const { selectedAccount, magic, providerWeb3Auth } = useAccountsContext();
const { sdk } = useSdkContext();
const { tokenId, collectionId } = useParams<{
tokenId: string;
collectionId: string;
}>();
const [receiver, setReceiver] = useState<string>("");
const [isLoading, setIsLoading] = useState(false);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const onMessageChange = (e: ChangeEvent<HTMLInputElement>) => {
setReceiver(e.target.value);
};
const { getUniqueNFTFactory } = useUniqueNFTFactory(collectionId);
const onSign = async () => {
if (!sdk || !receiver || !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) {
setErrorMessage("Failed to initialize NFT collection.");
setIsLoading(false);
return;
}
const fromCross = Address.extract.ethCrossAccountId(
selectedAccount.address
);
const toCross = Address.extract.ethCrossAccountId(receiver.trim());
await (
await collection.transferFromCross(fromCross, toCross, +tokenId)
).wait();
} else if (
selectedAccount.signerType === SignerTypeEnum.Magiclink ||
selectedAccount.signerType === SignerTypeEnum.Web3Auth
) {
const provider = selectedAccount.signerType === SignerTypeEnum.Magiclink
? magic?.rpcProvider
: providerWeb3Auth;
if (!provider) {
throw new Error(`No provider for ${selectedAccount.signerType}`);
}
await transferNFTWithProvider(
provider,
collectionId,
tokenId,
selectedAccount.address,
receiver.trim()
);
} else {
await sdk.token.transfer({
to: receiver.trim(),
collectionId,
tokenId: +tokenId,
});
}
setIsLoading(false);
window.location.reload();
} catch (error) {
console.error("Transfer failed:", error);
setErrorMessage("An error occurred");
setIsLoading(false);
}
};
const transferNFTWithProvider = async(
provider: ethers.Eip1193Provider,
collectionId: string,
tokenId: string,
fromAddress: string,
toAddress: string
) => {
const collection = await getCollection(provider, collectionId);
const fromCross = Address.extract.ethCrossAccountId(fromAddress);
const toCross = Address.extract.ethCrossAccountId(toAddress.trim());
await (await collection.transferFromCross(fromCross, toCross, +tokenId)).wait();
}
return (
<Modal isVisible={isVisible} onClose={onClose} isFlexible={true}>
<ContentWrapper>
<h3>Transfer NFT</h3>
<div className="form-item">
<input
type="text"
placeholder="Enter address to transfer"
value={receiver}
onChange={onMessageChange}
/>
</div>
{errorMessage && (
<div className="form-item">
<div className="error-message">{errorMessage}</div>
</div>
)}
{isLoading && <Loading>Processing...</Loading>}
<ButtonWrapper>
<Button onClick={onSign} disabled={isLoading}>
Submit
</Button>
<Button onClick={onClose}>Cancel</Button>
</ButtonWrapper>
</ContentWrapper>
</Modal>
);
};