Skip to content

Commit

Permalink
fix tests, prepare to deploy UniswapV3ConverterStrategy 3.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
dvpublic committed Apr 4, 2024
1 parent 892dc86 commit 4de479f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
11 changes: 0 additions & 11 deletions contracts/strategies/ConverterStrategyBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import "./ConverterStrategyBaseLib.sol";
import "./ConverterStrategyBaseLib2.sol";
import "./DepositorBase.sol";
import "../interfaces/IConverterStrategyBase.sol";
import "hardhat/console.sol";

/////////////////////////////////////////////////////////////////////
/// TERMS
Expand Down Expand Up @@ -166,24 +165,19 @@ abstract contract ConverterStrategyBase is IConverterStrategyBase, ITetuConverte
uint investedAssetsAfter,
uint balanceAfter
){
console.log("_depositToPoolUniversal.amount_", amount_);
address _asset = baseState.asset;

uint amountToDeposit = amount_ > earnedByPrices_
? amount_ - earnedByPrices_
: 0;
console.log("_depositToPoolUniversal.amountToDeposit", amountToDeposit);

// skip deposit for small amounts
bool needToDeposit = amountToDeposit > _csbs.reinvestThresholdPercent * investedAssets_ / DENOMINATOR;
uint balanceBefore = AppLib.balance(_asset);
console.log("_depositToPoolUniversal.balanceBefore", balanceBefore);

// send earned-by-prices to the insurance, ignore dust values
if (earnedByPrices_ > AppLib._getLiquidationThreshold(liquidationThresholds[_asset])) {
console.log("_depositToPoolUniversal.2");
if (needToDeposit || balanceBefore >= earnedByPrices_) {
console.log("_depositToPoolUniversal.3");
(amountSentToInsurance,) = ConverterStrategyBaseLib2.sendToInsurance(
_asset,
earnedByPrices_,
Expand All @@ -192,23 +186,20 @@ abstract contract ConverterStrategyBase is IConverterStrategyBase, ITetuConverte
balanceBefore
);
} else {
console.log("_depositToPoolUniversal.4");
// needToDeposit is false and we don't have enough amount to cover earned-by-prices, we need to withdraw
(,, strategyLoss, amountSentToInsurance) = _withdrawUniversal(0, earnedByPrices_, investedAssets_);
}
}

// make deposit
if (needToDeposit) {
console.log("_depositToPoolUniversal.5");
(address[] memory tokens, uint indexAsset) = _getTokens(_asset);

// prepare array of amounts ready to deposit, borrow missed amounts
uint[] memory amounts = _beforeDeposit(_csbs.converter, amountToDeposit, tokens, indexAsset);

// make deposit, actually consumed amounts can be different from the desired amounts
if (!ConverterStrategyBaseLib2.findZeroAmount(amounts)) {
console.log("_depositToPoolUniversal.6");

// we cannot enter to pool if at least one of amounts is zero
// we check != 0 and don't use thresholds because some strategies allow to enter to the pool with amount < liquidation threshold
Expand All @@ -217,11 +208,9 @@ abstract contract ConverterStrategyBase is IConverterStrategyBase, ITetuConverte
}
}

console.log("_depositToPoolUniversal.7");
// update _investedAssets with new deposited amount
investedAssetsAfter = _updateInvestedAssets();
balanceAfter = AppLib.balance(_asset);
console.log("_depositToPoolUniversal.8.balanceAfter", balanceAfter);

// we need to compensate difference if during deposit we lost some assets
(,strategyLoss) = ConverterStrategyBaseLib2._registerIncome(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ contract UniswapV3ConverterStrategy is UniswapV3Depositor, ConverterStrategyBase
) {
if (needRebalance()) {
// Rebalance is required, it's not allowed to enter to the pool
// So let's return zero amounts ready to invest
// So let's return zero ready-to-invest amounts
tokenAmounts = new uint[](2);
} else {
(uint prop0, uint prop1) = UniswapV3ConverterStrategyLogicLib.getEntryDataProportions(
Expand Down
36 changes: 29 additions & 7 deletions test/strategies/pair/PairBasedStrategyActionResponseIntTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,17 +522,18 @@ describe('PairBasedStrategyActionResponseIntTest', function() {
it("should deposit but NOT invest", async () => {
const converterStrategyBase = ConverterStrategyBase__factory.connect(b.strategy.address, signer);

const amountToDeposit = parseUnits('1000', 6);
const amountToDeposit = 1000;
const stateBefore = await StateUtilsNum.getState(signer, signer, converterStrategyBase, b.vault);
await b.vault.connect(signer).deposit(amountToDeposit, signer.address, {gasLimit: GAS_LIMIT});
await b.vault.connect(signer).deposit(parseUnits(amountToDeposit.toString(), 6), signer.address, {gasLimit: GAS_LIMIT});
const stateAfter = await StateUtilsNum.getState(signer, signer, converterStrategyBase, b.vault);

expect(stateAfter.vault.totalAssets).gt(stateBefore.vault.totalAssets);
expect(stateAfter.strategy.totalAssets).eq(stateBefore.strategy.totalAssets);
expect(stateAfter.strategy.assetBalance).eq(stateBefore.strategy.assetBalance + amountToDeposit.toNumber());

expect(stateAfter.strategy.totalAssets).approximately(stateBefore.strategy.totalAssets + amountToDeposit, 1);
expect(stateAfter.strategy.assetBalance).eq(stateBefore.strategy.assetBalance + amountToDeposit);
expect(stateAfter.strategy.investedAssets).approximately(stateBefore.strategy.investedAssets, 1);
});
it("should revert on withdraw", async () => {
/** 3.1.7 allows to withdraw when rebalance is required */
it.skip("should revert on withdraw", async () => {
const converterStrategyBase = ConverterStrategyBase__factory.connect(b.strategy.address, signer);
const platform = await converterStrategyBase.PLATFORM();

Expand All @@ -544,7 +545,17 @@ describe('PairBasedStrategyActionResponseIntTest', function() {
b.vault.connect(signer).withdraw(parseUnits('300', 6), signer.address, signer.address, {gasLimit: GAS_LIMIT})
).revertedWith(expectedErrorMessage);
});
it("should revert on withdraw-all", async () => {
it("should withdraw successfully", async () => {
const converterStrategyBase = ConverterStrategyBase__factory.connect(b.strategy.address, signer);

const stateBefore = await StateUtilsNum.getState(signer, signer, converterStrategyBase, b.vault);
await b.vault.connect(signer).withdraw(parseUnits('300', 6), signer.address, signer.address, {gasLimit: GAS_LIMIT});
const stateAfter = await StateUtilsNum.getState(signer, signer, converterStrategyBase, b.vault);

expect(stateAfter.user.assetBalance).eq(stateBefore.user.assetBalance + 300);
});
/** 3.1.7 allows to withdraw when rebalance is required */
it.skip("should revert on withdraw-all", async () => {
const converterStrategyBase = ConverterStrategyBase__factory.connect(b.strategy.address, signer);
const platform = await converterStrategyBase.PLATFORM();

Expand All @@ -556,6 +567,17 @@ describe('PairBasedStrategyActionResponseIntTest', function() {
b.vault.connect(signer).withdrawAll({gasLimit: GAS_LIMIT})
).revertedWith(expectedErrorMessage);
});
it("should withdraw-all successfully", async () => {
const converterStrategyBase = ConverterStrategyBase__factory.connect(b.strategy.address, signer);

const stateBefore = await StateUtilsNum.getState(signer, signer, converterStrategyBase, b.vault);
await b.vault.connect(signer).withdrawAll({gasLimit: GAS_LIMIT});
const stateAfter = await StateUtilsNum.getState(signer, signer, converterStrategyBase, b.vault);

expect(stateAfter.user.assetBalance).gt(stateBefore.user.assetBalance);
expect(stateBefore.vault.userShares).gt(0);
expect(stateAfter.vault.userShares).eq(0);
});
it("should revert on rebalance", async () => {
const converterStrategyBase = ConverterStrategyBase__factory.connect(b.strategy.address, signer);

Expand Down

0 comments on commit 4de479f

Please sign in to comment.