Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DMA V2] Gas-Optimised #525

Open
wants to merge 49 commits into
base: dev
Choose a base branch
from
Open

[DMA V2] Gas-Optimised #525

wants to merge 49 commits into from

Conversation

fenris85
Copy link
Contributor

Gas optimisations changes cherry picked from old Georgi's branch (https://github.com/OasisDEX/oasis-earn-sc/tree/gg/v2-sc-gas-optimized)

Main changes regard OperationRegistry and how we are storing operations - now it's hash of actions hashes along with operation name.

OperationExecutor has additional method for Automation purposes to get opName by hash easily.

Actions that were using read or write to the storage has changed (Aave V3 and Common so far).

Update to Actions required by Migration feature (add onBehalf to Aave V3 Payback, added params mapping Aave V3 Borrow, add Common TokenBalance Action)

function addOperation(StoredOperation calldata operation) external onlyOwner {
operations[operation.name] = operation;
function addOperation(string memory name, bytes32 operationHash) external onlyOwner {
operations[operationHash] = bytes32(bytes(name));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add the check if the operations[operationHash] != bytes32("")

aggregate(calls);
bytes32 operationName = this.getOperation(keccak256(abi.encodePacked(txStorage.actions)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is .this necessary ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that won't work I think, if the executeOp is delegatecall'ed - as this will be the proxy

Copy link
Contributor

@robercano robercano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the changes will break this PR, so we need to merge that one before we merge this.


constructor(address _registry) UseStore(_registry) {}
ServiceRegistry internal immutable registry;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to abstract this bit away in a separate contract:

contract UseRegistry {
  ServiceRegistry private immutable _registry;
  
  constructor(ServiceRegistry registry_) {
    _registry = registry_
  }
  
 function getRegisteredService(string service) return address
 {
   return _registry.getRegisteredService(service);
 }
}

Right now the end result is very similar, but moving forward this abstracts access to the registry and gives us more flexibility to introduce checks and advanced functionality if needed, without having to change all the actions

IERC20 token = IERC20(pull.asset);

if (pull.amount == type(uint256).max) {
pull.amount = token.balanceOf(pull.from);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a tricky one: do we want to pull the whole balance or rather the whole allowance? We will never be able to pull more than the allowance, but if the allowance is also type(uint256).max then we can pull the balance. Should we use the smaller of the two?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the use cases here is that frontend would set up allowance before the tx. And thus exact amount is not known before the tx (it could be aToken for example) the allowance is slightly bigger to cover any additional amount. But if we would pull this allowed amount it might turn out there is little less than the allowance. So I would stick to the token balance, as it's specific to use cases we have right now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the insight!

if (pull.amount == type(uint256).max) {
pull.amount = token.balanceOf(pull.from);
}
token.transferFrom(pull.from, address(this), pull.amount);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail for tokens like USDT, please use safeTransferFrom instead. Also, if possible, use SafeERC20 from OpenZeppelin library, instead of our custom implementation of SafeERC20. If the size of the contract is a concern, then we can copy/paste the latest version, but I'd suggest just using it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to touch the executor I'd suggest moving errors and events into an interface file to clean it up a bit. The interface would contain the public functions, errors, enums and events. ALso other interfaces (like IProxy) if it makes sense.

@@ -1,120 +1,131 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
//SPDX-License-Identifier: Unlicense
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? All of our contracts are AGPL


IERC20(asset).safeApprove(lender, paybackAmount);
return keccak256("ERC3156FlashBorrower.onFlashLoan");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could cache the value of this keccak256 in the constructor as immutable and then return it here, saving some gas

function receiveFlashLoan(
IERC20[] memory tokens,
uint256[] memory amounts,
uint256[] memory feeAmounts,
bytes memory data
) external override {

checkIfFlashloanIsInProgress();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment for ReentrancyGuard

*/
function addOperation(StoredOperation calldata operation) external onlyOwner {
operations[operation.name] = operation;
function addOperation(string memory name, bytes32 operationHash) external onlyOwner {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is breaking the public interface and at least 2 files are affected:

  • operations-registry.ts which I believe is used in tests and deployment system
  • tasks/operation-registry which is one of the tools I did.

Fix should be trivial though

) external view returns (bytes32[] memory actions, bool[] memory optional) {
if (keccak256(bytes(operations[name].name)) == keccak256(bytes(""))) {
revert("Operation doesn't exist");
function getOperationName(bytes32 operationHash) external view returns (bytes32 name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should return (string name) so it uses the same interface as addOperation. Otherwise I'd change addOperation to use bytes32 name as well

* @notice Changes the owner of the contract
* @param newOwner The address of the new owner of the Operations Registry
*/
function transferOwnership(address newOwner) public onlyOwner {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be external only

fenris85 and others added 13 commits January 18, 2024 15:39
fix: add operation name validation
* v0.5.21-dma-v2-workers.5

* fix exports

* v0.5.21-dma-v2-workers.6

---------

Co-authored-by: James Tuckett <[email protected]>
)

* refactor: improve readability of aave-like protocol operation resolution

* test: e2e smoke test aave v3 adjust up strategy

* v0.5.21-dma-v2-workers

* feat: update AdjustRiskDown op and copy placeholder for Op test

* feat: create adjust down operation util

* feat: update adjust down operation and op test

* chore: bump adjustdown aave v3 open name version

* refactor: Update operation name for adjusting risk up in AAVEV3Position

* v0.5.21-dma-v2-workers.7
@zerotucks zerotucks changed the title Gas optimisations [DMA V2] Gas-Optimised Jan 31, 2024
zerotucks and others added 13 commits January 31, 2024 11:48
* refactor: improve readability of aave-like protocol operation resolution

* test: e2e smoke test aave v3 adjust up strategy

* v0.5.21-dma-v2-workers

* feat: update AdjustRiskDown op and copy placeholder for Op test

* feat: create adjust down operation util

* feat: update adjust down operation and op test

* chore: bump adjustdown aave v3 open name version

* refactor: Update operation name for adjusting risk up in AAVEV3Position

* v0.5.21-dma-v2-workers.7

* feat: prepare Optimism config and service registry names

* fix: deploy Swap contract and update contract names in Optimism config

* refactor: update contract names and deployment status

* chore: update optimism deploy config

* feat: Add TakeFlashloanBalancer and AaveV3WithdrawAuto to config

* feat: Update service registry names in optimism.conf.ts

* refactor: Update addresses in optimism.conf.ts and deploy.ts

* chore: deploy updated DMA to optimism

* chore: correct optimism config flags

* chore: bump contract versions

* chore: dusable deploy flags
* refactor: improve readability of aave-like protocol operation resolution

* test: e2e smoke test aave v3 adjust up strategy

* v0.5.21-dma-v2-workers

* feat: update AdjustRiskDown op and copy placeholder for Op test

* feat: create adjust down operation util

* feat: update adjust down operation and op test

* chore: bump adjustdown aave v3 open name version

* refactor: Update operation name for adjusting risk up in AAVEV3Position

* v0.5.21-dma-v2-workers.7

* feat: prepare Optimism config and service registry names

* fix: deploy Swap contract and update contract names in Optimism config

* refactor: update contract names and deployment status

* chore: update optimism deploy config

* feat: Add TakeFlashloanBalancer and AaveV3WithdrawAuto to config

* feat: Update service registry names in optimism.conf.ts

* refactor: Update addresses in optimism.conf.ts and deploy.ts

* chore: deploy updated DMA to optimism

* chore: correct optimism config flags

* refactor: Update deployment configuration

* refactor(config): update constructor arguments for OperationExecutor

* refactor(deploy): re-enable SAFE logic to avoid adding entry to Service Registry

* chore: update registry name versions]

* feat: update contract versions

* refactor: Rename SWAP constant in Arbitrum contract names

* feat: verify remaining contracts

* chore: bump contract versions

* chore: dusable deploy flags
* refactor: improve readability of aave-like protocol operation resolution

* test: e2e smoke test aave v3 adjust up strategy

* v0.5.21-dma-v2-workers

* feat: update AdjustRiskDown op and copy placeholder for Op test

* feat: create adjust down operation util

* feat: update adjust down operation and op test

* chore: bump adjustdown aave v3 open name version

* refactor: Update operation name for adjusting risk up in AAVEV3Position

* v0.5.21-dma-v2-workers.7

* feat: prepare Optimism config and service registry names

* fix: deploy Swap contract and update contract names in Optimism config

* refactor: update contract names and deployment status

* chore: update optimism deploy config

* feat: Add TakeFlashloanBalancer and AaveV3WithdrawAuto to config

* feat: Update service registry names in optimism.conf.ts

* refactor: Update addresses in optimism.conf.ts and deploy.ts

* chore: deploy updated DMA to optimism

* chore: correct optimism config flags

* refactor: Update deployment configuration

* refactor(config): update constructor arguments for OperationExecutor

* refactor(deploy): re-enable SAFE logic to avoid adding entry to Service Registry

* chore: update registry name versions]

* feat: update versions for mainnet deployment

* feat: redeploy core contracts

* feat: update contract versions

* refactor: Rename SWAP constant in Arbitrum contract names

* feat: verify remaining contracts

* chore: bump contract versions

* chore: dusable deploy flags

* feat: update contract versions

* refactor: Rename SWAP constant in Arbitrum contract names

* feat: verify remaining contracts

* chore: bump contract versions

* chore: dusable deploy flags
* refactor: improve readability of aave-like protocol operation resolution

* test: e2e smoke test aave v3 adjust up strategy

* v0.5.21-dma-v2-workers

* feat: update AdjustRiskDown op and copy placeholder for Op test

* feat: create adjust down operation util

* feat: update adjust down operation and op test

* chore: bump adjustdown aave v3 open name version

* refactor: Update operation name for adjusting risk up in AAVEV3Position

* v0.5.21-dma-v2-workers.7

* feat: prepare Optimism config and service registry names

* fix: deploy Swap contract and update contract names in Optimism config

* refactor: update contract names and deployment status

* chore: update optimism deploy config

* feat: Add TakeFlashloanBalancer and AaveV3WithdrawAuto to config

* feat: Update service registry names in optimism.conf.ts

* refactor: Update addresses in optimism.conf.ts and deploy.ts

* chore: deploy updated DMA to optimism

* chore: correct optimism config flags

* refactor: Update deployment configuration

* refactor(config): update constructor arguments for OperationExecutor

* refactor(deploy): re-enable SAFE logic to avoid adding entry to Service Registry

* chore: update registry name versions]

* feat: update versions for mainnet deployment

* feat: redeploy core contracts

* feat: update contract versions

* refactor: Rename SWAP constant in Arbitrum contract names

* feat: verify remaining contracts

* chore: bump contract versions

* chore: dusable deploy flags

* feat: update contract versions

* refactor: Rename SWAP constant in Arbitrum contract names

* feat: verify remaining contracts

* chore: bump contract versions

* chore: dusable deploy flags

* fix(deploy-configurations): Update contract names

* chore: Update contract names in contract-names.base.ts
* refactor: improve readability of aave-like protocol operation resolution

* test: e2e smoke test aave v3 adjust up strategy

* v0.5.21-dma-v2-workers

* feat: update AdjustRiskDown op and copy placeholder for Op test

* feat: create adjust down operation util

* feat: update adjust down operation and op test

* chore: bump adjustdown aave v3 open name version

* refactor: Update operation name for adjusting risk up in AAVEV3Position

* v0.5.21-dma-v2-workers.7

* feat: prepare Optimism config and service registry names

* fix: deploy Swap contract and update contract names in Optimism config

* refactor: update contract names and deployment status

* chore: update optimism deploy config

* feat: Add TakeFlashloanBalancer and AaveV3WithdrawAuto to config

* feat: Update service registry names in optimism.conf.ts

* refactor: Update addresses in optimism.conf.ts and deploy.ts

* chore: deploy updated DMA to optimism

* chore: correct optimism config flags

* refactor: Update deployment configuration

* refactor(config): update constructor arguments for OperationExecutor

* refactor(deploy): re-enable SAFE logic to avoid adding entry to Service Registry

* chore: update registry name versions]

* feat: update versions for mainnet deployment

* feat: redeploy core contracts

* feat: update contract versions

* refactor: Rename SWAP constant in Arbitrum contract names

* feat: verify remaining contracts

* chore: bump contract versions

* chore: dusable deploy flags

* feat: update contract versions

* refactor: Rename SWAP constant in Arbitrum contract names

* feat: verify remaining contracts

* chore: bump contract versions

* chore: dusable deploy flags

* fix(deploy-configurations): Update contract names

* refactor: clean up adjust risk up/down op defs

* refactor: Update operation names for AAVEV3 positions

* chore: bump adjust risk up/down op names
* v0.5.21-dma-v2-workers.9

* v0.1.16-dma-v2-workers.4
* chore: add AutoBot_V2 addresses for Arb + Opt

* v0.5.21-dma-v2-workers.10

* v0.1.16-dma-v2-workers.5
…rks (#613)

* chore: prepare mainnet deployment

* chore: prepare optimism deployment

* chore: prepare arbitrum deployment

* chore: prepare base deployment

* chore: update op names

* chore: commit optimism addresses

* chore: commit arbitrum addresses

* chore: deploy DMA mainnet + flip deploy flags

* chore: bump `addresses` and `library`

* chore: update base sr names

* chore: bump action names

* chore: bump operation and lib

---------

Co-authored-by: halaprix <[email protected]>
* chore: add deploy flags

* chore: prepare deployment config

* chore: commit new arb addresses

* chore: update AutomationBotV2 address in config

---------

Co-authored-by: halaprix <[email protected]>
* Aave V3 close op test

* Aave v3 close op with Auto specific actions

* chore: adjust tests

* chore: remove only

* chore: bump version

---------

Co-authored-by: halaprix <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants