Skip to content

Commit 81c0a88

Browse files
committed
feat: add react hooks for deposit/withdraw/balance sGHO
1 parent dfd9c58 commit 81c0a88

File tree

3 files changed

+159
-13
lines changed

3 files changed

+159
-13
lines changed

packages/react/src/gho.ts

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
}

packages/react/src/incentives.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import type {
2-
MeritClaimRewardsRequest,
3-
MeritClaimRewardsTransaction,
4-
} from '@aave/graphql';
5-
import { MeritClaimRewardsQuery } from '@aave/graphql';
1+
import type { UserMeritRewards, UserMeritRewardsRequest } from '@aave/graphql';
2+
import { UserMeritRewardsQuery } from '@aave/graphql';
63
import type {
74
ReadResult,
85
Suspendable,
@@ -11,7 +8,7 @@ import type {
118
} from './helpers';
129
import { useSuspendableQuery } from './helpers';
1310

14-
export type UseMeritClaimRewardsArgs = MeritClaimRewardsRequest;
11+
export type UserMeritRewardsArgs = UserMeritRewardsRequest;
1512

1613
/**
1714
* Fetches Merit claim rewards for a user with the transaction request to claim them.
@@ -27,8 +24,8 @@ export type UseMeritClaimRewardsArgs = MeritClaimRewardsRequest;
2724
* ```
2825
*/
2926
export function useMeritClaimRewards(
30-
args: UseMeritClaimRewardsArgs & Suspendable,
31-
): SuspenseResult<MeritClaimRewardsTransaction | null>;
27+
args: UserMeritRewardsArgs & Suspendable,
28+
): SuspenseResult<UserMeritRewards | null>;
3229

3330
/**
3431
* Fetches Merit claim rewards for a user with the transaction request to claim them.
@@ -41,17 +38,17 @@ export function useMeritClaimRewards(
4138
* ```
4239
*/
4340
export function useMeritClaimRewards(
44-
args: UseMeritClaimRewardsArgs,
45-
): ReadResult<MeritClaimRewardsTransaction | null>;
41+
args: UserMeritRewardsArgs,
42+
): ReadResult<UserMeritRewards | null>;
4643

4744
export function useMeritClaimRewards({
4845
suspense = false,
4946
...request
50-
}: UseMeritClaimRewardsArgs & {
47+
}: UserMeritRewardsArgs & {
5148
suspense?: boolean;
52-
}): SuspendableResult<MeritClaimRewardsTransaction | null> {
49+
}): SuspendableResult<UserMeritRewards | null> {
5350
return useSuspendableQuery({
54-
document: MeritClaimRewardsQuery,
51+
document: UserMeritRewardsQuery,
5552
variables: {
5653
request,
5754
},

packages/react/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from '@aave/client';
22

33
export * from './AaveProvider';
44
export { useAaveClient } from './context';
5+
export * from './gho';
56
export * from './incentives';
67
export * from './markets';
78
export * from './misc';

0 commit comments

Comments
 (0)