Skip to content

Commit

Permalink
Merge pull request #567 from lidofinance/develop
Browse files Browse the repository at this point in the history
Merge develop to main
  • Loading branch information
jake4take authored Dec 16, 2024
2 parents b456ebf + 36c271e commit 47f7cf9
Show file tree
Hide file tree
Showing 36 changed files with 988 additions and 493 deletions.
2 changes: 1 addition & 1 deletion config/groups/stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const STAKE_GASLIMIT_FALLBACK = BigInt(
),
);

export const LIDO_ADDRESS = '0x11D00000000000000000000000000000000011D0';
export const LIDO_ADDRESS = '0x11d00000000000000000000000000000000011d0';

export const STAKE_FALLBACK_REFERRAL_ADDRESS = preConfig.ipfsMode
? IPFS_REFERRAL_ADDRESS
Expand Down
2 changes: 2 additions & 0 deletions config/groups/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export const PROVIDER_POLLING_INTERVAL = 12_000;
export const PROVIDER_BATCH_TIME = 150;
// max batch
export const PROVIDER_MAX_BATCH = 20;
// AA transaction polling timeout(ms)
export const AA_TX_POLLING_TIMEOUT = 180_000; // 3 minutes

// account for gas estimation
// will always have:
Expand Down
2 changes: 1 addition & 1 deletion consts/matomo-wallets-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const EVENTS_DATA_CONNECT_START: EventsData = {
okx: ['OKX', 'okx'],
trust: ['Trust', 'trust'],
walletConnect: ['WalletConnect', 'walletconnect'],
xdefi: ['XDEFI', 'xdefi'],
ctrl: ['Ctrl', 'ctrl'],
} as const;

