From 44cde492010c55c23e8d09ed909f10428bab927f Mon Sep 17 00:00:00 2001 From: lendihop Date: Wed, 10 Jan 2024 14:16:46 +0100 Subject: [PATCH] review fixes --- src/app/pages/AddAsset/AddAsset.tsx | 3 +- src/app/templates/DelegateForm.tsx | 13 +- src/app/templates/SendForm/Form.tsx | 10 +- .../SwapForm/SwapRoute/SwapRoute.tsx | 226 +++++++++--------- src/lib/temple/contract.ts | 2 +- src/lib/utils/numbers.ts | 6 +- src/lib/utils/swap.utils.ts | 4 +- 7 files changed, 135 insertions(+), 129 deletions(-) diff --git a/src/app/pages/AddAsset/AddAsset.tsx b/src/app/pages/AddAsset/AddAsset.tsx index f6bda8bde2..8acbac26e7 100644 --- a/src/app/pages/AddAsset/AddAsset.tsx +++ b/src/app/pages/AddAsset/AddAsset.tsx @@ -1,5 +1,6 @@ import React, { FC, ReactNode, useCallback, useEffect, useRef, useMemo } from 'react'; +import { ContractAbstraction, ContractProvider, Wallet } from '@taquito/taquito'; import classNames from 'clsx'; import { FormContextValues, useForm } from 'react-hook-form'; import { useDispatch } from 'react-redux'; @@ -116,7 +117,7 @@ const Form: FC = () => { let stateToSet: Partial; try { - let contract; + let contract: ContractAbstraction; try { contract = await loadContract(tezos, contractAddress, false); } catch { diff --git a/src/app/templates/DelegateForm.tsx b/src/app/templates/DelegateForm.tsx index dd68ecec6d..22bc57443a 100644 --- a/src/app/templates/DelegateForm.tsx +++ b/src/app/templates/DelegateForm.tsx @@ -1,6 +1,6 @@ import React, { FC, ReactNode, useCallback, useLayoutEffect, useMemo, useRef } from 'react'; -import { DEFAULT_FEE, WalletOperation } from '@taquito/taquito'; +import { DEFAULT_FEE, DelegateParams, TransactionOperation, WalletOperation } from '@taquito/taquito'; import BigNumber from 'bignumber.js'; import classNames from 'clsx'; import { Control, Controller, FieldError, FormStateProxy, NestDataObject, useForm } from 'react-hook-form'; @@ -112,7 +112,7 @@ const DelegateForm: FC = () => { return tezos.estimate.setDelegate({ source: accountPkh, delegate: to - }); + } as DelegateParams); } }, [tezos, accountPkh, acc.type, toResolved]); @@ -225,7 +225,8 @@ const DelegateForm: FC = () => { const estmtn = await getEstimation(); const addFee = tzToMutez(feeVal ?? 0); const fee = addFee.plus(estmtn.suggestedFeeMutez).toNumber(); - let op: WalletOperation; + let op: WalletOperation | TransactionOperation; + let opHash = ''; if (acc.type === TempleAccountType.ManagedKT) { const contract = await loadContract(tezos, acc.publicKeyHash); op = await contract.methods.do(setDelegate(to)).send({ amount: 0 }); @@ -237,13 +238,15 @@ const DelegateForm: FC = () => { fee } as any) .send(); + + opHash = op.opHash; } setOperation(op); reset({ to: '', fee: RECOMMENDED_ADD_FEE }); - if (to === RECOMMENDED_BAKER_ADDRESS) { - submitDelegation(op.opHash); + if (to === RECOMMENDED_BAKER_ADDRESS && opHash) { + submitDelegation(opHash); } formAnalytics.trackSubmitSuccess(analyticsProperties); diff --git a/src/app/templates/SendForm/Form.tsx b/src/app/templates/SendForm/Form.tsx index 9aacf664e2..8fd563d56d 100644 --- a/src/app/templates/SendForm/Form.tsx +++ b/src/app/templates/SendForm/Form.tsx @@ -11,7 +11,13 @@ import React, { } from 'react'; import { ManagerKeyResponse } from '@taquito/rpc'; -import { DEFAULT_FEE, TransferParams, WalletOperation, Estimate } from '@taquito/taquito'; +import { + DEFAULT_FEE, + TransferParams, + Estimate, + TransactionWalletOperation, + TransactionOperation +} from '@taquito/taquito'; import BigNumber from 'bignumber.js'; import classNames from 'clsx'; import { Controller, FieldError, useForm } from 'react-hook-form'; @@ -340,7 +346,7 @@ export const Form: FC = ({ assetSlug, setOperation, onAddContactReque try { if (!assetMetadata) throw new Error('Metadata not found'); - let op: WalletOperation; + let op: TransactionWalletOperation | TransactionOperation; if (isKTAddress(acc.publicKeyHash)) { const michelsonLambda = isKTAddress(toResolved) ? transferToContract : transferImplicit; diff --git a/src/app/templates/SwapForm/SwapRoute/SwapRoute.tsx b/src/app/templates/SwapForm/SwapRoute/SwapRoute.tsx index 60d778b7de..ff65a5839c 100644 --- a/src/app/templates/SwapForm/SwapRoute/SwapRoute.tsx +++ b/src/app/templates/SwapForm/SwapRoute/SwapRoute.tsx @@ -1,4 +1,4 @@ -import React, { FC, useEffect, useMemo, useState } from 'react'; +import React, { memo, useEffect, useMemo, useState } from 'react'; import { isDefined } from '@rnw-community/shared'; import { BigNumber } from 'bignumber.js'; @@ -22,119 +22,115 @@ interface Props { className?: string; } -export const SwapRoute: FC = ({ - isLbInput, - isLbOutput, - routingFeeIsTakenFromOutput, - outputToken, - className -}) => { - const [isVisible, setIsVisible] = useState(false); - - const { - data: { input, output, ...chains } - } = useSwapParamsSelector(); - - const chainsHeap = useMemo(() => { - let chainsHeapBeforeOutputFee: Route3Chain[] = []; - - if (isSwapChains(chains)) { - chainsHeapBeforeOutputFee = chains.chains; - } else { - const { tzbtcChain, xtzChain } = chains; - const tzbtcChains: Route3Chain[] = - tzbtcChain.chains.length === 0 - ? [ - { - input: tzbtcChain.input ?? '0', - output: tzbtcChain.output ?? '0', - hops: [] - } - ] - : tzbtcChain.chains; - const xtzChains: Route3Chain[] = - xtzChain.chains.length === 0 - ? [ - { - input: xtzChain.input ?? '0', - output: xtzChain.output ?? '0', - hops: [] - } - ] - : xtzChain.chains; - - chainsHeapBeforeOutputFee = tzbtcChains.concat(xtzChains); - } - - return routingFeeIsTakenFromOutput - ? chainsHeapBeforeOutputFee.map(chain => ({ - ...chain, - output: new BigNumber(chain.output) - .times(ROUTING_FEE_RATIO) - .toFixed(outputToken.decimals, BigNumber.ROUND_DOWN) - })) - : chainsHeapBeforeOutputFee; - }, [chains, routingFeeIsTakenFromOutput, outputToken.decimals]); - - const totalChains = chainsHeap.length; - - useEffect(() => { - if (totalChains === 0) { - setIsVisible(false); - } - }, [totalChains]); - - const totalHops = - chainsHeap.reduce((accum, chain) => accum + chain.hops.length, 0) + - ((isLbInput || isLbOutput) && chainsHeap.length > 0 ? 1 : 0); - const shouldShowRoute = isVisible && totalChains > 0; - - const outputAfterFee = useMemo( - () => - routingFeeIsTakenFromOutput && isDefined(output) - ? new BigNumber(output).times(ROUTING_FEE_RATIO).toFixed(outputToken.decimals, BigNumber.ROUND_DOWN) - : output, - [output, outputToken.decimals, routingFeeIsTakenFromOutput] - ); - - const hadleToggleVisible = () => setIsVisible(prev => !prev); - - const Chevron = isVisible ? ChevronUp : ChevronDown; - - return ( -
-

- - - - +export const SwapRoute = memo( + ({ isLbInput, isLbOutput, routingFeeIsTakenFromOutput, outputToken, className }) => { + const [isVisible, setIsVisible] = useState(false); + + const { + data: { input, output, ...chains } + } = useSwapParamsSelector(); + + const chainsHeap = useMemo(() => { + let chainsHeapBeforeOutputFee: Route3Chain[] = []; + + if (isSwapChains(chains)) { + chainsHeapBeforeOutputFee = chains.chains; + } else { + const { tzbtcChain, xtzChain } = chains; + const tzbtcChains: Route3Chain[] = + tzbtcChain.chains.length === 0 + ? [ + { + input: tzbtcChain.input ?? '0', + output: tzbtcChain.output ?? '0', + hops: [] + } + ] + : tzbtcChain.chains; + const xtzChains: Route3Chain[] = + xtzChain.chains.length === 0 + ? [ + { + input: xtzChain.input ?? '0', + output: xtzChain.output ?? '0', + hops: [] + } + ] + : xtzChain.chains; + + chainsHeapBeforeOutputFee = tzbtcChains.concat(xtzChains); + } + + return routingFeeIsTakenFromOutput + ? chainsHeapBeforeOutputFee.map(chain => ({ + ...chain, + output: new BigNumber(chain.output) + .times(ROUTING_FEE_RATIO) + .toFixed(outputToken.decimals, BigNumber.ROUND_DOWN) + })) + : chainsHeapBeforeOutputFee; + }, [chains, routingFeeIsTakenFromOutput, outputToken.decimals]); + + const totalChains = chainsHeap.length; + + useEffect(() => { + if (totalChains === 0) { + setIsVisible(false); + } + }, [totalChains]); + + const totalHops = + chainsHeap.reduce((accum, chain) => accum + chain.hops.length, 0) + + ((isLbInput || isLbOutput) && chainsHeap.length > 0 ? 1 : 0); + const shouldShowRoute = isVisible && totalChains > 0; + + const outputAfterFee = useMemo( + () => + routingFeeIsTakenFromOutput && isDefined(output) + ? new BigNumber(output).times(ROUTING_FEE_RATIO).toFixed(outputToken.decimals, BigNumber.ROUND_DOWN) + : output, + [output, outputToken.decimals, routingFeeIsTakenFromOutput] + ); + + const hadleToggleVisible = () => setIsVisible(prev => !prev); + + const Chevron = isVisible ? ChevronUp : ChevronDown; + + return ( +

+

+ + + + + + + + - - - - -

- {shouldShowRoute && ( -
- {isLbInput && } -
- {chainsHeap.map((chain, index) => ( - - ))} +

+ {shouldShowRoute && ( +
+ {isLbInput && } +
+ {chainsHeap.map((chain, index) => ( + + ))} +
+ {isLbOutput && }
- {isLbOutput && } -
- )} -
- ); -}; + )} +
+ ); + } +); diff --git a/src/lib/temple/contract.ts b/src/lib/temple/contract.ts index 38d18f5425..9e89461dbc 100644 --- a/src/lib/temple/contract.ts +++ b/src/lib/temple/contract.ts @@ -7,5 +7,5 @@ export const loadContract = memoize(fetchContract, { }); function fetchContract(tezos: TezosToolkit, address: string, walletAPI = true) { - return walletAPI ? tezos.wallet.at(address) : (tezos.contract.at(address) as any); + return walletAPI ? tezos.wallet.at(address) : tezos.contract.at(address); } diff --git a/src/lib/utils/numbers.ts b/src/lib/utils/numbers.ts index 8099b04aef..096e3ad98a 100644 --- a/src/lib/utils/numbers.ts +++ b/src/lib/utils/numbers.ts @@ -1,8 +1,8 @@ export const isPositiveNumber = (value?: number): value is number => value != null && value > 0; -const THOUSAND = 1000; -const MILLION = 1000 * 1000; -const BILLION = 1000 * 1000 * 1000; +const THOUSAND = 1_000; +const MILLION = 1_000_000; +const BILLION = 1_000_000_000; export const kFormatter = (num: number): string => { if (isNaN(num)) { diff --git a/src/lib/utils/swap.utils.ts b/src/lib/utils/swap.utils.ts index 5fed33ba7e..184af5ecc7 100644 --- a/src/lib/utils/swap.utils.ts +++ b/src/lib/utils/swap.utils.ts @@ -1,4 +1,4 @@ -import { ContractMethod, ContractProvider, TezosToolkit, TransferParams } from '@taquito/taquito'; +import { ContractMethod, ContractProvider, TezosToolkit, TransferParams, Wallet } from '@taquito/taquito'; import { BigNumber } from 'bignumber.js'; import { Route3Token } from 'lib/apis/route3/fetch-route3-tokens'; @@ -30,7 +30,7 @@ export const getSwapTransferParams = async ( accountPkh: string ) => { const resultParams: Array = []; - let swapMethod: ContractMethod; + let swapMethod: ContractMethod; if (isSwapChains(chains)) { const swapContract = await loadContract(tezos, ROUTE3_CONTRACT, false);