Skip to content

Commit

Permalink
refactor: Refactor sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
wenty22 committed Nov 14, 2024
1 parent 030d8c3 commit c90711e
Show file tree
Hide file tree
Showing 22 changed files with 723 additions and 613 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function useTransferConfig() {
defaultSelectedInfo: {
fromChainId: 1,
toChainId: 56,
tokenSymbol: 'USDT', // USDT
tokenAddress: '0xdac17f958d2ee523a2206206994597c13d831ec7', // USDT
amount: '',
},
order: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function useTestnetTransferConfig() {
defaultSelectedInfo: {
fromChainId: 97,
toChainId: 3448148188,
tokenSymbol: 'USDT', // USDT
tokenAddress: '0x337610d27c682E347C9cD60BD4b3b107C9d34dDd', // USDT
amount: '',
},
order: {
Expand Down
112 changes: 59 additions & 53 deletions packages/canonical-bridge-sdk/src/adapters/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { isSameAddress } from '@/shared/address';
export abstract class BaseAdapter<G extends object, C = unknown, T = unknown> {
public abstract bridgeType: BridgeType;

protected options: IBaseAdapterOptions<G>;

protected config: G;
protected excludedChains: number[] = [];
protected excludedTokens: Record<number, Array<string>> = {};
Expand All @@ -27,6 +29,7 @@ export abstract class BaseAdapter<G extends object, C = unknown, T = unknown> {
protected brandChains: number[] = [];
protected externalChains: IExternalChain[] = [];
protected displayTokenSymbols: Record<number, Record<string, string>> = {};
protected chainConfigs: IChainConfig[] = [];

protected chains: C[] = [];
protected chainMap = new Map<number, C>();
Expand All @@ -38,6 +41,7 @@ export abstract class BaseAdapter<G extends object, C = unknown, T = unknown> {
>();

constructor(options: IBaseAdapterOptions<G>) {
this.options = options;
this.config = options.config ?? {};
this.excludedChains = options.excludedChains ?? [];
this.excludedTokens = options.excludedTokens ?? {};
Expand All @@ -59,6 +63,7 @@ export abstract class BaseAdapter<G extends object, C = unknown, T = unknown> {
this.brandChains = initialOptions?.brandChains ?? [];
this.externalChains = initialOptions?.externalChains ?? [];
this.displayTokenSymbols = initialOptions?.displayTokenSymbols ?? {};
this.chainConfigs = initialOptions?.chainConfigs ?? [];
}

protected abstract initChains(): void;
Expand Down Expand Up @@ -98,21 +103,17 @@ export abstract class BaseAdapter<G extends object, C = unknown, T = unknown> {

public abstract getChainId(chain: C): number;

public abstract getTokenInfo({
public abstract getTokenBaseInfo({
chainId,
token,
}: {
chainId: number;
token: T;
}): IBridgeTokenBaseInfo;

public getChainInfo({
chainId,
chainConfig,
}: {
chainId: number;
chainConfig: IChainConfig;
}) {
public getChainBaseInfo({ chainId }: { chainId: number }) {
const chainConfig = this.chainConfigs.find((e) => e.id === chainId);

const explorerUrl = chainConfig?.explorer.url?.replace(/\/$/, '') ?? '';
const tmpUrlPattern = explorerUrl ? `${explorerUrl}/token/{0}` : '';
const tokenUrlPattern =
Expand Down Expand Up @@ -183,7 +184,7 @@ 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 getToToken({
protected getTransferToToken({
fromChainId,
toChainId,
fromTokenSymbol,
Expand Down Expand Up @@ -228,44 +229,53 @@ export abstract class BaseAdapter<G extends object, C = unknown, T = unknown> {
return toToken;
}

public getRealTokenSymbol({
fromChainId,
toChainId,
tokenSymbol,
public getTokenSymbolByTokenAddress({
chainId,
tokenAddress,
}: {
fromChainId: number;
toChainId: number;
tokenSymbol: string;
chainId: number;
tokenAddress: string;
}) {
return tokenSymbol;
const tokens = this.tokenMap.get(chainId);

let baseInfo: IBridgeTokenBaseInfo | undefined;
const token = tokens?.find((t) => {
baseInfo = this.getTokenBaseInfo({
chainId,
token: t,
});
return isSameAddress(baseInfo.address, tokenAddress);
});

if (token) {
return baseInfo?.symbol?.toUpperCase();
}
}

public getChain({ chainId }: { chainId: number }) {
public getChainById(chainId: number) {
return this.chainMap.get(chainId);
}

public getTokenPair({
fromChainId,
toChainId,
tokenSymbol,
tokenAddress,
}: {
fromChainId: number;
toChainId: number;
tokenSymbol: string;
tokenAddress: string;
}) {
const realTokenSymbol = this.getRealTokenSymbol({
fromChainId,
toChainId,
tokenSymbol,
const tokenSymbol = this.getTokenSymbolByTokenAddress({
chainId: fromChainId,
tokenAddress,
});

const tokenPair = this.transferMap
.get(fromChainId)
?.get(toChainId)
?.get(realTokenSymbol);
return {
tokenPair,
};
if (tokenSymbol) {
return this.transferMap
.get(fromChainId)
?.get(toChainId)
?.get(tokenSymbol);
}
}

public getFromChains() {
Expand All @@ -291,14 +301,23 @@ export abstract class BaseAdapter<G extends object, C = unknown, T = unknown> {
fromChainId: number;
toChainId: number;
}) {
const tokenPairs: ITransferTokenPair<T, unknown>[] = [];

const tokenPairsMap = this.transferMap.get(fromChainId)?.get(toChainId);
tokenPairsMap?.forEach((tokenPair) => {
tokenPairs.push(tokenPair);
const allMap = new Map<string, ITransferTokenPair<T, unknown>>();
this.transferMap.get(fromChainId)?.forEach((toMap) => {
toMap.forEach((tokenPair, tokenSymbol) => {
allMap.set(tokenSymbol.toUpperCase(), tokenPair);
});
});

return tokenPairs;
// The tokenPairs in `allMap` may not be the `tokenPair` in the toChain,
// so it needs to be overwritten
this.transferMap
.get(fromChainId)
?.get(toChainId)
?.forEach((tokenPair, tokenSymbol) => {
allMap.set(tokenSymbol.toUpperCase(), tokenPair);
});

return Array.from(allMap.values());
}

public isToChainCompatible({
Expand All @@ -311,24 +330,11 @@ export abstract class BaseAdapter<G extends object, C = unknown, T = unknown> {
return !!this.transferMap.get(fromChainId)?.get(toChainId);
}

public isTokenCompatible({
fromChainId,
toChainId,
tokenSymbol,
}: {
public isTokenCompatible(params: {
fromChainId: number;
toChainId: number;
tokenSymbol: string;
tokenAddress: string;
}) {
const realTokenSymbol = this.getRealTokenSymbol({
fromChainId,
toChainId,
tokenSymbol,
});

return !!this.transferMap
.get(fromChainId)
?.get(toChainId)
?.get(realTokenSymbol);
return !!this.getTokenPair(params);
}
}
7 changes: 6 additions & 1 deletion packages/canonical-bridge-sdk/src/adapters/base/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { INativeCurrency, IExternalChain } from '@/aggregator/types';
import {
INativeCurrency,
IExternalChain,
IChainConfig,
} from '@/aggregator/types';

export interface ITransferTokenPair<T, P = unknown> {
fromChainId: number;
Expand All @@ -25,6 +29,7 @@ export interface IInitialOptions {
brandChains?: number[];
externalChains?: IExternalChain[];
displayTokenSymbols?: Record<number, Record<string, string>>;
chainConfigs?: IChainConfig[];
}

export interface IBridgeTokenBaseInfo {
Expand Down
22 changes: 12 additions & 10 deletions packages/canonical-bridge-sdk/src/adapters/cBridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,26 @@ export class CBridgeAdapter extends BaseAdapter<
ICBridgeChain,
ICBridgeToken
> {
protected options: ICBridgeAdapterOptions;
private client: AxiosInstance;
public bridgeType: BridgeType = 'cBridge';

private peggedPairConfigs: ICBridgePeggedPairConfig[] = [];
private burnPairConfigs: ICBridgeBurnPairConfig[] = [];

constructor(options: ICBridgeAdapterOptions) {
const {
timeout = CLIENT_TIME_OUT,
endpoint = env.CBRIDGE_ENDPOINT,
...baseOptions
} = options;
const finalOptions = {
timeout: CLIENT_TIME_OUT,
endpoint: env.CBRIDGE_ENDPOINT,
...options,
};

super(baseOptions);
super(finalOptions);
this.options = finalOptions;

this.client = axios.create({
timeout,
baseURL: endpoint,
timeout: this.options.timeout,
baseURL: this.options.endpoint,
});
}

Expand Down Expand Up @@ -521,7 +523,7 @@ export class CBridgeAdapter extends BaseAdapter<
ITransferTokenPair<ICBridgeToken>
>();
fromTokens.forEach((fromToken) => {
const toToken = this.getToToken({
const toToken = this.getTransferToToken({
fromChainId: fromChain.id,
toChainId: toChain.id,
fromTokenSymbol: fromToken.token.symbol?.toUpperCase(),
Expand Down Expand Up @@ -706,7 +708,7 @@ export class CBridgeAdapter extends BaseAdapter<
return chain.id;
}

public getTokenInfo({
public getTokenBaseInfo({
chainId,
token,
}: {
Expand Down
33 changes: 17 additions & 16 deletions packages/canonical-bridge-sdk/src/adapters/deBridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,30 @@ export class DeBridgeAdapter extends BaseAdapter<
IDeBridgeChain,
IDeBridgeToken
> {
protected options: IDeBridgeAdapterOptions;
private client: AxiosInstance;
private statsClient: AxiosInstance;
public bridgeType: BridgeType = 'deBridge';

constructor(options: IDeBridgeAdapterOptions) {
const {
timeout = CLIENT_TIME_OUT,
endpoint = env.DEBRIDGE_ENDPOINT,
statsEndpoint = env.DEBRIDGE_STATS_ENDPOINT,
...baseOptions
} = options;
const finalOptions = {
timeout: CLIENT_TIME_OUT,
endpoint: env.DEBRIDGE_ENDPOINT,
statsEndpoint: env.DEBRIDGE_STATS_ENDPOINT,
...options,
};

super(baseOptions);
super(finalOptions);
this.options = finalOptions;

this.client = axios.create({
timeout,
baseURL: endpoint,
timeout: this.options.timeout,
baseURL: this.options.endpoint,
});

this.statsClient = axios.create({
timeout,
baseURL: statsEndpoint,
timeout: this.options.timeout,
baseURL: this.options.statsEndpoint,
});
}

Expand Down Expand Up @@ -72,7 +74,6 @@ export class DeBridgeAdapter extends BaseAdapter<
userAddress,
toUserAddress,
affiliateFeePercent = 0,
accesstoken = '',
prependOperatingExpenses = false,
}: IDeBridgeEstimatedFeesInput): Promise<IDeBridgeCreateQuoteResponse> {
try {
Expand All @@ -89,8 +90,8 @@ export class DeBridgeAdapter extends BaseAdapter<
srcChainOrderAuthorityAddress: userAddress,
} as any;

if (accesstoken) {
deBridgeParams.accesstoken = accesstoken;
if (this.options.accessToken) {
deBridgeParams.accesstoken = this.options.accessToken;
}
const urlParams = new URLSearchParams(deBridgeParams as any);
const deBridgeQuote = await this.createTxQuote(urlParams);
Expand Down Expand Up @@ -239,7 +240,7 @@ export class DeBridgeAdapter extends BaseAdapter<
ITransferTokenPair<IDeBridgeToken>
>();
fromTokens.forEach((fromToken) => {
const toToken = this.getToToken({
const toToken = this.getTransferToToken({
fromChainId: fromChain.chainId,
toChainId: toChain.chainId,
fromTokenSymbol: fromToken.symbol?.toUpperCase(),
Expand Down Expand Up @@ -286,7 +287,7 @@ export class DeBridgeAdapter extends BaseAdapter<
return chain.chainId;
}

public getTokenInfo({
public getTokenBaseInfo({
chainId,
token,
}: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface IDeBridgeAdapterOptions
timeout?: number;
endpoint?: string;
statsEndpoint?: string;
accessToken?: string;
}

export interface IDeBridgeChain {
Expand Down Expand Up @@ -150,7 +151,6 @@ export interface IDeBridgeEstimatedFeesInput {
toUserAddress?: string;
affiliateFeePercent?: number;
prependOperatingExpenses?: boolean;
accesstoken?: string;
}

export interface ISendDebridgeTokenInput {
Expand Down
Loading

0 comments on commit c90711e

Please sign in to comment.