|
| 1 | +import type { UnexpectedError } from '@aave/client'; |
| 2 | +import { savingsGhoDeposit, savingsGhoWithdraw } from '@aave/client/actions'; |
| 3 | +import type { |
| 4 | + ExecutionPlan, |
| 5 | + SavingsGhoBalanceRequest, |
| 6 | + SavingsGhoDepositRequest, |
| 7 | + SavingsGhoWithdrawRequest, |
| 8 | + TokenAmount, |
| 9 | + TransactionRequest, |
| 10 | +} from '@aave/graphql'; |
| 11 | +import { SavingsGhoBalanceQuery } from '@aave/graphql'; |
| 12 | +import { useAaveClient } from './context'; |
| 13 | +import type { |
| 14 | + ReadResult, |
| 15 | + Suspendable, |
| 16 | + SuspendableResult, |
| 17 | + SuspenseResult, |
| 18 | +} from './helpers'; |
| 19 | +import { |
| 20 | + type UseAsyncTask, |
| 21 | + useAsyncTask, |
| 22 | + useSuspendableQuery, |
| 23 | +} from './helpers'; |
| 24 | + |
| 25 | +export type SavingsGhoBalanceArgs = SavingsGhoBalanceRequest; |
| 26 | + |
| 27 | +/** |
| 28 | + * Fetches the current sGHO balance for a user. |
| 29 | + * |
| 30 | + * This signature supports React Suspense: |
| 31 | + * |
| 32 | + * ```tsx |
| 33 | + * const { data } = useSavingsGhoBalance({ |
| 34 | + * user: evmAddress('0x742d35cc…'), |
| 35 | + * suspense: true, |
| 36 | + * }); |
| 37 | + * ``` |
| 38 | + */ |
| 39 | +export function useSavingsGhoBalance( |
| 40 | + args: SavingsGhoBalanceArgs & Suspendable, |
| 41 | +): SuspenseResult<TokenAmount>; |
| 42 | + |
| 43 | +/** |
| 44 | + * Fetches the current sGHO balance for a user. |
| 45 | + * |
| 46 | + * ```tsx |
| 47 | + * const { data, loading } = useSavingsGhoBalance({ |
| 48 | + * user: evmAddress('0x742d35cc…'), |
| 49 | + * }); |
| 50 | + * ``` |
| 51 | + */ |
| 52 | +export function useSavingsGhoBalance( |
| 53 | + args: SavingsGhoBalanceArgs, |
| 54 | +): ReadResult<TokenAmount>; |
| 55 | + |
| 56 | +export function useSavingsGhoBalance({ |
| 57 | + suspense = false, |
| 58 | + ...request |
| 59 | +}: SavingsGhoBalanceArgs & { |
| 60 | + suspense?: boolean; |
| 61 | +}): SuspendableResult<TokenAmount> { |
| 62 | + return useSuspendableQuery({ |
| 63 | + document: SavingsGhoBalanceQuery, |
| 64 | + variables: { |
| 65 | + request, |
| 66 | + }, |
| 67 | + suspense, |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * A hook that provides a way to withdraw sGHO. |
| 73 | + * |
| 74 | + * ```ts |
| 75 | + * const [withdraw, withdrawing] = useSavingsGhoWithdraw(); |
| 76 | + * const [sendTransaction, sending] = useSendTransaction(wallet); |
| 77 | + * |
| 78 | + * const loading = withdrawing.loading && sending.loading; |
| 79 | + * const error = withdrawing.error || sending.error; |
| 80 | + * |
| 81 | + * // … |
| 82 | + * |
| 83 | + * const result = await withdraw({ ... }) |
| 84 | + * .andThen(sendTransaction); |
| 85 | + * |
| 86 | + * if (result.isErr()) { |
| 87 | + * console.error(result.error); |
| 88 | + * return; |
| 89 | + * } |
| 90 | + * |
| 91 | + * console.log('Transaction sent with hash:', result.value); |
| 92 | + * ``` |
| 93 | + */ |
| 94 | +export function useSavingsGhoWithdraw(): UseAsyncTask< |
| 95 | + SavingsGhoWithdrawRequest, |
| 96 | + TransactionRequest, |
| 97 | + UnexpectedError |
| 98 | +> { |
| 99 | + const client = useAaveClient(); |
| 100 | + |
| 101 | + return useAsyncTask((request: SavingsGhoWithdrawRequest) => |
| 102 | + savingsGhoWithdraw(client, request), |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * A hook that provides a way to deposit GHO into sGHO. |
| 108 | + * |
| 109 | + * ```ts |
| 110 | + * const [deposit, depositing] = useSavingsGhoDeposit(); |
| 111 | + * const [sendTransaction, sending] = useSendTransaction(wallet); |
| 112 | + * |
| 113 | + * const loading = depositing.loading && sending.loading; |
| 114 | + * const error = depositing.error || sending.error; |
| 115 | + * |
| 116 | + * // … |
| 117 | + * |
| 118 | + * const result = await deposit({ ... }) |
| 119 | + * .andThen((plan) => { |
| 120 | + * switch (plan.__typename) { |
| 121 | + * case 'TransactionRequest': |
| 122 | + * return sendTransaction(plan); |
| 123 | + * |
| 124 | + * case 'ApprovalRequired': |
| 125 | + * return sendTransaction(plan.approval) |
| 126 | + * .andThen(() => sendTransaction(plan.originalTransaction)); |
| 127 | + * } |
| 128 | + * }); |
| 129 | + * |
| 130 | + * if (result.isErr()) { |
| 131 | + * console.error(result.error); |
| 132 | + * return; |
| 133 | + * } |
| 134 | + * |
| 135 | + * console.log('Transaction sent with hash:', result.value); |
| 136 | + * ``` |
| 137 | + */ |
| 138 | +export function useSavingsGhoDeposit(): UseAsyncTask< |
| 139 | + SavingsGhoDepositRequest, |
| 140 | + ExecutionPlan, |
| 141 | + UnexpectedError |
| 142 | +> { |
| 143 | + const client = useAaveClient(); |
| 144 | + |
| 145 | + return useAsyncTask((request: SavingsGhoDepositRequest) => |
| 146 | + savingsGhoDeposit(client, request), |
| 147 | + ); |
| 148 | +} |
0 commit comments