Skip to content

Commit

Permalink
Merge pull request #19 from lidofinance/feature/si-762-withdrawals
Browse files Browse the repository at this point in the history
Withdrawals request
  • Loading branch information
DiRaiks committed Sep 6, 2023
2 parents 8f73602 + 6533bd3 commit 9c8a671
Show file tree
Hide file tree
Showing 28 changed files with 3,625 additions and 634 deletions.
385 changes: 378 additions & 7 deletions packages/sdk/README.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"clean": "rimraf dist"
},
"dependencies": {
"@ethersproject/bytes": "^5.7.0",
"tiny-invariant": "^1.3.1",
"viem": "^1.6.7"
},
Expand Down
91 changes: 91 additions & 0 deletions packages/sdk/src/common/decorators/ErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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,
context: ClassMethodDecoratorContext<
This,
(this: This, ...args: Args) => Return
>,
) {
const methodName = String(context.name);
const replacementMethod = function (this: This, ...args: Args): Return {
const callback = args[0].callback;

try {
const result = originalMethod.call(this, ...args);

if (result instanceof Promise) {
return result
.then((resolvedResult) => resolvedResult)
.catch((error) => {
callConsoleMessage(
headMessage,
`Error in method '${methodName}'.`,
'Error:',
);

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

throw txError;
}) as Return;
} else {
callConsoleMessage(headMessage, `Exiting method '${methodName}'.`);
return result;
}
} catch (error) {
callConsoleMessage(
headMessage,
`Error in method '${methodName}'.`,
'Error:',
);

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

throw txError;
}
};
return replacementMethod;
};
};
25 changes: 13 additions & 12 deletions packages/sdk/src/common/decorators/constants.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { HeadMessage } from "./types.js";
import { HeadMessage } from './types.js';

export const ConsoleCss: Record<HeadMessage, string> = {
"Provider:": "color: blue",
"Contracts:": "color: green",
"Balances:": "color: yellow",
"Utils:": "color: cyan",
"Views:": "color: aquamarine",
"Call:": "color: orange",
"Error:": "color: red",
"LOG:": "color: lightblue",
"Cache:": "color: mediumvioletred",
"Init:":
"color: #33F3FF;text-shadow: 0px 0px 0 #899CD5, 1px 1px 0 #8194CD, 2px 2px 0 #788BC4, 3px 3px 0 #6F82BB, 4px 4px 0 #677AB3, 5px 5px 0 #5E71AA, 6px 6px 0 #5568A1, 7px 7px 0 #4C5F98, 8px 8px 0 #445790, 9px 9px 0 #3B4E87, 10px 10px 0 #32457E, 11px 11px 0 #2A3D76, 12px 12px 0 #21346D, 13px 13px 0 #182B64, 14px 14px 0 #0F225B, 15px 15px 0 #071A53, 16px 16px 0 #02114A, 17px 17px 0 #0B0841, 18px 18px 0 #130039, 19px 19px 0 #1C0930, 20px 20px 0 #251227, 21px 21px 20px rgba(0,0,0,1), 21px 21px 1px rgba(0,0,0,0.5), 0px 0px 20px rgba(0,0,0,.2);font-size: 50px;",
'Provider:': 'color: blue',
'Contracts:': 'color: green',
'Balances:': 'color: yellow',
'Utils:': 'color: cyan',
'Views:': 'color: aquamarine',
'Call:': 'color: orange',
'Error:': 'color: red',
'LOG:': 'color: lightblue',
'Cache:': 'color: mediumvioletred',
'Permit:': 'color: lime',
'Init:':
'color: #33F3FF;text-shadow: 0px 0px 0 #899CD5, 1px 1px 0 #8194CD, 2px 2px 0 #788BC4, 3px 3px 0 #6F82BB, 4px 4px 0 #677AB3, 5px 5px 0 #5E71AA, 6px 6px 0 #5568A1, 7px 7px 0 #4C5F98, 8px 8px 0 #445790, 9px 9px 0 #3B4E87, 10px 10px 0 #32457E, 11px 11px 0 #2A3D76, 12px 12px 0 #21346D, 13px 13px 0 #182B64, 14px 14px 0 #0F225B, 15px 15px 0 #071A53, 16px 16px 0 #02114A, 17px 17px 0 #0B0841, 18px 18px 0 #130039, 19px 19px 0 #1C0930, 20px 20px 0 #251227, 21px 21px 20px rgba(0,0,0,1), 21px 21px 1px rgba(0,0,0,0.5), 0px 0px 20px rgba(0,0,0,.2);font-size: 50px;',
};
1 change: 1 addition & 0 deletions packages/sdk/src/common/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Cache } from './cache.js';
export { Logger } from './logger.js';
export { Initialize } from './Initialize.js';
export { ErrorHandler } from './ErrorHandler.js';
12 changes: 6 additions & 6 deletions packages/sdk/src/common/decorators/logger.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { callConsoleMessage } from "./utils.js";
import { HeadMessage } from "./types.js";
import { callConsoleMessage } from './utils.js';
import { HeadMessage } from './types.js';

