|
1 | | -import { useAccount, useDisconnect, useConnectors, useCallsStatus } from 'wagmi' |
| 1 | +import { |
| 2 | + useAccount, |
| 3 | + useDisconnect, |
| 4 | + useConnectors, |
| 5 | + useCallsStatus, |
| 6 | + useSendCalls, |
| 7 | + useWaitForCallsStatus, |
| 8 | +} from 'wagmi' |
2 | 9 | import * as React from 'react' |
| 10 | +import { parseEther } from 'viem' |
3 | 11 | import { Hooks } from 'porto/wagmi' |
4 | | -import { type Errors, Hex, Json } from 'ox' |
5 | 12 | import { useConnect } from 'porto/wagmi/Hooks' |
| 13 | +import { type Errors, type Hex, Json } from 'ox' |
6 | 14 | import { useMutation } from '@tanstack/react-query' |
7 | 15 |
|
8 | 16 | import { |
@@ -352,90 +360,67 @@ function GrantPermissions() { |
352 | 360 |
|
353 | 361 | function Fund() { |
354 | 362 | const { address, chain } = useAccount() |
| 363 | + const balance = useBalance() |
355 | 364 |
|
356 | | - const addFundsMutation = useMutation({ |
357 | | - mutationFn: async (event: React.FormEvent<HTMLFormElement>) => { |
358 | | - event.preventDefault() |
359 | | - event.stopPropagation() |
360 | | - |
361 | | - const result = await porto.provider.request({ |
362 | | - method: 'wallet_addFunds', |
363 | | - params: [ |
364 | | - { |
365 | | - address, |
366 | | - token: exp1Config.address, |
367 | | - value: Hex.fromString('100'), |
368 | | - }, |
369 | | - ], |
370 | | - }) |
| 365 | + const sendCalls = useSendCalls() |
371 | 366 |
|
372 | | - return result |
373 | | - }, |
374 | | - }) |
375 | | - const { |
376 | | - data: txHashData, |
377 | | - isLoading: isConfirming, |
378 | | - isSuccess: isConfirmed, |
379 | | - } = useCallsStatus({ |
380 | | - id: addFundsMutation.data?.id as Hex.Hex, |
381 | | - query: { |
382 | | - enabled: !!addFundsMutation.data?.id, |
383 | | - refetchInterval: ({ state }) => { |
384 | | - if (state.data?.status === 'success') return false |
385 | | - return 1_000 |
386 | | - }, |
387 | | - }, |
388 | | - }) |
| 367 | + const { isLoading: isConfirming, isSuccess: isConfirmed } = |
| 368 | + useWaitForCallsStatus({ |
| 369 | + id: sendCalls.data?.id, |
| 370 | + }) |
389 | 371 |
|
390 | | - const blockExplorer = chain?.blockExplorers?.default?.url |
| 372 | + const explorerUrl = chain?.blockExplorers?.default?.url |
391 | 373 | const transactionLink = (hash: string) => |
392 | | - blockExplorer ? `${blockExplorer}/tx/${hash}` : hash |
393 | | - |
394 | | - const balance = useBalance() |
395 | | - |
396 | | - const [transactions, setTransactions] = React.useState<Set<string>>(new Set()) |
397 | | - React.useEffect(() => { |
398 | | - if (!txHashData?.id) return |
399 | | - const hash = txHashData.receipts?.at(0)?.transactionHash |
400 | | - if (!hash) return |
401 | | - setTransactions((prev) => new Set([...prev, hash])) |
402 | | - }, [txHashData?.id, txHashData?.receipts]) |
| 374 | + explorerUrl ? `${explorerUrl}/tx/${hash}` : hash |
403 | 375 |
|
404 | 376 | return ( |
405 | 377 | <div> |
406 | 378 | <h3> |
407 | 379 | [client] Fund Wallet with EXP [balance:{' '} |
408 | 380 | {`${Number(balance).toFixed(2)} EXP`}] |
409 | 381 | </h3> |
410 | | - <form onSubmit={addFundsMutation.mutate}> |
| 382 | + <form |
| 383 | + onSubmit={(event) => { |
| 384 | + event.preventDefault() |
| 385 | + event.stopPropagation() |
| 386 | + sendCalls.sendCalls({ |
| 387 | + calls: [ |
| 388 | + { |
| 389 | + ...exp1Config, |
| 390 | + args: [address!, parseEther('100')], |
| 391 | + functionName: 'mint', |
| 392 | + to: exp1Config.address, |
| 393 | + }, |
| 394 | + ], |
| 395 | + }) |
| 396 | + }} |
| 397 | + > |
411 | 398 | <button |
412 | 399 | type="submit" |
413 | | - disabled={addFundsMutation.status === 'pending'} |
| 400 | + disabled={sendCalls.status === 'pending'} |
414 | 401 | style={{ marginBottom: '5px' }} |
415 | 402 | > |
416 | | - {addFundsMutation.status === 'pending' ? 'Confirming…' : 'Fund'} |
| 403 | + {sendCalls.status === 'pending' ? 'Confirming…' : 'Fund'} |
417 | 404 | </button> |
418 | 405 | </form> |
419 | 406 | <ul style={{ listStyleType: 'none', padding: 0 }}> |
420 | | - {Array.from(transactions).map((tx) => ( |
421 | | - <li key={tx}> |
422 | | - <a |
423 | | - target="_blank" |
424 | | - rel="noopener noreferrer" |
425 | | - href={transactionLink(tx)} |
426 | | - > |
427 | | - {tx} |
428 | | - </a> |
429 | | - </li> |
430 | | - ))} |
| 407 | + {sendCalls.data?.id && ( |
| 408 | + <a |
| 409 | + target="_blank" |
| 410 | + rel="noopener noreferrer" |
| 411 | + href={transactionLink(sendCalls.data!.id)} |
| 412 | + > |
| 413 | + {sendCalls.data?.id} |
| 414 | + </a> |
| 415 | + )} |
431 | 416 | </ul> |
432 | 417 | <p>{isConfirming && 'Waiting for confirmation...'}</p> |
433 | 418 | <p>{isConfirmed && 'Transaction confirmed.'}</p> |
434 | | - {addFundsMutation.error && ( |
| 419 | + {sendCalls.error && ( |
435 | 420 | <div> |
436 | 421 | Error:{' '} |
437 | | - {(addFundsMutation.error as Errors.BaseError).shortMessage || |
438 | | - addFundsMutation.error.message} |
| 422 | + {(sendCalls.error as Errors.BaseError).shortMessage || |
| 423 | + sendCalls.error.message} |
439 | 424 | </div> |
440 | 425 | )} |
441 | 426 | </div> |
|
0 commit comments