Skip to content

Commit

Permalink
Merge pull request #922 from oraichain/optimize/smart-route-api
Browse files Browse the repository at this point in the history
optimize smart route api
  • Loading branch information
haunv3 authored Aug 16, 2024
2 parents 04445d5 + 76e1514 commit d7674cb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@oraichain/orai-bitcoin": "2.0.0",
"@oraichain/oraidex-common-ui": "1.0.11",
"@oraichain/oraidex-contracts-sdk": "^1.0.47",
"@oraichain/oraidex-universal-swap": "1.0.98",
"@oraichain/oraidex-universal-swap": "1.1.1",
"@reduxjs/toolkit": "^1.9.3",
"@sentry/react": "^7.47.0",
"@tanstack/react-query": "^4.32.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@

.input {
text-align: right;
position: relative;

.mask {
position: absolute;
width: 100%;
height: 100%;
border-radius: 12px;
background: linear-gradient(90deg, #2325211a 33%, #181a17 50%, #2325211a 66%) #232521;
background-size: 300% 100%;

animation: maskInput ease-in-out 1.5s infinite;

@keyframes maskInput {
0% {
background-position: right;
}
100% {
background-position: left;
}
}
}
}

.amount {
Expand Down Expand Up @@ -238,7 +259,7 @@
}
}