const EVENTS_DATA_CONNECT_SUCCESS: EventsData = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ const getTxModalStagesStake = (transitStage: TransactionModalTransitStage) => ({
/>,
),

pending: (amount: bigint, txHash?: Hash) =>
pending: (amount: bigint, txHash?: Hash, isAA?: boolean) =>
transitStage(
<TxStageSignOperationAmount
{...STAGE_OPERATION_ARGS}
amount={amount}
isAA={isAA}
willReceive={amount}
isPending
txHash={txHash}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from 'shared/hook-form/form-controller';
import { useTokenMaxAmount } from 'shared/hooks/use-token-max-amount';
import { useStakingLimitInfo } from 'shared/hooks/useStakingLimitInfo';
import { useIsMultisig, useMaxGasPrice } from 'modules/web3';
import { useIsSmartAccount, useMaxGasPrice } from 'modules/web3';
import { useFormControllerRetry } from 'shared/hook-form/form-controller/use-form-controller-retry-delegate';

import {
Expand Down Expand Up @@ -63,7 +63,8 @@ const useStakeFormNetworkData = (): StakeFormNetworkData => {
refetch: updateStethBalance,
isLoading: isStethBalanceLoading,
} = useStethBalance();
const { isMultisig, isLoading: isMultisigLoading } = useIsMultisig();
const { isSmartAccount, isLoading: isSmartAccountLoading } =
useIsSmartAccount();
const gasLimit = useStethSubmitGasLimit();
const { maxGasPrice, isLoading: isMaxGasPriceLoading } = useMaxGasPrice();

Expand Down Expand Up @@ -97,10 +98,10 @@ const useStakeFormNetworkData = (): StakeFormNetworkData => {
const maxAmount = useTokenMaxAmount({
balance: etherBalance,
limit: stakingLimitInfo?.currentStakeLimit,
isPadded: !isMultisig,
isPadded: !isSmartAccount,
gasLimit: gasLimit,
padding: BALANCE_PADDING,
isLoading: isMultisigLoading,
isLoading: isSmartAccountLoading,
});

const revalidate = useCallback(async () => {
Expand All @@ -114,14 +115,14 @@ const useStakeFormNetworkData = (): StakeFormNetworkData => {
const loading = useMemo(
() => ({
isStethBalanceLoading,
isMultisigLoading,
isSmartAccountLoading,
isMaxGasPriceLoading,
isEtherBalanceLoading,
isStakeableEtherLoading: isStakingLimitIsLoading || isEtherBalanceLoading,
}),
[
isStethBalanceLoading,
isMultisigLoading,
isSmartAccountLoading,
isMaxGasPriceLoading,
isEtherBalanceLoading,
isStakingLimitIsLoading,
Expand All @@ -131,7 +132,7 @@ const useStakeFormNetworkData = (): StakeFormNetworkData => {
return {
stethBalance,
etherBalance,
isMultisig: isMultisigLoading ? undefined : isMultisig,
isSmartAccount,
stakeableEther,
stakingLimitInfo,
gasCost,
Expand Down
7 changes: 4 additions & 3 deletions features/stake/stake-form/stake-form-context/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export type StakeFormInput = {

export type StakeFormLoading = {
isStethBalanceLoading: boolean;
isMultisigLoading: boolean;
isSmartAccountLoading: boolean;
isMaxGasPriceLoading: boolean;
isEtherBalanceLoading: boolean;
isStakeableEtherLoading: boolean;
};

export type StakeFormNetworkData = {
etherBalance?: bigint;
isMultisig?: boolean;
isSmartAccount?: boolean;
stethBalance?: bigint;
stakeableEther?: bigint;
stakingLimitInfo?: StakeLimitFullInfo;
Expand All @@ -35,5 +35,6 @@ export type StakeFormValidationContext = {
currentStakeLimit: bigint;
gasCost: bigint;
etherBalance: bigint;
isMultisig: boolean;
isSmartAccount: boolean;
shouldValidateEtherBalance: boolean;
};
26 changes: 19 additions & 7 deletions features/stake/stake-form/stake-form-context/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useMemo } from 'react';
import type { Resolver } from 'react-hook-form';
import invariant from 'tiny-invariant';

import { useDappStatus } from 'modules/web3';
import { useAA, useDappStatus } from 'modules/web3';
import { VALIDATION_CONTEXT_TIMEOUT } from 'features/withdrawals/withdrawals-constants';
import { TOKENS_TO_WRAP } from 'features/wsteth/shared/types';
import { useAwaiter } from 'shared/hooks/use-awaiter';
Expand Down Expand Up @@ -36,7 +36,8 @@ export const stakeFormValidationResolver: Resolver<
currentStakeLimit,
etherBalance,
gasCost,
isMultisig,
isSmartAccount,
shouldValidateEtherBalance,
} = await awaitWithTimeout(
validationContextPromise,
VALIDATION_CONTEXT_TIMEOUT,
Expand All @@ -49,8 +50,9 @@ export const stakeFormValidationResolver: Resolver<
stakingLimitLevel,
currentStakeLimit,
etherBalance,
shouldValidateEtherBalance,
gasCost,
isMultisig,
isSmartAccount,
});

if (!isWalletActive) {
Expand All @@ -73,7 +75,9 @@ export const useStakeFormValidationContext = (
networkData: StakeFormNetworkData,
): Promise<StakeFormValidationContext> => {
const { isDappActive } = useDappStatus();
const { stakingLimitInfo, etherBalance, isMultisig, gasCost } = networkData;
const { areAuxiliaryFundsSupported } = useAA();
const { stakingLimitInfo, etherBalance, isSmartAccount, gasCost } =
networkData;

const validationContextAwaited = useMemo(() => {
if (
Expand All @@ -82,20 +86,28 @@ export const useStakeFormValidationContext = (
(!isDappActive ||
(etherBalance !== undefined &&
gasCost !== undefined &&
isMultisig !== undefined))
isSmartAccount !== undefined))
) {
return {
isWalletActive: isDappActive,
stakingLimitLevel: stakingLimitInfo.stakeLimitLevel,
currentStakeLimit: stakingLimitInfo.currentStakeLimit,
shouldValidateEtherBalance: !areAuxiliaryFundsSupported,
// condition above guaranties stubs will only be passed when isDappActive = false
etherBalance: etherBalance ?? 0n,
gasCost: gasCost ?? 0n,
isMultisig: isMultisig ?? false,
isSmartAccount: isSmartAccount ?? false,
};
}
return undefined;
}, [isDappActive, etherBalance, gasCost, isMultisig, stakingLimitInfo]);
}, [
stakingLimitInfo,
isDappActive,
etherBalance,
gasCost,
isSmartAccount,
areAuxiliaryFundsSupported,
]);

return useAwaiter(validationContextAwaited).awaiter;
};
73 changes: 66 additions & 7 deletions features/stake/stake-form/use-stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import {
} from '@lidofinance/lido-ethereum-sdk';

import { config } from 'config';
import { applyRoundUpGasLimit, useDappStatus, useLidoSDK } from 'modules/web3';
import {
applyRoundUpGasLimit,
useAA,
useDappStatus,
useLidoSDK,
useSendAACalls,
} from 'modules/web3';

import { MockLimitReachedError, getRefferalAddress } from './utils';
import { useTxModalStagesStake } from './hooks/use-tx-modal-stages-stake';
Expand All @@ -26,6 +32,8 @@ type StakeOptions = {

export const useStake = ({ onConfirm, onRetry }: StakeOptions) => {
const { address } = useDappStatus();
const { isAA } = useAA();
const sendAACalls = useSendAACalls();
const { stake, stETH } = useLidoSDK();
const { txModalStages } = useTxModalStagesStake();

Expand All @@ -42,11 +50,56 @@ export const useStake = ({ onConfirm, onRetry }: StakeOptions) => {
throw new MockLimitReachedError('Stake limit reached');
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const referralAddress = referral
? await getRefferalAddress(referral, stake.core.rpcProvider)
: config.STAKE_FALLBACK_REFERRAL_ADDRESS;

const onStakeTxConfirmed = async () => {
const [, balance] = await Promise.all([
onConfirm?.(),
stETH.balance(address),
]);
return balance;
};

//
// ERC5792 flow
//
if (isAA) {
const stakeCall = await stake.stakeEthPopulateTx({
value: amount,
referralAddress: getAddressViem(referralAddress),
});

await sendAACalls([stakeCall], async (props) => {
switch (props.stage) {
case TransactionCallbackStage.SIGN:
txModalStages.sign(amount);
break;
case TransactionCallbackStage.RECEIPT:
txModalStages.pending(amount, props.callId as Hash, isAA);
break;
case TransactionCallbackStage.DONE: {
const balance = await onStakeTxConfirmed();
txModalStages.success(balance, props.txHash);
break;
}
case TransactionCallbackStage.ERROR: {
txModalStages.failed(props.error, onRetry);
break;
}
default:
break;
}
});

return true;
}

//
// Legacy flow
//

let txHash: Hash | undefined = undefined;
const txCallback: TransactionCallback = async ({ stage, payload }) => {
switch (stage) {
Expand All @@ -62,10 +115,7 @@ export const useStake = ({ onConfirm, onRetry }: StakeOptions) => {
txHash = payload;
break;
case TransactionCallbackStage.DONE: {
const [, balance] = await Promise.all([
onConfirm?.(),
stETH.balance(address),
]);
const balance = await onStakeTxConfirmed();
txModalStages.success(balance, txHash);
break;
}
Expand All @@ -92,6 +142,15 @@ export const useStake = ({ onConfirm, onRetry }: StakeOptions) => {
return false;
}
},
[address, stake, txModalStages, onConfirm, stETH, onRetry],
[
address,
stake,
isAA,
txModalStages,
sendAACalls,
onConfirm,
stETH,
onRetry,
],
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ const getTxModalStagesClaim = (transitStage: TransactionModalTransitStage) => ({
<TxStageSignOperationAmount {...STAGE_OPERATION_ARGS} amount={amount} />,
),

pending: (amount: bigint, txHash?: Hash) =>
pending: (amount: bigint, txHash?: Hash, isAA?: boolean) =>
transitStage(
<TxStageSignOperationAmount
{...STAGE_OPERATION_ARGS}
amount={amount}
isAA={isAA}
txHash={txHash}
isPending
/>,
Expand Down
Loading

0 comments on commit 47f7cf9

Please sign in to comment.