-
Notifications
You must be signed in to change notification settings - Fork 128
Add useCrossVmSpendNft
hook
#2460
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 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1b12b8a
Add `useFullTokenBalance` hook
jribbink 797fc01
Merge remote-tracking branch 'origin/master' into jribbink/cross-vm-b…
jribbink 46146b2
Add Cross VM NFT Spend Transcation
jribbink 874607d
Merge remote-tracking branch 'origin/master' into jribbink/cross-vm-n…
jribbink b5658cf
delete unused
jribbink 7ed0e72
Add cross VM FT Spend hook
jribbink e40371f
Only return txid & fix tests
jribbink 6b62cb3
Merge branch 'master' into jribbink/cross-vm-nft-spend
jribbink c9e2a80
Merge branch 'master' into feature/cross-vm-hooks
jribbink 10449ed
Merge branch 'feature/cross-vm-hooks' into jribbink/cross-vm-nft-spend
jribbink 9787eb8
Add `useCrossVmSpendNft` hook
jribbink 62c98f6
delete extra
jribbink 282dfc8
add export
jribbink 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,5 @@ | ||
--- | ||
"@onflow/kit": minor | ||
--- | ||
|
||
Add `useCrossVmSpendNft` hook |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import {renderHook, act, waitFor} from "@testing-library/react" | ||
import * as fcl from "@onflow/fcl" | ||
import {FlowProvider} from "../provider" | ||
import { | ||
getCrossVmSpendNftransaction, | ||
useCrossVmSpendNft, | ||
} from "./useCrossVmSpendNft" | ||
import {useFlowChainId} from "./useFlowChainId" | ||
|
||
jest.mock("@onflow/fcl", () => require("../__mocks__/fcl").default) | ||
jest.mock("viem", () => ({ | ||
encodeFunctionData: jest.fn(), | ||
bytesToHex: jest.fn(x => `0x${x}`), | ||
})) | ||
jest.mock("./useFlowChainId", () => ({ | ||
useFlowChainId: jest.fn(), | ||
})) | ||
|
||
describe("useBatchEvmTransaction", () => { | ||
const mockCalls = [ | ||
{ | ||
address: "0x123", | ||
abi: [{type: "function", name: "test"}], | ||
functionName: "test", | ||
args: [1, 2], | ||
gasLimit: BigInt(100000), | ||
value: BigInt(0), | ||
}, | ||
] | ||
|
||
const mockTxId = "0x123" | ||
const mockTxResult = { | ||
events: [ | ||
{ | ||
type: "TransactionExecuted", | ||
data: { | ||
hash: ["1", "2", "3"], | ||
errorCode: "0", | ||
errorMessage: "", | ||
}, | ||
}, | ||
], | ||
} | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
jest.mocked(useFlowChainId).mockReturnValue({ | ||
data: "mainnet", | ||
isLoading: false, | ||
} as any) | ||
}) | ||
|
||
describe("getCrossVmSpendNftTransaction", () => { | ||
it("should return correct cadence for mainnet", () => { | ||
const result = getCrossVmSpendNftransaction("mainnet") | ||
expect(result).toContain("import EVM from 0xe467b9dd11fa00df") | ||
}) | ||
|
||
it("should return correct cadence for testnet", () => { | ||
const result = getCrossVmSpendNftransaction("testnet") | ||
expect(result).toContain("import EVM from 0x8c5303eaa26202d6") | ||
}) | ||
|
||
it("should throw error for unsupported chain", () => { | ||
expect(() => getCrossVmSpendNftransaction("unsupported")).toThrow( | ||
"Unsupported chain: unsupported" | ||
) | ||
}) | ||
}) | ||
|
||
describe("useCrossVmBatchTransaction", () => { | ||
test("should handle successful transaction", async () => { | ||
jest.mocked(fcl.mutate).mockResolvedValue(mockTxId) | ||
jest.mocked(fcl.tx).mockReturnValue({ | ||
onceExecuted: jest.fn().mockResolvedValue(mockTxResult), | ||
} as any) | ||
|
||
let result: any | ||
let rerender: any | ||
await act(async () => { | ||
;({result, rerender} = renderHook(useCrossVmSpendNft, { | ||
wrapper: FlowProvider, | ||
})) | ||
}) | ||
|
||
await act(async () => { | ||
await result.current.spendNft({ | ||
calls: mockCalls, | ||
nftIdentifier: "nft123", | ||
nftIds: ["1", "2"], | ||
}) | ||
rerender() | ||
}) | ||
|
||
await waitFor(() => result.current.isPending === false) | ||
|
||
expect(result.current.isError).toBe(false) | ||
expect(result.current.data).toBe(mockTxId) | ||
}) | ||
|
||
it("should handle missing chain ID", async () => { | ||
;(useFlowChainId as jest.Mock).mockReturnValue({ | ||
data: null, | ||
isLoading: false, | ||
}) | ||
|
||
let hookResult: any | ||
|
||
await act(async () => { | ||
const {result} = renderHook(() => useCrossVmSpendNft(), { | ||
wrapper: FlowProvider, | ||
}) | ||
hookResult = result | ||
}) | ||
|
||
await act(async () => { | ||
await hookResult.current.spendNft({calls: mockCalls}) | ||
}) | ||
|
||
await waitFor(() => expect(hookResult.current.isError).toBe(true)) | ||
expect(hookResult.current.error?.message).toBe("No current chain found") | ||
}) | ||
|
||
it("should handle loading chain ID", async () => { | ||
;(useFlowChainId as jest.Mock).mockReturnValue({ | ||
data: null, | ||
isLoading: true, | ||
}) | ||
|
||
let hookResult: any | ||
|
||
await act(async () => { | ||
const {result} = renderHook(() => useCrossVmSpendNft(), { | ||
wrapper: FlowProvider, | ||
}) | ||
hookResult = result | ||
}) | ||
|
||
await act(async () => { | ||
await hookResult.current.spendNft(mockCalls) | ||
}) | ||
|
||
await waitFor(() => expect(hookResult.current.isError).toBe(true)) | ||
expect(hookResult.current.error?.message).toBe("No current chain found") | ||
}) | ||
|
||
it("should handle mutation error", async () => { | ||
;(fcl.mutate as jest.Mock).mockRejectedValue(new Error("Mutation failed")) | ||
|
||
let hookResult: any | ||
|
||
await act(async () => { | ||
const {result} = renderHook(() => useCrossVmSpendNft(), { | ||
wrapper: FlowProvider, | ||
}) | ||
hookResult = result | ||
}) | ||
|
||
await act(async () => { | ||
await hookResult.current.spendNft({ | ||
calls: mockCalls, | ||
nftIdentifier: "nft123", | ||
nftIds: ["1", "2"], | ||
}) | ||
}) | ||
|
||
await waitFor(() => expect(hookResult.current.isError).toBe(true)) | ||
expect(hookResult.current.error?.message).toBe("Mutation failed") | ||
}) | ||
}) | ||
}) |
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.
Are we doing emulator or pushing that once we figure out how to solve it better?
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.
Yup, once the changes have been pushed thru to the CLI we can do the emulator but right now the VM bridge doesn't deploy.