From 5219fe7bcb76ac9b22d6347a0fc1b4c3dca4961d Mon Sep 17 00:00:00 2001 From: xiaoch05 Date: Thu, 7 Mar 2024 11:32:58 +0800 Subject: [PATCH 1/2] add soft transfer limit --- .../20240307030328_init/migration.sql | 8 +++++++ apollo/prisma/schema.prisma | 1 + .../aggregation/aggregation.history.graphql | 3 ++- .../src/aggregation/aggregation.resolver.ts | 21 +++++++++++++++---- apollo/src/graphql.ts | 3 ++- apollo/src/lnbridgev20/lnbridgev20.service.ts | 3 +++ apollo/src/lnv3/lnv3.service.ts | 1 + 7 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 apollo/prisma/migrations/20240307030328_init/migration.sql diff --git a/apollo/prisma/migrations/20240307030328_init/migration.sql b/apollo/prisma/migrations/20240307030328_init/migration.sql new file mode 100644 index 00000000..46a86b5d --- /dev/null +++ b/apollo/prisma/migrations/20240307030328_init/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - Added the required column `softTransferLimit` to the `LnBridgeRelayInfo` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "LnBridgeRelayInfo" ADD COLUMN "softTransferLimit" TEXT NOT NULL; diff --git a/apollo/prisma/schema.prisma b/apollo/prisma/schema.prisma index cc6ad865..6b5814a2 100644 --- a/apollo/prisma/schema.prisma +++ b/apollo/prisma/schema.prisma @@ -78,6 +78,7 @@ model LnBridgeRelayInfo { heartbeatTimestamp Int messageChannel String transferLimit String + softTransferLimit String paused Boolean } diff --git a/apollo/src/aggregation/aggregation.history.graphql b/apollo/src/aggregation/aggregation.history.graphql index 842d5422..533f8998 100644 --- a/apollo/src/aggregation/aggregation.history.graphql +++ b/apollo/src/aggregation/aggregation.history.graphql @@ -71,6 +71,7 @@ type LnBridgeRelayInfo { heartbeatTimestamp: Int messageChannel: String transferLimit: String + softTransferLimit: String paused: Boolean } @@ -106,6 +107,6 @@ type Query { type Mutation { addGuardSignature(id: String, dataHash: String, signature: String): String updateConfirmedBlock(id: String, block: String): String - lnBridgeHeartBeat(fromChainId: String, toChainId: String, version: String, relayer: String, tokenAddress: String): String + lnBridgeHeartBeat(fromChainId: String, toChainId: String, version: String, relayer: String, tokenAddress: String, softTransferLimit: String): String } diff --git a/apollo/src/aggregation/aggregation.resolver.ts b/apollo/src/aggregation/aggregation.resolver.ts index 2cf340e8..461ffd3e 100644 --- a/apollo/src/aggregation/aggregation.resolver.ts +++ b/apollo/src/aggregation/aggregation.resolver.ts @@ -171,7 +171,8 @@ export class AggregationResolver { @Args('toChainId') toChainId: string, @Args('version') version: string, @Args('relayer') relayer: string, - @Args('tokenAddress') tokenAddress: string + @Args('tokenAddress') tokenAddress: string, + @Args('softTransferLimit') softTransferLimit: string ) { const id = `${version}-${fromChainId}-${toChainId}-${relayer.toLowerCase()}-${tokenAddress.toLowerCase()}`; try { @@ -179,6 +180,7 @@ export class AggregationResolver { where: { id: id }, data: { heartbeatTimestamp: Math.floor(Date.now() / 1000), + softTransferLimit: softTransferLimit, }, }); } catch (e) { @@ -311,6 +313,8 @@ export class AggregationResolver { const take = row || 128; const sendToken = token?.toLowerCase(); const baseFilters = { fromChain, toChain, sendToken, bridge, version }; + amount = amount ?? '0'; + decimals = decimals ?? 18; const where = { ...baseFilters, @@ -328,9 +332,15 @@ export class AggregationResolver { var transferLimit = BigInt(0); const now = Math.floor(Date.now() / 1000); for (const record of records.records) { - const limit = record.version == 'lnv2' ? BigInt(record.margin) : BigInt(record.transferLimit); - if (limit > transferLimit) { - transferLimit = limit; + 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 providerFee = BigInt(amount) * BigInt(record.liquidityFeeRate) / BigInt(100000) + BigInt(record.baseFee); if (limit < BigInt(amount) + providerFee + BigInt(record.protocolFee) || record.paused) { @@ -349,6 +359,9 @@ export class AggregationResolver { if (point == null) { continue; } + if (limit > transferLimit) { + transferLimit = limit; + } sortedRelayers.push({ record, point }); } return { diff --git a/apollo/src/graphql.ts b/apollo/src/graphql.ts index ec3f5a1c..0581da98 100644 --- a/apollo/src/graphql.ts +++ b/apollo/src/graphql.ts @@ -108,6 +108,7 @@ export class LnBridgeRelayInfo { heartbeatTimestamp?: Nullable; messageChannel?: Nullable; transferLimit?: Nullable; + softTransferLimit?: Nullable; paused?: Nullable; } @@ -131,7 +132,7 @@ export abstract class IMutation { abstract updateConfirmedBlock(id?: Nullable, block?: Nullable): Nullable | Promise>; - abstract lnBridgeHeartBeat(fromChainId?: Nullable, toChainId?: Nullable, version?: Nullable, relayer?: Nullable, tokenAddress?: Nullable): Nullable | Promise>; + abstract lnBridgeHeartBeat(fromChainId?: Nullable, toChainId?: Nullable, version?: Nullable, relayer?: Nullable, tokenAddress?: Nullable, softTransferLimit?: Nullable): Nullable | Promise>; } export type BigInt = any; diff --git a/apollo/src/lnbridgev20/lnbridgev20.service.ts b/apollo/src/lnbridgev20/lnbridgev20.service.ts index bd88f097..7f65ccb2 100644 --- a/apollo/src/lnbridgev20/lnbridgev20.service.ts +++ b/apollo/src/lnbridgev20/lnbridgev20.service.ts @@ -606,6 +606,7 @@ export class Lnbridgev20Service implements OnModuleInit { lastTransferId: '0x0000000000000000000000000000000000000000000000000000000000000000', messageChannel: tokenPair.channel, transferLimit: '0', + softTransferLimit: '0', paused: false, }); } @@ -697,6 +698,7 @@ export class Lnbridgev20Service implements OnModuleInit { lastTransferId: '0x0000000000000000000000000000000000000000000000000000000000000000', messageChannel: tokenPair.channel, transferLimit: '0', + softTransferLimit: '0', paused: false, }); } else { @@ -803,6 +805,7 @@ export class Lnbridgev20Service implements OnModuleInit { heartbeatTimestamp: 0, messageChannel: tokenPair.channel, transferLimit: '0', + softTransferLimit: '0', paused: false, }); } else { diff --git a/apollo/src/lnv3/lnv3.service.ts b/apollo/src/lnv3/lnv3.service.ts index 12263846..d91b6790 100644 --- a/apollo/src/lnv3/lnv3.service.ts +++ b/apollo/src/lnv3/lnv3.service.ts @@ -373,6 +373,7 @@ export class Lnv3Service implements OnModuleInit { profit: '0', heartbeatTimestamp: 0, transferLimit: record.transferLimit ?? '0', + softTransferLimit: '0', paused: record.paused ?? false, messageChannel: channel.channel, }); From 9270a536fa26bc10022495f165cbc6590cd2c4d7 Mon Sep 17 00:00:00 2001 From: xiaoch05 Date: Thu, 7 Mar 2024 13:54:35 +0800 Subject: [PATCH 2/2] limit default value 0 if not set --- apollo/src/aggregation/aggregation.resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apollo/src/aggregation/aggregation.resolver.ts b/apollo/src/aggregation/aggregation.resolver.ts index 461ffd3e..be679e09 100644 --- a/apollo/src/aggregation/aggregation.resolver.ts +++ b/apollo/src/aggregation/aggregation.resolver.ts @@ -180,7 +180,7 @@ export class AggregationResolver { where: { id: id }, data: { heartbeatTimestamp: Math.floor(Date.now() / 1000), - softTransferLimit: softTransferLimit, + softTransferLimit: softTransferLimit ?? '0', }, }); } catch (e) {