Skip to content

Commit

Permalink
fix: update errorHandler decorator, fix context
Browse files Browse the repository at this point in the history
  • Loading branch information
DiRaiks committed Sep 6, 2023
1 parent 3fe0c7a commit 6533bd3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 27 deletions.
58 changes: 42 additions & 16 deletions packages/sdk/src/common/decorators/ErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
import { type LidoSDKCore } from '../../core/index.js';
import { callConsoleMessage } from './utils.js';
import { HeadMessage } from './types.js';

const isBus = function (
value: unknown,
): value is { bus: { core?: LidoSDKCore } } {
return !!value && typeof value === 'object' && 'bus' in value;
};

const isCore = function (value: unknown): value is { core?: LidoSDKCore } {
return !!value && typeof value === 'object' && 'core' in value;
};

const extractError = function <This>(this: This, error: unknown) {
let txError = error;

if (isBus(this)) {
const { message, code } = (this.bus.core as LidoSDKCore)?.getErrorMessage?.(
error,
);

txError = (this.bus?.core as LidoSDKCore)?.error?.({
message,
error,
code,
});
} else if (isCore(this)) {
const { message, code } = (this.core as LidoSDKCore)?.getErrorMessage?.(
error,
);
txError = (this.core as LidoSDKCore)?.error?.({
message,
error,
code,
});
}

return txError;
};

export const ErrorHandler = function (headMessage: HeadMessage = 'Error:') {
return function ErrorHandlerMethod<This, Args extends any[], Return>(
originalMethod: (this: This, ...args: Args) => Return,
Expand All @@ -25,14 +63,8 @@ export const ErrorHandler = function (headMessage: HeadMessage = 'Error:') {
`Error in method '${methodName}'.`,
'Error:',
);
// @ts-ignore
const { message, code } = this.core.getErrorMessage(error);
// @ts-ignore
const txError = this.core.error({
message,
error,
code,
});

let txError = extractError.call(this, error);
callback?.({ stage: 'error', payload: txError });

throw txError;
Expand All @@ -47,14 +79,8 @@ export const ErrorHandler = function (headMessage: HeadMessage = 'Error:') {
`Error in method '${methodName}'.`,
'Error:',
);
// @ts-ignore
const { message, code } = this.core.getErrorMessage(error);
// @ts-ignore
const txError = this.core.error({
message,
error,
code,
});

let txError = extractError.call(this, error);
callback?.({ stage: 'error', payload: txError });

throw txError;
Expand Down
14 changes: 9 additions & 5 deletions packages/sdk/src/withdrawals/withdrawals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class LidoSDKWithdrawals extends Bus {

callback?.({ stage: RequestCallbackStages.SIGN, payload: gasLimit });

const transaction = await tokenRequestMethod([...params], {
const transaction = await tokenRequestMethod.call(this, [...params], {
...overrides,
chain: this.core.chain,
});
Expand Down Expand Up @@ -206,7 +206,7 @@ export class LidoSDKWithdrawals extends Bus {

callback?.({ stage: RequestCallbackStages.SIGN, payload: gasLimit });

const transaction = await tokenRequestMethod([...params], {
const transaction = await tokenRequestMethod.call(this, [...params], {
chain: this.core.chain,
gas: gasLimit,
...overrides,
Expand Down Expand Up @@ -253,7 +253,7 @@ export class LidoSDKWithdrawals extends Bus {

callback?.({ stage: RequestCallbackStages.SIGN });

const transaction = await tokenRequestMethod([...params], {
const transaction = await tokenRequestMethod.call(this, [...params], {
account,
chain: this.core.chain,
});
Expand Down Expand Up @@ -298,7 +298,9 @@ export class LidoSDKWithdrawals extends Bus {
},
] as const;

const gasLimit = await tokenRequestMethod([...params], { account });
const gasLimit = await tokenRequestMethod.call(this, [...params], {
account,
});

return gasLimit;
}
Expand All @@ -320,7 +322,9 @@ export class LidoSDKWithdrawals extends Bus {
else tokenRequestMethod = contract.estimateGas.requestWithdrawalsWstETH;

const params = [requests, account] as const;
const gasLimit = await tokenRequestMethod([...params], { account });
const gasLimit = await tokenRequestMethod.call(this, [...params], {
account,
});

return gasLimit;
}
Expand Down
16 changes: 10 additions & 6 deletions packages/sdk/src/withdrawals/withdrawalsApprove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class LidoSDKWithdrawalsApprove {

callback?.({ stage: ApproveCallbackStages.GAS_LIMIT });

const gasLimit = await gasLimitMethod(amount, account);
const gasLimit = await gasLimitMethod.call(this, amount, account);
const feeData = await this.bus.core.getFeeData();
const overrides = {
account,
Expand All @@ -90,7 +90,8 @@ export class LidoSDKWithdrawalsApprove {

callback?.({ stage: ApproveCallbackStages.SIGN, payload: gasLimit });

const transaction = await tokenApproveMethod(
const transaction = await tokenApproveMethod.call(
this,
[addressWithdrawalsQueue, parseEther(amount)],
{ chain: this.bus.core.chain, ...overrides, gas: gasLimit },
);
Expand Down Expand Up @@ -156,7 +157,8 @@ export class LidoSDKWithdrawalsApprove {

callback?.({ stage: ApproveCallbackStages.SIGN });

const transaction = await tokenApproveMethod(
const transaction = await tokenApproveMethod.call(
this,
[addressWithdrawalsQueue, parseEther(amount)],
{ chain: this.bus.core.chain, account },
);
Expand All @@ -183,7 +185,7 @@ export class LidoSDKWithdrawalsApprove {
}

@Logger('Utils:')
@Cache(30 * 1000, ['core.chain.id'])
@Cache(30 * 1000, ['bus.core.chain.id'])
private async approveGasLimitByToken(
amount: string,
account: Address,
Expand All @@ -204,7 +206,8 @@ export class LidoSDKWithdrawalsApprove {
const addressWithdrawalsQueue =
await this.bus.contract.contractAddressWithdrawalsQueue();

const gasLimit = await estimateGasMethod(
const gasLimit = await estimateGasMethod.call(
this,
[addressWithdrawalsQueue, parseEther(amount)],
{ account },
);
Expand Down Expand Up @@ -245,7 +248,8 @@ export class LidoSDKWithdrawalsApprove {
const addressWithdrawalsQueue =
await this.bus.contract.contractAddressWithdrawalsQueue();

const allowance = await allowanceMethod(
const allowance = await allowanceMethod.call(
this,
[account, addressWithdrawalsQueue],
{ account },
);
Expand Down

0 comments on commit 6533bd3

Please sign in to comment.