export const Logger = function (headMessage: HeadMessage = "LOG:") {
export const Logger = function (headMessage: HeadMessage = 'LOG:') {
return function LoggerMethod<This, Args extends any[], Return>(
originalMethod: (this: This, ...args: Args) => Return,
context: ClassMethodDecoratorContext<
This,
(this: This, ...args: Args) => Return
>
>,
) {
const methodName = String(context.name);

Expand All @@ -24,8 +24,8 @@ export const Logger = function (headMessage: HeadMessage = "LOG:") {
.catch((error) => {
callConsoleMessage(
headMessage,
`Exiting method '${methodName}' with error: ${error}.`,
"Error:"
`Exiting method '${methodName}' with error.`,
'Error:',
);
throw error;
}) as Return;
Expand Down
21 changes: 11 additions & 10 deletions packages/sdk/src/common/decorators/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
export type HeadMessage =
| "Provider:"
| "Contracts:"
| "Balances:"
| "Utils:"
| "Views:"
| "Call:"
| "Error:"
| "LOG:"
| "Cache:"
| "Init:";
| 'Provider:'
| 'Contracts:'
| 'Balances:'
| 'Utils:'
| 'Views:'
| 'Call:'
| 'Error:'
| 'LOG:'
| 'Cache:'
| 'Init:'
| 'Permit:';
9 changes: 5 additions & 4 deletions packages/sdk/src/common/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { checkIsContract } from "./checkIsContract.js";
export { getFeeData, type FeeData } from "./getFeeData.js";
export { SDKError, type SDKErrorProps } from "./SDKError.js";
export { getErrorMessage, type ErrorMessage } from "./getErrorMessage.js";
export { checkIsContract } from './checkIsContract.js';
export { getFeeData, type FeeData } from './getFeeData.js';
export { SDKError, type SDKErrorProps } from './SDKError.js';
export { getErrorMessage, type ErrorMessage } from './getErrorMessage.js';
export { isBigint } from './isBigint.js';
3 changes: 3 additions & 0 deletions packages/sdk/src/common/utils/isBigint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isBigint = (value: unknown): value is BigInt => {
return typeof value === 'bigint';
};
10 changes: 9 additions & 1 deletion packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ export { LidoSDK } from './sdk.js';
export { type SDKError } from './common/utils/SDKError.js';
export * from './common/decorators/index.js';
export {
StakeCallbackStage,
StakeCallbackStages,
StakeStageCallback,
StakeProps,
} from './staking/index.js';
export {
RequestCallbackStages,
RequestStageCallback,
RequestProps,
RequestWithPermitProps,
ApproveCallbackStages,
ApproveStageCallback,
} from './withdrawals/index.js';
export { LIDO_CONTRACT_NAMES } from './common/constants.js';
10 changes: 7 additions & 3 deletions packages/sdk/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { LidoSDKCore, LidoSDKCoreProps } from "./core/index.js";
import { LidoSDKStaking } from "./staking/index.js";
import { LidoSDKCore, LidoSDKCoreProps } from './core/index.js';
import { LidoSDKStaking } from './staking/index.js';
import { LidoSDKWithdrawals } from './withdrawals/index.js';

import { version } from "./version.js";
import { version } from './version.js';

export class LidoSDK {
readonly core: LidoSDKCore;
readonly staking: LidoSDKStaking;
readonly withdrawals: LidoSDKWithdrawals;

constructor(props: LidoSDKCoreProps) {
// Core functionality
this.core = new LidoSDKCore(props, version);
// Staking functionality
this.staking = new LidoSDKStaking({ ...props, core: this.core });
// Withdrawals functionality
this.withdrawals = new LidoSDKWithdrawals({ ...props, core: this.core });
}
}
Loading

0 comments on commit 9c8a671

Please sign in to comment.