Skip to content

Commit

Permalink
fix the error: wrong auto lending supply status
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoch05 committed Oct 29, 2024
1 parent a658d77 commit a636d35
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 83 deletions.
33 changes: 1 addition & 32 deletions src/liquidity/lend/aave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
LendMarket,
TxInfo,
WithdrawBorrowBalance,
DebtStatus,
CollateralStatus,
maxU256,
} from "./market";
import {
Expand Down Expand Up @@ -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[];
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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<WaitingRepayInfo[]> {
if (this.debtStatus.get(account) === DebtStatus.NoDebt) {
return [];
}
const tokens: string[] = this.debtTokens
.map((token) => [
token.address,
Expand All @@ -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];
Expand All @@ -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<WaitingSupplyInfo[]> {
if (
this.collateralStatus.get(account) === CollateralStatus.CollateralFull
) {
return [];
}
const tokens: string[] = this.collateralTokens
.map((token) => [
token.aTokenContract.address,
Expand All @@ -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
Expand All @@ -854,7 +827,6 @@ export class Aave extends LendMarket {
}
result.push({ token: collateralToken, amount: avaiableSupplyAmount });
}
this.collateralStatus.set(account, collateralStatus);
return result;
}

Expand Down Expand Up @@ -996,15 +968,13 @@ export class Aave extends LendMarket {
onBehalfOf
),
});
this.enableDebtStatus(onBehalfOf);
} else {
this.logger.warn(
`[${this.name}] withdraw fixed amount not enough fixed: ${fixedWithdrawBalance}, amount: ${amount}`
);
return [];
}
}
this.enableCollateralLack(onBehalfOf);
} else if (avaiable === BigInt(0)) {
avaiable = await this.borrowAvailable(onBehalfOf, token);
this.logger.log(
Expand All @@ -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}`);
Expand Down
10 changes: 0 additions & 10 deletions src/liquidity/lend/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
Expand Down
32 changes: 1 addition & 31 deletions src/liquidity/lend/moonwell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import {
LendMarket,
TxInfo,
WithdrawBorrowBalance,
DebtStatus,
CollateralStatus,
maxU256,
} from "./market";
import { Any } from "../../base/bignumber";
Expand Down Expand Up @@ -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[];
Expand Down Expand Up @@ -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<LendInfo | null> {
const allMtokens = await this.comptrollerContract.getAssetsIn(account);
let args: MulticallArgs[] = [];
Expand Down Expand Up @@ -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}`
Expand All @@ -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}`);
Expand Down Expand Up @@ -516,11 +502,6 @@ export class Moonwell extends LendMarket {
}

async checkCollateralToSupply(account: string): Promise<WaitingSupplyInfo[]> {
if (
this.collateralStatus.get(account) === CollateralStatus.CollateralFull
) {
return [];
}
const tokens: string[] = this.collateralTokens.map(
(token) => token.underlyingAddress
);
Expand All @@ -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 {
Expand Down Expand Up @@ -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 =
Expand All @@ -583,7 +561,6 @@ export class Moonwell extends LendMarket {
}
result.push({ token: collateralToken, amount: avaiableSupplyAmount });
}
this.collateralStatus.set(account, collateralStatus);
return result;
}

Expand Down Expand Up @@ -634,9 +611,6 @@ export class Moonwell extends LendMarket {
}

async checkDebtToRepay(account: string): Promise<WaitingRepayInfo[]> {
if (this.debtStatus.get(account) === DebtStatus.NoDebt) {
return [];
}
const tokens: string[] = this.debtTokens.map(
(token) => token.underlyingAddress
);
Expand All @@ -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 {
Expand All @@ -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];
Expand All @@ -696,7 +667,6 @@ export class Moonwell extends LendMarket {
}
result.push({ token: debtToken, amount: fixedDebtBalance });
}
this.debtStatus.set(account, debtStatus);
return result;
}

Expand Down
21 changes: 11 additions & 10 deletions src/relayer/relayer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down

0 comments on commit a636d35

Please sign in to comment.