Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix one-to-many logic of special tokens do not take effect #278

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading