From 9c82abf711de43081b13d9aee06cf4129b3286f9 Mon Sep 17 00:00:00 2001 From: Andrei Date: Wed, 20 Sep 2023 23:25:21 +0700 Subject: [PATCH 1/4] feat: change modules' structure and naming BREAKING CHANGE: rename staking, withdrawals modules --- packages/sdk/README.md | 267 +++++++++++++++--- packages/sdk/package.json | 16 +- packages/sdk/src/index.ts | 4 +- packages/sdk/src/sdk.ts | 12 +- .../sdk/src/{staking => stake}/abi/steth.ts | 0 packages/sdk/src/stake/index.ts | 2 + .../{staking/staking.ts => stake/stake.ts} | 24 +- packages/sdk/src/{staking => stake}/types.ts | 2 +- packages/sdk/src/staking/index.ts | 2 - .../abi/partStETH.ts | 0 .../abi/partWstETH.ts | 0 .../abi/withdrawalQueue.ts} | 2 +- packages/sdk/src/withdraw/bus.ts | 147 ++++++++++ .../{withdrawals => withdraw}/claim/claim.ts | 8 +- .../{withdrawals => withdraw}/claim/index.ts | 2 +- .../{withdrawals => withdraw}/claim/types.ts | 0 .../src/{withdrawals => withdraw}/index.ts | 2 +- .../request/approve.ts | 12 +- packages/sdk/src/withdraw/request/index.ts | 2 + .../request/request.ts | 12 +- .../request/types.ts | 0 .../src/{withdrawals => withdraw}/types.ts | 2 +- packages/sdk/src/withdraw/withdraw.ts | 10 + .../withdrawContract.ts} | 18 +- .../withdrawRequestsInfo.ts} | 2 +- .../withdrawViews.ts} | 22 +- packages/sdk/src/withdrawals/bus.ts | 145 ---------- packages/sdk/src/withdrawals/request/index.ts | 1 - packages/sdk/src/withdrawals/withdrawals.ts | 10 - 29 files changed, 453 insertions(+), 273 deletions(-) rename packages/sdk/src/{staking => stake}/abi/steth.ts (100%) create mode 100644 packages/sdk/src/stake/index.ts rename packages/sdk/src/{staking/staking.ts => stake/stake.ts} (91%) rename packages/sdk/src/{staking => stake}/types.ts (92%) delete mode 100644 packages/sdk/src/staking/index.ts rename packages/sdk/src/{withdrawals => withdraw}/abi/partStETH.ts (100%) rename packages/sdk/src/{withdrawals => withdraw}/abi/partWstETH.ts (100%) rename packages/sdk/src/{withdrawals/abi/withdrawalsQueue.ts => withdraw/abi/withdrawalQueue.ts} (99%) create mode 100644 packages/sdk/src/withdraw/bus.ts rename packages/sdk/src/{withdrawals => withdraw}/claim/claim.ts (98%) rename packages/sdk/src/{withdrawals => withdraw}/claim/index.ts (64%) rename packages/sdk/src/{withdrawals => withdraw}/claim/types.ts (100%) rename packages/sdk/src/{withdrawals => withdraw}/index.ts (82%) rename packages/sdk/src/{withdrawals => withdraw}/request/approve.ts (95%) create mode 100644 packages/sdk/src/withdraw/request/index.ts rename packages/sdk/src/{withdrawals => withdraw}/request/request.ts (98%) rename packages/sdk/src/{withdrawals => withdraw}/request/types.ts (100%) rename packages/sdk/src/{withdrawals => withdraw}/types.ts (89%) create mode 100644 packages/sdk/src/withdraw/withdraw.ts rename packages/sdk/src/{withdrawals/withdrawalsContract.ts => withdraw/withdrawContract.ts} (86%) rename packages/sdk/src/{withdrawals/withdrawalsRequestsInfo.ts => withdraw/withdrawRequestsInfo.ts} (99%) rename packages/sdk/src/{withdrawals/withdrawalsViews.ts => withdraw/withdrawViews.ts} (95%) delete mode 100644 packages/sdk/src/withdrawals/bus.ts delete mode 100644 packages/sdk/src/withdrawals/request/index.ts delete mode 100644 packages/sdk/src/withdrawals/withdrawals.ts diff --git a/packages/sdk/README.md b/packages/sdk/README.md index de340bec..ae1fe6cc 100644 --- a/packages/sdk/README.md +++ b/packages/sdk/README.md @@ -13,22 +13,33 @@ The project is currently under development and may change in the future. - [Usage](#usage) - [Initialization](#initialization) - [Examples](#examples) - - [Core](#core) - - [Staking](#staking) + + - [Core](#core-example) + - [Stake](#stake-example) + - [Withdraw](#withdraw-example) + - [Wrap](#wrap-example) + - [Stake](#stake) - [Call](#call) - [Populate transaction](#populate-transaction) - [Simulate transaction](#simulate-transaction) - [Wrap](#wrap) -- [Withdrawal](#withdrawal) + - [Calls](#calls) + - [Wrap ETH](#wrap-eth) + - [Wrap stETH](#wrap-steth) + - [Unwrap](#unwrap) + - [Wrap utilities](#wrap-utilities) +- [Withdraw](#withdraw) - [Call](#call-1) - [Send request withdrawal with Permit](#send-request-withdrawal-with-permit) - [Send request withdrawal with preallocated allowance](#send-request-withdrawal-with-preallocated-allowance) - [`Request` other methods](#request-other-methods) + - [Claim requests](#claim-requests) + - [`Claim` other methods](#claim-other-methods) - - [Utils](#utils) + - [Withdraw utilities](#withdraw-utilities) - [Set allowance of WithdrawalQueue contract](#set-allowance-of-withdrawalqueue-contract) - [`Allowance` other methods](#allowance-other-methods) @@ -38,7 +49,7 @@ The project is currently under development and may change in the future. - [Constants](#constants) - [Requests info](#requests-info) -- [(w)stETH](<#(w)steth>) +- [(w)stETH](#wsteth) - [unstETH NFT](#unsteth-nft) - [Lido contract addresses](#lido-contract-addresses) @@ -63,8 +74,11 @@ npm install @lidofinance/lido-ethereum-sdk The Lido Ethereum SDK consists of several modules: - **Core** - provides access to the SDK core functionality -- **Staking** - provides access to the Lido staking functionality - TODO: add more modules +- **Stake** - provides access to the Lido staking functionality +- **Withdraw** - provides access to the Lido withdrawals functionality +- **Wrap** - provides access to the Lido wrap functionality +- **(w)stETH** - provides access to the stETH and wstETH tokens functionality +- **unstETH NFT** - provides access to the unstETH NFT functionality ## Usage @@ -73,16 +87,36 @@ To get started with the Lido Ethereum SDK, you need to import the necessary modu ```ts const { LidoSDK } = require('@lidofinance/lido-ethereum-sdk'); // or -const { LidoSDKStaking } = require('@lidofinance/lido-ethereum-sdk/staking'); +const { LidoSDKStake } = require('@lidofinance/lido-ethereum-sdk/stake'); +// or +const { LidoSDKWithdraw } = require('@lidofinance/lido-ethereum-sdk/withdraw'); +// or +const { LidoSDKWrap } = require('@lidofinance/lido-ethereum-sdk/wrap'); // or const { LidoSDKCore } = require('@lidofinance/lido-ethereum-sdk/core'); +// or +const { LidoSDKstETH } = require('@lidofinance/lido-ethereum-sdk/erc20'); +// or +const { LidoSDKwstETH } = require('@lidofinance/lido-ethereum-sdk/erc20'); +// or +const { LidoSDKUnstETH } = require('@lidofinance/lido-ethereum-sdk/unsteth'); // Or, if you are using ES6+: import { LidoSDK } from '@lidofinance/lido-ethereum-sdk'; // or -import { LidoSDKStaking } from '@lidofinance/lido-ethereum-sdk/staking'; +import { LidoSDKStake } from '@lidofinance/lido-ethereum-sdk/stake'; +// or +import { LidoSDKWithdraw } from '@lidofinance/lido-ethereum-sdk/withdraw'; +// or +import { LidoSDKWrap } from '@lidofinance/lido-ethereum-sdk/wrap'; // or import { LidoSDKCore } from '@lidofinance/lido-ethereum-sdk/core'; +// or +import { LidoSDKstETH } from '@lidofinance/lido-ethereum-sdk/erc20'; +// or +import { LidoSDKwstETH } from '@lidofinance/lido-ethereum-sdk/erc20'; +// or +import { LidoSDKUnstETH } from '@lidofinance/lido-ethereum-sdk/unsteth'; ``` ## Initialization @@ -114,7 +148,7 @@ Replace "https://eth-goerli.alchemyapi.io/v2/{ALCHEMY_API_KEY}" with the address ## Examples -### Core +### Core example ```ts const lidoSDK = new LidoSDK({ @@ -128,7 +162,7 @@ const balanceETH = await lidoSDK.core.balanceETH(address); console.log(balanceETH.toString(), 'ETH balance'); ``` -### Staking +### Stake example ```ts const lidoSDK = new LidoSDK({ @@ -137,11 +171,11 @@ const lidoSDK = new LidoSDK({ }); // Contracts -const addressStETH = await lidoSDK.staking.contractAddressStETH(); -const contractStETH = await lidoSDK.staking.getContractStETH(); +const addressStETH = await lidoSDK.stake.contractAddressStETH(); +const contractStETH = await lidoSDK.stake.getContractStETH(); // Calls -const stakeResult = await lidoSDK.staking.stake({ +const stakeResult = await lidoSDK.stake.stakeEth({ value, callback, referralAddress, @@ -153,6 +187,57 @@ console.log(contractStETH, 'stETH contract'); console.log(stakeResult, 'stake result'); ``` +### Withdraw example + +```ts +const lidoSDK = new LidoSDK({ + chainId: 5, + rpcUrls: ['https://eth-goerli.alchemyapi.io/v2/{ALCHEMY_API_KEY}'], +}); + +// Contracts +const addressWithdrawalQueue = + await lidoSDK.withdraw.contract.contractAddressWithdrawalQueue(); +const contractWithdrawalQueue = + await lidoSDK.withdraw.contract.getContractWithdrawalQueue(); + +// Calls +const requestResult = await lidoSDK.withdraw.request.requestByToken({ + account, + requests: requestsArray, + token: 'stETH', + callback, +}); + +console.log(addressWithdrawalQueue, 'Withdrawal Queue contract address'); +console.log(contractWithdrawalQueue, 'Withdrawal Queue contract'); +console.log(requestResult, 'request result'); +``` + +### Wrap example + +```ts +const lidoSDK = new LidoSDK({ + chainId: 5, + rpcUrls: ['https://eth-goerli.alchemyapi.io/v2/{ALCHEMY_API_KEY}'], +}); + +// Contracts +const addressWstETH = await lidoSDK.wrap.contractAddressWstETH(); +const contractWstETH = await lidoSDK.withdraw.getContractWstETH(); + +// Calls +const wrapResult = await lidoSDK.wrap.wrapEth({ + value, + account, + callback, +}); + +console.log(addressWstETH, 'wstETH contract address'); +console.log(contractWstETH, 'wstETH contract'); +console.log(wrapResult, 'wrap result'); +``` + ## Stake ### Call @@ -214,7 +299,7 @@ const callback: StakeStageCallback = ({ stage, payload }) => { }; try { - const stakeResult = await lidoSDK.staking.stake({ + const stakeResult = await lidoSDK.stake.stakeEth({ value, callback, referralAddress, @@ -240,7 +325,7 @@ const lidoSDK = new LidoSDK({ chainId: 5, }); -const populateResult = await lidoSDK.staking.stakePopulateTx({ +const populateResult = await lidoSDK.stake.stakeEthPopulateTx({ value, callback, referralAddress, @@ -260,7 +345,7 @@ const lidoSDK = new LidoSDK({ chainId: 5, }); -const simulateResult = await lidoSDK.staking.stakeSimulateTx({ +const simulateResult = await lidoSDK.staking.stakeEthSimulateTx({ value, callback, referralAddress, @@ -270,7 +355,9 @@ const simulateResult = await lidoSDK.staking.stakeSimulateTx({ ## Wrap -### Wrap ETH +### Calls + +#### Wrap ETH Arguments: @@ -334,7 +421,7 @@ try { } ``` -### Wrap stETH +#### Wrap stETH To wrap stETH you first need to approve stETH to wrap contract: @@ -365,7 +452,7 @@ const approveResult = await lidoSDK.wrap.approveStethForWrap({ const wrapResult = await lidoSDK.wrap.wrapSteth({ value, account, callback }); ``` -### Unwrap +#### Unwrap ```ts // unwrap wstETH to receive stETH @@ -376,7 +463,7 @@ const unwrapResult = await lidoSDK.wrap.unwrap({ }); ``` -### Utilities +### Wrap utilities For all transaction methods helper methods are available similar to `stake` module: @@ -385,7 +472,7 @@ For all transaction methods helper methods are available similar to `stake` modu For `wrapEth` only `wrapEthEstimateGas` is available instead of `simulateTx` but you can use it all the same for checking transaction validity. -## Withdrawal +## Withdraw ### Call @@ -393,11 +480,18 @@ For `wrapEth` only `wrapEthEstimateGas` is available instead of `simulateTx` but `Supports EOA and Multisig` +Arguments: + +- `requests`: (Type: bigint[] ) - array of requests ids +- `token`: (Type: string) - token name ('stETH' | 'wstETH') +- `callback`: (Type: TransactionCallback) - callback function that will be on each stage of the transaction +- `account` (Type: Address): The account address. + ```ts import { LidoSDK, - RequestStageCallback, - RequestCallbackStages, + TransactionCallback, + TransactionCallbackStage, SDKError, } from '@lidofinance/lido-ethereum-sdk'; @@ -409,34 +503,34 @@ const lidoSDK = new LidoSDK({ // Define default web3 provider in sdk (window.ethereum) if web3Provider is not defined in constructor lidoSDK.core.defineWeb3Provider(); -const callback: RequestStageCallback = ({ stage, payload }) => { +const callback: TransactionCallback = ({ stage, payload }) => { switch (stage) { - case RequestCallbackStages.PERMIT: + case TransactionCallbackStage.PERMIT: console.log('wait for permit'); break; - case RequestCallbackStages.GAS_LIMIT: + case TransactionCallbackStage.GAS_LIMIT: console.log('wait for gas limit'); break; - case RequestCallbackStages.SIGN: + case TransactionCallbackStage.SIGN: console.log('wait for sign'); break; - case RequestCallbackStages.RECEIPT: + case TransactionCallbackStage.RECEIPT: console.log('wait for receipt'); console.log(payload, 'transaction hash'); break; - case RequestCallbackStages.CONFIRMATION: + case TransactionCallbackStage.CONFIRMATION: console.log('wait for confirmation'); console.log(payload, 'transaction receipt'); break; - case RequestCallbackStages.DONE: + case TransactionCallbackStage.DONE: console.log('done'); console.log(payload, 'transaction confirmations'); break; - case RequestCallbackStages.MULTISIG_DONE: + case TransactionCallbackStage.MULTISIG_DONE: console.log('multisig_done'); console.log(payload, 'transaction confirmations'); break; - case RequestCallbackStages.ERROR: + case TransactionCallbackStage.ERROR: console.log('error'); console.log(payload, 'error object with code and message'); break; @@ -465,11 +559,18 @@ try { `Supports EOA and Multisig` +Arguments: + +- `requests`: (Type: bigint[] ) - array of requests ids +- `token`: (Type: string) - token name ('stETH' | 'wstETH') +- `callback`: (Type: TransactionCallback) - callback function that will be on each stage of the transaction +- `account` (Type: Address): The account address. + ```ts import { LidoSDK, - RequestStageCallback, - RequestCallbackStages, + TransactionCallback, + TransactionCallbackStage, SDKError, } from '@lidofinance/lido-ethereum-sdk'; @@ -481,27 +582,27 @@ const lidoSDK = new LidoSDK({ // Define default web3 provider in sdk (window.ethereum) if web3Provider is not defined in constructor lidoSDK.core.defineWeb3Provider(); -const callback: RequestStageCallback = ({ stage, payload }) => { +const callback: TransactionCallback = ({ stage, payload }) => { switch (stage) { - case RequestCallbackStages.GAS_LIMIT: + case TransactionCallbackStage.GAS_LIMIT: console.log('wait for gas limit'); break; - case RequestCallbackStages.SIGN: + case TransactionCallbackStage.SIGN: console.log('wait for sign'); break; - case RequestCallbackStages.RECEIPT: + case TransactionCallbackStage.RECEIPT: console.log('wait for receipt'); console.log(payload, 'transaction hash'); break; - case RequestCallbackStages.CONFIRMATION: + case TransactionCallbackStage.CONFIRMATION: console.log('wait for confirmation'); console.log(payload, 'transaction receipt'); break; - case RequestCallbackStages.DONE: + case TransactionCallbackStage.DONE: console.log('done'); console.log(payload, 'transaction confirmations'); break; - case RequestCallbackStages.ERROR: + case TransactionCallbackStage.ERROR: console.log('error'); console.log(payload, 'error object with code and message'); break; @@ -543,7 +644,87 @@ try { - requestWstethMultisig - requestMultisigByToken -### Utils +#### Claim requests + +Arguments: + +- `account`: (Type: Address ): The account address. +- `requestsIds`: (Type: bigint[]): An array of request ids. +- `hints` (Type: bigint[]): An array of hints for each request. +- `callback`: (Type: TransactionCallback): callback function that will be on each stage of the transaction + +```ts +import { + LidoSDK, + TransactionCallback, + TransactionCallbackStage, + SDKError, +} from '@lidofinance/lido-ethereum-sdk'; + +const lidoSDK = new LidoSDK({ + rpcUrls: ['https://rpc-url'], + chainId: 5, +}); + +// Define default web3 provider in sdk (window.ethereum) if web3Provider is not defined in constructor +lidoSDK.core.defineWeb3Provider(); + +const callback: TransactionCallback = ({ stage, payload }) => { + switch (stage) { + case TransactionCallbackStage.GAS_LIMIT: + console.log('wait for gas limit'); + break; + case TransactionCallbackStage.SIGN: + console.log('wait for sign'); + break; + case TransactionCallbackStage.RECEIPT: + console.log('wait for receipt'); + console.log(payload, 'transaction hash'); + break; + case TransactionCallbackStage.CONFIRMATION: + console.log('wait for confirmation'); + console.log(payload, 'transaction receipt'); + break; + case TransactionCallbackStage.DONE: + console.log('done'); + console.log(payload, 'transaction confirmations'); + break; + case TransactionCallbackStage.ERROR: + console.log('error'); + console.log(payload, 'error object with code and message'); + break; + default: + } +}; + +try { + const claimResult = await lidoSDK.withdrawals.claim.claimRequests({ + account, + requestsIds, + hints, + callback, + }); + + console.log( + claimResult, + 'transaction hash, transaction receipt, confirmations', + ); +} catch (error) { + console.log((error as SDKError).errorMessage, (error as SDKError).code); +} +``` + +#### `Claim` other methods + +##### EOA + +- claimRequestsEOA + +##### Multisig + +- claimRequestsMultisig + +### Withdraw utilities #### Set allowance of WithdrawalQueue contract diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 0f367e04..0570e81e 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -26,20 +26,20 @@ "default": "./dist/cjs/core/index.js", "types": "./dist/types/core/index.d.ts" }, - "./staking": { - "import": "./dist/esm/staking/index.js", - "default": "./dist/cjs/staking/index.js", - "types": "./dist/types/staking/index.d.ts" + "./stake": { + "import": "./dist/esm/stake/index.js", + "default": "./dist/cjs/stake/index.js", + "types": "./dist/types/stake/index.d.ts" }, "./wrap": { "import": "./dist/esm/wrap/index.js", "default": "./dist/cjs/wrap/index.js", "types": "./dist/types/wrap/index.d.ts" }, - "./withdrawals": { - "import": "./dist/esm/withdrawals/index.js", - "default": "./dist/cjs/withdrawals/index.js", - "types": "./dist/types/withdrawals/index.d.ts" + "./withdraw": { + "import": "./dist/esm/withdraw/index.js", + "default": "./dist/cjs/withdraw/index.js", + "types": "./dist/types/withdraw/index.d.ts" }, "./erc20": { "import": "./dist/esm/erc20/index.js", diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index bd4bddfe..f836230d 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -5,7 +5,7 @@ export { TransactionCallbackStage, type TransactionCallback, } from './core/index.js'; -export { type StakeProps } from './staking/index.js'; +export { type StakeProps } from './stake/index.js'; export { type ClaimRequestsProps, type RequestProps, @@ -13,6 +13,6 @@ export { type ApproveCallbackStages, type ApproveStageCallback, type RequestStatusWithId, -} from './withdrawals/index.js'; +} from './withdraw/index.js'; export { LIDO_CONTRACT_NAMES } from './common/constants.js'; export { type WrapProps } from './wrap/index.js'; diff --git a/packages/sdk/src/sdk.ts b/packages/sdk/src/sdk.ts index 4863b1a7..a9b624b3 100644 --- a/packages/sdk/src/sdk.ts +++ b/packages/sdk/src/sdk.ts @@ -1,8 +1,8 @@ import { LidoSDKCore, LidoSDKCoreProps } from './core/index.js'; -import { LidoSDKStaking } from './staking/index.js'; +import { LidoSDKStake } from './stake/index.js'; import { LidoSDKWrap } from './wrap/index.js'; -import { LidoSDKWithdrawals } from './withdrawals/index.js'; +import { LidoSDKWithdraw } from './withdraw/index.js'; import { LidoSDKstETH, LidoSDKwstETH } from './erc20/index.js'; import { LidoSDKUnstETH } from './unsteth/unsteth.js'; @@ -10,9 +10,9 @@ import { version } from './version.js'; export class LidoSDK { readonly core: LidoSDKCore; - readonly staking: LidoSDKStaking; + readonly stake: LidoSDKStake; readonly wrap: LidoSDKWrap; - readonly withdrawals: LidoSDKWithdrawals; + readonly withdraw: LidoSDKWithdraw; readonly steth: LidoSDKstETH; readonly wsteth: LidoSDKwstETH; readonly unsteth: LidoSDKUnstETH; @@ -22,11 +22,11 @@ export class LidoSDK { this.core = new LidoSDKCore(props, version); const core = this.core; // Staking functionality - this.staking = new LidoSDKStaking({ ...props, core }); + this.stake = new LidoSDKStake({ ...props, core }); // Wrap functionality this.wrap = new LidoSDKWrap({ ...props, core }); // Withdrawals functionality - this.withdrawals = new LidoSDKWithdrawals({ ...props, core }); + this.withdraw = new LidoSDKWithdraw({ ...props, core }); // Tokens functionality this.steth = new LidoSDKstETH({ ...props, core }); this.wsteth = new LidoSDKwstETH({ ...props, core }); diff --git a/packages/sdk/src/staking/abi/steth.ts b/packages/sdk/src/stake/abi/steth.ts similarity index 100% rename from packages/sdk/src/staking/abi/steth.ts rename to packages/sdk/src/stake/abi/steth.ts diff --git a/packages/sdk/src/stake/index.ts b/packages/sdk/src/stake/index.ts new file mode 100644 index 00000000..1ee693eb --- /dev/null +++ b/packages/sdk/src/stake/index.ts @@ -0,0 +1,2 @@ +export { LidoSDKStake } from './stake.js'; +export { type StakeProps } from './types.js'; diff --git a/packages/sdk/src/staking/staking.ts b/packages/sdk/src/stake/stake.ts similarity index 91% rename from packages/sdk/src/staking/staking.ts rename to packages/sdk/src/stake/stake.ts index 37f6adff..3184c2b5 100644 --- a/packages/sdk/src/staking/staking.ts +++ b/packages/sdk/src/stake/stake.ts @@ -22,7 +22,7 @@ import { version } from '../version.js'; import { StethAbi } from './abi/steth.js'; import { - type LidoSDKStakingProps, + type LidoSDKStakeProps, type StakeProps, type StakeResult, type StakeEncodeDataProps, @@ -31,10 +31,10 @@ import { import { TransactionCallbackStage } from '../core/types.js'; import { parseValue } from '../common/utils/parse-value.js'; -export class LidoSDKStaking { +export class LidoSDKStake { readonly core: LidoSDKCore; - constructor(props: LidoSDKStakingProps) { + constructor(props: LidoSDKStakeProps) { const { core, ...rest } = props; if (core) this.core = core; @@ -70,19 +70,19 @@ export class LidoSDKStaking { @Logger('Call:') @ErrorHandler('Error:') - public async stake(props: StakeProps): Promise { + public async stakeEth(props: StakeProps): Promise { invariant(this.core.web3Provider, 'Web3 provider is not defined'); const parsedProps = this.parseProps(props); const { account } = props; const isContract = await this.core.isContract(account); - if (isContract) return await this.stakeMultisig(parsedProps); - else return await this.stakeEOA(parsedProps); + if (isContract) return await this.stakeEthMultisig(parsedProps); + else return await this.stakeEthEOA(parsedProps); } @Logger('LOG:') - private async stakeEOA(props: StakeInnerProps): Promise { + private async stakeEthEOA(props: StakeInnerProps): Promise { const { value, callback, referralAddress, account } = props; invariant(this.core.rpcProvider, 'RPC provider is not defined'); @@ -137,7 +137,7 @@ export class LidoSDKStaking { } @Logger('LOG:') - private async stakeMultisig(props: StakeInnerProps): Promise { + private async stakeEthMultisig(props: StakeInnerProps): Promise { const { value, callback, referralAddress, account } = props; callback({ stage: TransactionCallbackStage.SIGN }); @@ -156,7 +156,7 @@ export class LidoSDKStaking { @Logger('Call:') @ErrorHandler('Error:') - public async stakeSimulateTx( + public async stakeEthSimulateTx( props: StakeProps, ): Promise { const { referralAddress, value, account } = this.parseProps(props); @@ -240,7 +240,7 @@ export class LidoSDKStaking { } @Logger('Utils:') - private stakeEncodeData(props: StakeEncodeDataProps): Hash { + private stakeEthEncodeData(props: StakeEncodeDataProps): Hash { const { referralAddress = zeroAddress } = props; return encodeFunctionData({ @@ -251,11 +251,11 @@ export class LidoSDKStaking { } @Logger('Utils:') - public async stakePopulateTx( + public async stakeEthPopulateTx( props: StakeProps, ): Promise> { const { referralAddress, value, account } = this.parseProps(props); - const data = this.stakeEncodeData({ referralAddress }); + const data = this.stakeEthEncodeData({ referralAddress }); const address = await this.contractAddressStETH(); return { diff --git a/packages/sdk/src/staking/types.ts b/packages/sdk/src/stake/types.ts similarity index 92% rename from packages/sdk/src/staking/types.ts rename to packages/sdk/src/stake/types.ts index 2413d825..8f130ad5 100644 --- a/packages/sdk/src/staking/types.ts +++ b/packages/sdk/src/stake/types.ts @@ -3,7 +3,7 @@ import { type LidoSDKCoreProps, type LidoSDKCore } from '../core/index.js'; import { TransactionCallback } from '../core/types.js'; import { EtherValue } from '../core/types.js'; -export type LidoSDKStakingProps = LidoSDKCoreProps & { +export type LidoSDKStakeProps = LidoSDKCoreProps & { core?: LidoSDKCore; }; diff --git a/packages/sdk/src/staking/index.ts b/packages/sdk/src/staking/index.ts deleted file mode 100644 index d64dac65..00000000 --- a/packages/sdk/src/staking/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { LidoSDKStaking } from './staking.js'; -export { type StakeProps } from './types.js'; diff --git a/packages/sdk/src/withdrawals/abi/partStETH.ts b/packages/sdk/src/withdraw/abi/partStETH.ts similarity index 100% rename from packages/sdk/src/withdrawals/abi/partStETH.ts rename to packages/sdk/src/withdraw/abi/partStETH.ts diff --git a/packages/sdk/src/withdrawals/abi/partWstETH.ts b/packages/sdk/src/withdraw/abi/partWstETH.ts similarity index 100% rename from packages/sdk/src/withdrawals/abi/partWstETH.ts rename to packages/sdk/src/withdraw/abi/partWstETH.ts diff --git a/packages/sdk/src/withdrawals/abi/withdrawalsQueue.ts b/packages/sdk/src/withdraw/abi/withdrawalQueue.ts similarity index 99% rename from packages/sdk/src/withdrawals/abi/withdrawalsQueue.ts rename to packages/sdk/src/withdraw/abi/withdrawalQueue.ts index 442ae90e..b737170c 100644 --- a/packages/sdk/src/withdrawals/abi/withdrawalsQueue.ts +++ b/packages/sdk/src/withdraw/abi/withdrawalQueue.ts @@ -1,4 +1,4 @@ -export const WithdrawalsQueueAbi = [ +export const WithdrawalQueueAbi = [ { inputs: [], name: 'AdminZeroAddress', type: 'error' }, { inputs: [], name: 'ApprovalToOwner', type: 'error' }, { inputs: [], name: 'ApproveToCaller', type: 'error' }, diff --git a/packages/sdk/src/withdraw/bus.ts b/packages/sdk/src/withdraw/bus.ts new file mode 100644 index 00000000..e329c12f --- /dev/null +++ b/packages/sdk/src/withdraw/bus.ts @@ -0,0 +1,147 @@ +import { LidoSDKCore } from '../core/index.js'; + +import { LidoSDKWithdrawContract } from './withdrawContract.js'; +import { LidoSDKWithdrawViews } from './withdrawViews.js'; +import { LidoSDKWithdrawRequestsInfo } from './withdrawRequestsInfo.js'; +import { LidoSDKWithdrawClaim } from './claim/claim.js'; +import { + LidoSDKWithdrawRequest, + LidoSDKWithdrawApprove, +} from './request/index.js'; +import { type LidoSDKWithdrawProps } from './types.js'; + +export class Bus { + private props: LidoSDKWithdrawProps; + private version: string | undefined; + + private coreInstance: LidoSDKCore | undefined; + private contractInstance: LidoSDKWithdrawContract | undefined; + private viewsInstance: LidoSDKWithdrawViews | undefined; + private requestsInfoInstance: LidoSDKWithdrawRequestsInfo | undefined; + private approvalInstance: LidoSDKWithdrawApprove | undefined; + private claimInstance: LidoSDKWithdrawClaim | undefined; + private requestInstance: LidoSDKWithdrawRequest | undefined; + + constructor(props: LidoSDKWithdrawProps, version?: string) { + this.props = props; + this.version = version; + + if (props.core) this.core = props.core; + } + + // core + + get core(): LidoSDKCore { + if (!this.coreInstance) { + this.coreInstance = new LidoSDKCore(this.props, this.version); + return this.coreInstance; + } + return this.coreInstance; + } + + set core(core: LidoSDKCore) { + this.coreInstance = core; + } + + // Contract + + get contract(): LidoSDKWithdrawContract { + if (!this.contractInstance) { + this.contractInstance = new LidoSDKWithdrawContract({ + ...this.props, + bus: this, + }); + return this.contractInstance; + } + return this.contractInstance; + } + + set contract(contract: LidoSDKWithdrawContract) { + this.contractInstance = contract; + } + + // Views + + get views(): LidoSDKWithdrawViews { + if (!this.viewsInstance) { + this.viewsInstance = new LidoSDKWithdrawViews({ + ...this.props, + bus: this, + }); + return this.viewsInstance; + } + return this.viewsInstance; + } + + set views(views: LidoSDKWithdrawViews) { + this.viewsInstance = views; + } + + // Requests Info + + get requestsInfo(): LidoSDKWithdrawRequestsInfo { + if (!this.requestsInfoInstance) { + this.requestsInfoInstance = new LidoSDKWithdrawRequestsInfo({ + ...this.props, + bus: this, + }); + return this.requestsInfoInstance; + } + return this.requestsInfoInstance; + } + + set requestsInfo(requestsInfo: LidoSDKWithdrawRequestsInfo) { + this.requestsInfoInstance = requestsInfo; + } + + // Approval + + get approval(): LidoSDKWithdrawApprove { + if (!this.approvalInstance) { + this.approvalInstance = new LidoSDKWithdrawApprove({ + ...this.props, + bus: this, + }); + return this.approvalInstance; + } + return this.approvalInstance; + } + + set approval(approve: LidoSDKWithdrawApprove) { + this.approvalInstance = approve; + } + + // Claim + + get claim(): LidoSDKWithdrawClaim { + if (!this.claimInstance) { + this.claimInstance = new LidoSDKWithdrawClaim({ + ...this.props, + bus: this, + }); + return this.claimInstance; + } + return this.claimInstance; + } + + set claim(approve: LidoSDKWithdrawClaim) { + this.claimInstance = approve; + } + + // Request + + get request(): LidoSDKWithdrawRequest { + if (!this.requestInstance) { + this.requestInstance = new LidoSDKWithdrawRequest({ + ...this.props, + bus: this, + }); + return this.requestInstance; + } + return this.requestInstance; + } + + set request(approve: LidoSDKWithdrawRequest) { + this.requestInstance = approve; + } +} diff --git a/packages/sdk/src/withdrawals/claim/claim.ts b/packages/sdk/src/withdraw/claim/claim.ts similarity index 98% rename from packages/sdk/src/withdrawals/claim/claim.ts rename to packages/sdk/src/withdraw/claim/claim.ts index 420f27b5..12abf3d1 100644 --- a/packages/sdk/src/withdrawals/claim/claim.ts +++ b/packages/sdk/src/withdraw/claim/claim.ts @@ -13,7 +13,7 @@ import { } from './types.js'; import { NOOP } from '../../common/constants.js'; -export class LidoSDKWithdrawalsClaim { +export class LidoSDKWithdrawClaim { private readonly bus: Bus; constructor(props: LidoSDKCoreProps & { bus?: Bus }) { @@ -40,7 +40,7 @@ export class LidoSDKWithdrawalsClaim { private async claimRequestsEOA(props: ClaimRequestsProps) { const { account, requestsIds, hints, callback = NOOP } = props; - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); callback({ stage: TransactionCallbackStage.GAS_LIMIT }); @@ -93,7 +93,7 @@ export class LidoSDKWithdrawalsClaim { private async claimRequestsMultisig(props: ClaimRequestsProps) { const { account, requestsIds, hints, callback = NOOP } = props; - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); callback({ stage: TransactionCallbackStage.SIGN }); @@ -116,7 +116,7 @@ export class LidoSDKWithdrawalsClaim { const { account, requestsIds, hints } = props; invariant(this.bus.core.rpcProvider, 'RPC provider is not defined'); - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); const params = [requestsIds, hints] as const; const gasLimit = await contract.estimateGas.claimWithdrawals(params, { diff --git a/packages/sdk/src/withdrawals/claim/index.ts b/packages/sdk/src/withdraw/claim/index.ts similarity index 64% rename from packages/sdk/src/withdrawals/claim/index.ts rename to packages/sdk/src/withdraw/claim/index.ts index baf7d6c7..889a5c7b 100644 --- a/packages/sdk/src/withdrawals/claim/index.ts +++ b/packages/sdk/src/withdraw/claim/index.ts @@ -1,4 +1,4 @@ -export { LidoSDKWithdrawalsClaim } from './claim.js'; +export { LidoSDKWithdrawClaim } from './claim.js'; export { type ClaimRequestsProps, type ClaimRequestsPropsWithoutCallback, diff --git a/packages/sdk/src/withdrawals/claim/types.ts b/packages/sdk/src/withdraw/claim/types.ts similarity index 100% rename from packages/sdk/src/withdrawals/claim/types.ts rename to packages/sdk/src/withdraw/claim/types.ts diff --git a/packages/sdk/src/withdrawals/index.ts b/packages/sdk/src/withdraw/index.ts similarity index 82% rename from packages/sdk/src/withdrawals/index.ts rename to packages/sdk/src/withdraw/index.ts index 75912c7f..74d8f347 100644 --- a/packages/sdk/src/withdrawals/index.ts +++ b/packages/sdk/src/withdraw/index.ts @@ -1,4 +1,4 @@ -export { LidoSDKWithdrawals } from './withdrawals.js'; +export { LidoSDKWithdraw } from './withdraw.js'; export { type ClaimRequestsProps } from './claim/types.js'; export { type RequestProps, diff --git a/packages/sdk/src/withdrawals/request/approve.ts b/packages/sdk/src/withdraw/request/approve.ts similarity index 95% rename from packages/sdk/src/withdrawals/request/approve.ts rename to packages/sdk/src/withdraw/request/approve.ts index 6cb869ae..385d497f 100644 --- a/packages/sdk/src/withdrawals/request/approve.ts +++ b/packages/sdk/src/withdraw/request/approve.ts @@ -10,7 +10,7 @@ import { type ApproveProps, ApproveCallbackStages } from './types.js'; import { NOOP } from '../../common/constants.js'; import { parseValue } from '../../common/utils/parse-value.js'; -export class LidoSDKWithdrawalsApprove { +export class LidoSDKWithdrawApprove { private readonly bus: Bus; constructor(props: LidoSDKCoreProps & { bus?: Bus }) { @@ -73,7 +73,7 @@ export class LidoSDKWithdrawalsApprove { let gasLimitMethod; const addressWithdrawalsQueue = - await this.bus.contract.contractAddressWithdrawalsQueue(); + await this.bus.contract.contractAddressWithdrawalQueue(); if (isSteth) { tokenApproveMethod = (await this.bus.contract.getContractStETH()).write .approve; @@ -160,7 +160,7 @@ export class LidoSDKWithdrawalsApprove { .approve; const addressWithdrawalsQueue = - await this.bus.contract.contractAddressWithdrawalsQueue(); + await this.bus.contract.contractAddressWithdrawalQueue(); callback({ stage: ApproveCallbackStages.SIGN }); @@ -211,7 +211,7 @@ export class LidoSDKWithdrawalsApprove { .estimateGas.approve; const addressWithdrawalsQueue = - await this.bus.contract.contractAddressWithdrawalsQueue(); + await this.bus.contract.contractAddressWithdrawalQueue(); const gasLimit = await estimateGasMethod.call( this, @@ -256,7 +256,7 @@ export class LidoSDKWithdrawalsApprove { .allowance; const addressWithdrawalsQueue = - await this.bus.contract.contractAddressWithdrawalsQueue(); + await this.bus.contract.contractAddressWithdrawalQueue(); const allowance = await allowanceMethod.call( this, @@ -291,7 +291,7 @@ export class LidoSDKWithdrawalsApprove { .allowance; const addressWithdrawalsQueue = - await this.bus.contract.contractAddressWithdrawalsQueue(); + await this.bus.contract.contractAddressWithdrawalQueue(); const allowance = await allowanceMethod.call( this, diff --git a/packages/sdk/src/withdraw/request/index.ts b/packages/sdk/src/withdraw/request/index.ts new file mode 100644 index 00000000..c43bb2d9 --- /dev/null +++ b/packages/sdk/src/withdraw/request/index.ts @@ -0,0 +1,2 @@ +export { LidoSDKWithdrawRequest } from './request.js'; +export { LidoSDKWithdrawApprove } from './approve.js'; diff --git a/packages/sdk/src/withdrawals/request/request.ts b/packages/sdk/src/withdraw/request/request.ts similarity index 98% rename from packages/sdk/src/withdrawals/request/request.ts rename to packages/sdk/src/withdraw/request/request.ts index b4445fbf..5866c1c8 100644 --- a/packages/sdk/src/withdrawals/request/request.ts +++ b/packages/sdk/src/withdraw/request/request.ts @@ -15,7 +15,7 @@ import { Bus } from '../bus.js'; import { type RequestWithPermitProps, type RequestProps } from './types.js'; -export class LidoSDKWithdrawalsRequest { +export class LidoSDKWithdrawRequest { private readonly bus: Bus; constructor(props: LidoSDKCoreProps & { bus?: Bus }) { @@ -96,7 +96,7 @@ export class LidoSDKWithdrawalsRequest { invariant(this.bus.core.web3Provider, 'Web3 provider is not defined'); invariant(this.bus.core.rpcProvider, 'RPC provider is not defined'); - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); const isSteth = token === 'stETH'; let tokenRequestMethod; @@ -143,7 +143,7 @@ export class LidoSDKWithdrawalsRequest { invariant(this.bus.core.web3Provider, 'Web3 provider is not defined'); invariant(this.bus.core.rpcProvider, 'RPC provider is not defined'); - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); const isSteth = token === LIDO_TOKENS.steth; let tokenRequestMethod; @@ -209,7 +209,7 @@ export class LidoSDKWithdrawalsRequest { invariant(this.bus.core.web3Provider, 'Web3 provider is not defined'); invariant(this.bus.core.rpcProvider, 'RPC provider is not defined'); - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); const isSteth = token === 'stETH'; let tokenRequestMethod; @@ -242,7 +242,7 @@ export class LidoSDKWithdrawalsRequest { ): Promise { invariant(this.bus.core.rpcProvider, 'RPC provider is not defined'); - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); const isSteth = token === 'stETH'; let tokenRequestMethod; @@ -281,7 +281,7 @@ export class LidoSDKWithdrawalsRequest { ): Promise { invariant(this.bus.core.rpcProvider, 'RPC provider is not defined'); - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); const isSteth = token === 'stETH'; let tokenRequestMethod; diff --git a/packages/sdk/src/withdrawals/request/types.ts b/packages/sdk/src/withdraw/request/types.ts similarity index 100% rename from packages/sdk/src/withdrawals/request/types.ts rename to packages/sdk/src/withdraw/request/types.ts diff --git a/packages/sdk/src/withdrawals/types.ts b/packages/sdk/src/withdraw/types.ts similarity index 89% rename from packages/sdk/src/withdrawals/types.ts rename to packages/sdk/src/withdraw/types.ts index 384ae300..df698613 100644 --- a/packages/sdk/src/withdrawals/types.ts +++ b/packages/sdk/src/withdraw/types.ts @@ -2,7 +2,7 @@ import { type Address } from 'viem'; import { type LidoSDKCoreProps, type LidoSDKCore } from '../core/index.js'; -export type LidoSDKWithdrawalsProps = LidoSDKCoreProps & { +export type LidoSDKWithdrawProps = LidoSDKCoreProps & { core?: LidoSDKCore; }; diff --git a/packages/sdk/src/withdraw/withdraw.ts b/packages/sdk/src/withdraw/withdraw.ts new file mode 100644 index 00000000..0a5db656 --- /dev/null +++ b/packages/sdk/src/withdraw/withdraw.ts @@ -0,0 +1,10 @@ +import { version } from '../version.js'; + +import { Bus } from './bus.js'; +import { LidoSDKWithdrawProps } from './types.js'; + +export class LidoSDKWithdraw extends Bus { + constructor(props: LidoSDKWithdrawProps) { + super(props, version); + } +} diff --git a/packages/sdk/src/withdrawals/withdrawalsContract.ts b/packages/sdk/src/withdraw/withdrawContract.ts similarity index 86% rename from packages/sdk/src/withdrawals/withdrawalsContract.ts rename to packages/sdk/src/withdraw/withdrawContract.ts index 5e37c140..55220faf 100644 --- a/packages/sdk/src/withdrawals/withdrawalsContract.ts +++ b/packages/sdk/src/withdraw/withdrawContract.ts @@ -12,12 +12,12 @@ import { LIDO_CONTRACT_NAMES } from '../common/constants.js'; import { version } from '../version.js'; import { type LidoSDKCoreProps } from '../core/index.js'; -import { WithdrawalsQueueAbi } from './abi/withdrawalsQueue.js'; +import { WithdrawalQueueAbi } from './abi/withdrawalQueue.js'; import { PartStethAbi } from './abi/partStETH.js'; import { PartWstethAbi } from './abi/partWstETH.js'; import { Bus } from './bus.js'; -export class LidoSDKWithdrawalsContract { +export class LidoSDKWithdrawContract { private readonly bus: Bus; constructor(props: LidoSDKCoreProps & { bus?: Bus }) { @@ -29,7 +29,7 @@ export class LidoSDKWithdrawalsContract { @Logger('Contracts:') @Cache(30 * 60 * 1000, ['bus.core.chain.id']) - public async contractAddressWithdrawalsQueue(): Promise
{ + public async contractAddressWithdrawalQueue(): Promise
{ invariant(this.bus.core.chain, 'Chain is not defined'); return await this.bus.core.getContractAddress( @@ -42,18 +42,14 @@ export class LidoSDKWithdrawalsContract { 'bus.core.chain.id', 'contractAddressWithdrawalsQueue', ]) - public async getContractWithdrawalsQueue(): Promise< - GetContractReturnType< - typeof WithdrawalsQueueAbi, - PublicClient, - WalletClient - > + public async getContractWithdrawalQueue(): Promise< + GetContractReturnType > { - const address = await this.contractAddressWithdrawalsQueue(); + const address = await this.contractAddressWithdrawalQueue(); return getContract({ address, - abi: WithdrawalsQueueAbi, + abi: WithdrawalQueueAbi, publicClient: this.bus.core.rpcProvider, walletClient: this.bus.core.web3Provider, }); diff --git a/packages/sdk/src/withdrawals/withdrawalsRequestsInfo.ts b/packages/sdk/src/withdraw/withdrawRequestsInfo.ts similarity index 99% rename from packages/sdk/src/withdrawals/withdrawalsRequestsInfo.ts rename to packages/sdk/src/withdraw/withdrawRequestsInfo.ts index 982d88e5..3e03c2fe 100644 --- a/packages/sdk/src/withdrawals/withdrawalsRequestsInfo.ts +++ b/packages/sdk/src/withdraw/withdrawRequestsInfo.ts @@ -8,7 +8,7 @@ import { type LidoSDKCoreProps } from '../core/index.js'; import { Bus } from './bus.js'; import { type RequestStatusWithId } from './types.js'; -export class LidoSDKWithdrawalsRequestsInfo { +export class LidoSDKWithdrawRequestsInfo { private readonly bus: Bus; constructor(props: LidoSDKCoreProps & { bus?: Bus }) { diff --git a/packages/sdk/src/withdrawals/withdrawalsViews.ts b/packages/sdk/src/withdraw/withdrawViews.ts similarity index 95% rename from packages/sdk/src/withdrawals/withdrawalsViews.ts rename to packages/sdk/src/withdraw/withdrawViews.ts index 10634aaf..b5120f61 100644 --- a/packages/sdk/src/withdrawals/withdrawalsViews.ts +++ b/packages/sdk/src/withdraw/withdrawViews.ts @@ -8,7 +8,7 @@ import { type LidoSDKCoreProps } from '../core/index.js'; import { Bus } from './bus.js'; import { type RequestStatusWithId } from './types.js'; -export class LidoSDKWithdrawalsViews { +export class LidoSDKWithdrawViews { private readonly bus: Bus; constructor(props: LidoSDKCoreProps & { bus?: Bus }) { @@ -21,14 +21,14 @@ export class LidoSDKWithdrawalsViews { public async getWithdrawalRequestsIds(props: { account: Address; }): Promise { - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); return contract.read.getWithdrawalRequests([props.account]); } @Logger('Views:') public async getLastCheckpointIndex(): Promise { - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); return contract.read.getLastCheckpointIndex(); } @@ -39,7 +39,7 @@ export class LidoSDKWithdrawalsViews { }): Promise { const { requestsIds } = props; - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); const requests = await contract.read.getWithdrawalStatus([requestsIds]); invariant(requests.length === requestsIds.length, 'Invalid requests ids'); @@ -58,7 +58,7 @@ export class LidoSDKWithdrawalsViews { lastIndex: bigint; }): Promise { const { sortedIds, firstIndex = BigInt(1), lastIndex } = props; - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); return contract.read.findCheckpointHints([ sortedIds, @@ -73,14 +73,14 @@ export class LidoSDKWithdrawalsViews { hints: readonly bigint[]; }): Promise { const { sortedIds, hints } = props; - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); return contract.read.getClaimableEther([sortedIds, hints]); } @Logger('Views:') public async getUnfinalizedStETH(): Promise { - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); return contract.read.unfinalizedStETH(); } @@ -89,7 +89,7 @@ export class LidoSDKWithdrawalsViews { @Logger('Views:') public async minStethWithdrawalAmount(): Promise { - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); return contract.read.MIN_STETH_WITHDRAWAL_AMOUNT(); } @@ -97,21 +97,21 @@ export class LidoSDKWithdrawalsViews { @Logger('Views:') @Cache(30 * 60 * 1000, ['bus.core.chain.id']) public async maxStethWithdrawalAmount(): Promise { - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); return contract.read.MAX_STETH_WITHDRAWAL_AMOUNT(); } @Logger('Views:') public async isPaused(): Promise { - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); return contract.read.isPaused(); } @Logger('Views:') public async isBunkerModeActive(): Promise { - const contract = await this.bus.contract.getContractWithdrawalsQueue(); + const contract = await this.bus.contract.getContractWithdrawalQueue(); return contract.read.isBunkerModeActive(); } diff --git a/packages/sdk/src/withdrawals/bus.ts b/packages/sdk/src/withdrawals/bus.ts deleted file mode 100644 index ec7c0ed2..00000000 --- a/packages/sdk/src/withdrawals/bus.ts +++ /dev/null @@ -1,145 +0,0 @@ -import { LidoSDKCore } from '../core/index.js'; - -import { LidoSDKWithdrawalsContract } from './withdrawalsContract.js'; -import { LidoSDKWithdrawalsViews } from './withdrawalsViews.js'; -import { LidoSDKWithdrawalsRequestsInfo } from './withdrawalsRequestsInfo.js'; -import { LidoSDKWithdrawalsApprove } from './request/approve.js'; -import { LidoSDKWithdrawalsClaim } from './claim/claim.js'; -import { LidoSDKWithdrawalsRequest } from './request/request.js'; -import { type LidoSDKWithdrawalsProps } from './types.js'; - -export class Bus { - private props: LidoSDKWithdrawalsProps; - private version: string | undefined; - - private coreInstance: LidoSDKCore | undefined; - private contractInstance: LidoSDKWithdrawalsContract | undefined; - private viewsInstance: LidoSDKWithdrawalsViews | undefined; - private requestsInfoInstance: LidoSDKWithdrawalsRequestsInfo | undefined; - private approvalInstance: LidoSDKWithdrawalsApprove | undefined; - private claimInstance: LidoSDKWithdrawalsClaim | undefined; - private requestInstance: LidoSDKWithdrawalsRequest | undefined; - - constructor(props: LidoSDKWithdrawalsProps, version?: string) { - this.props = props; - this.version = version; - - if (props.core) this.core = props.core; - } - - // core - - get core(): LidoSDKCore { - if (!this.coreInstance) { - this.coreInstance = new LidoSDKCore(this.props, this.version); - return this.coreInstance; - } - return this.coreInstance; - } - - set core(core: LidoSDKCore) { - this.coreInstance = core; - } - - // Contract - - get contract(): LidoSDKWithdrawalsContract { - if (!this.contractInstance) { - this.contractInstance = new LidoSDKWithdrawalsContract({ - ...this.props, - bus: this, - }); - return this.contractInstance; - } - return this.contractInstance; - } - - set contract(contract: LidoSDKWithdrawalsContract) { - this.contractInstance = contract; - } - - // Views - - get views(): LidoSDKWithdrawalsViews { - if (!this.viewsInstance) { - this.viewsInstance = new LidoSDKWithdrawalsViews({ - ...this.props, - bus: this, - }); - return this.viewsInstance; - } - return this.viewsInstance; - } - - set views(views: LidoSDKWithdrawalsViews) { - this.viewsInstance = views; - } - - // Requests Info - - get requestsInfo(): LidoSDKWithdrawalsRequestsInfo { - if (!this.requestsInfoInstance) { - this.requestsInfoInstance = new LidoSDKWithdrawalsRequestsInfo({ - ...this.props, - bus: this, - }); - return this.requestsInfoInstance; - } - return this.requestsInfoInstance; - } - - set requestsInfo(requestsInfo: LidoSDKWithdrawalsRequestsInfo) { - this.requestsInfoInstance = requestsInfo; - } - - // Approval - - get approval(): LidoSDKWithdrawalsApprove { - if (!this.approvalInstance) { - this.approvalInstance = new LidoSDKWithdrawalsApprove({ - ...this.props, - bus: this, - }); - return this.approvalInstance; - } - return this.approvalInstance; - } - - set approval(approve: LidoSDKWithdrawalsApprove) { - this.approvalInstance = approve; - } - - // Claim - - get claim(): LidoSDKWithdrawalsClaim { - if (!this.claimInstance) { - this.claimInstance = new LidoSDKWithdrawalsClaim({ - ...this.props, - bus: this, - }); - return this.claimInstance; - } - return this.claimInstance; - } - - set claim(approve: LidoSDKWithdrawalsClaim) { - this.claimInstance = approve; - } - - // Request - - get request(): LidoSDKWithdrawalsRequest { - if (!this.requestInstance) { - this.requestInstance = new LidoSDKWithdrawalsRequest({ - ...this.props, - bus: this, - }); - return this.requestInstance; - } - return this.requestInstance; - } - - set request(approve: LidoSDKWithdrawalsRequest) { - this.requestInstance = approve; - } -} diff --git a/packages/sdk/src/withdrawals/request/index.ts b/packages/sdk/src/withdrawals/request/index.ts deleted file mode 100644 index 4af6b5e4..00000000 --- a/packages/sdk/src/withdrawals/request/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { LidoSDKWithdrawalsRequest } from './request.js'; diff --git a/packages/sdk/src/withdrawals/withdrawals.ts b/packages/sdk/src/withdrawals/withdrawals.ts deleted file mode 100644 index a6101954..00000000 --- a/packages/sdk/src/withdrawals/withdrawals.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { version } from '../version.js'; - -import { Bus } from './bus.js'; -import { LidoSDKWithdrawalsProps } from './types.js'; - -export class LidoSDKWithdrawals extends Bus { - constructor(props: LidoSDKWithdrawalsProps) { - super(props, version); - } -} From 282d0f25ce34950efeba3b0eb47a5b3ff02caa60 Mon Sep 17 00:00:00 2001 From: Andrei Date: Wed, 20 Sep 2023 23:25:53 +0700 Subject: [PATCH 2/4] feat: update sdk structure --- playground/demo/core/index.tsx | 6 ++--- playground/demo/index.tsx | 2 ++ playground/demo/stake/index.tsx | 14 +++++------ playground/demo/withdrawals/claim.tsx | 6 ++--- playground/demo/withdrawals/contract.tsx | 20 ++++++++++++++++ playground/demo/withdrawals/index.ts | 1 + playground/demo/withdrawals/request.tsx | 30 ++++++++++-------------- playground/demo/withdrawals/views.tsx | 18 +++++++------- 8 files changed, 57 insertions(+), 40 deletions(-) create mode 100644 playground/demo/withdrawals/contract.tsx diff --git a/playground/demo/core/index.tsx b/playground/demo/core/index.tsx index 7233b072..a4ff8058 100644 --- a/playground/demo/core/index.tsx +++ b/playground/demo/core/index.tsx @@ -5,14 +5,14 @@ import { Action } from 'components/action'; import { useLidoSDK } from 'providers/sdk'; export const CoreDemo = () => { - const { core, staking } = useLidoSDK(); + const { core, stake } = useLidoSDK(); const { account: web3account = '0x0' } = useWeb3(); const [contractAddress, setContractAddress] = useState(''); const getStethContract = useCallback(async () => { - const address = await staking.contractAddressStETH(); + const address = await stake.contractAddressStETH(); setContractAddress(address); - }, [staking]); + }, [stake]); useEffect(() => { getStethContract(); diff --git a/playground/demo/index.tsx b/playground/demo/index.tsx index 0198394f..bf319102 100644 --- a/playground/demo/index.tsx +++ b/playground/demo/index.tsx @@ -5,6 +5,7 @@ import { WithdrawalsRequestDemo, WithdrawalsViewsDemo, WithdrawalsClaimDemo, + WithdrawalsContractDemo, } from './withdrawals'; import { StethDemo, WstethDemo } from './tokens'; import { UnstethDemo } from './unsteth'; @@ -18,6 +19,7 @@ export const Demo = () => { + diff --git a/playground/demo/stake/index.tsx b/playground/demo/stake/index.tsx index a25898fa..591beaa7 100644 --- a/playground/demo/stake/index.tsx +++ b/playground/demo/stake/index.tsx @@ -13,7 +13,7 @@ export const StakeDemo = () => { useState(DEFAULT_VALUE); const stakingValue = stakingValueState ?? BigInt(0); const [referralAddressState, setReferralAddress] = useState(''); - const { staking } = useLidoSDK(); + const { stake } = useLidoSDK(); const account = web3account as `0x{string}`; const referralAddress = referralAddressState @@ -25,7 +25,7 @@ export const StakeDemo = () => { - staking.stake({ + stake.stakeEth({ value: stakingValue, account, callback: transactionToast, @@ -48,7 +48,7 @@ export const StakeDemo = () => { - staking.stakePopulateTx({ + stake.stakeEthPopulateTx({ account, value: stakingValue, referralAddress, @@ -58,21 +58,21 @@ export const StakeDemo = () => { - staking.stakeSimulateTx({ + stake.stakeEthSimulateTx({ account, value: stakingValue, referralAddress, }) } /> - staking.getStakeLimitInfo()} /> + stake.getStakeLimitInfo()} /> staking.contractAddressStETH()} + action={() => stake.contractAddressStETH()} /> (await staking.getContractStETH()).abi} + action={async () => (await stake.getContractStETH()).abi} /> ); diff --git a/playground/demo/withdrawals/claim.tsx b/playground/demo/withdrawals/claim.tsx index 8dd384d2..1956da2c 100644 --- a/playground/demo/withdrawals/claim.tsx +++ b/playground/demo/withdrawals/claim.tsx @@ -18,7 +18,7 @@ export const WithdrawalsClaimDemo = () => { sortedIds: readonly bigint[]; }>(); const [selectedIds, setSelectedIds] = useState(); - const { withdrawals } = useLidoSDK(); + const { withdraw } = useLidoSDK(); const account = web3account as `0x{string}`; @@ -28,7 +28,7 @@ export const WithdrawalsClaimDemo = () => { title="Get claimable request info" action={async () => { const result = - await withdrawals.requestsInfo.getClaimableRequestsETHByAccount({ + await withdraw.requestsInfo.getClaimableRequestsETHByAccount({ account, }); @@ -40,7 +40,7 @@ export const WithdrawalsClaimDemo = () => { - withdrawals.claim.claimRequests({ + withdraw.claim.claimRequests({ account, requestsIds: selectedIds ?? [], hints: diff --git a/playground/demo/withdrawals/contract.tsx b/playground/demo/withdrawals/contract.tsx new file mode 100644 index 00000000..b7016f58 --- /dev/null +++ b/playground/demo/withdrawals/contract.tsx @@ -0,0 +1,20 @@ +import { Accordion } from '@lidofinance/lido-ui'; +import { useWeb3 } from '@reef-knot/web3-react'; +import { Action } from 'components/action'; +import { useLidoSDK } from 'providers/sdk'; + +export const WithdrawalsContractDemo = () => { + const { account: web3account = '0x0' } = useWeb3(); + const { withdraw } = useLidoSDK(); + + const account = web3account as `0x{string}`; + + return ( + + withdraw.contract.contractAddressWithdrawalQueue()} + /> + + ); +}; diff --git a/playground/demo/withdrawals/index.ts b/playground/demo/withdrawals/index.ts index e9ab995f..700fd31a 100644 --- a/playground/demo/withdrawals/index.ts +++ b/playground/demo/withdrawals/index.ts @@ -1,3 +1,4 @@ export { WithdrawalsRequestDemo } from './request'; export { WithdrawalsViewsDemo } from './views'; export { WithdrawalsClaimDemo } from './claim'; +export { WithdrawalsContractDemo } from './contract'; diff --git a/playground/demo/withdrawals/request.tsx b/playground/demo/withdrawals/request.tsx index 5a3eb924..a6de3413 100644 --- a/playground/demo/withdrawals/request.tsx +++ b/playground/demo/withdrawals/request.tsx @@ -12,12 +12,12 @@ const ZERO = BigInt(0); export const WithdrawalsRequestDemo = () => { const { account: web3account = '0x0' } = useWeb3(); const [requestValue, setRequestValue] = useState(DEFAULT_VALUE); - const { withdrawals } = useLidoSDK(); + const { withdraw } = useLidoSDK(); const account = web3account as `0x{string}`; return ( - + { - withdrawals.request.requestByToken({ + withdraw.request.requestByToken({ account, requests: [requestValue ?? ZERO], token: 'stETH', @@ -38,7 +38,7 @@ export const WithdrawalsRequestDemo = () => { - withdrawals.request.requestByToken({ + withdraw.request.requestByToken({ account, requests: [requestValue ?? ZERO], token: 'wstETH', @@ -49,7 +49,7 @@ export const WithdrawalsRequestDemo = () => { - withdrawals.request.requestWithoutPermit({ + withdraw.request.requestWithoutPermit({ account, requests: [requestValue ?? ZERO], token: 'stETH', @@ -60,7 +60,7 @@ export const WithdrawalsRequestDemo = () => { - withdrawals.request.requestWithoutPermit({ + withdraw.request.requestWithoutPermit({ account, requests: [requestValue ?? ZERO], token: 'wstETH', @@ -71,7 +71,7 @@ export const WithdrawalsRequestDemo = () => { - withdrawals.approval.approveSteth({ + withdraw.approval.approveSteth({ account, amount: requestValue ?? ZERO, }) @@ -80,7 +80,7 @@ export const WithdrawalsRequestDemo = () => { - withdrawals.approval.approveWsteth({ + withdraw.approval.approveWsteth({ account, amount: requestValue ?? ZERO, }) @@ -90,32 +90,26 @@ export const WithdrawalsRequestDemo = () => { title="Get allowance stETH" renderResult={renderTokenResult('stETH')} action={() => - withdrawals.approval.getAllowanceByToken({ account, token: 'stETH' }) + withdraw.approval.getAllowanceByToken({ account, token: 'stETH' }) } /> - withdrawals.approval.getAllowanceByToken({ account, token: 'wstETH' }) + withdraw.approval.getAllowanceByToken({ account, token: 'wstETH' }) } renderResult={renderTokenResult('wstETH')} /> - withdrawals.approval.checkAllowanceSteth( - requestValue ?? ZERO, - account, - ) + withdraw.approval.checkAllowanceSteth(requestValue ?? ZERO, account) } /> - withdrawals.approval.checkAllowanceWsteth( - requestValue ?? ZERO, - account, - ) + withdraw.approval.checkAllowanceWsteth(requestValue ?? ZERO, account) } /> diff --git a/playground/demo/withdrawals/views.tsx b/playground/demo/withdrawals/views.tsx index a7630e79..b0724781 100644 --- a/playground/demo/withdrawals/views.tsx +++ b/playground/demo/withdrawals/views.tsx @@ -5,7 +5,7 @@ import { useLidoSDK } from 'providers/sdk'; export const WithdrawalsViewsDemo = () => { const { account: web3account = '0x0' } = useWeb3(); - const { withdrawals } = useLidoSDK(); + const { withdraw } = useLidoSDK(); const account = web3account as `0x{string}`; @@ -13,35 +13,35 @@ export const WithdrawalsViewsDemo = () => { withdrawals.views.getWithdrawalRequestsIds({ account })} + action={() => withdraw.views.getWithdrawalRequestsIds({ account })} /> withdrawals.views.getLastCheckpointIndex()} + action={() => withdraw.views.getLastCheckpointIndex()} /> withdrawals.views.getUnfinalizedStETH()} + action={() => withdraw.views.getUnfinalizedStETH()} renderResult={renderTokenResult('stETH')} /> withdrawals.views.minStethWithdrawalAmount()} + action={() => withdraw.views.minStethWithdrawalAmount()} renderResult={renderTokenResult('stETH')} /> withdrawals.views.maxStethWithdrawalAmount()} + action={() => withdraw.views.maxStethWithdrawalAmount()} renderResult={renderTokenResult('stETH')} /> - withdrawals.views.isPaused()} /> + withdraw.views.isPaused()} /> withdrawals.views.isBunkerModeActive()} + action={() => withdraw.views.isBunkerModeActive()} /> withdrawals.views.isTurboModeActive()} + action={() => withdraw.views.isTurboModeActive()} /> ); From ea82cb5ed017aec52ddf2e0c8c13d0ae04ab7732 Mon Sep 17 00:00:00 2001 From: Andrei Date: Wed, 20 Sep 2023 23:41:35 +0700 Subject: [PATCH 3/4] docs: update root docs --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 633c3fa3..99826481 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ Replace "https://eth-goerli.alchemyapi.io/v2/{ALCHEMY_API_KEY}" with the address ## Examples +All examples and usage instructions can be found in the [Docs SDK package](./packages/sdk/README.md). + ```ts const lidoSDK = new LidoSDK({ chainId: 5, @@ -67,7 +69,7 @@ lidoSDK.core.defineWeb3Provider(); const balanceETH = await lidoSDK.core.balanceETH(address); // Calls -const stakeResult = await lidoSDK.staking.stake({ +const stakeResult = await lidoSDK.stake.stakeEth({ value, callback, referralAddress, From 03cfae1101af42bc210c04dbf125d2ebe2c02703 Mon Sep 17 00:00:00 2001 From: Andrei Date: Thu, 21 Sep 2023 16:51:11 +0700 Subject: [PATCH 4/4] fix: fix contract cache --- packages/sdk/src/withdraw/withdrawContract.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sdk/src/withdraw/withdrawContract.ts b/packages/sdk/src/withdraw/withdrawContract.ts index 55220faf..55322b2a 100644 --- a/packages/sdk/src/withdraw/withdrawContract.ts +++ b/packages/sdk/src/withdraw/withdrawContract.ts @@ -40,7 +40,7 @@ export class LidoSDKWithdrawContract { @Logger('Contracts:') @Cache(30 * 60 * 1000, [ 'bus.core.chain.id', - 'contractAddressWithdrawalsQueue', + 'contractAddressWithdrawalQueue', ]) public async getContractWithdrawalQueue(): Promise< GetContractReturnType