diff --git a/apollo/src/base/TransferServiceT2.ts b/apollo/src/base/TransferServiceT2.ts index 6c78e3b6..7b3a9cd4 100644 --- a/apollo/src/base/TransferServiceT2.ts +++ b/apollo/src/base/TransferServiceT2.ts @@ -35,6 +35,7 @@ export interface PartnerSymbol { symbol: string; address: string; protocolFee: number; + decimals: number; } export interface PartnerT2 { diff --git a/apollo/src/lnbridgev20/lnbridgev20.service.ts b/apollo/src/lnbridgev20/lnbridgev20.service.ts index e29c07ef..51074503 100644 --- a/apollo/src/lnbridgev20/lnbridgev20.service.ts +++ b/apollo/src/lnbridgev20/lnbridgev20.service.ts @@ -160,7 +160,7 @@ export class Lnbridgev20Service implements OnModuleInit { id: firstPendingRecord.id }); this.fetchCache[index].confirmedNonce = this.formatSortedMessageNonce(Number(firstPendingRecord.nonce)); - const toPartner = this.findPartnerByChainId(record.remoteChainId); + const toPartner = this.findPartnerByChainId(record.remoteChainId, transfer.bridge); // add correct tx await this.aggregationService.createHistoryRecord({ @@ -235,7 +235,7 @@ export class Lnbridgev20Service implements OnModuleInit { } const fromToken = fromSymbol.symbol; - const toPartner = this.findPartnerByChainId(record.remoteChainId); + const toPartner = this.findPartnerByChainId(record.remoteChainId, transfer.bridge); const toSymbol = toPartner.symbols.find((item) => item.address.toLowerCase() === record.targetToken) ?? null; if (!toSymbol) { this.logger.warn(`cannot find to symbol, toChain: ${toPartner.chain} address ${record.targetToken}`); @@ -366,8 +366,8 @@ export class Lnbridgev20Service implements OnModuleInit { return `lnv20-${fromChainId}-${toChainId}-${provider}-${sourceToken}`; } - private findPartnerByChainId(chainId: number) { - return this.transferService.transfers.find((item) => item.chainId === chainId) ?? null; + private findPartnerByChainId(chainId: number, bridge: string) { + return this.transferService.transfers.find((item) => item.chainId === chainId && item.bridge === bridge) ?? null; } private findPartnerByChainName(chainName: string, bridge: string) { @@ -375,7 +375,7 @@ export class Lnbridgev20Service implements OnModuleInit { } async fetchMarginInfoFromTarget(transfer: PartnerT2, index: number) { - const { chain: toChain } = transfer; + const { chain: toChain, symbols } = transfer; let latestNonce = this.fetchCache[index].latestRelayerInfoTargetNonce; try { if (latestNonce == -1) { @@ -407,8 +407,22 @@ export class Lnbridgev20Service implements OnModuleInit { id: id, }); if (relayerInfo) { + // transfer target margin to source margin + const toSymbol = symbols.find((item) => item.address.toLowerCase() === record.targetToken) ?? null; + if (!toSymbol) { + this.logger.warn(`to symbol not find ${record.targetToken}`); + return; + } + const sourcePartner = this.findPartnerByChainId(record.remoteChainId, transfer.bridge); + const fromSymbol = sourcePartner.symbols.find((item) => item.address.toLowerCase() === record.sourceToken) ?? null; + if (!fromSymbol) { + this.logger.warn(`to symbol not find ${record.sourceToken}`); + return; + } + + const sourceMargin = Number(record.margin) * Math.pow(10, fromSymbol.decimals - toSymbol.decimals); const updateData = { - margin: record.margin, + margin: BigInt(sourceMargin).toString(), slashCount: relayerInfo.slashCount, withdrawNonce: relayerInfo.withdrawNonce, targetNonce: latestNonce + 1, @@ -474,7 +488,7 @@ export class Lnbridgev20Service implements OnModuleInit { if (symbol == null) { return; } - const toPartner = this.findPartnerByChainId(record.remoteChainId); + const toPartner = this.findPartnerByChainId(record.remoteChainId, transfer.bridge); if (!relayerInfo) { // if not exist create await this.aggregationService.createLnv20RelayInfo({ @@ -558,7 +572,7 @@ export class Lnbridgev20Service implements OnModuleInit { if (symbol == null) { return; } - const toPartner = this.findPartnerByChainId(record.remoteChainId); + const toPartner = this.findPartnerByChainId(record.remoteChainId, transfer.bridge); if (!relayerInfo) { // if not exist create const margin = record.margin === null ? '0' : record.margin; diff --git a/apollo/src/lnbridgev20/transfer.service.ts b/apollo/src/lnbridgev20/transfer.service.ts index 2602883c..c418622e 100644 --- a/apollo/src/lnbridgev20/transfer.service.ts +++ b/apollo/src/lnbridgev20/transfer.service.ts @@ -43,11 +43,13 @@ export class TransferService extends BaseTransferServiceT2 { symbol: 'USDC', address: '0x1a70127284b774ff4a4dbfe0115114642f0eca65', protocolFee: 100000000, + decimals: 6, }, { symbol: 'USDT', address: '0x2303e4d55BF16a897Cb5Ab71c6225399509d9314', protocolFee: 100000000000000000000, + decimals: 18, }, ], }, @@ -61,11 +63,13 @@ export class TransferService extends BaseTransferServiceT2 { symbol: 'USDC', address: '0x1a70127284B774fF4A4dbfe0115114642f0eca65', protocolFee: 100000000, + decimals: 6, }, { symbol: 'USDT', address: '0x2303e4d55BF16a897Cb5Ab71c6225399509d9314', protocolFee: 100000000000000000000, + decimals: 18, }, ], }, @@ -79,11 +83,13 @@ export class TransferService extends BaseTransferServiceT2 { symbol: 'USDC', address: '0x39de82e1d9b8f62e11022fc3fc127a82f93fe47e', protocolFee: 100000000, + decimals: 6, }, { symbol: 'USDT', address: '0x6d828718c1097a4c573bc25c638cc05bf10dfeaf', protocolFee: 100000000000000000000, + decimals: 18, }, ], }, @@ -97,11 +103,13 @@ export class TransferService extends BaseTransferServiceT2 { symbol: 'USDC', address: '0x39de82e1d9b8f62e11022fc3fc127a82f93fe47e', protocolFee: 100000000, + decimals: 6, }, { symbol: 'USDT', address: '0x6d828718c1097a4c573bc25c638cc05bf10dfeaf', protocolFee: 100000000000000000000, + decimals: 18, }, ], }, @@ -115,11 +123,13 @@ export class TransferService extends BaseTransferServiceT2 { symbol: 'USDC', address: '0x0258eb547bfed540ed17843658c018569fe1e328', protocolFee: 100000000, + decimals: 6, }, { symbol: 'USDT', address: '0x5f8d4232367759bce5d9488d3ade77fcff6b9b6b', protocolFee: 100000000000000000000, + decimals: 18, }, ], }, @@ -133,11 +143,13 @@ export class TransferService extends BaseTransferServiceT2 { symbol: 'USDC', address: '0x0258eb547bfed540ed17843658c018569fe1e328', protocolFee: 100000000, + decimals: 6, }, { symbol: 'USDT', address: '0x5f8d4232367759bce5d9488d3ade77fcff6b9b6b', protocolFee: 100000000000000000000, + decimals: 18, }, ], }, @@ -151,11 +163,13 @@ export class TransferService extends BaseTransferServiceT2 { symbol: 'USDC', address: '0xb5e028f980df5533cb0e8f04530b76637383d993', protocolFee: 100000000, + decimals: 6, }, { symbol: 'USDT', address: '0xbc1a2f123dc9cd2ec8d3ce42ef16c28f3c9ba686', protocolFee: 100000000000000000000, + decimals: 6, }, ], }, @@ -169,11 +183,13 @@ export class TransferService extends BaseTransferServiceT2 { symbol: 'USDC', address: '0xb5e028f980df5533cb0e8f04530b76637383d993', protocolFee: 100000000, + decimals: 6, }, { symbol: 'USDT', address: '0xbc1a2f123dc9cd2ec8d3ce42ef16c28f3c9ba686', protocolFee: 100000000000000000000, + decimals: 6, }, ], },