From 842b69a64c2f2cab0d77d9b7241baf9d9f9ad8ba Mon Sep 17 00:00:00 2001 From: Evgeny Taktarov Date: Thu, 21 Nov 2024 20:05:18 +0700 Subject: [PATCH 1/2] feat: async callback --- packages/sdk/src/core/core.ts | 12 ++++++------ packages/sdk/src/core/types.ts | 8 +++++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/sdk/src/core/core.ts b/packages/sdk/src/core/core.ts index 1d7e66f..3bd2c9f 100644 --- a/packages/sdk/src/core/core.ts +++ b/packages/sdk/src/core/core.ts @@ -518,7 +518,7 @@ export default class LidoSDKCore extends LidoSDKCacheable { nonce: 1, }; } else { - callback({ stage: TransactionCallbackStage.GAS_LIMIT }); + await callback({ stage: TransactionCallbackStage.GAS_LIMIT }); const feeData = await this.getFeeData(); overrides.maxFeePerGas = feeData.maxFeePerGas; overrides.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas; @@ -541,7 +541,7 @@ export default class LidoSDKCore extends LidoSDKCacheable { } } - const customGas = callback({ + const customGas = await callback({ stage: TransactionCallbackStage.SIGN, payload: overrides.gas, }); @@ -556,11 +556,11 @@ export default class LidoSDKCore extends LidoSDKCacheable { ); if (isContract) { - callback({ stage: TransactionCallbackStage.MULTISIG_DONE }); + await callback({ stage: TransactionCallbackStage.MULTISIG_DONE }); return { hash }; } - callback({ + await callback({ stage: TransactionCallbackStage.RECEIPT, payload: hash, }); @@ -574,7 +574,7 @@ export default class LidoSDKCore extends LidoSDKCacheable { ERROR_CODE.TRANSACTION_ERROR, ); - callback({ + await callback({ stage: TransactionCallbackStage.CONFIRMATION, payload: receipt, }); @@ -585,7 +585,7 @@ export default class LidoSDKCore extends LidoSDKCacheable { const result = await decodeResult?.(receipt); - callback({ + await callback({ stage: TransactionCallbackStage.DONE, payload: confirmations, }); diff --git a/packages/sdk/src/core/types.ts b/packages/sdk/src/core/types.ts index 7240865..8bb6acc 100644 --- a/packages/sdk/src/core/types.ts +++ b/packages/sdk/src/core/types.ts @@ -122,12 +122,18 @@ export type TransactionCallbackProps = | { stage: TransactionCallbackStage.MULTISIG_DONE; payload?: undefined } | { stage: TransactionCallbackStage.ERROR; payload: SDKError }; -export type TransactionCallbackResult = TProps extends { +// callback return type based on stage +type TransactionCallbackReturn = TProps extends { stage: TransactionCallbackStage.SIGN; } ? bigint | undefined : void; +// support both async and non async callbacks +export type TransactionCallbackResult = + | TransactionCallbackReturn + | Promise>; + export type TransactionCallback = ( props: TransactionCallbackProps, ) => TransactionCallbackResult; From 8ec22a670de2496f373df5da358bcc3927642efe Mon Sep 17 00:00:00 2001 From: Evgeny Taktarov Date: Thu, 21 Nov 2024 20:15:58 +0700 Subject: [PATCH 2/2] fix: lint --- packages/sdk/src/core/__tests__/core-wallet.test.ts | 2 +- packages/sdk/src/withdraw/request/request.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/sdk/src/core/__tests__/core-wallet.test.ts b/packages/sdk/src/core/__tests__/core-wallet.test.ts index 004f91f..2137573 100644 --- a/packages/sdk/src/core/__tests__/core-wallet.test.ts +++ b/packages/sdk/src/core/__tests__/core-wallet.test.ts @@ -178,7 +178,7 @@ describe('Account hoisting', () => { }); test('useAccount requests account from web3Provider', async () => { - const mockFn = jest.fn(); + const mockFn = jest.fn(); const mockTransport = useMockTransport(async (args, originalRequest) => { mockFn(args.method); if (args.method === 'eth_requestAccounts') { diff --git a/packages/sdk/src/withdraw/request/request.ts b/packages/sdk/src/withdraw/request/request.ts index 17e0876..8928424 100644 --- a/packages/sdk/src/withdraw/request/request.ts +++ b/packages/sdk/src/withdraw/request/request.ts @@ -234,7 +234,7 @@ export class LidoSDKWithdrawRequest extends BusModule { if (permitProp) { permit = permitProp; } else { - callback({ stage: TransactionCallbackStage.PERMIT }); + await callback({ stage: TransactionCallbackStage.PERMIT }); const isContract = await this.bus.core.isContract(account.address); invariant( !isContract,