Skip to content

Commit

Permalink
Merge pull request #211 from lidofinance/develop
Browse files Browse the repository at this point in the history
Develop to main
  • Loading branch information
itaven authored Jan 22, 2024
2 parents 38c225d + 281d6f9 commit 4baef8e
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-ipfs-test-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
ETH_API_BASE_PATH: ${{ vars.ETH_API_BASE_PATH }}
PREFILL_UNSAFE_EL_RPC_URLS_1: ${{ secrets.PREFILL_UNSAFE_EL_RPC_URLS_1 }}
WALLETCONNECT_PROJECT_ID: ${{ secrets.WALLETCONNECT_PROJECT_ID }}
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: ${{ env.ipfs_folder }}
path: ${{ env.ipfs_folder }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-ipfs-testnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
PREFILL_UNSAFE_EL_RPC_URLS_5: ${{ secrets.PREFILL_UNSAFE_EL_RPC_URLS_5 }}
PREFILL_UNSAFE_EL_RPC_URLS_17000: ${{ secrets.PREFILL_UNSAFE_EL_RPC_URLS_17000 }}
WALLETCONNECT_PROJECT_ID: ${{ secrets.WALLETCONNECT_PROJECT_ID }}
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: ${{ env.ipfs_folder }}
path: ${{ env.ipfs_folder }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-ipfs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
ETH_API_BASE_PATH: ${{ vars.ETH_API_BASE_PATH }}
PREFILL_UNSAFE_EL_RPC_URLS_1: ${{ secrets.PREFILL_UNSAFE_EL_RPC_URLS_1 }}
WALLETCONNECT_PROJECT_ID: ${{ secrets.WALLETCONNECT_PROJECT_ID }}
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: ${{ env.ipfs_folder }}
path: ${{ env.ipfs_folder }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
STAND_URL: ${{ inputs.stand_url }}
STAND_TYPE: ${{ inputs.stand_type }}

- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
Expand Down
1 change: 1 addition & 0 deletions assets/icons/attention-triangle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 37 additions & 1 deletion assets/icons/l2-swap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 78 additions & 2 deletions features/withdrawals/hooks/useWithdrawalRates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@ import { useWatch } from 'react-hook-form';
import { BigNumber } from 'ethers';

import { Zero } from '@ethersproject/constants';
import { TOKENS } from '@lido-sdk/constants';
import { CHAINS, getTokenAddress, TOKENS } from '@lido-sdk/constants';
import { useLidoSWR } from '@lido-sdk/react';

import { useDebouncedValue } from 'shared/hooks/useDebouncedValue';
import { STRATEGY_LAZY } from 'utils/swrStrategies';

import { RequestFormInputType } from '../request/request-form-context';
import { getOpenOceanRate } from 'utils/get-open-ocean-rate';
import { standardFetcher } from 'utils/standardFetcher';

type GetWithdrawalRateParams = {
amount: BigNumber;
token: TOKENS.STETH | TOKENS.WSTETH;
};

type RateCalculationResult = {
rate: number;
toReceive: BigNumber;
};

type SingleWithdrawalRateResult = {
name: string;
rate: number | null;
Expand All @@ -29,6 +35,20 @@ type GetRateType = (

type GetWithdrawalRateResult = SingleWithdrawalRateResult[];

const RATE_PRECISION = 100000;
const RATE_PRECISION_BN = BigNumber.from(RATE_PRECISION);

const calculateRateReceive = (
amount: BigNumber,
src: BigNumber,
dest: BigNumber,
): RateCalculationResult => {
const _rate = dest.mul(RATE_PRECISION_BN).div(src);
const toReceive = amount.mul(dest).div(src);
const rate = _rate.toNumber() / RATE_PRECISION;
return { rate, toReceive };
};

const getOpenOceanWithdrawalRate: GetRateType = async ({ amount, token }) => {
if (amount && amount.gt(Zero)) {
try {
Expand All @@ -49,10 +69,66 @@ const getOpenOceanWithdrawalRate: GetRateType = async ({ amount, token }) => {
};
};

type ParaSwapPriceResponsePartial = {
priceRoute: {
srcAmount: string;
destAmount: string;
};
};

const getParaSwapRate: GetRateType = async ({ amount, token }) => {
let rateInfo: RateCalculationResult | null;

try {
if (amount.isZero() || amount.isNegative()) {
return {
name: 'paraswap',
rate: 0,
toReceive: BigNumber.from(0),
};
}
const capped_amount = amount;
const api = `https://apiv5.paraswap.io/prices`;
const query = new URLSearchParams({
srcToken: getTokenAddress(CHAINS.Mainnet, token),
srcDecimals: '18',
destToken: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
destDecimals: '18',
side: 'SELL',
excludeDirectContractMethods: 'true',
userAddress: '0x0000000000000000000000000000000000000000',
amount: capped_amount.toString(),
network: '1',
partner: 'lido',
});

const url = `${api}?${query.toString()}`;
const data: ParaSwapPriceResponsePartial =
await standardFetcher<ParaSwapPriceResponsePartial>(url);

rateInfo = calculateRateReceive(
amount,
BigNumber.from(data.priceRoute.srcAmount),
BigNumber.from(data.priceRoute.destAmount),
);
} catch {
rateInfo = null;
}

return {
name: 'paraswap',
rate: rateInfo?.rate ?? null,
toReceive: rateInfo?.toReceive ?? null,
};
};

const getWithdrawalRates = async (
params: GetWithdrawalRateParams,
): Promise<GetWithdrawalRateResult> => {
const rates = await Promise.all([getOpenOceanWithdrawalRate(params)]);
const rates = await Promise.all([
getOpenOceanWithdrawalRate(params),
getParaSwapRate(params),
]);

if (rates.length > 1) {
// sort by rate, then alphabetic
Expand Down
Loading

0 comments on commit 4baef8e

Please sign in to comment.