Skip to content

Commit

Permalink
Merge branch 'master' into xiaoch05-for-xtoken
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoch05 committed Mar 28, 2024
2 parents 3f9050c + 11aec31 commit 586ac90
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions apollo/src/aggregation/aggregation.history.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type Query {
queryLnBridgeRelayInfos(fromChain: String, toChain: String, version: String, bridge: String, relayer: String, row: Int, page: Int): LnBridgeRelayInfos
sortedLnBridgeRelayInfos(fromChain: String, toChain: String, version: String, bridge: String, token: String, row: Int, amount: String, decimals: Int): SortedLnBridgeRelayInfos
queryLnBridgeSupportChains(tokenKey: String): [SupportChains]
queryMaxTransfer(fromChain: String, toChain: String, bridge: String, token: String, balance: String): BigInt
}

type Mutation {
Expand Down
61 changes: 61 additions & 0 deletions apollo/src/aggregation/aggregation.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,65 @@ export class AggregationResolver {
records: sortedRelayers.sort((l, r) => l.point - r.point).map((item) => item.record),
};
}

/**
* For one relayer
* userBalance, transferLimitV2(margin), transferLimitV3 baseFee, liquidityFeeRate, protocolFee
* suppose user transfer amount is X
* then totalFee = X * liquidityFeeRate / 100000 + baseFee + protocolFee
* 1. totalFee + X <= userBalance => X <= (userBalance - baseFee - protocolFee)/(1 + liquidityFeeRate / 100000)
* 2. V2: totalFee + X <= transferLimitV2 => X <= (transferLimitV2 - baseFee - protocolFee)/(1 + liquidityFeeRate / 100000)
* 3. V3: X <= transferLimitV3 => X <= transferLimitV3
*/
@Query()
async queryMaxTransfer(
@Args('fromChain') fromChain: string,
@Args('toChain') toChain: string,
@Args('bridge') bridge: string,
@Args('token') token: string,
@Args('balance') balance: string,
) {
const sendToken = token?.toLowerCase();
const baseFilters = { fromChain, toChain, sendToken, bridge };

const where = {
...baseFilters,
};

const records = await this.aggregationService.queryLnBridgeRelayInfos({
skip: 0,
where,
});
var maxTransferAmount = BigInt(0);
const now = Math.floor(Date.now() / 1000);
const liquidityFeeScale = BigInt(100000);
for (const record of records.records) {
// offline
if (record.heartbeatTimestamp + this.heartbeatTimeout < now || record.paused) {
continue;
}

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);

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 limit = limitRestrict < userBalanceRestrict ? limitRestrict : userBalanceRestrict;
if (maxTransferAmount < limit) {
maxTransferAmount = limit;
}
}
return maxTransferAmount;
}
}
2 changes: 2 additions & 0 deletions apollo/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export abstract class IQuery {
abstract sortedLnBridgeRelayInfos(fromChain?: Nullable<string>, toChain?: Nullable<string>, version?: Nullable<string>, bridge?: Nullable<string>, token?: Nullable<string>, row?: Nullable<number>, amount?: Nullable<string>, decimals?: Nullable<number>): Nullable<SortedLnBridgeRelayInfos> | Promise<Nullable<SortedLnBridgeRelayInfos>>;

abstract queryLnBridgeSupportChains(tokenKey?: Nullable<string>): Nullable<Nullable<SupportChains>[]> | Promise<Nullable<Nullable<SupportChains>[]>>;

abstract queryMaxTransfer(fromChain?: Nullable<string>, toChain?: Nullable<string>, bridge?: Nullable<string>, token?: Nullable<string>, balance?: Nullable<string>): Nullable<BigInt> | Promise<Nullable<BigInt>>;
}

export class HistoryRecord {
Expand Down

0 comments on commit 586ac90

Please sign in to comment.