Skip to content

Commit

Permalink
Merge branch 'wenty/aggregator' into feat/upperLimit0107
Browse files Browse the repository at this point in the history
  • Loading branch information
Halibao-Lala committed Jan 10, 2025
2 parents faddfa8 + 9e284a1 commit 5e97e6c
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 57 deletions.
48 changes: 21 additions & 27 deletions packages/canonical-bridge-sdk/src/adapters/base/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IBaseAdapterOptions, ITokenPair } from '@/adapters/base/types';
import { isSameAddress } from '@/shared/address';
import { uniqueArr } from '@/shared/object';
import {
BridgeType,
ChainType,
Expand Down Expand Up @@ -157,9 +158,6 @@ export abstract class BaseAdapter<G extends object, C = unknown, T = unknown> {
};
}

// 1. Native currency is ETH -> Native currency is ETH, all transfer to ETH
// 2. Native currency is ETH -> Native currency is NOT ETH, transfer to ETH first, if not, WETH
// 3. Native currency is NOT ETH -> Native currency is ETH, all transfer to ETH
protected getToTokensForPair({
fromChainId,
toChainId,
Expand All @@ -175,34 +173,30 @@ export abstract class BaseAdapter<G extends object, C = unknown, T = unknown> {
this.nativeCurrencies[toChainId].symbol.toUpperCase();
const tokenMap = this.symbolMap.get(toChainId);

if (['ETH', 'WETH'].includes(fromTokenSymbol)) {
if (fromNativeSymbol === 'ETH') {
if (toNativeSymbol === 'ETH') {
return tokenMap?.get(fromTokenSymbol);
} else {
return tokenMap?.get('ETH') || tokenMap?.get('WETH');
}
} else {
if (toNativeSymbol === 'ETH') {
return tokenMap?.get('ETH');
}
}
const toTokens = [...(tokenMap?.get(fromTokenSymbol) ?? [])];
if (
['ETH', 'WETH'].includes(fromTokenSymbol) &&
(fromNativeSymbol === 'ETH' || toNativeSymbol === 'ETH') &&
this.id === 'deBridge'
) {
const ethTokens = tokenMap?.get('ETH') ?? [];
const wethTokens = tokenMap?.get('WETH') ?? [];

toTokens.push(...ethTokens);
toTokens.push(...wethTokens);
}

let toTokens = tokenMap?.get(fromTokenSymbol);
if (!toTokens) {
const bridgedGroup = this.bridgedTokenGroups.find((group) =>
group.includes(fromTokenSymbol)
);
const nextToken = bridgedGroup?.find(
(item) => item.toUpperCase() !== fromTokenSymbol
);
if (nextToken) {
toTokens = tokenMap?.get(nextToken?.toUpperCase());
const bridgedGroup = this.bridgedTokenGroups.find((e) =>
e.includes(fromTokenSymbol)
);
bridgedGroup?.forEach((anotherTokenSymbol) => {
const anotherToTokens = tokenMap?.get(anotherTokenSymbol.toUpperCase());
if (anotherToTokens?.length) {
toTokens.push(...anotherToTokens);
}
}
});

return toTokens;
return uniqueArr(toTokens);
}

protected getTokenSymbolByTokenAddress({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,15 @@ export class StargateAdapter extends BaseAdapter<
toChainId: toChain.chainId,
fromToken,
toToken,
fromTokenAddress: fromToken.address,
toTokenAddress: toToken.address,
fromTokenAddress: fromToken.token.address,
toTokenAddress: toToken.token.address,
});
});

tokenPairsMap.set(fromToken.address?.toLowerCase(), tokenPairs);
tokenPairsMap.set(
fromToken.token.address?.toLowerCase(),
tokenPairs
);
}
});

Expand Down
11 changes: 11 additions & 0 deletions packages/canonical-bridge-sdk/src/shared/object.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export function isEmpty(obj?: Object) {
return !obj || Object.entries(obj).length === 0;
}

