|
| 1 | +import { delay, GqlClient, TimeoutError, UnexpectedError } from '@aave/core'; |
| 2 | +import type { HasProcessedKnownTransactionRequest } from '@aave/graphql'; |
| 3 | +import { invariant, okAsync, ResultAsync, type TxHash } from '@aave/types'; |
| 4 | +import { hasProcessedKnownTransaction } from './actions'; |
| 5 | +import { type ClientConfig, configureContext } from './config'; |
| 6 | +import { |
| 7 | + isHasProcessedKnownTransactionRequest, |
| 8 | + type TransactionExecutionResult, |
| 9 | +} from './types'; |
| 10 | + |
| 11 | +export class AaveClient extends GqlClient { |
| 12 | + /** |
| 13 | + * Create a new instance of the {@link AaveClient}. |
| 14 | + * |
| 15 | + * ```ts |
| 16 | + * const client = AaveClient.create({ |
| 17 | + * environment: production, |
| 18 | + * }); |
| 19 | + * ``` |
| 20 | + * |
| 21 | + * @param options - The options to configure the client. |
| 22 | + * @returns The new instance of the client. |
| 23 | + */ |
| 24 | + static create(options?: ClientConfig): AaveClient { |
| 25 | + return new AaveClient(configureContext(options ?? {})); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @internal |
| 30 | + */ |
| 31 | + readonly waitForSupportedTransaction = ( |
| 32 | + result: TransactionExecutionResult, |
| 33 | + ): ResultAsync<TxHash, TimeoutError | UnexpectedError> => { |
| 34 | + if (isHasProcessedKnownTransactionRequest(result)) { |
| 35 | + return this.waitForTransaction(result); |
| 36 | + } |
| 37 | + return okAsync(result.txHash); |
| 38 | + }; |
| 39 | + |
| 40 | + /** |
| 41 | + * Given the transaction hash of an Aave protocol transaction, wait for the transaction to be |
| 42 | + * processed by the Aave v3 API. |
| 43 | + * |
| 44 | + * Returns a {@link TimeoutError} if the transaction is not processed within the expected timeout period. |
| 45 | + * |
| 46 | + * @param result - The transaction execution result to wait for. |
| 47 | + * @returns The transaction hash or a TimeoutError |
| 48 | + */ |
| 49 | + readonly waitForTransaction = ( |
| 50 | + result: TransactionExecutionResult, |
| 51 | + ): ResultAsync<TxHash, TimeoutError | UnexpectedError> => { |
| 52 | + invariant( |
| 53 | + isHasProcessedKnownTransactionRequest(result), |
| 54 | + `Received a transaction result for an untracked operation. Make sure you're following the instructions in the docs.`, |
| 55 | + ); |
| 56 | + |
| 57 | + return ResultAsync.fromPromise( |
| 58 | + this.pollTransactionStatus(result), |
| 59 | + (err) => { |
| 60 | + if (err instanceof TimeoutError || err instanceof UnexpectedError) { |
| 61 | + return err; |
| 62 | + } |
| 63 | + return UnexpectedError.from(err); |
| 64 | + }, |
| 65 | + ); |
| 66 | + }; |
| 67 | + |
| 68 | + protected async pollTransactionStatus( |
| 69 | + request: HasProcessedKnownTransactionRequest, |
| 70 | + ): Promise<TxHash> { |
| 71 | + const startedAt = Date.now(); |
| 72 | + |
| 73 | + while (Date.now() - startedAt < this.context.environment.indexingTimeout) { |
| 74 | + const processed = await hasProcessedKnownTransaction(this, request).match( |
| 75 | + (ok) => ok, |
| 76 | + (err) => { |
| 77 | + throw err; |
| 78 | + }, |
| 79 | + ); |
| 80 | + |
| 81 | + if (processed) { |
| 82 | + return request.txHash; |
| 83 | + } |
| 84 | + |
| 85 | + await delay(this.context.environment.pollingInterval); |
| 86 | + } |
| 87 | + throw TimeoutError.from( |
| 88 | + `Timeout waiting for transaction ${request.txHash} to be processed.`, |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
0 commit comments