Created
March 20, 2024 22:58
-
-
Save Yuripetusko/9bf5a3c3c27c6bafa00d89152299092d to your computer and use it in GitHub Desktop.
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
| type Props = { | |
| collectionId: EthereumAddress; | |
| onSuccess?: (data: TransactionReceipt) => void; | |
| onSettled?: (data?: TransactionReceipt) => void; | |
| onError?: (error?: Error) => void; | |
| network?: EVM_NETWORKS; | |
| newOwner: EthereumAddress; | |
| enabled?: boolean; | |
| }; | |
| export const useChangeOwner = ({ | |
| collectionId, | |
| onSuccess, | |
| onSettled, | |
| onError, | |
| network, | |
| newOwner, | |
| enabled, | |
| }: Props) => { | |
| const urlNetwork = useUrlNetwork<EVM_NETWORKS>(); | |
| const networkToUse = network || urlNetwork; | |
| const chainId = networkToUse ? getChainIdByNetwork(networkToUse) : undefined; | |
| const { | |
| data: config, | |
| refetch, | |
| isLoading: isLoadingPrepare, | |
| isError: isErrorPrepare, | |
| error: errorPrepare, | |
| } = useSimulateContract({ | |
| chainId, | |
| address: collectionId, | |
| abi: RMRKNestableMultiAssetImpl, | |
| args: [newOwner], | |
| functionName: 'transferOwnership', | |
| query: { enabled: enabled && !!chainId }, | |
| }); | |
| const { | |
| writeContractAsync, | |
| data: transactionHash, | |
| isPending: isLoadingWrite, | |
| isError: isErrorWrite, | |
| error: errorWrite, | |
| } = useWriteContract(); | |
| const { | |
| isFetching: isFetchingReceipt, | |
| isLoading: isLoadingReceipt, | |
| data: receipt, | |
| isFetched, | |
| isSuccess, | |
| isError: isErrorReceipt, | |
| error: errorTransaction, | |
| } = useWaitForTransactionReceipt({ | |
| hash: transactionHash, | |
| query: { | |
| enabled, | |
| }, | |
| }); | |
| useWriteContractCallbacks({ | |
| receipt, | |
| isFetched, | |
| onSuccess, | |
| onSettled, | |
| onError, | |
| error: errorWrite, | |
| }); | |
| const onChangeOwner = async () => { | |
| if (config && enabled) { | |
| try { | |
| return await writeContractAsync(config.request); | |
| } catch (e) { | |
| onError?.(errorWrite instanceof Error ? errorWrite : new Error('Something went wrong')); | |
| } | |
| } | |
| return; | |
| }; | |
| const isLoading = isLoadingReceipt || isLoadingPrepare || isLoadingWrite || isFetchingReceipt; | |
| const isError = isErrorWrite || isErrorReceipt || isErrorPrepare; | |
| const error = errorWrite || errorTransaction || errorPrepare; | |
| return { | |
| error, | |
| isLoading, | |
| isSuccess, | |
| isError, | |
| onChangeOwner, | |
| refetch, | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment