-
Notifications
You must be signed in to change notification settings - Fork 133
Add useBridgeNftFromEvm hook #2624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
513851a
Implemented cross-vm-receive-nft hook and updated demo
mfbz 3e9f6cd
Added changeset
mfbz e79e7ca
Fixed prettier check
mfbz d6ffa01
Merge commit '430bb0813bab6c2de59e152184256238bda20d99' into mfbz/hoo…
mfbz bd5b2e7
Fixed demo after master merge conflict
mfbz 622cf1c
Minor spacing fix
mfbz aa27405
Renamed hook
mfbz 12814f3
Improved changeset
mfbz 596b89a
Implemented playground cards for new hook and the opposite one for te…
mfbz e16ffdb
Merge branch 'master' into mfbz/hook-nft-evm-to-cadence
mfbz 95b5e5e
Prettier fix
mfbz 5cff1ac
Merge branch 'master' into mfbz/hook-nft-evm-to-cadence
mfbz 716eed4
Merge branch 'master' into mfbz/hook-nft-evm-to-cadence
mfbz 2dd6443
Merge branch 'master' into mfbz/hook-nft-evm-to-cadence
mfbz 3d8ff6e
Renamed crossvm nft hooks
mfbz 4d40847
Prettier fix
mfbz 34a22b2
Merge branch 'master' into mfbz/hook-nft-evm-to-cadence
mfbz 9d28358
Merge branch 'master' into mfbz/hook-nft-evm-to-cadence
mfbz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@onflow/react-sdk": minor | ||
| "@onflow/demo": minor | ||
| --- | ||
|
|
||
| Added hook to move NFT from evm to cadence |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
packages/demo/src/components/cards/cross-vm-receive-nft-card.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import {useCrossVmReceiveNft, useFlowConfig} from "@onflow/react-sdk" | ||
| import {useState, useMemo} from "react" | ||
| import {getContractAddress} from "../../constants" | ||
|
|
||
| export function CrossVmReceiveNftCard() { | ||
| const config = useFlowConfig() | ||
| const currentNetwork = config.flowNetwork || "emulator" | ||
| const [nftIdentifier, setNftIdentifier] = useState("") | ||
| const [nftId, setNftId] = useState("1") // NFT ID to bridge | ||
|
|
||
| const { | ||
| receiveNft, | ||
| isPending, | ||
| data: transactionId, | ||
| error, | ||
| } = useCrossVmReceiveNft() | ||
|
|
||
| const isNetworkSupported = currentNetwork === "testnet" | ||
|
|
||
| const exampleNftData = useMemo(() => { | ||
| if (currentNetwork !== "testnet") return null | ||
|
|
||
| const exampleNftAddress = getContractAddress("ExampleNFT", currentNetwork) | ||
| return { | ||
| name: "Example NFT", | ||
| nftIdentifier: `A.${exampleNftAddress.replace("0x", "")}.ExampleNFT.NFT`, | ||
| nftId: "1", | ||
| } | ||
| }, [currentNetwork]) | ||
|
|
||
| // Set default NFT identifier when network changes | ||
| useMemo(() => { | ||
| if (exampleNftData && !nftIdentifier) { | ||
| setNftIdentifier(exampleNftData.nftIdentifier) | ||
| } | ||
| }, [exampleNftData, nftIdentifier]) | ||
|
|
||
| const handleReceiveNft = () => { | ||
| receiveNft({ | ||
| nftIdentifier, | ||
| nftId, | ||
| }) | ||
| } | ||
|
|
||
| if (!isNetworkSupported) { | ||
| return ( | ||
| <div className="p-4 border border-gray-200 rounded-xl bg-white mb-8"> | ||
| <h2 className="text-black mt-0 mb-6 text-xl font-bold"> | ||
| useCrossVmReceiveNft | ||
| </h2> | ||
| <div className="p-4 bg-yellow-100 border border-yellow-200 rounded text-yellow-800"> | ||
| <p className="m-0"> | ||
| <strong>Network not supported:</strong> This feature is only | ||
| available on testnet. | ||
| </p> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| return ( | ||
| <div className="p-4 border border-gray-200 rounded-xl bg-white mb-8"> | ||
| <h2 className="text-black mt-0 mb-6 text-xl font-bold"> | ||
| useCrossVmReceiveNft | ||
| </h2> | ||
| <div className="mb-6"> | ||
| <label className="block mb-2 text-black"> | ||
| <strong>Note:</strong> Example prefilled with ExampleNFT type | ||
| identifier | ||
| </label> | ||
| <label className="block mb-2 text-black font-medium"> | ||
| NFT Identifier: | ||
| </label> | ||
| <input | ||
| type="text" | ||
| value={nftIdentifier} | ||
| onChange={e => setNftIdentifier(e.target.value)} | ||
| placeholder={ | ||
| exampleNftData | ||
| ? exampleNftData.nftIdentifier | ||
| : "e.g., A.dfc20aee650fcbdf.ExampleNFT.NFT" | ||
| } | ||
| className="p-3 border-2 border-[#00EF8B] rounded-md text-sm text-black bg-white | ||
| outline-none transition-colors duration-200 ease-in-out w-full mb-4 font-mono" | ||
| /> | ||
|
|
||
| <label className="block mb-2 text-black font-medium"> | ||
| NFT ID (UInt256): | ||
| </label> | ||
| <input | ||
| type="text" | ||
| value={nftId} | ||
| onChange={e => setNftId(e.target.value)} | ||
| placeholder="e.g., 1" | ||
| className="p-3 border-2 border-[#00EF8B] rounded-md text-sm text-black bg-white | ||
| outline-none transition-colors duration-200 ease-in-out w-full mb-4 font-mono" | ||
| /> | ||
|
|
||
| <button | ||
| onClick={handleReceiveNft} | ||
| className={`py-3 px-6 text-base font-semibold rounded-md transition-all duration-200 | ||
| ease-in-out mr-4 ${ | ||
| isPending || !nftIdentifier || !nftId | ||
| ? "bg-gray-300 text-gray-500 cursor-not-allowed" | ||
| : "bg-[#00EF8B] text-black cursor-pointer" | ||
| }`} | ||
| disabled={isPending || !nftIdentifier || !nftId} | ||
| > | ||
| {isPending ? "Receiving..." : "Receive NFT"} | ||
| </button> | ||
| </div> | ||
|
|
||
| <div className="p-4 bg-[#f8f9fa] rounded-md border border-[#00EF8B]"> | ||
| <h4 className="text-black m-0 mb-4">Transaction Status:</h4> | ||
|
|
||
| {isPending && ( | ||
| <p className="text-gray-500 m-0">Receiving NFT from EVM...</p> | ||
| )} | ||
|
|
||
| {error && ( | ||
| <div className="p-4 bg-red-100 border border-red-200 rounded text-red-800 m-0"> | ||
| <strong>Error:</strong> {error.message} | ||
| </div> | ||
| )} | ||
|
|
||
| {transactionId && !isPending && !error && ( | ||
| <div className="p-4 bg-green-100 border border-green-200 rounded m-0"> | ||
| <p className="text-green-800 m-0 mb-2"> | ||
| <strong>NFT received successfully!</strong> | ||
| </p> | ||
| <p className="text-green-800 m-0 font-mono"> | ||
| <strong>Transaction ID:</strong> {transactionId} | ||
| </p> | ||
| </div> | ||
| )} | ||
|
|
||
| {!transactionId && !isPending && !error && ( | ||
| <div className="text-gray-500 m-0"> | ||
| <p className="mb-2"> | ||
| Click "Receive NFT" to bridge an NFT from EVM to Cadence | ||
| </p> | ||
| </div> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
146 changes: 146 additions & 0 deletions
146
packages/demo/src/components/cards/cross-vm-spend-nft-card.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import {useCrossVmSpendNft, useFlowConfig} from "@onflow/react-sdk" | ||
| import {useState, useMemo} from "react" | ||
| import {getContractAddress} from "../../constants" | ||
|
|
||
| export function CrossVmSpendNftCard() { | ||
| const config = useFlowConfig() | ||
| const currentNetwork = config.flowNetwork || "emulator" | ||
| const [nftIdentifier, setNftIdentifier] = useState("") | ||
| const [nftIds, setNftIds] = useState("1") // NFT IDs to bridge (comma-separated) | ||
|
|
||
| const {spendNft, isPending, data: transactionId, error} = useCrossVmSpendNft() | ||
|
|
||
| const isNetworkSupported = currentNetwork === "testnet" | ||
|
|
||
| const exampleNftData = useMemo(() => { | ||
| if (currentNetwork !== "testnet") return null | ||
|
|
||
| const exampleNftAddress = getContractAddress("ExampleNFT", currentNetwork) | ||
| return { | ||
| name: "Example NFT", | ||
| nftIdentifier: `A.${exampleNftAddress.replace("0x", "")}.ExampleNFT.NFT`, | ||
| nftIds: "1", | ||
| } | ||
| }, [currentNetwork]) | ||
|
|
||
| // Set default NFT identifier when network changes | ||
| useMemo(() => { | ||
| if (exampleNftData && !nftIdentifier) { | ||
| setNftIdentifier(exampleNftData.nftIdentifier) | ||
| setNftIds(exampleNftData.nftIds) | ||
| } | ||
| }, [exampleNftData, nftIdentifier]) | ||
|
|
||
| const handleSpendNft = () => { | ||
| const nftIdArray = nftIds.split(",").map(id => id.trim()) | ||
|
|
||
| spendNft({ | ||
| nftIdentifier, | ||
| nftIds: nftIdArray, | ||
| calls: [], // No EVM calls, just bridging | ||
| }) | ||
| } | ||
|
|
||
| if (!isNetworkSupported) { | ||
| return ( | ||
| <div className="p-4 border border-gray-200 rounded-xl bg-white mb-8"> | ||
| <h2 className="text-black mt-0 mb-6 text-xl font-bold"> | ||
| useCrossVmSpendNft | ||
| </h2> | ||
| <div className="p-4 bg-yellow-100 border border-yellow-200 rounded text-yellow-800"> | ||
| <p className="m-0"> | ||
| <strong>Network not supported:</strong> This feature is only | ||
| available on testnet. | ||
| </p> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| return ( | ||
| <div className="p-4 border border-gray-200 rounded-xl bg-white mb-8"> | ||
| <h2 className="text-black mt-0 mb-6 text-xl font-bold"> | ||
| useCrossVmSpendNft | ||
| </h2> | ||
| <div className="mb-6"> | ||
| <label className="block mb-2 text-black"> | ||
| <strong>Note:</strong> Bridge NFTs from Cadence to EVM | ||
| </label> | ||
|
|
||
| <label className="block mb-2 text-black font-medium"> | ||
| NFT Identifier: | ||
| </label> | ||
| <input | ||
| type="text" | ||
| value={nftIdentifier} | ||
| onChange={e => setNftIdentifier(e.target.value)} | ||
| placeholder={ | ||
| exampleNftData | ||
| ? exampleNftData.nftIdentifier | ||
| : "e.g., A.012e4d204a60ac6f.ExampleNFT.NFT" | ||
| } | ||
| className="p-3 border-2 border-[#00EF8B] rounded-md text-sm text-black bg-white | ||
| outline-none transition-colors duration-200 ease-in-out w-full mb-4 font-mono" | ||
| /> | ||
|
|
||
| <label className="block mb-2 text-black font-medium"> | ||
| NFT IDs (UInt64, comma-separated): | ||
| </label> | ||
| <input | ||
| type="text" | ||
| value={nftIds} | ||
| onChange={e => setNftIds(e.target.value)} | ||
| placeholder="e.g., 1,2,3" | ||
| className="p-3 border-2 border-[#00EF8B] rounded-md text-sm text-black bg-white | ||
| outline-none transition-colors duration-200 ease-in-out w-full mb-4 font-mono" | ||
| /> | ||
|
|
||
| <button | ||
| onClick={handleSpendNft} | ||
| className={`py-3 px-6 text-base font-semibold rounded-md transition-all duration-200 | ||
| ease-in-out mr-4 ${ | ||
| isPending || !nftIdentifier || !nftIds | ||
| ? "bg-gray-300 text-gray-500 cursor-not-allowed" | ||
| : "bg-[#00EF8B] text-black cursor-pointer" | ||
| }`} | ||
| disabled={isPending || !nftIdentifier || !nftIds} | ||
| > | ||
| {isPending ? "Spending..." : "Spend NFT"} | ||
| </button> | ||
| </div> | ||
|
|
||
| <div className="p-4 bg-[#f8f9fa] rounded-md border border-[#00EF8B]"> | ||
| <h4 className="text-black m-0 mb-4">Transaction Status:</h4> | ||
|
|
||
| {isPending && ( | ||
| <p className="text-gray-500 m-0">Bridging NFTs to EVM...</p> | ||
| )} | ||
|
|
||
| {error && ( | ||
| <div className="p-4 bg-red-100 border border-red-200 rounded text-red-800 m-0"> | ||
| <strong>Error:</strong> {error.message} | ||
| </div> | ||
| )} | ||
|
|
||
| {transactionId && !isPending && !error && ( | ||
| <div className="p-4 bg-green-100 border border-green-200 rounded m-0"> | ||
| <p className="text-green-800 m-0 mb-2"> | ||
| <strong>NFTs spent successfully!</strong> | ||
| </p> | ||
| <p className="text-green-800 m-0 font-mono"> | ||
| <strong>Transaction ID:</strong> {transactionId} | ||
| </p> | ||
| </div> | ||
| )} | ||
|
|
||
| {!transactionId && !isPending && !error && ( | ||
| <div className="text-gray-500 m-0"> | ||
| <p className="mb-2"> | ||
| Click "Spend NFT" to bridge NFTs from Cadence to EVM | ||
| </p> | ||
| </div> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The label text says 'Example prefilled with ExampleNFT type identifier' but should be 'Example pre-filled with ExampleNFT type identifier' (hyphenated).