From b20af4e4bcbfd36bd91b6cf92afeee953a1e56d0 Mon Sep 17 00:00:00 2001 From: xiaoch05 Date: Wed, 24 Apr 2024 10:57:57 +0800 Subject: [PATCH] format --- apollo/src/aggregation/aggregation.module.ts | 2 +- .../src/aggregation/aggregation.resolver.ts | 216 ++++++++++-------- apollo/src/aggregation/aggregation.service.ts | 119 +++++----- apollo/src/lnv2/lnv2.service.ts | 33 +-- apollo/src/lnv2/transfer.service.ts | 4 +- apollo/src/lnv3/lnv3.service.ts | 26 ++- apollo/src/lnv3/transfer.service.ts | 66 +++--- apollo/src/tasks/tasks.service.ts | 6 +- tsconfig.json | 1 + 9 files changed, 257 insertions(+), 216 deletions(-) create mode 100644 tsconfig.json diff --git a/apollo/src/aggregation/aggregation.module.ts b/apollo/src/aggregation/aggregation.module.ts index fd25a613..b3d723e2 100644 --- a/apollo/src/aggregation/aggregation.module.ts +++ b/apollo/src/aggregation/aggregation.module.ts @@ -3,7 +3,7 @@ import { AggregationService } from './aggregation.service'; import { AggregationResolver } from './aggregation.resolver'; import { TasksModule } from '../tasks/tasks.module'; import { TransferService as Lnv2Service } from '../lnv2/transfer.service'; -import { TransferService as Lnv3Service} from '../lnv3/transfer.service'; +import { TransferService as Lnv3Service } from '../lnv3/transfer.service'; @Module({ imports: [TasksModule], diff --git a/apollo/src/aggregation/aggregation.resolver.ts b/apollo/src/aggregation/aggregation.resolver.ts index 8a631e21..08dd0b03 100644 --- a/apollo/src/aggregation/aggregation.resolver.ts +++ b/apollo/src/aggregation/aggregation.resolver.ts @@ -12,9 +12,9 @@ export class AggregationResolver { private readonly signatureExpire = 120; // todo: move this to contract private readonly relayerProxy = { - "0x570fca2c6f902949dbb90664be5680fec94a84f6": "0x000000000bb6a011db294ce3f3423f00eac4959e", - "0xc5a809900b5bfb46b1b3892e419e69331b8fbc6c": "0x000000000bb6a011db294ce3f3423f00eac4959e", - "0x3f63bce51d3c6665bfe919816780a2109d42238d": "0x000000000bb6a011db294ce3f3423f00eac4959e" + '0x570fca2c6f902949dbb90664be5680fec94a84f6': '0x000000000bb6a011db294ce3f3423f00eac4959e', + '0xc5a809900b5bfb46b1b3892e419e69331b8fbc6c': '0x000000000bb6a011db294ce3f3423f00eac4959e', + '0x3f63bce51d3c6665bfe919816780a2109d42238d': '0x000000000bb6a011db294ce3f3423f00eac4959e', }; constructor(private aggregationService: AggregationService) {} @@ -29,17 +29,25 @@ export class AggregationResolver { return ethUtil.bufferToHex(ethUtil.publicToAddress(pubkey)).toLowerCase(); } - private checkMessageSender(timestamp: number, message: string, relayer: string, sig: string): boolean { + private checkMessageSender( + timestamp: number, + message: string, + relayer: string, + sig: string + ): boolean { try { const now = Math.floor(Date.now() / 1000); if (timestamp + this.signatureExpire < now) { - return false; + return false; } - const messageHash = this.web3.utils.soliditySha3({value: `${timestamp}`, type: 'uint256'}, {value: message, type: 'string'}); + const messageHash = this.web3.utils.soliditySha3( + { value: `${timestamp}`, type: 'uint256' }, + { value: message, type: 'string' } + ); const dataHash = this.web3.utils.soliditySha3( - { value: '\x19Ethereum Signed Message:\n32', type: 'string' }, - { value: messageHash, type: 'bytes' } + { value: '\x19Ethereum Signed Message:\n32', type: 'string' }, + { value: messageHash, type: 'bytes' } ); const signer = this.ecrecover(dataHash, sig); return signer === relayer || this.relayerProxy[signer] === relayer; @@ -72,7 +80,7 @@ export class AggregationResolver { @Args('relayer') relayer: string, @Args('token') token: string, @Args('order') order: string, - @Args('notsubmited') notsubmited: boolean, + @Args('notsubmited') notsubmited: boolean ) { const orderCondition = order?.split('_'); const orderBy = @@ -80,7 +88,7 @@ export class AggregationResolver { ? { [orderCondition[0]]: orderCondition[1] } : { startTime: Prisma.SortOrder.desc }; const resultCondition = results && results.length ? { result: { in: results } } : {}; - const submitCondition = notsubmited ? { confirmedBlocks: { not: { contains: '0x' } } } : {}; + const submitCondition = notsubmited ? { confirmedBlocks: { not: { contains: '0x' } } } : {}; return this.aggregationService.queryHistoryRecordFirst( { @@ -124,7 +132,9 @@ export class AggregationResolver { !Object.values(item).some((value) => isUndefined(value) || isNull(value) || value === ''); const accFilters = [{ sender: sender?.toLowerCase() }, { recipient }].filter(isValid); - const relayerFilters = [{ relayer: relayer?.toLowerCase() }, { needWithdrawLiquidity }].filter(isValid); + const relayerFilters = [{ relayer: relayer?.toLowerCase() }, { needWithdrawLiquidity }].filter( + isValid + ); const accountCondition = accFilters.length ? { OR: accFilters } : {}; const relayerCondition = relayerFilters.length ? { AND: relayerFilters } : {}; const resultCondition = results && results.length ? { result: { in: results } } : {}; @@ -133,7 +143,9 @@ export class AggregationResolver { const toChainCondition = toChains && toChains.length ? { toChain: { in: toChains } } : {}; const bridgeCondition = bridges && bridges.length ? { bridge: { in: bridges } } : {}; const recvTokenCondition = - recvTokenAddress && recvTokenAddress.length ? { recvTokenAddress: recvTokenAddress?.toLowerCase() } : {}; + recvTokenAddress && recvTokenAddress.length + ? { recvTokenAddress: recvTokenAddress?.toLowerCase() } + : {}; const chainConditions = { AND: { ...resultCondition, @@ -161,13 +173,10 @@ export class AggregationResolver { } /** - * @deprecated instead, please use signConfirmedBlock - **/ + * @deprecated instead, please use signConfirmedBlock + **/ @Mutation() - async updateConfirmedBlock( - @Args('id') id: string, - @Args('block') block: string - ) { + async updateConfirmedBlock(@Args('id') id: string, @Args('block') block: string) { await this.aggregationService.updateConfirmedBlock({ where: { id: id }, block: block, @@ -175,8 +184,8 @@ export class AggregationResolver { } /** - * @deprecated instead, please use signHeartBeat - **/ + * @deprecated instead, please use signHeartBeat + **/ @Mutation() async lnBridgeHeartBeat( @Args('fromChainId') fromChainId: string, @@ -184,15 +193,15 @@ export class AggregationResolver { @Args('version') version: string, @Args('relayer') relayer: string, @Args('tokenAddress') tokenAddress: string, - @Args('softTransferLimit') softTransferLimit: string, + @Args('softTransferLimit') softTransferLimit: string ) { const id = `${version}-${fromChainId}-${toChainId}-${relayer.toLowerCase()}-${tokenAddress.toLowerCase()}`; const now = Math.floor(Date.now() / 1000); - let updateData = { + const updateData = { heartbeatTimestamp: now, }; - + if (softTransferLimit !== undefined && softTransferLimit !== '0') { // the softTransferLimit is on target chain, transfer it to source chain const transferLimit = this.aggregationService.targetAmountToSourceAmount({ @@ -200,7 +209,7 @@ export class AggregationResolver { sourceChainId: Number(fromChainId), targetChainId: Number(toChainId), sourceToken: tokenAddress, - version + version, }); updateData['softTransferLimit'] = transferLimit; } @@ -218,13 +227,18 @@ export class AggregationResolver { @Mutation() async signConfirmedBlock( - @Args('id') id: string, - @Args('relayer') relayer: string, - @Args('block') block: string, - @Args('timestamp') timestamp: number, - @Args('signature') signature: string + @Args('id') id: string, + @Args('relayer') relayer: string, + @Args('block') block: string, + @Args('timestamp') timestamp: number, + @Args('signature') signature: string ) { - const allowSetConfirmed = this.checkMessageSender(timestamp, block, relayer?.toLowerCase(), signature); + const allowSetConfirmed = this.checkMessageSender( + timestamp, + block, + relayer?.toLowerCase(), + signature + ); if (!allowSetConfirmed) { return; } @@ -248,15 +262,20 @@ export class AggregationResolver { const id = `${version}-${fromChainId}-${toChainId}-${relayer?.toLowerCase()}-${tokenAddress?.toLowerCase()}`; const now = Math.floor(Date.now() / 1000); - const allowHeartBeat = this.checkMessageSender(timestamp, softTransferLimit, relayer?.toLowerCase(), signature); + const allowHeartBeat = this.checkMessageSender( + timestamp, + softTransferLimit, + relayer?.toLowerCase(), + signature + ); if (!allowHeartBeat) { - return + return; } - let updateData = { + const updateData = { heartbeatTimestamp: now, }; - + if (softTransferLimit !== undefined && softTransferLimit !== '0') { // the softTransferLimit is on target chain, transfer it to source chain const transferLimit = this.aggregationService.targetAmountToSourceAmount({ @@ -264,7 +283,7 @@ export class AggregationResolver { sourceChainId: Number(fromChainId), targetChainId: Number(toChainId), sourceToken: tokenAddress, - version + version, }); updateData['softTransferLimit'] = transferLimit; } @@ -295,9 +314,14 @@ export class AggregationResolver { ) { const id = `${version}-${fromChainId}-${toChainId}-${relayer?.toLowerCase()}-${tokenAddress?.toLowerCase()}`; const message = `${dynamicFee}:${dynamicFeeExpire}:${dynamicFeeSignature}`; - const allowSetDynamicFee = this.checkMessageSender(timestamp, message, relayer?.toLowerCase(), signature); + const allowSetDynamicFee = this.checkMessageSender( + timestamp, + message, + relayer?.toLowerCase(), + signature + ); if (!allowSetDynamicFee) { - return + return; } try { @@ -306,7 +330,7 @@ export class AggregationResolver { data: { dynamicFee, dynamicFeeExpire, - dynamicFeeSignature + dynamicFeeSignature, }, }); } catch (e) { @@ -332,19 +356,17 @@ export class AggregationResolver { } @Query() - tasksHealthCheck( - @Args('name') name: string - ) { - const healthChecks = this.aggregationService.tasksHealthCheck(); - if (name !== null) { - return [ - { - name: name, - callTimes: healthChecks[name] - } - ]; - } - return Array.from(healthChecks, ([name, callTimes]) => ({ name, callTimes })); + tasksHealthCheck(@Args('name') name: string) { + const healthChecks = this.aggregationService.tasksHealthCheck(); + if (name !== null) { + return [ + { + name: name, + callTimes: healthChecks[name], + }, + ]; + } + return Array.from(healthChecks, ([name, callTimes]) => ({ name, callTimes })); } @Query() @@ -398,13 +420,11 @@ export class AggregationResolver { } @Query() - async queryLnBridgeSupportChains( - @Args('tokenKey') tokenKey: string - ) { - const baseFilters = { - tokenKey, - paused: false, - OR: [{transferLimit: { not: '0' }}, {margin: { not: '0' }}] + async queryLnBridgeSupportChains(@Args('tokenKey') tokenKey: string) { + const baseFilters = { + tokenKey, + paused: false, + OR: [{ transferLimit: { not: '0' } }, { margin: { not: '0' } }], }; const where = { @@ -414,26 +434,26 @@ export class AggregationResolver { const records = await this.aggregationService.queryLnBridgeRelayInfos({ where, }); - let supportChains = new Map(); + const supportChains = new Map(); const now = Math.floor(Date.now() / 1000); for (const record of records.records) { - if (record.heartbeatTimestamp + this.heartbeatTimeout < now) { - continue; - } + if (record.heartbeatTimestamp + this.heartbeatTimeout < now) { + continue; + } - let toChains = supportChains.get(record.fromChain); + const toChains = supportChains.get(record.fromChain); - if (!toChains) { - supportChains.set(record.fromChain, [ record.toChain ]); - } else { - if (!toChains.includes(record.toChain)) { - toChains.push(record.toChain); - } + if (!toChains) { + supportChains.set(record.fromChain, [record.toChain]); + } else { + if (!toChains.includes(record.toChain)) { + toChains.push(record.toChain); } + } } return Array.from(supportChains, ([fromChain, toChains]) => ({ - fromChain, - toChains, + fromChain, + toChains, })); } @@ -466,19 +486,19 @@ export class AggregationResolver { // w=P * 0.5 + max(R - S*0.001, 0) * 0.1 + max(1-T_0 * 0.001, 0)*0.1 + T_1 * 0.2 //const validRecords = records.records.filter((record) => BigInt(record.margin) > BigInt(amount)); // query all pending txs - var sortedRelayers = []; - var transferLimit = BigInt(0); + const sortedRelayers = []; + let transferLimit = BigInt(0); const now = Math.floor(Date.now() / 1000); for (const record of records.records) { let limit = record.version == 'lnv2' ? BigInt(record.margin) : BigInt(record.transferLimit); try { - const softTransferLimit = BigInt(record.softTransferLimit); - if (limit > softTransferLimit && softTransferLimit > 0) { - limit = softTransferLimit; - } - } catch(e) { - console.log(`get softTransferLimit failed ${record.id}, exception: ${e}`); - continue; + const softTransferLimit = BigInt(record.softTransferLimit); + if (limit > softTransferLimit && softTransferLimit > 0) { + limit = softTransferLimit; + } + } catch (e) { + console.log(`get softTransferLimit failed ${record.id}, exception: ${e}`); + continue; } // offline if (record.heartbeatTimestamp + this.heartbeatTimeout < now) { @@ -488,7 +508,9 @@ export class AggregationResolver { if (limit > transferLimit) { transferLimit = limit; } - const providerFee = BigInt(amount) * BigInt(record.liquidityFeeRate) / BigInt(100000) + BigInt(record.baseFee); + const providerFee = + (BigInt(amount) * BigInt(record.liquidityFeeRate)) / BigInt(100000) + + BigInt(record.baseFee); if (limit < BigInt(amount) + providerFee + BigInt(record.protocolFee) || record.paused) { continue; } @@ -524,7 +546,7 @@ export class AggregationResolver { @Args('toChain') toChain: string, @Args('bridge') bridge: string, @Args('token') token: string, - @Args('balance') balance: string, + @Args('balance') balance: string ) { const sendToken = token?.toLowerCase(); const baseFilters = { fromChain, toChain, sendToken, bridge }; @@ -537,7 +559,7 @@ export class AggregationResolver { skip: 0, where, }); - var maxTransferAmount = BigInt(0); + let maxTransferAmount = BigInt(0); const now = Math.floor(Date.now() / 1000); const liquidityFeeScale = BigInt(100000); for (const record of records.records) { @@ -547,24 +569,28 @@ export class AggregationResolver { } const fixFee = BigInt(record.baseFee) + BigInt(record.protocolFee); - const userBalanceRestrict = (BigInt(balance) - fixFee) * liquidityFeeScale / (liquidityFeeScale + BigInt(record.liquidityFeeRate)); - let limitRestrict = record.version === 'lnv2' ? - (BigInt(record.margin) - fixFee) * liquidityFeeScale / (liquidityFeeScale + BigInt(record.liquidityFeeRate)) : - BigInt(record.transferLimit); + const userBalanceRestrict = + ((BigInt(balance) - fixFee) * liquidityFeeScale) / + (liquidityFeeScale + BigInt(record.liquidityFeeRate)); + let limitRestrict = + record.version === 'lnv2' + ? ((BigInt(record.margin) - fixFee) * liquidityFeeScale) / + (liquidityFeeScale + BigInt(record.liquidityFeeRate)) + : BigInt(record.transferLimit); try { - const softTransferLimit = BigInt(record.softTransferLimit); - if (limitRestrict > softTransferLimit && softTransferLimit > 0) { - limitRestrict = softTransferLimit; - } - } catch(e) { - console.log(`get softTransferLimit failed ${record.id}, exception: ${e}`); - continue; + const softTransferLimit = BigInt(record.softTransferLimit); + if (limitRestrict > softTransferLimit && softTransferLimit > 0) { + limitRestrict = softTransferLimit; + } + } catch (e) { + console.log(`get softTransferLimit failed ${record.id}, exception: ${e}`); + continue; } const limit = limitRestrict < userBalanceRestrict ? limitRestrict : userBalanceRestrict; if (maxTransferAmount < limit) { - maxTransferAmount = limit; + maxTransferAmount = limit; } } return maxTransferAmount; diff --git a/apollo/src/aggregation/aggregation.service.ts b/apollo/src/aggregation/aggregation.service.ts index 3959535a..ced3cf0e 100644 --- a/apollo/src/aggregation/aggregation.service.ts +++ b/apollo/src/aggregation/aggregation.service.ts @@ -2,9 +2,8 @@ import { INestApplication, Injectable, Logger, OnModuleInit } from '@nestjs/comm import { HistoryRecord, Prisma, PrismaClient } from '@prisma/client'; import { HistoryRecords, LnBridgeRelayInfo, LnBridgeRelayInfos } from '../graphql'; // export lnbridge service configure -import { last } from 'lodash'; import { TransferService as Lnv2Service } from '../lnv2/transfer.service'; -import { TransferService as Lnv3Service} from '../lnv3/transfer.service'; +import { TransferService as Lnv3Service } from '../lnv3/transfer.service'; import { TasksService } from '../tasks/tasks.service'; @Injectable() @@ -16,9 +15,9 @@ export class AggregationService extends PrismaClient implements OnModuleInit { } constructor( - private lnv2Service: Lnv2Service, - private lnv3Service: Lnv3Service, - private tasksService: TasksService + private lnv2Service: Lnv2Service, + private lnv3Service: Lnv3Service, + private tasksService: TasksService ) { super(); } @@ -166,59 +165,59 @@ export class AggregationService extends PrismaClient implements OnModuleInit { } tasksHealthCheck() { - return this.tasksService.queryHealthChecks(); + return this.tasksService.queryHealthChecks(); } targetAmountToSourceAmount(params: { - amount: string; - sourceChainId: number; - targetChainId: number; - sourceToken: string; - version: string; + amount: string; + sourceChainId: number; + targetChainId: number; + sourceToken: string; + version: string; }): string { - const { amount, sourceChainId, targetChainId, sourceToken, version } = params; - let transferDecimals = (value: string, decimals: number) => { - if (decimals > 0) { - return value.padEnd(value.length + decimals, '0'); - } else if (value.length + decimals > 0) { - return value.substr(0, value.length + decimals); - } else { - return '0'; - } + const { amount, sourceChainId, targetChainId, sourceToken, version } = params; + const transferDecimals = (value: string, decimals: number) => { + if (decimals > 0) { + return value.padEnd(value.length + decimals, '0'); + } else if (value.length + decimals > 0) { + return value.substr(0, value.length + decimals); + } else { + return '0'; } + }; - if (version === 'lnv2') { - const sourceNode = this.lnv2Service.transfers.find((item) => item.chainId === sourceChainId); - const sourceTokenInfo = sourceNode?.tokens.find( - (item) => item.fromAddress.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 - ); - if (targetTokenInfo === undefined) { - return '0'; - } - - return transferDecimals(amount, sourceTokenInfo.decimals - targetTokenInfo.decimals); - } else { - const lnv3SourceBridge = this.lnv3Service.transfers.find((item) => item.chainId === sourceChainId); - const sourceSymbol = lnv3SourceBridge?.symbols.find( - (item) => item.address.toLowerCase() === sourceToken.toLowerCase() - ); - if (sourceSymbol === undefined) { - return '0'; - } - const lnv3TargetBridge = this.lnv3Service.transfers.find((item) => item.chainId === targetChainId); - const targetSymbol = lnv3TargetBridge?.symbols.find( - (item) => item.key === sourceSymbol.key - ); + if (version === 'lnv2') { + const sourceNode = this.lnv2Service.transfers.find((item) => item.chainId === sourceChainId); + const sourceTokenInfo = sourceNode?.tokens.find( + (item) => item.fromAddress.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); + if (targetTokenInfo === undefined) { + return '0'; + } - return transferDecimals(amount, sourceSymbol.decimals - targetSymbol.decimals); + return transferDecimals(amount, sourceTokenInfo.decimals - targetTokenInfo.decimals); + } else { + const lnv3SourceBridge = this.lnv3Service.transfers.find( + (item) => item.chainId === sourceChainId + ); + const sourceSymbol = lnv3SourceBridge?.symbols.find( + (item) => item.address.toLowerCase() === sourceToken.toLowerCase() + ); + if (sourceSymbol === 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); + } } checkLnBridgeConfigure(params: { @@ -242,26 +241,31 @@ export class AggregationService extends PrismaClient implements OnModuleInit { } const targetInfo = tokenBridge.remoteInfos.find( (item) => - item.toChain === targetChainId && item.toAddress.toLowerCase() === targetToken.toLowerCase() + item.toChain === targetChainId && + item.toAddress.toLowerCase() === targetToken.toLowerCase() ); return targetInfo !== undefined; } else { - const lnv3SourceBridge = this.lnv3Service.transfers.find((item) => item.chainId === sourceChainId); + const lnv3SourceBridge = this.lnv3Service.transfers.find( + (item) => item.chainId === sourceChainId + ); if (lnv3SourceBridge === undefined) { return false; } const sourceSymbol = lnv3SourceBridge.symbols.find( - (item) => item.address.toLowerCase() === sourceToken.toLowerCase() + (item) => item.address.toLowerCase() === sourceToken.toLowerCase() ); if (sourceSymbol === undefined) { return false; } - const lnv3TargetBridge = this.lnv3Service.transfers.find((item) => item.chainId === targetChainId); + const lnv3TargetBridge = this.lnv3Service.transfers.find( + (item) => item.chainId === targetChainId + ); if (lnv3TargetBridge === undefined) { return false; } const targetSymbol = lnv3TargetBridge.symbols.find( - (item) => item.address.toLowerCase() === targetToken.toLowerCase() + (item) => item.address.toLowerCase() === targetToken.toLowerCase() ); return targetSymbol !== undefined; } @@ -287,7 +291,7 @@ export class AggregationService extends PrismaClient implements OnModuleInit { }); if (relayerInfo.version == 'lnv2') { - var marginUsed = BigInt(0); + let marginUsed = BigInt(0); for (const pendingRecord of pendingRecords.records) { marginUsed += BigInt(pendingRecord.sendAmount); } @@ -300,7 +304,8 @@ export class AggregationService extends PrismaClient implements OnModuleInit { } } const F = - BigInt(relayerInfo.baseFee) + BigInt(relayerInfo.protocolFee) + + BigInt(relayerInfo.baseFee) + + BigInt(relayerInfo.protocolFee) + (BigInt(relayerInfo.liquidityFeeRate) * amount) / BigInt(100000); const R = relayerInfo.slashCount; const w = 1 + R * 0.1; diff --git a/apollo/src/lnv2/lnv2.service.ts b/apollo/src/lnv2/lnv2.service.ts index 33c4933b..2862c5fa 100644 --- a/apollo/src/lnv2/lnv2.service.ts +++ b/apollo/src/lnv2/lnv2.service.ts @@ -280,7 +280,7 @@ export class Lnv2Service implements OnModuleInit { .catch((err) => { this.logger.warn(`query transfer records failed err ${err}`); }); - var ignored = 0; + let ignored = 0; if (records && records.length > 0) { for (const record of records) { const tokenPair = this.findTokenPair( @@ -506,13 +506,13 @@ export class Lnv2Service implements OnModuleInit { } transferDecimals(value: string, decimals: number): string { - if (decimals > 0) { - return value.padEnd(value.length + decimals, '0'); - } else if (value.length + decimals > 0) { - return value.substr(0, value.length + decimals); - } else { - return '0'; - } + if (decimals > 0) { + return value.padEnd(value.length + decimals, '0'); + } else if (value.length + decimals > 0) { + return value.substr(0, value.length + decimals); + } else { + return '0'; + } } async fetchMarginInfoFromTarget(transfer: PartnerT3, indexInfo: BridgeIndexInfo) { @@ -551,7 +551,9 @@ export class Lnv2Service implements OnModuleInit { ); const sourcePartner = this.findPartnerByChainId(record.remoteChainId); if (sourcePartner === null) { - this.logger.warn(`can't find partner chain source:${transfer.chainId} remote:${record.remoteChainId}`); + this.logger.warn( + `can't find partner chain source:${transfer.chainId} remote:${record.remoteChainId}` + ); latestNonce += 1; this.fetchCache[index].latestRelayerInfoTargetNonce = latestNonce; return; @@ -571,7 +573,10 @@ export class Lnv2Service implements OnModuleInit { const relayerInfo = await this.aggregationService.queryLnBridgeRelayInfoById({ id: id, }); - const sourceMargin = this.transferDecimals(record.margin, tokenPair.fromDecimals - tokenPair.toDecimals); + const sourceMargin = this.transferDecimals( + record.margin, + tokenPair.fromDecimals - tokenPair.toDecimals + ); if (relayerInfo) { // transfer target margin to source margin const updateData = { @@ -660,8 +665,8 @@ export class Lnv2Service implements OnModuleInit { }); if (records === undefined) { - this.logger.warn(`query record failed, url: ${indexInfo.url}, query: ${query}`); - return; + this.logger.warn(`query record failed, url: ${indexInfo.url}, query: ${query}`); + return; } // query nonce big then latestNonce for (const record of records) { @@ -772,8 +777,8 @@ export class Lnv2Service implements OnModuleInit { }); if (records === undefined) { - this.logger.warn(`query record failed, url: ${indexInfo.url}, query: ${query}`); - return; + this.logger.warn(`query record failed, url: ${indexInfo.url}, query: ${query}`); + return; } // query nonce big then latestNonce for (const record of records) { diff --git a/apollo/src/lnv2/transfer.service.ts b/apollo/src/lnv2/transfer.service.ts index 4e7fbb28..4aa042c8 100644 --- a/apollo/src/lnv2/transfer.service.ts +++ b/apollo/src/lnv2/transfer.service.ts @@ -761,7 +761,7 @@ export class TransferService extends BaseTransferServiceT3 { decimals: 6, bridgeType: 'default', channel: 'layerzero', - } + }, ], }, { @@ -903,7 +903,7 @@ export class TransferService extends BaseTransferServiceT3 { ], }, ], - } + }, ]; readonly isTest = this.configService.get('CHAIN_TYPE') === 'test'; diff --git a/apollo/src/lnv3/lnv3.service.ts b/apollo/src/lnv3/lnv3.service.ts index 430adfc3..9e26ab87 100644 --- a/apollo/src/lnv3/lnv3.service.ts +++ b/apollo/src/lnv3/lnv3.service.ts @@ -195,7 +195,7 @@ export class Lnv3Service implements OnModuleInit { if (relayRecord) { let needWithdrawLiquidity = record.needWithdrawLiquidity; - let requestWithdrawTimestamp = Number(relayRecord.requestWithdrawTimestamp); + const requestWithdrawTimestamp = Number(relayRecord.requestWithdrawTimestamp); let endTxHash = record.endTxHash; if (record.result !== RecordStatus.success) { const providerId = this.genRelayInfoID( @@ -235,10 +235,10 @@ export class Lnv3Service implements OnModuleInit { where: { id: record.id }, data: updateData, }); - + const cost = relayRecord.slashed ? 0 : relayRecord.fee; const profit = relayRecord.slashed ? 0 : record.fee; - + await this.aggregationService.updateLnBridgeRelayInfo({ where: { id: providerId }, data: { @@ -256,12 +256,16 @@ export class Lnv3Service implements OnModuleInit { // query result on source const query = `query { lnv3TransferRecord(id: "${transferId}") { id, hasWithdrawn }}`; const transferRecord = await axios - .post(transfer.url, { - query: query, - variables: null, - }) - .then((res) => res.data?.data?.lnv3TransferRecord); - if (transferRecord && (transferRecord.hasWithdrawn || record.lastRequestWithdraw < requestWithdrawTimestamp)) { + .post(transfer.url, { + query: query, + variables: null, + }) + .then((res) => res.data?.data?.lnv3TransferRecord); + if ( + transferRecord && + (transferRecord.hasWithdrawn || + record.lastRequestWithdraw < requestWithdrawTimestamp) + ) { await this.aggregationService.updateHistoryRecord({ where: { id: record.id }, data: { @@ -324,8 +328,8 @@ export class Lnv3Service implements OnModuleInit { // maybe this query is archived and can't access if (records === undefined) { - this.logger.warn(`query record failed, url: ${transfer.url}, query: ${query}`); - return; + this.logger.warn(`query record failed, url: ${transfer.url}, query: ${query}`); + return; } // query nonce big then latestNonce diff --git a/apollo/src/lnv3/transfer.service.ts b/apollo/src/lnv3/transfer.service.ts index 2258f3d1..1ce2c489 100644 --- a/apollo/src/lnv3/transfer.service.ts +++ b/apollo/src/lnv3/transfer.service.ts @@ -44,7 +44,7 @@ export class TransferService extends BaseTransferServiceT2 { outerAddress: '0xc2132D05D31c914a87C6611C10748AEb04B58e8F', protocolFee: 100000, decimals: 6, - } + }, ], channels: [ { @@ -79,7 +79,7 @@ export class TransferService extends BaseTransferServiceT2 { chain: 'darwinia-dvm', channel: 'msgline', }, - ] + ], }, { chainId: 42161, @@ -110,7 +110,7 @@ export class TransferService extends BaseTransferServiceT2 { outerAddress: '0x0000000000000000000000000000000000000000', protocolFee: 10000000000000, decimals: 18, - } + }, ], channels: [ { @@ -152,8 +152,8 @@ export class TransferService extends BaseTransferServiceT2 { { chain: 'astar-zkevm', channel: 'layerzero', - } - ] + }, + ], }, { chainId: 56, @@ -168,7 +168,7 @@ export class TransferService extends BaseTransferServiceT2 { outerAddress: '0x55d398326f99059fF775485246999027B3197955', protocolFee: 100000000000000000, decimals: 18, - } + }, ], channels: [ { @@ -198,8 +198,8 @@ export class TransferService extends BaseTransferServiceT2 { { chain: 'scroll', channel: 'layerzero', - } - ] + }, + ], }, { chainId: 59144, @@ -222,7 +222,7 @@ export class TransferService extends BaseTransferServiceT2 { outerAddress: '0x0000000000000000000000000000000000000000', protocolFee: 10000000000000, decimals: 18, - } + }, ], channels: [ { @@ -252,8 +252,8 @@ export class TransferService extends BaseTransferServiceT2 { { chain: 'scroll', channel: 'layerzero', - } - ] + }, + ], }, { chainId: 10, @@ -268,7 +268,7 @@ export class TransferService extends BaseTransferServiceT2 { outerAddress: '0x94b008aA00579c1307B0EF2c499aD98a8ce58e58', protocolFee: 100000, decimals: 6, - } + }, ], channels: [ { @@ -298,8 +298,8 @@ export class TransferService extends BaseTransferServiceT2 { { chain: 'scroll', channel: 'layerzero', - } - ] + }, + ], }, { chainId: 100, @@ -314,7 +314,7 @@ export class TransferService extends BaseTransferServiceT2 { outerAddress: '0x4ECaBa5870353805a9F068101A40E0f32ed605C6', protocolFee: 100000, decimals: 6, - } + }, ], channels: [ { @@ -344,8 +344,8 @@ export class TransferService extends BaseTransferServiceT2 { { chain: 'scroll', channel: 'layerzero', - } - ] + }, + ], }, { chainId: 5000, @@ -360,7 +360,7 @@ export class TransferService extends BaseTransferServiceT2 { outerAddress: '0x201EBa5CC46D216Ce6DC03F6a759e8E766e956aE', protocolFee: 100000, decimals: 6, - } + }, ], channels: [ { @@ -390,8 +390,8 @@ export class TransferService extends BaseTransferServiceT2 { { chain: 'scroll', channel: 'layerzero', - } - ] + }, + ], }, { chainId: 534352, @@ -406,7 +406,7 @@ export class TransferService extends BaseTransferServiceT2 { outerAddress: '0xf55BEC9cafDbE8730f096Aa55dad6D22d44099Df', protocolFee: 100000, decimals: 6, - } + }, ], channels: [ { @@ -436,8 +436,8 @@ export class TransferService extends BaseTransferServiceT2 { { chain: 'mantle', channel: 'layerzero', - } - ] + }, + ], }, { chainId: 46, @@ -452,7 +452,7 @@ export class TransferService extends BaseTransferServiceT2 { outerAddress: '0x0000000000000000000000000000000000000000', protocolFee: 30000000000000000000, decimals: 18, - } + }, ], channels: [ { @@ -462,8 +462,8 @@ export class TransferService extends BaseTransferServiceT2 { { chain: 'polygon', channel: 'msgline', - } - ] + }, + ], }, { chainId: 81457, @@ -478,14 +478,14 @@ export class TransferService extends BaseTransferServiceT2 { outerAddress: '0x0000000000000000000000000000000000000000', protocolFee: 10000000000000, decimals: 18, - } + }, ], channels: [ { chain: 'arbitrum', channel: 'layerzero', - } - ] + }, + ], }, { chainId: 3776, @@ -500,15 +500,15 @@ export class TransferService extends BaseTransferServiceT2 { outerAddress: '0x0000000000000000000000000000000000000000', protocolFee: 10000000000000, decimals: 18, - } + }, ], channels: [ { chain: 'arbitrum', channel: 'layerzero', - } - ] - } + }, + ], + }, ]; testChainTransfers: PartnerT2[] = [ diff --git a/apollo/src/tasks/tasks.service.ts b/apollo/src/tasks/tasks.service.ts index 8b90ea80..fd67b096 100644 --- a/apollo/src/tasks/tasks.service.ts +++ b/apollo/src/tasks/tasks.service.ts @@ -13,9 +13,9 @@ export class TasksService { this.logger.log(`new interval task added name:${name}, ms: ${milliseconds}`); this.healthChecks.set(name, 0); const interval = setInterval(async () => { - var callTimes: number = this.healthChecks.get(name); - await callback(); - this.healthChecks.set(name, callTimes + 1); + const callTimes: number = this.healthChecks.get(name); + await callback(); + this.healthChecks.set(name, callTimes + 1); }, milliseconds); this.schedulerRegistry.addInterval(name, interval); } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1 @@ +{}