Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/fix/swap-input-update' into tmp/…
Browse files Browse the repository at this point in the history
…20241226
  • Loading branch information
dmy147 committed Dec 26, 2024
2 parents 0ad3c3d + 207ea6a commit 38933d2
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@rabby-wallet/rabby-action": "0.1.8",
"@rabby-wallet/rabby-api": "0.9.7",
"@rabby-wallet/rabby-security-engine": "2.0.7",
"@rabby-wallet/rabby-swap": "0.0.43",
"@rabby-wallet/rabby-swap": "0.0.45",
"@rabby-wallet/widgets": "1.0.9",
"@rematch/core": "2.2.0",
"@rematch/select": "3.1.2",
Expand Down
1 change: 0 additions & 1 deletion src/ui/component/ChainSelector/InForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ export const ChainRender = ({
)}
{...other}
>
{/* <img className="logo" src={CHAINS[chain].logo} alt={CHAINS[chain].name} /> */}
{chain && (
<ChainIcon
chain={chain}
Expand Down
6 changes: 5 additions & 1 deletion src/ui/views/Approval/components/BroadcastMode/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useTranslation } from 'react-i18next';
import styled, { createGlobalStyle } from 'styled-components';
import { Card } from '../Card';
import { Divide } from '../Divide';
import { findChainByEnum } from '@/utils/chain';

const GlobalStyle = createGlobalStyle`
.broadcast-mode-popup {
Expand Down Expand Up @@ -189,7 +190,10 @@ export const BroadcastMode = ({
const [account] = useAccount();
const wallet = useWallet();
const { data: supportedPushType } = useRequest(
() => wallet.openapi.gasSupportedPushType(CHAINS[chain]?.serverId),
() =>
wallet.openapi.gasSupportedPushType(
findChainByEnum(chain)!.serverId || CHAINS[chain]?.serverId
),
{
refreshDeps: [chain],
}
Expand Down
6 changes: 3 additions & 3 deletions src/ui/views/Swap/Component/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
SWAP_SUPPORT_CHAINS,
} from '@/constant';
import ChainSelectorInForm from '@/ui/component/ChainSelector/InForm';
import { findChainByServerID } from '@/utils/chain';
import { findChainByEnum, findChainByServerID } from '@/utils/chain';
import type { SelectChainItemProps } from '@/ui/component/ChainSelector/components/SelectChainItem';
import i18n from '@/i18n';
import { useTranslation } from 'react-i18next';
Expand Down Expand Up @@ -471,7 +471,7 @@ export const Main = () => {
}
setPayToken(token);
}}
chainId={CHAINS[chain].serverId}
chainId={findChainByEnum(chain)!.serverId}
type={'from'}
excludeTokens={receiveToken?.id ? [receiveToken?.id] : undefined}
/>
Expand Down Expand Up @@ -516,7 +516,7 @@ export const Main = () => {
setLowCreditVisible(true);
}
}}
chainId={CHAINS[chain].serverId}
chainId={findChainByEnum(chain)!.serverId || CHAINS[chain].serverId}
type={'to'}
excludeTokens={payToken?.id ? [payToken?.id] : undefined}
currentQuote={activeProvider}
Expand Down
36 changes: 22 additions & 14 deletions src/ui/views/Swap/hooks/quote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useRabbySelector } from '@/ui/store';
import stats from '@/stats';
import { ChainGas } from '@/background/service/preference';
import { verifySdk } from './verify';
import { findChainByEnum } from '@/utils/chain';

export interface validSlippageParams {
chain: CHAINS_ENUM;
Expand All @@ -31,6 +32,7 @@ export interface validSlippageParams {
export const useQuoteMethods = () => {
const walletController = useWallet();
const walletOpenapi = walletController.openapi;

const validSlippage = React.useCallback(
async ({
chain,
Expand All @@ -40,7 +42,7 @@ export const useQuoteMethods = () => {
}: validSlippageParams) => {
const p = {
slippage: new BigNumber(slippage).div(100).toString(),
chain_id: CHAINS[chain].serverId,
chain_id: findChainByEnum(chain)!.serverId,
from_token_id: payTokenId,
to_token_id: receiveTokenId,
};
Expand Down Expand Up @@ -98,8 +100,8 @@ export const useQuoteMethods = () => {
async ({ addr, chain, tokenId }: getTokenParams) => {
return walletOpenapi.getToken(
addr,
CHAINS[chain].serverId,
tokenId // CHAINS[chain].nativeTokenAddress
findChainByEnum(chain)!.serverId,
tokenId
);
},
[walletOpenapi]
Expand All @@ -116,15 +118,16 @@ export const useQuoteMethods = () => {
getDexQuoteParams,
'payToken' | 'receiveToken' | 'payAmount' | 'chain' | 'dexId'
>) => {
const chainInfo = findChainByEnum(chain)!;
if (
payToken?.id === CHAINS[chain].nativeTokenAddress ||
payToken?.id === chainInfo.nativeTokenAddress ||
isSwapWrapToken(payToken.id, receiveToken.id, chain)
) {
return [true, false];
}

const allowance = await walletController.getERC20Allowance(
CHAINS[chain].serverId,
chainInfo.serverId,
payToken.id,
getSpender(dexId, chain)
);
Expand Down Expand Up @@ -156,19 +159,20 @@ export const useQuoteMethods = () => {
dexId,
quote,
}: getPreExecResultParams) => {
const chainInfo = findChainByEnum(chain)!;
const nonce = await walletController.getRecommendNonce({
from: userAddress,
chainId: CHAINS[chain].id,
chainId: chainInfo.id,
});
const lastTimeGas: ChainGas | null = await walletController.getLastTimeGasSelection(
CHAINS[chain].id
chainInfo.id
);
const gasMarket = await walletController.gasMarketV2({
chain: CHAINS[chain],
chain: chainInfo,
tx: {
...quote.tx,
nonce,
chainId: CHAINS[chain].id,
chainId: chainInfo.id,
gas: '0x0',
},
});
Expand Down Expand Up @@ -205,7 +209,7 @@ export const useQuoteMethods = () => {
{
from: userAddress,
to: payToken.id,
chainId: CHAINS[chain].id,
chainId: chainInfo.id,
spender: getSpender(dexId, chain),
amount,
}
Expand Down Expand Up @@ -270,7 +274,7 @@ export const useQuoteMethods = () => {
tx: {
...quote.tx,
nonce: nextNonce,
chainId: CHAINS[chain].id,
chainId: chainInfo.id,
value: `0x${new BigNumber(quote.tx.value).toString(16)}`,
gasPrice: `0x${new BigNumber(gasPrice).toString(16)}`,
gas: '0x0',
Expand Down Expand Up @@ -329,7 +333,7 @@ export const useQuoteMethods = () => {
let gasPrice: number;
if (isOpenOcean) {
const gasMarket = await walletController.gasMarketV2({
chainId: CHAINS[chain].serverId,
chainId: findChainByEnum(chain)!.serverId,
});
gasPrice = gasMarket?.[1]?.price;
}
Expand Down Expand Up @@ -362,6 +366,8 @@ export const useQuoteMethods = () => {
chain,
gasPrice,
fee: true,
chainServerId: findChainByEnum(chain)!.serverId,
nativeTokenAddress: findChainByEnum(chain)!.nativeTokenAddress,
},
walletOpenapi
);
Expand Down Expand Up @@ -476,7 +482,9 @@ export const useQuoteMethods = () => {

return Promise.all([
...(supportedDEXList.filter((e) => DEX[e]) as DEX_ENUM[]).map((dexId) =>
getDexQuote({ ...params, dexId })
getDexQuote({ ...params, dexId }).finally(() => {
console.log('dexid end', dexId);
})
),
]);
},
Expand Down Expand Up @@ -572,7 +580,7 @@ export function isSwapWrapToken(
) {
const wrapTokens = [
WrapTokenAddressMap[chain as keyof typeof WrapTokenAddressMap],
CHAINS[chain].nativeTokenAddress,
findChainByEnum(chain)!.nativeTokenAddress,
];
return (
!!wrapTokens.find((token) => isSameAddress(payTokenId, token)) &&
Expand Down
25 changes: 15 additions & 10 deletions src/ui/views/Swap/hooks/token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useRbiSource } from '@/ui/utils/ga-event';
import stats from '@/stats';
import { useAsyncInitializeChainList } from '@/ui/hooks/useChain';
import { SWAP_SUPPORT_CHAINS } from '@/constant';
import { findChain } from '@/utils/chain';
import { findChain, findChainByEnum } from '@/utils/chain';
import { GasLevelType } from '../Component/ReserveGasPopup';
import { useSwapSlippage } from './slippage';
import { useLowCreditState } from '../Component/LowCreditModal';
Expand All @@ -41,7 +41,7 @@ const useTokenInfo = ({
if (userAddress && token?.id && chain) {
const data = await wallet.openapi.getToken(
userAddress,
CHAINS[chain].serverId,
findChainByEnum(chain)!.serverId,
token.id
);
return data;
Expand Down Expand Up @@ -214,17 +214,18 @@ export const useTokenPair = (userAddress: string) => {
setIsDraggingSlider(true);
setSwapUseSlider(true);
setSlider(v);

if (syncAmount) {
setIsDraggingSlider(false);
}

if (v === 100) {
handleSlider100();
return;
}
setPayAmount(
new BigNumber(v).div(100).times(tokenAmountBn(payToken)).toString(10)
);

if (syncAmount) {
setIsDraggingSlider(false);
}
}
},
[payToken]
Expand All @@ -247,7 +248,10 @@ export const useTokenPair = (userAddress: string) => {

const payTokenIsNativeToken = useMemo(() => {
if (payToken) {
return isSameAddress(payToken.id, CHAINS[chain].nativeTokenAddress);
return isSameAddress(
payToken.id,
findChainByEnum(chain)!.nativeTokenAddress
);
}
return false;
}, [chain, payToken]);
Expand Down Expand Up @@ -281,7 +285,7 @@ export const useTokenPair = (userAddress: string) => {
const { value: gasList } = useAsync(() => {
gasPriceRef.current = undefined;
setGasLevel('normal');
return wallet.gasMarketV2({ chainId: CHAINS[chain].serverId });
return wallet.gasMarketV2({ chainId: findChainByEnum(chain)!.serverId });
}, [chain]);

const normalGasPrice = useMemo(
Expand Down Expand Up @@ -356,7 +360,7 @@ export const useTokenPair = (userAddress: string) => {
if (payToken?.id && receiveToken?.id) {
const wrapTokens = [
WrapTokenAddressMap[chain],
CHAINS[chain].nativeTokenAddress,
findChainByEnum(chain)!.nativeTokenAddress,
];
const res =
!!wrapTokens.find((token) => isSameAddress(payToken?.id, token)) &&
Expand Down Expand Up @@ -445,6 +449,7 @@ export const useTokenPair = (userAddress: string) => {
setQuotesList((e) =>
e.map((q) => ({ ...q, loading: true, isBest: false }))
);
setActiveProvider(undefined);
return getAllQuotes({
userAddress,
payToken,
Expand Down Expand Up @@ -708,7 +713,7 @@ export const useTokenPair = (userAddress: string) => {
};

function getChainDefaultToken(chain: CHAINS_ENUM) {
const chainInfo = CHAINS[chain];
const chainInfo = findChainByEnum(chain)!;
return {
id: chainInfo.nativeTokenAddress,
decimals: chainInfo.nativeTokenDecimals,
Expand Down
8 changes: 4 additions & 4 deletions src/ui/views/Swap/hooks/verify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { useMemo } from 'react';
import { getRouter, getSpender, isSwapWrapToken } from './quote';
import BigNumber from 'bignumber.js';
import { findChain } from '@/utils/chain';
import { findChain, findChainByEnum } from '@/utils/chain';

type ValidateTokenParam = {
id: string;
Expand All @@ -35,7 +35,7 @@ export const verifyRouterAndSpender = (
const spenderWhitelist = getSpender(dexId, chain);
const isNativeToken = isSameAddress(
payTokenId,
CHAINS[chain].nativeTokenAddress
findChainByEnum(chain)!.nativeTokenAddress
);
const isWrapTokens = isSwapWrapToken(payTokenId, receiveTokenId, chain);

Expand All @@ -48,7 +48,7 @@ export const verifyRouterAndSpender = (
};

const isNativeToken = (chain: CHAINS_ENUM, tokenId: string) =>
isSameAddress(tokenId, CHAINS[chain].nativeTokenAddress);
isSameAddress(tokenId, findChainByEnum(chain)!.nativeTokenAddress);

export const verifyCalldata = <T extends Parameters<typeof decodeCalldata>[1]>(
data: QuoteResult | null,
Expand Down Expand Up @@ -123,7 +123,7 @@ export const verifySdk = <T extends ValidateTokenParam>(
data,
actualDexId,
new BigNumber(slippage).div(100).toFixed(),
data?.tx ? { ...data?.tx, chainId: CHAINS[chain].id } : undefined
data?.tx ? { ...data?.tx, chainId: findChainByEnum(chain)!.id } : undefined
);

return {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4900,10 +4900,10 @@
sha256-uint8array "^0.10.3"
url-parse "^1.5.1"

"@rabby-wallet/[email protected].43":
version "0.0.43"
resolved "https://registry.yarnpkg.com/@rabby-wallet/rabby-swap/-/rabby-swap-0.0.43.tgz#79b24115a438a4910da247be6e7b652f02f26e64"
integrity sha512-Zyhb54fV0JRqJwXP3wt7L+BAd6fSCdOM/oTqhduA3M419ZVZI/DiN7hMo6S/EvLizKPIJiiIMokT8J9Iml5eAQ==
"@rabby-wallet/[email protected].45":
version "0.0.45"
resolved "https://registry.yarnpkg.com/@rabby-wallet/rabby-swap/-/rabby-swap-0.0.45.tgz#523f07ebf869ceac171a5c36494a0d053394ef2e"
integrity sha512-ps5N9ZyTgqMo+5VWrbzuchHI4L+VQngjpPbRSVNd+6M8Mdqx02haMJy+lGmanKpTWConRMcdZ2AY4qo6I8tBqA==
dependencies:
"@ethersproject/abi" "^5.7.0"
axios "^0.27.2"
Expand Down

0 comments on commit 38933d2

Please sign in to comment.