|
| 1 | +import { |
| 2 | + type ExecutionPlan, |
| 3 | + SavingsGhoBalanceQuery, |
| 4 | + type SavingsGhoBalanceRequest, |
| 5 | + SavingsGhoDepositQuery, |
| 6 | + type SavingsGhoDepositRequest, |
| 7 | + SavingsGhoWithdrawQuery, |
| 8 | + type SavingsGhoWithdrawRequest, |
| 9 | + type TokenAmount, |
| 10 | + type TransactionRequest, |
| 11 | +} from '@aave/graphql'; |
| 12 | +import type { ResultAsync } from '@aave/types'; |
| 13 | +import type { AaveClient } from '../client'; |
| 14 | +import type { UnexpectedError } from '../errors'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Fetches the current sGHO balance for a user. |
| 18 | + * |
| 19 | + * ```ts |
| 20 | + * const result = await savingsGhoBalance(client, { |
| 21 | + * user: evmAddress('0x742d35cc…'), |
| 22 | + * }); |
| 23 | + * ``` |
| 24 | + * |
| 25 | + * @param client - Aave client. |
| 26 | + * @param request - The sGHO balance request parameters. |
| 27 | + * @returns The user's sGHO balance. |
| 28 | + */ |
| 29 | +export function savingsGhoBalance( |
| 30 | + client: AaveClient, |
| 31 | + request: SavingsGhoBalanceRequest, |
| 32 | +): ResultAsync<TokenAmount, UnexpectedError> { |
| 33 | + return client.query(SavingsGhoBalanceQuery, { request }); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Creates a transaction to withdraw sGHO. |
| 38 | + * |
| 39 | + * ```ts |
| 40 | + * const result = await savingsGhoWithdraw(client, { |
| 41 | + * amount: { |
| 42 | + * exact: '1000', |
| 43 | + * }, |
| 44 | + * sharesOwner: evmAddress('0x9abc…'), |
| 45 | + * }).andThen(sendWith(wallet)).andThen(client.waitForTransaction); |
| 46 | + * |
| 47 | + * if (result.isErr()) { |
| 48 | + * // Handle error, e.g. signing error, etc. |
| 49 | + * return; |
| 50 | + * } |
| 51 | + * |
| 52 | + * // result.value: TxHash |
| 53 | + * ``` |
| 54 | + * |
| 55 | + * @param client - Aave client. |
| 56 | + * @param request - The sGHO withdraw request parameters. |
| 57 | + * @returns The transaction request data to withdraw sGHO. |
| 58 | + */ |
| 59 | +export function savingsGhoWithdraw( |
| 60 | + client: AaveClient, |
| 61 | + request: SavingsGhoWithdrawRequest, |
| 62 | +): ResultAsync<TransactionRequest, UnexpectedError> { |
| 63 | + return client.query(SavingsGhoWithdrawQuery, { request }); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Creates a transaction to deposit GHO into sGHO. |
| 68 | + * |
| 69 | + * ```ts |
| 70 | + * const result = await savingsGhoDeposit(client, { |
| 71 | + * amount: { |
| 72 | + * value: '1000', |
| 73 | + * }, |
| 74 | + * depositor: evmAddress('0x9abc…'), |
| 75 | + * }).andThen(sendWith(wallet)).andThen(client.waitForTransaction); |
| 76 | + * |
| 77 | + * if (result.isErr()) { |
| 78 | + * // Handle error, e.g. insufficient balance, signing error, etc. |
| 79 | + * return; |
| 80 | + * } |
| 81 | + * |
| 82 | + * // result.value: TxHash |
| 83 | + * ``` |
| 84 | + * |
| 85 | + * @param client - Aave client. |
| 86 | + * @param request - The sGHO deposit request parameters. |
| 87 | + * @returns The transaction data, approval requirements, or insufficient balance error. |
| 88 | + */ |
| 89 | +export function savingsGhoDeposit( |
| 90 | + client: AaveClient, |
| 91 | + request: SavingsGhoDepositRequest, |
| 92 | +): ResultAsync<ExecutionPlan, UnexpectedError> { |
| 93 | + return client.query(SavingsGhoDepositQuery, { request }); |
| 94 | +} |
0 commit comments