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

use new configure #176

Merged
merged 6 commits into from
Jul 23, 2024
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
1 change: 1 addition & 0 deletions apollo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"generate:schema": "ts-node generate-typings.ts && cp src/graphql.schema.ts src/graphql.ts"
},
"dependencies": {
"@helixbridge/helixconf": "1.0.1",
"@nestjs/apollo": "^10.0.9",
"@nestjs/common": "^8.4.6",
"@nestjs/config": "^2.1.0",
Expand Down
127 changes: 90 additions & 37 deletions apollo/src/aggregation/aggregation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,36 +187,62 @@ export class AggregationService extends PrismaClient implements OnModuleInit {
};

if (version === 'lnv2') {
const sourceNode = this.lnv2Service.transfers.find((item) => item.chainId === sourceChainId);
const sourceTokenInfo = sourceNode?.tokens.find(
(item) => item.fromAddress.toLowerCase() === sourceToken.toLowerCase()
const sourceNode = this.lnv2Service.transfers.find(
(item) => Number(item.chainConfig.id) === sourceChainId
);
const sourceTokenInfo = sourceNode?.chainConfig.tokens.find(
(item) => item.address.toLowerCase() === sourceToken.toLowerCase()
);
if (sourceTokenInfo === undefined) {
return '0';
}
const targetNode = this.lnv2Service.transfers.find((item) => item.chainId === targetChainId);
const targetTokenInfo = targetNode?.tokens.find((item) => item.key === sourceTokenInfo.key);
const couple = sourceNode.chainConfig.couples.find(
(item) =>
Number(item.chain.id) === targetChainId &&
item.protocol.name.startsWith('lnv2') &&
item.symbol.from === sourceTokenInfo.symbol
);
if (couple === undefined) {
return '0';
}
const targetNode = this.lnv2Service.transfers.find(
(item) => Number(item.chainConfig.id) === targetChainId
);
const targetTokenInfo = targetNode?.chainConfig.tokens.find(
(item) => item.symbol === couple.symbol.to
);
if (targetTokenInfo === undefined) {
return '0';
}

return transferDecimals(amount, sourceTokenInfo.decimals - targetTokenInfo.decimals);
} else {
const lnv3SourceBridge = this.lnv3Service.transfers.find(
(item) => item.chainId === sourceChainId
const srcChainConfig = this.lnv3Service.transfers.find(
(item) => Number(item.chainConfig.id) === sourceChainId
)?.chainConfig;
const dstChainConfig = this.lnv3Service.transfers.find(
(item) => Number(item.chainConfig.id) === targetChainId
)?.chainConfig;

const srcToken = srcChainConfig?.tokens.find(
(token) => token.address.toLowerCase() === sourceToken.toLowerCase()
);
const sourceSymbol = lnv3SourceBridge?.symbols.find(
(item) => item.address.toLowerCase() === sourceToken.toLowerCase()
const couple = srcChainConfig?.couples.find(
(couple) =>
Number(couple.chain.id) === targetChainId &&
couple.protocol.name === 'lnv3' &&
couple.symbol.from === srcToken.symbol
);
if (sourceSymbol === undefined) {
const dstTokenSymbol = couple?.symbol.to;
const dstToken = dstChainConfig?.tokens.find((token) => token.symbol === dstTokenSymbol);

const srcDecimals = srcToken?.decimals;
const dstDecimals = dstToken?.decimals;
if (srcDecimals === undefined || dstDecimals === undefined) {
return '0';
}
const lnv3TargetBridge = this.lnv3Service.transfers.find(
(item) => item.chainId === targetChainId
);
const targetSymbol = lnv3TargetBridge?.symbols.find((item) => item.key === sourceSymbol.key);

return transferDecimals(amount, sourceSymbol.decimals - targetSymbol.decimals);
return transferDecimals(amount, srcDecimals - dstDecimals);
}
}

Expand All @@ -229,45 +255,72 @@ export class AggregationService extends PrismaClient implements OnModuleInit {
}): boolean {
const { sourceChainId, targetChainId, sourceToken, targetToken, version } = params;
if (version === 'lnv2') {
const bridge = this.lnv2Service.transfers.find((item) => item.chainId === sourceChainId);
if (bridge === undefined) {
const sourceNode = this.lnv2Service.transfers.find(
(item) => Number(item.chainConfig.id) === sourceChainId
);
if (sourceNode === undefined) {
return false;
}
const tokenBridge = bridge.tokens.find(
(item) => item.fromAddress.toLowerCase() === sourceToken.toLowerCase()
const sourceTokenInfo = sourceNode.chainConfig.tokens.find(
(item) => item.address.toLowerCase() === sourceToken.toLowerCase()
);
if (tokenBridge === undefined) {
if (sourceTokenInfo === undefined) {
return false;
}
const targetInfo = tokenBridge.remoteInfos.find(
const couple = sourceNode.chainConfig.couples.find(
(item) =>
item.toChain === targetChainId &&
item.toAddress.toLowerCase() === targetToken.toLowerCase()
Number(item.chain.id) === targetChainId &&
item.protocol.name.startsWith('lnv2') &&
item.symbol.from === sourceTokenInfo.symbol
);
return targetInfo !== undefined;
} else {
const lnv3SourceBridge = this.lnv3Service.transfers.find(
(item) => item.chainId === sourceChainId
if (couple === undefined) {
return false;
}
const targetNode = this.lnv2Service.transfers.find(
(item) => Number(item.chainConfig.id) === targetChainId
);
if (lnv3SourceBridge === undefined) {
if (targetNode === undefined) {
return false;
}
const sourceSymbol = lnv3SourceBridge.symbols.find(
(item) => item.address.toLowerCase() === sourceToken.toLowerCase()
const targetTokenInfo = targetNode.chainConfig.tokens.find(
(item) => item.address.toLowerCase() === couple.symbol.to.toLowerCase()
);
if (sourceSymbol === undefined) {
return targetTokenInfo.address.toLowerCase() === targetToken.toLowerCase();
} else {
const srcChainConfig = this.lnv3Service.transfers.find(
(item) => Number(item.chainConfig.id) === sourceChainId
)?.chainConfig;
const dstChainConfig = this.lnv3Service.transfers.find(
(item) => Number(item.chainConfig.id) === targetChainId
)?.chainConfig;

if (srcChainConfig === undefined || dstChainConfig === undefined) {
return false;
}
const lnv3TargetBridge = this.lnv3Service.transfers.find(
(item) => item.chainId === targetChainId
const srcToken = srcChainConfig.tokens.find(
(token) => token.address.toLowerCase() === sourceToken.toLowerCase()
);
if (lnv3TargetBridge === undefined) {
const dstToken = dstChainConfig.tokens.find(
(token) => token.address.toLowerCase() === targetToken.toLowerCase()
);
if (srcToken === undefined || dstToken === undefined) {
return false;
}
const targetSymbol = lnv3TargetBridge.symbols.find(
(item) => item.address.toLowerCase() === targetToken.toLowerCase()
const srcCouple = srcChainConfig?.couples.find(
(couple) =>
Number(couple.chain.id) === targetChainId &&
couple.protocol.name === 'lnv3' &&
couple.symbol.from === srcToken.symbol &&
couple.symbol.to === dstToken.symbol
);
const dstCouple = dstChainConfig?.couples.find(
(couple) =>
Number(couple.chain.id) === sourceChainId &&
couple.protocol.name === 'lnv3' &&
couple.symbol.from === dstToken.symbol &&
couple.symbol.to === srcToken.symbol
);
return targetSymbol !== undefined;
return srcCouple !== undefined && dstCouple !== undefined;
}
}

Expand Down
23 changes: 3 additions & 20 deletions apollo/src/base/TransferServiceT2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ConfigService } from '@nestjs/config';
import { AddressToken } from './AddressToken';
import { HelixChainConf } from '@helixbridge/helixconf';

/*
This model is suitable for multi-chain interconnection scenarios,
Expand Down Expand Up @@ -36,28 +37,10 @@ export interface BridgeBaseConfigure {
takeEachTime: number;
}

export interface PartnerSymbol {
key: string;
symbol: string;
address: string;
outerAddress: string;
protocolFee: number;
decimals: number;
}

export interface Channel {
chain: string;
channel: string;
}

export interface PartnerT2 {
chainId: number;
chain: string;
url: string; // record api endpoint
level0Indexer: number;
bridge: string;
symbols: PartnerSymbol[];
channels: Channel[];
indexerUrl: string;
chainConfig: HelixChainConf;
}

export abstract class BaseTransferServiceT2 extends AddressToken {
Expand Down
23 changes: 2 additions & 21 deletions apollo/src/base/TransferServiceT3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConfigService } from '@nestjs/config';
import { HelixChainConf } from '@helixbridge/helixconf';

/*
This model is suitable for multi-chain interconnection scenarios,
Expand Down Expand Up @@ -30,30 +31,10 @@ export interface BridgeBaseConfigure {
takeEachTime: number;
}

export interface RemoteInfo {
toChain: number;
toSymbol: string;
toAddress: string;
protocolFee: number;
decimals: number;
bridgeType: string;
channel: string;
}

export interface Token {
key: string;
fromSymbol: string;
fromAddress: string;
decimals: number;
remoteInfos: RemoteInfo[];
}

export interface PartnerT3 {
chainId: number;
chainName: string;
defaultEndpoint: string;
oppositeEndpoint: string;
tokens: Token[];
chainConfig: HelixChainConf;
}

export abstract class BaseTransferServiceT3 {
Expand Down
Loading
Loading