export function uniqueArr<T = unknown>(arr: T[]) {
const map = new Map<any, boolean>();
return arr.filter((item) => {
if (map.get(item)) {
return false;
}
map.set(item, true);
return true;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,45 @@ export function AggregatorProvider(props: AggregatorProviderProps) {
tokenSorter,
});

// output all token pairs
// const result: any[] = [];
// const fromChains = aggregator.getFromChains();
// fromChains.forEach((fromChain) => {
// const toChains = aggregator.getToChains({ fromChainId: fromChain.id });
// toChains.forEach((toChain) => {
// if (fromChain.id !== toChain.id) {
// const tokens = aggregator.getTokens({
// fromChainId: fromChain.id,
// toChainId: toChain.id,
// });
// tokens.forEach((token) => {
// if (token.isCompatible) {
// const toTokens = aggregator.getToTokens({
// fromChainId: fromChain.id,
// toChainId: toChain.id,
// tokenAddress: token.address,
// });

// result.push({
// fromChainId: fromChain.id,
// fromChainName: fromChain.name,
// toChainId: toChain.id,
// toChainName: toChain.name,
// toTokenCount: toTokens.length,
// fromToken: token.symbol,
// fromTokenAddress: token.address,
// toTokens: toTokens.map((e) => ({
// symbol: e.symbol,
// address: e.address,
// })),
// });
// }
// });
// }
// });
// });
// console.log(JSON.stringify(result));

return {
aggregator,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ export function BridgeRoutes() {
const isRoutesModalOpen = useAppSelector((state) => state.transfer.isRoutesModalOpen);
const transferActionInfo = useAppSelector((state) => state.transfer.transferActionInfo);
const isManuallyReload = useAppSelector((state) => state.transfer.isManuallyReload);
const toToken = useAppSelector((state) => state.transfer.toToken);

useFeeRefreshProgress();

// Load estimated bridge fees every 30 seconds when there is bridge route available
useEffect(() => {
let mount = true;
if (!mount) return;
if (!mount || !toToken) return;

if (transferActionInfo) {
const params = {
triggerType: 'refresh' as TriggerType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const ReceiveInfo = ({ onOpen }: ReceiveInfoProps) => {
const selectedToken = useAppSelector((state) => state.transfer.selectedToken);
const estimatedAmount = useAppSelector((state) => state.transfer.estimatedAmount);
const isBase = useBreakpointValue({ base: true, lg: false }) ?? false;
const toToken = useAppSelector((state) => state.transfer.toToken);

const receiveAmt = useMemo(() => {
if (!Number(sendValue)) return null;
Expand Down Expand Up @@ -84,7 +85,8 @@ export const ReceiveInfo = ({ onOpen }: ReceiveInfoProps) => {
const debouncedSendValue = useDebounce(sendValue, DEBOUNCE_DELAY);

useEffect(() => {
if (!isBase) return;
if (!isBase || !toToken) return;

// On mobile
if (sendValue === debouncedSendValue) {
dispatch(setTransferActionInfo(undefined));
Expand All @@ -101,10 +103,11 @@ export const ReceiveInfo = ({ onOpen }: ReceiveInfoProps) => {
} else {
dispatch(setIsGlobalFeeLoading(true));
}
}, [selectedToken, debouncedSendValue, dispatch, sendValue, loadingBridgeFees, isBase]);
}, [selectedToken, debouncedSendValue, dispatch, sendValue, loadingBridgeFees, isBase, toToken]);

const isHideSection = useMemo(() => {
// no receive amount and some routes are displayed
if (!toToken) return true;
if (!Number(sendValue)) return true;
if (isGlobalFeeLoading) return false;
return (
Expand All @@ -114,7 +117,7 @@ export const ReceiveInfo = ({ onOpen }: ReceiveInfoProps) => {
!Object.values(estimatedAmount).every((element) => element === undefined) &&
!receiveAmt)
);
}, [sendValue, estimatedAmount, receiveAmt, isGlobalFeeLoading, isBase]);
}, [toToken, sendValue, isGlobalFeeLoading, isBase, estimatedAmount, receiveAmt]);

const isHideRouteButton = useMemo(() => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Link,
Center,
} from '@bnb-chain/space';
import { IBridgeToken, isSameAddress } from '@bnb-chain/canonical-bridge-sdk';
import { IBridgeToken, isNativeToken, isSameAddress } from '@bnb-chain/canonical-bridge-sdk';

import { useAppSelector } from '@/modules/store/StoreProvider';
import { IconImage } from '@/core/components/IconImage';
Expand Down Expand Up @@ -129,26 +129,39 @@ function ToTokenItem({ token, isSelected }: { token: IBridgeToken; isSelected: b
{token.displaySymbol}
</Text>

{isMobile ? (
<Flex alignItems="center" gap="4px" textDecoration={{ base: 'underline', md: 'unset' }}>
{formatAppAddress({
address: token.address,
})}
<Center boxSize="16px">
{isSelected && (
<Link isExternal href={tokenUrl} color="inherit" _hover={{ color: 'inherit' }}>
<ExLinkIcon boxSize="100%" />
</Link>
)}
</Center>
</Flex>
) : (
<Link isExternal href={tokenUrl} color="inherit" _hover={{ color: 'inherit' }}>
{formatAppAddress({
address: token.address,
headLen: 4,
})}
</Link>
{!isNativeToken(token.address, fromChain?.chainType) && (
<>
{isMobile ? (
<Flex
alignItems="center"
gap="4px"
textDecoration={{ base: 'underline', md: 'unset' }}
>
{formatAppAddress({
address: token.address,
})}
<Center boxSize="16px">
{isSelected && (
<Link
isExternal
href={tokenUrl}
color="inherit"
_hover={{ color: 'inherit' }}
>
<ExLinkIcon boxSize="100%" />
</Link>
)}
</Center>
</Flex>
) : (
<Link isExternal href={tokenUrl} color="inherit" _hover={{ color: 'inherit' }}>
{formatAppAddress({
address: token.address,
headLen: 4,
})}
</Link>
)}
</>
)}
</Flex>
</Flex>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ export function TransferOverview(props: TransferOverviewProps) {
const toTokenInfo = useAppSelector((state) => state.transfer.toToken);
const debouncedSendValue = useDebounce(sendValue, DEBOUNCE_DELAY);
const isBase = useBreakpointValue({ base: true, lg: false }) ?? false;
const toToken = useAppSelector((state) => state.transfer.toToken);

useEffect(() => {
if (isBase) return;
if (isBase || !toToken) return;

if (sendValue === debouncedSendValue) {
dispatch(setTransferActionInfo(undefined));
if (!selectedToken || !Number(debouncedSendValue)) {
Expand All @@ -73,7 +75,7 @@ export function TransferOverview(props: TransferOverviewProps) {
} else {
dispatch(setIsGlobalFeeLoading(true));
}
}, [selectedToken, debouncedSendValue, dispatch, sendValue, loadingBridgeFees, isBase]);
}, [selectedToken, debouncedSendValue, dispatch, sendValue, loadingBridgeFees, isBase, toToken]);

const sortedReceivedAmt = useMemo(() => getSortedReceiveAmount(), [getSortedReceiveAmount]);
const options = useMemo(() => {
Expand Down

0 comments on commit 5e97e6c

Please sign in to comment.