From a636d35e47f551087af4b8202c383bfcc43e1f48 Mon Sep 17 00:00:00 2001 From: xiaoch05 Date: Tue, 29 Oct 2024 17:35:49 +0800 Subject: [PATCH] fix the error: wrong auto lending supply status --- src/liquidity/lend/aave.ts | 33 +-------------------------------- src/liquidity/lend/market.ts | 10 ---------- src/liquidity/lend/moonwell.ts | 32 +------------------------------- src/relayer/relayer.service.ts | 21 +++++++++++---------- 4 files changed, 13 insertions(+), 83 deletions(-) diff --git a/src/liquidity/lend/aave.ts b/src/liquidity/lend/aave.ts index 4d805ee..76f3973 100644 --- a/src/liquidity/lend/aave.ts +++ b/src/liquidity/lend/aave.ts @@ -11,8 +11,6 @@ import { LendMarket, TxInfo, WithdrawBorrowBalance, - DebtStatus, - CollateralStatus, maxU256, } from "./market"; import { @@ -537,8 +535,6 @@ export class Aave extends LendMarket { public oracle: AaveOracle; public multicall: MulticallContract; public wethContract: WETHContract; - public debtStatus = new Map(); - public collateralStatus = new Map(); public debtTokens: DebtToken[]; public collateralTokens: CollateralToken[]; @@ -625,14 +621,6 @@ export class Aave extends LendMarket { }); } - public enableDebtStatus(account: string) { - this.debtStatus.set(account, DebtStatus.HasDebt); - } - - public enableCollateralLack(account: string) { - this.collateralStatus.set(account, CollateralStatus.CollateralLack); - } - // https://github.com/aave/aave-v3-core/blob/v1.19.4/contracts/protocol/libraries/types/DataTypes.sol#L65-L67 public isCollateral(userConfig: bigint, token: CollateralToken): boolean { return ( @@ -762,9 +750,6 @@ export class Aave extends LendMarket { // the balanceOf(debtToken) > 0 and balanceOf(underlyingToken) > 0 // repay amount = min(balanceOf(debtToken), balanceOf(underlyingToken)) async checkDebtToRepay(account: string): Promise { - if (this.debtStatus.get(account) === DebtStatus.NoDebt) { - return []; - } const tokens: string[] = this.debtTokens .map((token) => [ token.address, @@ -776,12 +761,9 @@ export class Aave extends LendMarket { // [debt, underlying, debt, underlying, ...] const balances: bigint[] = await this.multicall.getBalance(account, tokens); let result: WaitingRepayInfo[] = []; - let debtStatus: DebtStatus = DebtStatus.NoDebt; for (let i = 0; i < balances.length; i += 2) { const debtBalance = balances[i]; - if (debtBalance > BigInt(0)) { - debtStatus = DebtStatus.HasDebt; - } else { + if (debtBalance <= BigInt(0)) { continue; } const underlyingBalance = balances[i + 1]; @@ -807,16 +789,10 @@ export class Aave extends LendMarket { } result.push({ token: debtToken, amount: fixedDebtBalance }); } - this.debtStatus.set(account, debtStatus); return result; } async checkCollateralToSupply(account: string): Promise { - if ( - this.collateralStatus.get(account) === CollateralStatus.CollateralFull - ) { - return []; - } const tokens: string[] = this.collateralTokens .map((token) => [ token.aTokenContract.address, @@ -828,14 +804,11 @@ export class Aave extends LendMarket { // [aToken, underlying, aToken, underlying, ...] const balances: bigint[] = await this.multicall.getBalance(account, tokens); let result: WaitingSupplyInfo[] = []; - let collateralStatus: CollateralStatus = CollateralStatus.CollateralFull; for (let i = 0; i < balances.length; i += 2) { const aTokenBalance = balances[i]; const collateralToken = this.collateralTokens[(i / 2) | 0]; if (aTokenBalance >= collateralToken.autosupplyAmount) { continue; - } else { - collateralStatus = CollateralStatus.CollateralLack; } const underlyingBalance = balances[i + 1]; const maxInterest = (aTokenBalance * BigInt(20)) / BigInt(100 * 365); // 20 APY 1 day @@ -854,7 +827,6 @@ export class Aave extends LendMarket { } result.push({ token: collateralToken, amount: avaiableSupplyAmount }); } - this.collateralStatus.set(account, collateralStatus); return result; } @@ -996,7 +968,6 @@ export class Aave extends LendMarket { onBehalfOf ), }); - this.enableDebtStatus(onBehalfOf); } else { this.logger.warn( `[${this.name}] withdraw fixed amount not enough fixed: ${fixedWithdrawBalance}, amount: ${amount}` @@ -1004,7 +975,6 @@ export class Aave extends LendMarket { return []; } } - this.enableCollateralLack(onBehalfOf); } else if (avaiable === BigInt(0)) { avaiable = await this.borrowAvailable(onBehalfOf, token); this.logger.log( @@ -1019,7 +989,6 @@ export class Aave extends LendMarket { value: "0", data: this.borrowRawData(token, amount, onBehalfOf), }); - this.enableDebtStatus(onBehalfOf); } } else { this.logger.warn(`[${this.name}] not enough balance, need ${amount}`); diff --git a/src/liquidity/lend/market.ts b/src/liquidity/lend/market.ts index fb53aff..8819e77 100644 --- a/src/liquidity/lend/market.ts +++ b/src/liquidity/lend/market.ts @@ -9,16 +9,6 @@ export interface WithdrawBorrowBalance { borrow: bigint; } -export enum DebtStatus { - HasDebt, - NoDebt, -} - -export enum CollateralStatus { - CollateralFull, - CollateralLack, -} - export const maxU256: bigint = BigInt( "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ); diff --git a/src/liquidity/lend/moonwell.ts b/src/liquidity/lend/moonwell.ts index 09f9262..c26a646 100644 --- a/src/liquidity/lend/moonwell.ts +++ b/src/liquidity/lend/moonwell.ts @@ -20,8 +20,6 @@ import { LendMarket, TxInfo, WithdrawBorrowBalance, - DebtStatus, - CollateralStatus, maxU256, } from "./market"; import { Any } from "../../base/bignumber"; @@ -165,8 +163,6 @@ export class Moonwell extends LendMarket { public oracleContract: MoonwellOracle; public multicallContract: MulticallContract; public wethContract: WETHContract; - public debtStatus = new Map(); - public collateralStatus = new Map(); public debtTokens: DebtToken[]; public collateralTokens: CollateralToken[]; @@ -271,14 +267,6 @@ export class Moonwell extends LendMarket { ); } - public enableDebtStatus(account: string) { - this.debtStatus.set(account, DebtStatus.HasDebt); - } - - public enableCollateralLack(account: string) { - this.collateralStatus.set(account, CollateralStatus.CollateralLack); - } - async userLendInfo(account: string): Promise { const allMtokens = await this.comptrollerContract.getAssetsIn(account); let args: MulticallArgs[] = []; @@ -435,7 +423,6 @@ export class Moonwell extends LendMarket { onBehalfOf ), }); - this.enableDebtStatus(onBehalfOf); } else { this.logger.warn( `[${this.name}] withdraw fixed amount not enough fixed: ${fixedWithdrawBalance}, amount: ${amount}` @@ -457,7 +444,6 @@ export class Moonwell extends LendMarket { value: "0", data: this.borrowRawData(token, amount, onBehalfOf), }); - this.enableDebtStatus(onBehalfOf); } } else { this.logger.warn(`[${this.name}] not enough balance, need ${amount}`); @@ -516,11 +502,6 @@ export class Moonwell extends LendMarket { } async checkCollateralToSupply(account: string): Promise { - if ( - this.collateralStatus.get(account) === CollateralStatus.CollateralFull - ) { - return []; - } const tokens: string[] = this.collateralTokens.map( (token) => token.underlyingAddress ); @@ -530,7 +511,6 @@ export class Moonwell extends LendMarket { tokens ); let result: WaitingSupplyInfo[] = []; - let collateralStatus: CollateralStatus = CollateralStatus.CollateralFull; const collateralSnapshotData = this.collateralTokens.map((token) => { return { @@ -559,8 +539,6 @@ export class Moonwell extends LendMarket { (Number(mTokenBalance) * Number(exchangeRateMantissa)) / 1e18; if (undelyingBalanceInCollateral >= collateralToken.autoSupplyAmount) { continue; - } else { - collateralStatus = CollateralStatus.CollateralLack; } const underlyingBalance = balances[i]; const maxInterest = @@ -583,7 +561,6 @@ export class Moonwell extends LendMarket { } result.push({ token: collateralToken, amount: avaiableSupplyAmount }); } - this.collateralStatus.set(account, collateralStatus); return result; } @@ -634,9 +611,6 @@ export class Moonwell extends LendMarket { } async checkDebtToRepay(account: string): Promise { - if (this.debtStatus.get(account) === DebtStatus.NoDebt) { - return []; - } const tokens: string[] = this.debtTokens.map( (token) => token.underlyingAddress ); @@ -646,7 +620,6 @@ export class Moonwell extends LendMarket { tokens ); let result: WaitingRepayInfo[] = []; - let debtStatus: DebtStatus = DebtStatus.NoDebt; const collateralSnapshotData = this.debtTokens.map((token) => { return { @@ -669,9 +642,7 @@ export class Moonwell extends LendMarket { ["uint", "uint", "uint", "uint"], mtokenInfo ); - if (borrowBalance > BigInt(0)) { - debtStatus = DebtStatus.HasDebt; - } else { + if (borrowBalance <= BigInt(0)) { continue; } const underlyingBalance = balances[i]; @@ -696,7 +667,6 @@ export class Moonwell extends LendMarket { } result.push({ token: debtToken, amount: fixedDebtBalance }); } - this.debtStatus.set(account, debtStatus); return result; } diff --git a/src/relayer/relayer.service.ts b/src/relayer/relayer.service.ts index 1aa624c..3389722 100644 --- a/src/relayer/relayer.service.ts +++ b/src/relayer/relayer.service.ts @@ -145,15 +145,6 @@ export class RelayerService implements OnModuleInit { continue; } try { - if (timer.lastRepayLend === 0) { - const busy = await this.repayDept(key); - if (busy) { - timer.isProcessing = false; - this.adjustClock(key); - timer.lastRepayLend = 0; - return; - } - } const txPending = await this.relay( item, timer.lastAdjustTime === 0, @@ -166,7 +157,17 @@ export class RelayerService implements OnModuleInit { return; } } catch (err) { - this.logger.warn(`relay bridge failed, err: ${err}`); + this.logger.warn(`[${key}]relay bridge failed, err: ${err}`); + } + } + if (timer.lastRepayLend === 0) { + try { + const busy = await this.repayDept(key); + if (busy) { + timer.lastRepayLend = 0; + } + } catch (err) { + this.logger.warn(`[${key}]try to check repay dept failed, err: ${err}`); } } timer.isProcessing = false;