Skip to content

Commit

Permalink
Merge pull request #154 from helix-bridge/xiaoch05-softTransferLimit
Browse files Browse the repository at this point in the history
add soft transfer limit
  • Loading branch information
xiaoch05 authored Mar 7, 2024
2 parents f024b36 + 9270a53 commit 66831c6
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
8 changes: 8 additions & 0 deletions apollo/prisma/migrations/20240307030328_init/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions apollo/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ model LnBridgeRelayInfo {
heartbeatTimestamp Int
messageChannel String
transferLimit String
softTransferLimit String
paused Boolean
}

3 changes: 2 additions & 1 deletion apollo/src/aggregation/aggregation.history.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type LnBridgeRelayInfo {
heartbeatTimestamp: Int
messageChannel: String
transferLimit: String
softTransferLimit: String
paused: Boolean
}

Expand Down Expand Up @@ -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
}

21 changes: 17 additions & 4 deletions apollo/src/aggregation/aggregation.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,16 @@ 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 {
await this.aggregationService.updateLnBridgeRelayInfo({
where: { id: id },
data: {
heartbeatTimestamp: Math.floor(Date.now() / 1000),
softTransferLimit: softTransferLimit ?? '0',
},
});
} catch (e) {
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand All @@ -349,6 +359,9 @@ export class AggregationResolver {
if (point == null) {
continue;
}
if (limit > transferLimit) {
transferLimit = limit;
}
sortedRelayers.push({ record, point });
}
return {
Expand Down
3 changes: 2 additions & 1 deletion apollo/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export class LnBridgeRelayInfo {
heartbeatTimestamp?: Nullable<number>;
messageChannel?: Nullable<string>;
transferLimit?: Nullable<string>;
softTransferLimit?: Nullable<string>;
paused?: Nullable<boolean>;
}

Expand All @@ -131,7 +132,7 @@ export abstract class IMutation {

abstract updateConfirmedBlock(id?: Nullable<string>, block?: Nullable<string>): Nullable<string> | Promise<Nullable<string>>;

abstract lnBridgeHeartBeat(fromChainId?: Nullable<string>, toChainId?: Nullable<string>, version?: Nullable<string>, relayer?: Nullable<string>, tokenAddress?: Nullable<string>): Nullable<string> | Promise<Nullable<string>>;
abstract lnBridgeHeartBeat(fromChainId?: Nullable<string>, toChainId?: Nullable<string>, version?: Nullable<string>, relayer?: Nullable<string>, tokenAddress?: Nullable<string>, softTransferLimit?: Nullable<string>): Nullable<string> | Promise<Nullable<string>>;
}

export type BigInt = any;
Expand Down
3 changes: 3 additions & 0 deletions apollo/src/lnbridgev20/lnbridgev20.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ export class Lnbridgev20Service implements OnModuleInit {
lastTransferId: '0x0000000000000000000000000000000000000000000000000000000000000000',
messageChannel: tokenPair.channel,
transferLimit: '0',
softTransferLimit: '0',
paused: false,
});
}
Expand Down Expand Up @@ -697,6 +698,7 @@ export class Lnbridgev20Service implements OnModuleInit {
lastTransferId: '0x0000000000000000000000000000000000000000000000000000000000000000',
messageChannel: tokenPair.channel,
transferLimit: '0',
softTransferLimit: '0',
paused: false,
});
} else {
Expand Down Expand Up @@ -803,6 +805,7 @@ export class Lnbridgev20Service implements OnModuleInit {
heartbeatTimestamp: 0,
messageChannel: tokenPair.channel,
transferLimit: '0',
softTransferLimit: '0',
paused: false,
});
} else {
Expand Down
1 change: 1 addition & 0 deletions apollo/src/lnv3/lnv3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down

0 comments on commit 66831c6

Please sign in to comment.