Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
99e1e95
feat: lifi swaps levered
rhlsthrm Dec 4, 2024
b9f6f68
feat: test
rhlsthrm Dec 4, 2024
5aba4d2
test fix
vminkov Dec 6, 2024
58c006d
testing the aggregator swap changes on mode leveraged positions for w…
vminkov Dec 6, 2024
2219fe7
Merge branch 'development' into feat/lifi-swaps-levered
rhlsthrm Dec 9, 2024
a9ea977
feat: lev
rhlsthrm Dec 9, 2024
3404c0c
compiling expected slippage input refactoring
vminkov Dec 9, 2024
aee9a0c
merged with upstream
vminkov Dec 9, 2024
8d9e300
leveraged tests fixes
vminkov Dec 9, 2024
0c7849a
testing the aggregator swaps for mode weth-usdc leveraged positions
vminkov Dec 9, 2024
9ea07aa
working test for min/max leverage ratio with aggregator swaps
vminkov Dec 10, 2024
684f752
close position with aggregator test
vminkov Dec 10, 2024
84047b9
whitelisted swap routers config
vminkov Dec 10, 2024
9bdd02b
assumed slippage defaults refactoring
vminkov Dec 10, 2024
311bc80
Merge branch 'development' into feat/lifi-swaps-levered
rhlsthrm Dec 11, 2024
4816bed
feat: generate
rhlsthrm Dec 11, 2024
a3c5f30
moved calculateAdjustmentAmountDeltas in the factory
vminkov Dec 11, 2024
af68c89
Merge branch 'feat/lifi-swaps-levered' of github.com:ionicprotocol/mo…
vminkov Dec 11, 2024
5ca05f5
fixing the leveraged positions aggregator swaps tests
vminkov Dec 11, 2024
5865877
Merge branch 'development' into feat/lifi-swaps-levered
rhlsthrm Dec 12, 2024
681914e
lint fix
vminkov Dec 12, 2024
fa0e431
fix: build
rhlsthrm Dec 12, 2024
444074b
removing an unnecessary param
vminkov Dec 12, 2024
5e98c58
Merge branch 'feat/lifi-swaps-levered' of github.com:ionicprotocol/mo…
vminkov Dec 12, 2024
daa757e
Merge branch 'development' into feat/lifi-swaps-levered
rhlsthrm Dec 12, 2024
67aa95c
refactoring to reduce the code size of the factory 2nd ext
vminkov Dec 12, 2024
96b5ba6
Merge branch 'development' into feat/lifi-swaps-levered
rhlsthrm Dec 16, 2024
e85bde1
Merge pull request #852 from ionicprotocol/refactoring/factory-code-s…
rhlsthrm Dec 16, 2024
e27e5fa
feat: deploy
rhlsthrm Dec 16, 2024
92abaa7
Merge branch 'development' into feat/lifi-swaps-levered
rhlsthrm Dec 17, 2024
fa39637
feat: deploy
rhlsthrm Dec 17, 2024
3d4a661
feat: front end for levered
rhlsthrm Dec 17, 2024
f781120
feat: extra sim
rhlsthrm Dec 17, 2024
f929d49
fix: slippage
rhlsthrm Dec 18, 2024
83b9cfe
previewDeposit and previewRedeem fns for the CToken
vminkov Dec 20, 2024
3832e4e
tested the previewDeposit/previewRedeem fns
vminkov Dec 20, 2024
701bee6
Merge branch 'development' into feat/lifi-swaps-levered
rhlsthrm Dec 30, 2024
31f93d1
code size fix - the moved fns are not tested if they work anymore
vminkov Dec 30, 2024
46880cf
tested the ionic fees fn
vminkov Dec 30, 2024
926a762
feat: generated
rhlsthrm Dec 30, 2024
7cc540d
feat: frontend
rhlsthrm Dec 30, 2024
1c74025
feat: deployments
rhlsthrm Dec 30, 2024
5cc5eae
feat: lev configure
rhlsthrm Dec 30, 2024
3e6b2d2
optimism position test
vminkov Dec 30, 2024
d95260a
feat: leverage
rhlsthrm Jan 2, 2025
f35a9d8
fix: leverage
rhlsthrm Jan 3, 2025
2276159
Merge branch 'development' into feat/lifi-swaps-levered
rhlsthrm Jan 3, 2025
513a71d
fix: imports
rhlsthrm Jan 3, 2025
615997d
Merge branch 'development' into feat/lifi-swaps-levered
rhlsthrm Jan 3, 2025
2e35231
Merge branch 'feat/lifi-swaps-levered' of github.com:ionicprotocol/mo…
vminkov Jan 4, 2025
5b17dcb
supply delta calcs refactoring
vminkov Jan 4, 2025
4e85346
delta calcs refactoring
vminkov Jan 4, 2025
d06fc53
the current ratio is known from the other params of the calc delta fn
vminkov Jan 4, 2025
c3f7326
dynamic swap amount calcs for the aggregator data
vminkov Jan 5, 2025
32e0156
feat: readme
rhlsthrm Jan 6, 2025
4e47b92
adjusting the ratio of a position and closing a position frontend logic
vminkov Jan 7, 2025
e28e280
Merge branch 'feat/lifi-swaps-levered' of github.com:ionicprotocol/mo…
vminkov Jan 7, 2025
5158b13
Merge branch 'development' into feat/lifi-swaps-levered
rhlsthrm Jan 7, 2025
f28e37a
fix: issues
rhlsthrm Jan 7, 2025
11f19e7
Merge branch 'development' into feat/lifi-swaps-levered
rhlsthrm Jan 7, 2025
8c03d3f
feat: fix lev
rhlsthrm Jan 7, 2025
36ff8ed
feat: add loop
rhlsthrm Jan 8, 2025
d31e26e
feat: hh
rhlsthrm Jan 9, 2025
1468452
testing the levered positions on optimism with forge
vminkov Jan 9, 2025
682e022
Merge branch 'feat/lifi-swaps-levered' of github.com:ionicprotocol/mo…
vminkov Jan 9, 2025
aa288b6
test to check why there is some difference between the quote swap amo…
vminkov Jan 9, 2025
c7750ec
fixing the slippages of the aggregator swaps in the levered positions…
vminkov Jan 9, 2025
5440256
initial refacctoring for the special levered positions with aggregato…
vminkov Jan 10, 2025
15c2a51
levered positions separation - compiling, but over the code size limit
vminkov Jan 13, 2025
5c9b267
levered positions factory third extension - not integrated yet
vminkov Jan 13, 2025
20fd1dc
increase leverage ratio logic for the separated leveraged positions w…
vminkov Jan 14, 2025
6bd0124
decrease leverage ratio for positions with aggregator swaps tested
vminkov Jan 14, 2025
66e893c
refactoring the tests for the removal of the adjust ratio fn of the l…
vminkov Jan 14, 2025
ec551c5
test fix
vminkov Jan 15, 2025
0f4905d
little fixes
vminkov Jan 15, 2025
e7ef916
optimism forge vix
vminkov Jan 15, 2025
34b9454
little fixes
vminkov Jan 17, 2025
feace44
Merge pull request #941 from ionicprotocol/feat/aggregator-swaps-leve…
rhlsthrm Jan 17, 2025
18cf353
Merge branch 'development' into feat/lifi-swaps-levered
rhlsthrm Jan 20, 2025
416f04b
feat: deploy
rhlsthrm Jan 20, 2025
c87fec5
testing and small changes
vminkov Jan 25, 2025
fd323cc
ui fn fix to create aggr positions
vminkov Jan 25, 2025
3d4996b
feat: deploy
rhlsthrm Jan 26, 2025
c95fa0d
merge
vminkov Jan 26, 2025
9166be7
Merge branch 'feat/lifi-swaps-levered' of github.com:ionicprotocol/mo…
vminkov Jan 26, 2025
1e493ab
feat: compile
rhlsthrm Jan 27, 2025
127686b
feat: deploy script for levered
rhlsthrm Jan 27, 2025
5b60c24
converting unused fns to fallback fns
vminkov Jan 27, 2025
e4787c6
Merge branch 'feat/lifi-swaps-levered' of github.com:ionicprotocol/mo…
vminkov Jan 27, 2025
60ccf76
feat deploy
rhlsthrm Jan 27, 2025
4d6627e
even lower than the code size limit
vminkov Jan 27, 2025
bea7fb7
Merge branch 'feat/lifi-swaps-levered' of github.com:ionicprotocol/mo…
vminkov Jan 27, 2025
04bde80
feat: deploy
rhlsthrm Jan 27, 2025
fc7754e
feat: egenerated
rhlsthrm Jan 27, 2025
8733179
frontend logic fixes according to the contracts changes - aggr swaps …
vminkov Jan 27, 2025
36d824c
feat: levered
rhlsthrm Jan 27, 2025
e8f8e13
fix: build
rhlsthrm Jan 27, 2025
f424b5d
fix: lint
rhlsthrm Jan 28, 2025
89e772e
int number fix
vminkov Jan 28, 2025
5b3b2af
Merge branch 'feat/lifi-swaps-levered' of github.com:ionicprotocol/mo…
vminkov Jan 28, 2025
096647f
slippage scaling fix
vminkov Jan 28, 2025
1c638bd
typo fix
vminkov Jan 28, 2025
3d17581
fixing once again the slippage calcs
vminkov Jan 28, 2025
a2b4fff
reverting the expected slippage param for at ratio calls
vminkov Jan 28, 2025
d8751eb
feat: deplou
rhlsthrm Jan 29, 2025
b315510
feat: deploy
rhlsthrm Jan 29, 2025
15ef30b
ui contracts call fix
vminkov Jan 29, 2025
e4e3768
fix: generated
rhlsthrm Jan 29, 2025
9f2bc6c
Merge branch 'feat/lifi-swaps-levered' of github.com:ionicprotocol/mo…
rhlsthrm Jan 29, 2025
e8037d5
ui interface fix
vminkov Jan 29, 2025
338cbe0
feat: funding
rhlsthrm Jan 29, 2025
96ddff4
Merge branch 'feat/lifi-swaps-levered' of github.com:ionicprotocol/mo…
rhlsthrm Jan 29, 2025
aba5781
Merge branch 'development' into feat/lifi-swaps-levered
rhlsthrm Jan 29, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 16 additions & 120 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ If you already pulled using just `git clone`, you can use below command to updat