&>span {
& > span {
text-align: right;
}

Expand Down Expand Up @@ -278,4 +299,4 @@
padding-top: 6px;
font-size: 14px;
font-weight: 400;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ export default function InputSwapV4({
</div>
</div>
<div className={cx('box-input')}>
<div className={cx('input')}>
<div className={styles.input}>
{loadingSimulate && <div className={styles.mask} />}
<NumberFormat
placeholder="0"
thousandSeparator
Expand Down
11 changes: 10 additions & 1 deletion src/pages/UniversalSwap/Swap/hooks/useCalculateDataSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getAverageRatio, getRemoteDenom, isAllowAlphaSmartRouter, isAllowIBCWas
import { fetchTokenInfos } from 'rest/api';
import { useSimulate } from './useSimulate';
import { useSwapFee } from './useSwapFee';
import { useEffect, useState } from 'react';

export const SIMULATE_INIT_AMOUNT = 1;

Expand All @@ -36,6 +37,8 @@ const useCalculateDataSwap = ({ originalFromToken, originalToToken, fromToken, t
originalToToken
);

const [isAvgSimulate, setIsAvgSimulate] = useState<boolean>(false);

const {
data: [fromTokenInfoData, toTokenInfoData]
} = useQuery(['token-infos', fromToken, toToken], () => fetchTokenInfos([fromToken!, toToken!]), { initialData: [] });
Expand Down Expand Up @@ -69,10 +72,16 @@ const useCalculateDataSwap = ({ originalFromToken, originalToToken, fromToken, t
useAlphaSmartRoute,
useIbcWasm,
isAIRoute,
protocols
protocols,
isAvgSimulate
}
);

useEffect(() => {
if (isAvgSimulate || !averageSimulateData?.amount) return;
if (!isAvgSimulate && !!averageSimulateData?.amount) setIsAvgSimulate(true);
}, [averageSimulateData]);

const fromAmountTokenBalance =
fromTokenInfoData &&
toAmount(fromAmountToken, originalFromToken?.decimals || fromTokenInfoData?.decimals || CW20_DECIMALS);
Expand Down
8 changes: 7 additions & 1 deletion src/pages/UniversalSwap/Swap/hooks/useSimulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ export const useSimulate = (
useIbcWasm?: boolean;
isAIRoute?: boolean;
protocols?: string[];
isAvgSimulate?: boolean;
}
) => {
const [[fromAmountToken, toAmountToken], setSwapAmount] = useState([initAmount || null, 0]);
const debouncedFromAmount = useDebounce(fromAmountToken, 800);
let enabled = !!fromTokenInfoData && !!toTokenInfoData && !!debouncedFromAmount && fromAmountToken > 0;
if (simulateOption.isAvgSimulate) enabled = false;
const { data: simulateData, isPreviousData: isPreviousSimulate } = useQuery(
[queryKey, fromTokenInfoData, toTokenInfoData, debouncedFromAmount, simulateOption?.isAIRoute],
() => {
Expand All @@ -53,7 +56,10 @@ export const useSimulate = (
keepPreviousData: true,
refetchInterval: 15000,
staleTime: 1000,
enabled: !!fromTokenInfoData && !!toTokenInfoData && !!debouncedFromAmount && fromAmountToken > 0
enabled,
onError: (error) => {
console.log('isAvgSimulate:', simulateOption?.isAvgSimulate, 'error when simulate: ', error);
}
}
);

Expand Down
22 changes: 11 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4401,29 +4401,29 @@
resolved "https://registry.yarnpkg.com/@oraichain/oraidex-contracts-build/-/oraidex-contracts-build-1.0.22.tgz#70b765dd8ea3b500a77cc37e6a45ea9d75f0c5c4"
integrity sha512-eJniKkXlOghPhgSYeH59szYjT3aWaE4K04HZaTPhi435sm9kaTfESj68pr0AsGepf4YeAcoQ9ZwUMbIeSmZtYw==

"@oraichain/oraidex-contracts-sdk@^1.0.43", "@oraichain/oraidex-contracts-sdk@latest":
version "1.0.45"
resolved "https://registry.yarnpkg.com/@oraichain/oraidex-contracts-sdk/-/oraidex-contracts-sdk-1.0.45.tgz#42dae0fdd9e005f920ba305b987009f791acc365"
integrity sha512-/nYztdxEX5LQM4DMJQmi9HvZrBVoY3nLAmYqSKZGZ0U1h1SxU7O/o22R3/pQwB+sAJdcibaI8ygC0ov7jC8paA==

"@oraichain/oraidex-contracts-sdk@^1.0.47":
version "1.0.47"
resolved "https://registry.yarnpkg.com/@oraichain/oraidex-contracts-sdk/-/oraidex-contracts-sdk-1.0.47.tgz#2e9df9900d38ebf3741f98f02b531a9671554726"
integrity sha512-VkMYwyo6vG8En/GICZ+J8YfSuRJpBIK6ppEifuvbeGLUmiLrlA1TsMdqs2rIa1UJQNvEVO3mXodiSXr4V2IDLg==

"@oraichain/oraidex-contracts-sdk@^1.0.48":
"@oraichain/oraidex-contracts-sdk@^1.0.48", "@oraichain/oraidex-contracts-sdk@^1.0.49":
version "1.0.49"
resolved "https://registry.yarnpkg.com/@oraichain/oraidex-contracts-sdk/-/oraidex-contracts-sdk-1.0.49.tgz#093221564552e92901c73f6043ce4dc72208d857"
integrity sha512-J15JR0+wueYMLMFjz/Omg5ADSKOmmTvsBstVf76NQjrvYpekzdlIGvoTIUCGjyf+DOdzu8MevOk5HDAnOrj3Fw==

"@oraichain/[email protected]":
version "1.0.98"
resolved "https://registry.yarnpkg.com/@oraichain/oraidex-universal-swap/-/oraidex-universal-swap-1.0.98.tgz#3bbd3ce0fedf1c60c6c44c9fbd9ccc3512871858"
integrity sha512-wjVO0jRu5m7yDBPPUdK41ruh9+fjxwu+RhYrFzDlkwEODdBFy2MZs5xDWdDFCc0O7toEPVY/FNTc5GR915vFFw==
"@oraichain/oraidex-contracts-sdk@latest":
version "1.0.45"
resolved "https://registry.yarnpkg.com/@oraichain/oraidex-contracts-sdk/-/oraidex-contracts-sdk-1.0.45.tgz#42dae0fdd9e005f920ba305b987009f791acc365"
integrity sha512-/nYztdxEX5LQM4DMJQmi9HvZrBVoY3nLAmYqSKZGZ0U1h1SxU7O/o22R3/pQwB+sAJdcibaI8ygC0ov7jC8paA==

"@oraichain/[email protected]":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@oraichain/oraidex-universal-swap/-/oraidex-universal-swap-1.1.1.tgz#584e85e947680572d4cc2adbee5df014180643f7"
integrity sha512-VD/hq7uXBF4Li3mgrWPj4rvPWYuiaWm2eEYKGw6kmdzCWIlx4AFbp4TzWWmZdYgXJC5mqsO6UJz8sS1slGbuaw==
dependencies:
"@oraichain/common" "^1.0.3"
"@oraichain/oraidex-common" "^1.0.91"
"@oraichain/oraidex-contracts-sdk" "^1.0.43"
"@oraichain/oraidex-contracts-sdk" "^1.0.49"
bech32 "1.1.4"
ethers "^5.0.15"
tronweb "5.3.2"
Expand Down

0 comments on commit d7674cb

Please sign in to comment.