Skip to content

Commit

Permalink
Merge pull request #278 from bnb-chain/wenty/aggregator
Browse files Browse the repository at this point in the history
fix: Fix one-to-many logic of special tokens do not take effect
  • Loading branch information
wenty22 authored Jan 9, 2025
2 parents e9bedcb + 24282ef commit 6f3783a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
30 changes: 14 additions & 16 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,30 +173,30 @@ export abstract class BaseAdapter<G extends object, C = unknown, T = unknown> {
this.nativeCurrencies[toChainId].symbol.toUpperCase();
const tokenMap = this.symbolMap.get(toChainId);

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') ?? [];
return [...ethTokens, ...wethTokens];

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
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

0 comments on commit 6f3783a

Please sign in to comment.