### Dependencies

- Node [`16.x`](https://nodejs.org/en/download/)
- Yarn (preinstalled with Node 16)
- Node [`22.x`](https://nodejs.org/en/download/)
- Yarn (preinstalled with Node 22)
- [Foundry](https://book.getfoundry.sh/getting-started/installation.html)

From fresh clone:

- `yarn`
- `yarn workspace @ionicprotocol/sdk forge install`
- `yarn workspace @ionicprotocol/contracts hardhat compile`
- `yarn workspace @ionicprotocol/sdk build`

## Running the UI

After installing the dependencies, run:

- `yarn dev:ui`

Now load the UI at `http://localhost:3000`.

## Tips

Run _everything_ from the top level.
Expand All @@ -34,12 +42,12 @@ Ask for **.env** file from the team and add it under packages/sdk

If you want to add packages to modules, you can do so like:

- `yarn workspace @ionicprotocol/sdk add ethers`
- `yarn workspace @ionicprotocol/sdk add viem`

To run `forge` commands inside the `sdk` package, run:
To run `forge` commands inside the `contracts` package, run:

- `yarn workspace @ionicprotocol/sdk forge install <SOME_DEP>`
- `yarn workspace @ionicprotocol/sdk forge build`
- `yarn workspace @ionicprotocol/contracts forge install <SOME_DEP>`
- `yarn workspace @ionicprotocol/contracts forge build`
- etc

## Adding a Package
Expand Down Expand Up @@ -70,116 +78,4 @@ To run `forge` commands inside the `sdk` package, run:
}
}
- `yarn` from top-level to update dependencies/symlinks.
```

## Working with Forks

Forking requires access to a archive RPC node. For BSC we had a great experience using an RPC Node from NodeReals MegaNode service - https://nodereal.io/meganode .

For forking Polygon we haven't found a free service to use an archive node yet.

You general purpose forking you have to set `FORK_RPC_URL` and `FORK_CHAIN_ID` in your `packages/sdk/.env` file.

And than start a forked local node by running.

```
> yarn dev:node:fork
```

In order to start multiple forks at once we streamlined the process a bit. In your executing shell make sure to set `BSC_RPC_URL` and or `POLYGON_RPC_URL`.

```
> yarn dev:node:bsc
// Will start a CHAIN_ID=56 fork using $BSC_RPC_URL at https://localhost:8545
```

```
> yarn dev:node:polygon
// Will start a CHAIN_ID=137 fork using $POLYGON_RPC_URL at https://localhost:8546
```

The idea is to increase the port number with each fork we want to support.

```
8545: BSC
8546: Polygon
8547: Moonbeam?
```

For convenience we have a Hardhat task to make some token swaps to on the forked node for you.

```
> yarn workspace @ionicprotocol/sdk hardhat fork:fund-accounts --network fork
// Works with https://localhost:8545

> yarn workspace @ionicprotocol/sdk hardhat fork:fund-accounts --network localbsc
// Works with https://localhost:8545

> yarn workspace @ionicprotocol/sdk hardhat fork:fund-accounts --network localpolygon
// Works with https://localhost:8546
```

## Running UI Tests

We are using [ChainSafe/dappeteer](https://github.com/ChainSafe/dappeteer) to test our UI with Metamask. The test are expecting to work on a fork of BSC mainnet for now.

Follow this step to run the UI test locally

1. Start local forked BSC node

```
> yarn dev:node:fork
```

2. Start local UI dev server

```
> yarn dev:ui
```

3. Fund test accounts

```
> yarn workspace @ionicprotocol/sdk hardhat fork:fund-accounts --network fork
```

4. Run dAppateer

```
> yarn test:ui
```

### UI Test With WSL2

If you are on windows you need an X-server to run dappeteer.
These instructions were taken from here: https://www.rickmakes.com/windows-subsystem-for-linux-2-installing-vcxrv-x-server/

Follow these steps before running `yarn test:ui`

1. Download VcXsrv, this is your X-server that will display the GUI
https://sourceforge.net/projects/vcxsrv/
2. Run VcXsrv. Use these settings

```
Multiple Windows -> Start No Client -> Check All Boxes in Extra Settings -> Finish
```

If Windows Defender Firewall prompts you, check both Private & Public Networks and Allow Access

3. Find your Windows IP Address by running the following command

```
cat /etc/resolv.conf
```

4. Set DISPLAY Variable (replace ip_address with your IP Address found above)

```
export DISPLAY=ip_address:0.0
```

5. export opengl Variable

```
export LIBGL_ALWAYS_INDIRECT=1
```
```
116 changes: 41 additions & 75 deletions packages/contracts/contracts/compound/CToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { TokenErrorReporter } from "./ErrorReporter.sol";
import { Exponential } from "./Exponential.sol";
import { EIP20Interface } from "./EIP20Interface.sol";
import { InterestRateModel } from "./InterestRateModel.sol";
import { ComptrollerV3Storage } from "./ComptrollerStorage.sol";
import { IFeeDistributor } from "./IFeeDistributor.sol";
import { CTokenOracleProtected } from "./CTokenOracleProtected.sol";

Expand Down Expand Up @@ -57,8 +56,8 @@ abstract contract CErc20 is CTokenOracleProtected, CTokenSecondExtensionBase, To
functionSelectors[--fnsCount] = this.seize.selector;
functionSelectors[--fnsCount] = this.selfTransferOut.selector;
functionSelectors[--fnsCount] = this.selfTransferIn.selector;
functionSelectors[--fnsCount] = this._withdrawIonicFees.selector;
functionSelectors[--fnsCount] = this._withdrawAdminFees.selector;
functionSelectors[--fnsCount] = this.previewDeposit.selector;
functionSelectors[--fnsCount] = this.previewRedeem.selector;

require(fnsCount == 0, "use the correct array length");
return functionSelectors;
Expand All @@ -67,7 +66,42 @@ abstract contract CErc20 is CTokenOracleProtected, CTokenSecondExtensionBase, To
/*** User Interface ***/

/**
* @notice Sender supplies assets into the market and receives cTokens in exchange
* @notice Simulate the effects of a deposit at the current block, given current on-chain conditions.
* @param assets Exact amount of underlying `asset` token to deposit
* @return of the vault issued in exchange to the user for `assets`
*/
function previewDeposit(uint256 assets) public view returns (uint256) {
(, uint256 mintedCTokens) = _previewDeposit(assets, asCTokenExtension().exchangeRateCurrent());
return mintedCTokens;
}

function _previewDeposit(uint256 assets, uint256 exchangeRateMantissa) internal pure returns (MathError, uint256) {
// mintedCTokens is rounded down here - correct
return divScalarByExpTruncate(
assets,
Exp({ mantissa: exchangeRateMantissa })
);
}

/**
* @notice Simulate the effects of a redemption at the current block, given current on-chain conditions.
* @param redeemTokensIn Exact amount of `cTokens` to redeem
* @return quantity of underlying returned in exchange for `cTokens`.
*/
function previewRedeem(uint256 redeemTokensIn) public view returns (uint256) {
(, uint256 redeemAmount) = _previewRedeem(redeemTokensIn, asCTokenExtension().exchangeRateCurrent());
return redeemAmount;
}

function _previewRedeem(uint256 redeemTokensIn, uint256 exchangeRateMantissa) internal pure returns (MathError, uint256) {
return mulScalarTruncate(
Exp({ mantissa: exchangeRateMantissa }),
redeemTokensIn
);
}

/**
* @notice Sender supplies assets into the market and receives cTokens in exchange
* @dev Accrues interest whether or not the operation succeeds, unless reverted
* @param mintAmount The amount of the underlying asset to supply
* @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
Expand Down Expand Up @@ -179,71 +213,6 @@ abstract contract CErc20 is CTokenOracleProtected, CTokenSecondExtensionBase, To
return doTransferIn(from, amount);
}

/**
* @notice Accrues interest and reduces Ionic fees by transferring to Ionic
* @param withdrawAmount Amount of fees to withdraw
* @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
*/
function _withdrawIonicFees(uint256 withdrawAmount) external override nonReentrant(false) onlyOracleApproved returns (uint256) {
asCTokenExtension().accrueInterest();

if (accrualBlockNumber != block.number) {
return fail(Error.MARKET_NOT_FRESH, FailureInfo.WITHDRAW_IONIC_FEES_FRESH_CHECK);
}

if (getCashInternal() < withdrawAmount) {
return fail(Error.TOKEN_INSUFFICIENT_CASH, FailureInfo.WITHDRAW_IONIC_FEES_CASH_NOT_AVAILABLE);
}

if (withdrawAmount > totalIonicFees) {
return fail(Error.BAD_INPUT, FailureInfo.WITHDRAW_IONIC_FEES_VALIDATION);
}

/////////////////////////
// EFFECTS & INTERACTIONS
// (No safe failures beyond this point)

uint256 totalIonicFeesNew = totalIonicFees - withdrawAmount;
totalIonicFees = totalIonicFeesNew;

// doTransferOut reverts if anything goes wrong, since we can't be sure if side effects occurred.
doTransferOut(address(ionicAdmin), withdrawAmount);

return uint256(Error.NO_ERROR);
}

/**
* @notice Accrues interest and reduces admin fees by transferring to admin
* @param withdrawAmount Amount of fees to withdraw
* @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
*/
function _withdrawAdminFees(uint256 withdrawAmount) external override nonReentrant(false) onlyOracleApproved returns (uint256) {
asCTokenExtension().accrueInterest();

if (accrualBlockNumber != block.number) {
return fail(Error.MARKET_NOT_FRESH, FailureInfo.WITHDRAW_ADMIN_FEES_FRESH_CHECK);
}

// Fail gracefully if protocol has insufficient underlying cash
if (getCashInternal() < withdrawAmount) {
return fail(Error.TOKEN_INSUFFICIENT_CASH, FailureInfo.WITHDRAW_ADMIN_FEES_CASH_NOT_AVAILABLE);
}

if (withdrawAmount > totalAdminFees) {
return fail(Error.BAD_INPUT, FailureInfo.WITHDRAW_ADMIN_FEES_VALIDATION);
}

/////////////////////////
// EFFECTS & INTERACTIONS
// (No safe failures beyond this point)
totalAdminFees = totalAdminFees - withdrawAmount;

// doTransferOut reverts if anything goes wrong, since we can't be sure if side effects occurred.
doTransferOut(ComptrollerV3Storage(address(comptroller)).admin(), withdrawAmount);

return uint256(Error.NO_ERROR);
}

/*** Safe Token ***/

/**
Expand Down Expand Up @@ -376,9 +345,9 @@ abstract contract CErc20 is CTokenOracleProtected, CTokenSecondExtensionBase, To
*/

// mintTokens is rounded down here - correct
(vars.mathErr, vars.mintTokens) = divScalarByExpTruncate(
(vars.mathErr, vars.mintTokens) = _previewDeposit(
vars.actualMintAmount,
Exp({ mantissa: vars.exchangeRateMantissa })
vars.exchangeRateMantissa
);
require(vars.mathErr == MathError.NO_ERROR, "MINT_EXCHANGE_CALCULATION_FAILED");
require(vars.mintTokens > 0, "MINT_ZERO_CTOKENS_REJECTED");
Expand Down Expand Up @@ -475,10 +444,7 @@ abstract contract CErc20 is CTokenOracleProtected, CTokenSecondExtensionBase, To
*/
vars.redeemTokens = redeemTokensIn;

(vars.mathErr, vars.redeemAmount) = mulScalarTruncate(
Exp({ mantissa: vars.exchangeRateMantissa }),
redeemTokensIn
);
(vars.mathErr, vars.redeemAmount) = _previewRedeem(redeemTokensIn, vars.exchangeRateMantissa);
if (vars.mathErr != MathError.NO_ERROR) {
return
failOpaque(Error.MATH_ERROR, FailureInfo.REDEEM_EXCHANGE_TOKENS_CALCULATION_FAILED, uint256(vars.mathErr));
Expand Down
Loading
Loading