Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lendihop committed Jan 10, 2024
1 parent 7a46e9e commit 44cde49
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 129 deletions.
3 changes: 2 additions & 1 deletion src/app/pages/AddAsset/AddAsset.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -116,7 +117,7 @@ const Form: FC = () => {
let stateToSet: Partial<ComponentState>;

try {
let contract;
let contract: ContractAbstraction<Wallet | ContractProvider>;
try {
contract = await loadContract(tezos, contractAddress, false);
} catch {
Expand Down
13 changes: 8 additions & 5 deletions src/app/templates/DelegateForm.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -112,7 +112,7 @@ const DelegateForm: FC = () => {
return tezos.estimate.setDelegate({
source: accountPkh,
delegate: to
});
} as DelegateParams);
}
}, [tezos, accountPkh, acc.type, toResolved]);

Expand Down Expand Up @@ -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 });
Expand All @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions src/app/templates/SendForm/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -340,7 +346,7 @@ export const Form: FC<FormProps> = ({ 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;

Expand Down
226 changes: 111 additions & 115 deletions src/app/templates/SwapForm/SwapRoute/SwapRoute.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -22,119 +22,115 @@ interface Props {
className?: string;
}

export const SwapRoute: FC<Props> = ({
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 (
<div className={className}>
<p
className="flex justify-between items-center text-xs text-gray-500 cursor-pointer"
onClick={hadleToggleVisible}
>
<T id="swapRoute" />
<span className="flex items-center gap-1">
<span>
<T id="route3ChainsDexes" substitutions={[totalChains, totalHops]} />
export const SwapRoute = memo<Props>(
({ 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 (
<div className={className}>
<p
className="flex justify-between items-center text-xs text-gray-500 cursor-pointer"
onClick={hadleToggleVisible}
>
<T id="swapRoute" />
<span className="flex items-center gap-1">
<span>
<T id="route3ChainsDexes" substitutions={[totalChains, totalHops]} />
</span>
<span>
<Chevron className={'h-4 w-auto stroke-3 stroke-current'} />
</span>
</span>
<span>
<Chevron className={'h-4 w-auto stroke-3 stroke-current'} />
</span>
</span>
</p>
{shouldShowRoute && (
<div className="flex flex-row items-center my-2">
{isLbInput && <LbPoolPart isLbOutput={false} amount={input} totalChains={totalChains} />}
<div className="flex flex-1 flex-col gap-2">
{chainsHeap.map((chain, index) => (
<SwapRouteItem
key={index}
chain={chain}
baseInput={input}
baseOutput={output}
shouldShowInput={!isLbInput}
shouldShowOutput={!isLbOutput}
/>
))}
</p>
{shouldShowRoute && (
<div className="flex flex-row items-center my-2">
{isLbInput && <LbPoolPart isLbOutput={false} amount={input} totalChains={totalChains} />}
<div className="flex flex-1 flex-col gap-2">
{chainsHeap.map((chain, index) => (
<SwapRouteItem
key={index}
chain={chain}
baseInput={input}
baseOutput={output}
shouldShowInput={!isLbInput}
shouldShowOutput={!isLbOutput}
/>
))}
</div>
{isLbOutput && <LbPoolPart isLbOutput amount={outputAfterFee} totalChains={totalChains} />}
</div>
{isLbOutput && <LbPoolPart isLbOutput amount={outputAfterFee} totalChains={totalChains} />}
</div>
)}
</div>
);
};
)}
</div>
);
}
);
2 changes: 1 addition & 1 deletion src/lib/temple/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
6 changes: 3 additions & 3 deletions src/lib/utils/numbers.ts
Original file line number Diff line number Diff line change
@@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/swap.utils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -30,7 +30,7 @@ export const getSwapTransferParams = async (
accountPkh: string
) => {
const resultParams: Array<TransferParams> = [];
let swapMethod: ContractMethod<ContractProvider>;
let swapMethod: ContractMethod<Wallet | ContractProvider>;

if (isSwapChains(chains)) {
const swapContract = await loadContract(tezos, ROUTE3_CONTRACT, false);
Expand Down

0 comments on commit 44cde49

Please sign in to comment.