Skip to content

Commit

Permalink
feat(stake): round up gas limit and change fallback ref address
Browse files Browse the repository at this point in the history
  • Loading branch information
solidovic committed Aug 15, 2024
1 parent 42e6a05 commit 67443ac
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 27 deletions.
5 changes: 2 additions & 3 deletions config/groups/stake.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BigNumber } from 'ethers';
import { parseEther } from '@ethersproject/units';
import { AddressZero } from '@ethersproject/constants';

import { StakeSwapDiscountIntegrationKey } from 'features/stake/swap-discount-banner';

Expand All @@ -26,11 +25,11 @@ export const STAKE_GASLIMIT_FALLBACK = BigNumber.from(
),
);

export const STAKE_WIDGET_METRIC_SUFFIX = '01';
export const LIDO_ADDRESS = '0x11D00000000000000000000000000000000011D0';

export const STAKE_FALLBACK_REFERRAL_ADDRESS = preConfig.ipfsMode
? IPFS_REFERRAL_ADDRESS
: AddressZero;
: LIDO_ADDRESS;

export const STAKE_SWAP_INTEGRATION: StakeSwapDiscountIntegrationKey =
'one-inch';
13 changes: 2 additions & 11 deletions features/stake/stake-form/use-stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import { useCurrentStaticRpcProvider } from 'shared/hooks/use-current-static-rpc
import { isContract } from 'utils/isContract';
import { runWithTransactionLogger } from 'utils';

import {
MockLimitReachedError,
getAddress,
applyCalldataSuffix,
} from './utils';
import { MockLimitReachedError, getAddress } from './utils';
import { useTxModalStagesStake } from './hooks/use-tx-modal-stages-stake';

import { sendTx } from 'utils/send-tx';
Expand All @@ -41,9 +37,6 @@ export const useStake = ({ onConfirm, onRetry }: StakeOptions) => {
const { providerWeb3 } = useSDK();
const { txModalStages } = useTxModalStagesStake();

// temporary disable until Ledger is fixed
const shouldApplyCalldataSuffix = false;

return useCallback(
async ({ amount, referral }: StakeArguments): Promise<boolean> => {
try {
Expand Down Expand Up @@ -77,14 +70,13 @@ export const useStake = ({ onConfirm, onRetry }: StakeOptions) => {
},
);

if (shouldApplyCalldataSuffix) applyCalldataSuffix(tx);

return sendTx({
tx,
isMultisig,
staticProvider: staticRpcProvider,
walletProvider: providerWeb3,
shouldApplyGasLimitRatio: true,
shouldRoundUpGasLimit: true,
});
};

Expand Down Expand Up @@ -128,7 +120,6 @@ export const useStake = ({ onConfirm, onRetry }: StakeOptions) => {
staticRpcProvider,
stethContract,
onConfirm,
shouldApplyCalldataSuffix,
onRetry,
],
);
Expand Down
13 changes: 0 additions & 13 deletions features/stake/stake-form/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { PopulatedTransaction } from 'ethers';
import { isAddress } from 'ethers/lib/utils';
import type { BaseProvider } from '@ethersproject/providers';

import { config } from 'config';
import invariant from 'tiny-invariant';

export const getAddress = async (
input: string,
providerRpc: BaseProvider,
Expand All @@ -20,15 +16,6 @@ export const getAddress = async (
throw new ReferralAddressError();
};

// adds metrics indicator for widget tx
export const applyCalldataSuffix = (tx: PopulatedTransaction) => {
if (!config.ipfsMode) {
invariant(tx.data, 'transaction must have calldata');
tx.data = tx.data + config.STAKE_WIDGET_METRIC_SUFFIX;
}
return tx;
};

export class ReferralAddressError extends Error {
reason: string;
constructor() {
Expand Down
8 changes: 8 additions & 0 deletions utils/apply-round-up-gas-limit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { BigNumber } from 'ethers';

export const applyRoundUpGasLimit = (number: BigNumber): BigNumber => {
const bn1000 = BigNumber.from(1000);

// 94567 -> 94 -> 94000 -> 94999
return number.div(bn1000).mul(bn1000).add(BigNumber.from(999));
};
7 changes: 7 additions & 0 deletions utils/send-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import type { PopulatedTransaction } from 'ethers';
import { getFeeData } from './getFeeData';
import { estimateGas } from './estimate-gas';
import { applyGasLimitRatio } from 'utils/apply-gas-limit-ratio';
import { applyRoundUpGasLimit } from 'utils/apply-round-up-gas-limit';

export type SendTxOptions = {
tx: PopulatedTransaction;
isMultisig: boolean;
walletProvider: Web3Provider;
staticProvider: JsonRpcBatchProvider;
shouldApplyGasLimitRatio?: boolean;
shouldRoundUpGasLimit?: boolean;
};

export const sendTx = async ({
Expand All @@ -22,6 +24,7 @@ export const sendTx = async ({
staticProvider,
walletProvider,
shouldApplyGasLimitRatio = false,
shouldRoundUpGasLimit = false,
}: SendTxOptions) => {
if (!isMultisig) {
const { maxFeePerGas, maxPriorityFeePerGas } =
Expand All @@ -35,6 +38,10 @@ export const sendTx = async ({
tx.gasLimit = shouldApplyGasLimitRatio
? applyGasLimitRatio(gasLimit)
: gasLimit;

tx.gasLimit = shouldRoundUpGasLimit
? applyRoundUpGasLimit(tx.gasLimit)
: gasLimit;
}
return walletProvider.getSigner().sendUncheckedTransaction(tx);
};

0 comments on commit 67443ac

Please sign in to comment.