Skip to content

Commit

Permalink
fix: don't modify estimations for contract calls
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Jun 16, 2023
1 parent bf3aeac commit 23f8997
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 158 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"lerna": "^7.0.1",
"lerna": "^7.0.2",
"prettier": "^2.8.8",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet-management/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@ethersproject/abstract-signer": "^5.7.0",
"@ethersproject/experimental": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@lifi/sdk": "^2.0.0",
"@lifi/sdk": "^2.0.1",
"@walletconnect/ethereum-provider": "^1.8.0",
"@walletconnect/web3-provider": "^1.8.0",
"events": "^3.3.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/widget-embedded/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
"author": "Eugene Chybisov <eugene@li.finance>",
"dependencies": {
"@ethersproject/abstract-signer": "^5.7.0",
"@lifi/sdk": "^2.0.0",
"@lifi/sdk": "^2.0.1",
"@lifi/wallet-management": "^2.0.0",
"@lifi/widget": "^2.0.0",
"@mui/icons-material": "^5.11.16",
"@mui/lab": "^5.0.0-alpha.134",
"@mui/material": "^5.13.5",
"@opensea/seaport-js": "2.0.3",
"@tanstack/react-query": "^4.29.13",
"@tanstack/react-query": "^4.29.14",
"bignumber.js": "^9.1.1",
"ethers": "^5.7.2",
"events": "^3.3.0",
Expand All @@ -44,7 +44,7 @@
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
"@vitejs/plugin-react": "^4.0.0",
"buffer": "^6.0.3",
"esbuild": "^0.18.2",
"esbuild": "^0.18.3",
"rollup": "^3.25.1",
"rollup-plugin-polyfill-node": "^0.12.0",
"typescript": "^5.1.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/widget-playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"author": "Eugene Chybisov <eugene@li.finance>",
"dependencies": {
"@lifi/sdk": "^2.0.0",
"@lifi/sdk": "^2.0.1",
"@lifi/wallet-management": "^2.0.0",
"@lifi/widget": "^2.0.0",
"@mui/icons-material": "^5.11.16",
Expand Down
6 changes: 3 additions & 3 deletions packages/widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@
"@ethersproject/address": "^5.7.0",
"@ethersproject/experimental": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@lifi/sdk": "^2.0.0",
"@lifi/sdk": "^2.0.1",
"@lifi/wallet-management": "^2.0.0",
"@mui/icons-material": "^5.11.16",
"@mui/lab": "^5.0.0-alpha.134",
"@mui/material": "^5.13.5",
"@tanstack/react-query": "^4.29.13",
"@tanstack/react-query": "^4.29.14",
"@tanstack/react-virtual": "^3.0.0-beta.54",
"big.js": "^6.2.1",
"i18next": "^23.0.1",
"i18next": "^23.0.2",
"i18next-browser-languagedetector": "^7.0.2",
"microdiff": "^1.3.2",
"mitt": "^3.0.0",
Expand Down
8 changes: 6 additions & 2 deletions packages/widget/src/components/Step/StepList.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Route, TokenAmount } from '@lifi/sdk';
import { Fragment } from 'react';
import { StepDivider } from '../../components/StepDivider';
import type { WidgetSubvariant } from '../../types';
import { Step } from './Step';

export const getStepList = (route?: Route) =>
export const getStepList = (route?: Route, subvariant?: WidgetSubvariant) =>
route?.steps.map((step, index, steps) => {
const lastIndex = steps.length - 1;
const fromToken: TokenAmount | undefined =
Expand All @@ -14,7 +15,10 @@ export const getStepList = (route?: Route) =>
index === lastIndex
? {
...(step.execution?.toToken ?? step.action?.toToken),
amount: step.execution?.toAmount ?? step.estimate.toAmount,
amount:
step.execution?.toAmount ?? subvariant === 'nft'
? route.toAmount
: step.estimate.toAmount,
}
: undefined;
const toAddress =
Expand Down
27 changes: 22 additions & 5 deletions packages/widget/src/components/StepActions/StepActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ export const StepActions: React.FC<StepActionsProps> = ({

const hasCollapsedSteps = dense && step.includedSteps?.length > 1;

// FIXME: step transaction request overrides step tool details, but not included step tool details
const toolDetails =
subvariant === 'nft'
? step.includedSteps.find(
(step) => step.tool === 'custom' && step.toolDetails.key !== 'custom',
)?.toolDetails || step.toolDetails
: step.toolDetails;

return (
<Box {...other}>
<Box display="flex" alignItems="center">
Expand All @@ -60,15 +68,15 @@ export const StepActions: React.FC<StepActionsProps> = ({
>
<StepAvatar
variant="circular"
src={step.toolDetails.logoURI}
alt={step.toolDetails.name}
src={toolDetails.logoURI}
alt={toolDetails.name}
>
{step.toolDetails.name[0]}
{toolDetails.name[0]}
</StepAvatar>
</Badge>
<Typography ml={2} fontSize={18} fontWeight="500" flex={1}>
{t(`main.stepDetails`, {
tool: step.toolDetails.name,
tool: toolDetails.name,
})}
</Typography>
{hasCollapsedSteps ? (
Expand Down Expand Up @@ -226,10 +234,19 @@ export const CustomStepDetailsLabel: React.FC<StepDetailsLabelProps> = ({
return null;
}

// FIXME: step transaction request overrides step tool details, but not included step tool details
const toolDetails =
subvariant === 'nft' &&
(step as unknown as LifiStep).includedSteps?.length > 0
? (step as unknown as LifiStep).includedSteps.find(
(step) => step.tool === 'custom' && step.toolDetails.key !== 'custom',
)?.toolDetails || step.toolDetails
: step.toolDetails;

return (
<Typography fontSize={12} fontWeight="500" color="text.secondary">
{t(`main.${subvariant}StepDetails`, {
tool: step.toolDetails.name,
tool: toolDetails.name,
})}
</Typography>
);
Expand Down
7 changes: 2 additions & 5 deletions packages/widget/src/hooks/useRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ export const useRoutes = ({ insurableRoute }: RoutesProps = {}) => {
{ signal },
);

contractCallQuote.estimate.toAmount = toTokenAmount;
contractCallQuote.estimate.toAmountMin = toTokenAmount;
contractCallQuote.action.toToken = toToken!;

const customStep =
Expand Down Expand Up @@ -215,9 +213,8 @@ export const useRoutes = ({ insurableRoute }: RoutesProps = {}) => {
fromAddress: contractCallQuote.action.fromAddress,
toChainId: contractCallQuote.action.toChainId,
toAmountUSD: contractCallQuote.estimate.toAmountUSD || '',
toAmount: contractCallQuote.estimate.toAmount || toTokenAmount,
toAmountMin:
contractCallQuote.estimate.toAmountMin || toTokenAmount,
toAmount: toTokenAmount,
toAmountMin: toTokenAmount,
toToken: toToken!,
toAddress: toWalletAddress,
gasCostUSD: contractCallQuote.estimate.gasCosts?.[0].amountUSD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const TransactionDetailsPage: React.FC = () => {
}).format(startedAt)}
</Typography>
</Box>
{getStepList(routeExecution?.route)}
{getStepList(routeExecution?.route, subvariant)}
{subvariant === 'nft' ? (
<ContractComponent mt={2}>
{contractSecondaryComponent || contractComponent}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ const ExchangeRateBottomSheetContent: React.FC<
const { t } = useTranslation();
const ref = useRef<HTMLElement>();
useSetContentHeight(ref);

const oldAmount = Big(data?.oldToAmount || 1);
return (
<Box p={3} ref={ref}>
<CenterContainer>
Expand Down Expand Up @@ -137,7 +139,7 @@ const ExchangeRateBottomSheetContent: React.FC<
<Typography>{t('main.rateChange')}</Typography>
<Typography fontWeight={600}>
{Big(data?.newToAmount || 0)
.div(Big(data?.oldToAmount || 0))
.div(oldAmount.eq(0) ? Big(1) : oldAmount)
.minus(1)
.mul(100)
.round(2, Big.roundUp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export const TransactionPage: React.FC = () => {
tokenValueBottomSheetRef.current?.close();
executeRoute();
setValue(FormKey.FromAmount, '');
if (subvariant === 'nft') {
setValue(FormKey.FromToken, '');
setValue(FormKey.ToToken, '');
}
};

const handleStartClick = async () => {
Expand Down Expand Up @@ -157,7 +161,7 @@ export const TransactionPage: React.FC = () => {

return (
<Container>
{getStepList(route)}
{getStepList(route, subvariant)}
{subvariant === 'nft' ? (
<ContractComponent mt={2}>
{contractSecondaryComponent || contractComponent}
Expand Down
6 changes: 6 additions & 0 deletions packages/widget/src/stores/routes/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export const isRouteActive = (route?: Route) => {
return !isDone && !isFailed && alreadyStarted;
};

export const doesRouteHaveCustomTool = (route: Route) => {
return route.steps.some(
(step) => step.tool === 'custom' || step.toolDetails.key === 'custom',
);
};

export const getUpdatedProcess = (
currentRoute: Route,
updatedRoute: Route,
Expand Down
Loading

0 comments on commit 23f8997

Please sign in to comment.