Skip to content

Commit

Permalink
feat: add WithdrawAuto and correct dumb mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
zerotucks committed Apr 30, 2024
1 parent f403be7 commit aeb70cb
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.15;

import { Executable } from "../../common/Executable.sol";
import { UseStorageSlot, StorageSlot, Write, Read } from "../../../libs/UseStorageSlot.sol";
import { ServiceRegistry } from "../../../core/ServiceRegistry.sol";
import { Executable } from "../common/Executable.sol";
import { UseStorageSlot, StorageSlot, Write, Read } from "../../libs/UseStorageSlot.sol";
import { ServiceRegistry } from "../../core/ServiceRegistry.sol";
import { BorrowData } from "../../core/types/MorphoBlue.sol";
import { MORPHO_BLUE } from "../../core/constants/MorphoBlue.sol";
import { IMorpho } from "../../interfaces/morpho-blue/IMorpho.sol";
import { UseRegistry } from "../../libs/UseRegistry.sol";

/**
* @title Borrow | Morpho Blue Action contract
Expand All @@ -23,7 +24,7 @@ contract MorphoBlueBorrow is Executable, UseStorageSlot, UseRegistry {
function execute(bytes calldata data, uint8[] memory) external payable override {
BorrowData memory borrowData = parseInputs(data);

IMorpho morphoBlue = IMorpho(registry.getRegisteredService(MORPHO_BLUE));
IMorpho morphoBlue = IMorpho(getRegisteredService(MORPHO_BLUE));
morphoBlue.borrow(borrowData.marketParams, borrowData.amount, 0, address(this), address(this));

store().write(bytes32(borrowData.amount));
Expand Down
14 changes: 7 additions & 7 deletions packages/dma-contracts/contracts/actions/morpho-blue/Deposit.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.15;

import { Executable } from "../../common/Executable.sol";
import { UseStorageSlot, StorageSlot, Write, Read } from "../../../libs/UseStorageSlot.sol";
import { ServiceRegistry } from "../../../core/ServiceRegistry.sol";
import { Executable } from "../common/Executable.sol";
import { UseStorageSlot, StorageSlot, Write, Read } from "../../libs/UseStorageSlot.sol";
import { ServiceRegistry } from "../../core/ServiceRegistry.sol";
import { DepositData } from "../../core/types/MorphoBlue.sol";
import { MORPHO_BLUE } from "../../core/constants/MorphoBlue.sol";
import { IMorpho } from "../../interfaces/morpho-blue/IMorpho.sol";
import { UseRegistry } from "../../libs/UseRegistry.sol";

/**
* @title Deposit | Morpho Blue Action contract
Expand All @@ -16,7 +17,7 @@ contract MorphoBlueDeposit is Executable, UseStorageSlot, UseRegistry {
using Write for StorageSlot.TransactionStorage;
using Read for StorageSlot.TransactionStorage;

constructor(address _registry) UseStore(_registry) {}
constructor(address _registry) UseRegistry(ServiceRegistry(_registry)) {}

/**
* @param data Encoded calldata that conforms to the DepositData struct
Expand All @@ -27,15 +28,14 @@ contract MorphoBlueDeposit is Executable, UseStorageSlot, UseRegistry {

uint256 mappedDepositAmount = store().readUint(
bytes32(depositData.amount),
paramsMap[1],
address(this)
paramsMap[1]
);

uint256 actualDepositAmount = depositData.sumAmounts
? mappedDepositAmount + depositData.amount
: mappedDepositAmount;

IMorpho morphoBlue = IMorpho(registry.getRegisteredService(MORPHO_BLUE));
IMorpho morphoBlue = IMorpho(getRegisteredService(MORPHO_BLUE));
morphoBlue.supplyCollateral(
depositData.marketParams,
actualDepositAmount,
Expand Down
13 changes: 7 additions & 6 deletions packages/dma-contracts/contracts/actions/morpho-blue/Payback.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.15;

import { Executable } from "../../common/Executable.sol";
import { UseStorageSlot, StorageSlot, Write, Read } from "../../../libs/UseStorageSlot.sol";
import { ServiceRegistry } from "../../../core/ServiceRegistry.sol";
import { Executable } from "../common/Executable.sol";
import { UseStorageSlot, StorageSlot, Write, Read } from "../../libs/UseStorageSlot.sol";
import { ServiceRegistry } from "../../core/ServiceRegistry.sol";
import { PaybackData } from "../../core/types/MorphoBlue.sol";
import { MORPHO_BLUE } from "../../core/constants/MorphoBlue.sol";
import { Id, IMorpho, MarketParams } from "../../interfaces/morpho-blue/IMorpho.sol";
import { MarketParamsLib } from "../../libs/morpho-blue/MarketParamsLib.sol";
import { MorphoLib } from "../../libs/morpho-blue/MorphoLib.sol";
import { SharesMathLib } from "../../libs/morpho-blue/SharesMathLib.sol";
import { UseRegistry } from "../../libs/UseRegistry.sol";

/**
* @title Payback | MorphoBlue Action contract
Expand All @@ -22,7 +23,7 @@ contract MorphoBluePayback is Executable, UseStorageSlot, UseRegistry {
using MorphoLib for IMorpho;
using SharesMathLib for uint256;

constructor(address _registry) UseStore(_registry) {}
constructor(address _registry) UseRegistry(ServiceRegistry(_registry)) {}

/**
* @param data Encoded calldata that conforms to the PaybackData struct
Expand All @@ -31,9 +32,9 @@ contract MorphoBluePayback is Executable, UseStorageSlot, UseRegistry {
function execute(bytes calldata data, uint8[] memory paramsMap) external payable override {
PaybackData memory paybackData = parseInputs(data);

paybackData.amount = store().readUint(bytes32(paybackData.amount), paramsMap[0], address(this));
paybackData.amount = store().readUint(bytes32(paybackData.amount), paramsMap[0]);

IMorpho morphoBlue = IMorpho(registry.getRegisteredService(MORPHO_BLUE));
IMorpho morphoBlue = IMorpho(getRegisteredService(MORPHO_BLUE));

address onBehalf = paybackData.onBehalf == address(0) ? address(this) : paybackData.onBehalf;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.15;

import { Executable } from "../../common/Executable.sol";
import { UseStorageSlot, StorageSlot, Write, Read } from "../../../libs/UseStorageSlot.sol";
import { ServiceRegistry } from "../../../core/ServiceRegistry.sol";
import { Executable } from "../common/Executable.sol";
import { UseStorageSlot, StorageSlot, Write, Read } from "../../libs/UseStorageSlot.sol";
import { ServiceRegistry } from "../../core/ServiceRegistry.sol";
import { WithdrawData } from "../../core/types/MorphoBlue.sol";
import { MORPHO_BLUE } from "../../core/constants/MorphoBlue.sol";
import { IMorpho } from "../../interfaces/morpho-blue/IMorpho.sol";
import { UseRegistry } from "../../libs/UseRegistry.sol";

/**
* @title Withdraw | MorphoBlue Action contract
* @notice Withdraw collateral from Morpho Blue's lending pool
* with the amount to withdraw being read from the proxies t/x storage slot
*/
contract MorphoBlueWithdraw is Executable, UseStorageSlot, UseRegistry {
using Write for StorageSlot.TransactionStorage;

constructor(address _registry) UseStore(_registry) {}
constructor(address _registry) UseRegistry(ServiceRegistry(_registry)) {}

/**
* @param data Encoded calldata that conforms to the WithdrawData struct
*/
function execute(bytes calldata data, uint8[] memory) external payable override {
WithdrawData memory withdrawData = parseInputs(data);

IMorpho morphoBlue = IMorpho(registry.getRegisteredService(MORPHO_BLUE));
IMorpho morphoBlue = IMorpho(getRegisteredService(MORPHO_BLUE));
morphoBlue.withdrawCollateral(
withdrawData.marketParams,
withdrawData.amount,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.15;

import { Executable } from "../common/Executable.sol";
import { UseStorageSlot, StorageSlot, Write, Read } from "../../libs/UseStorageSlot.sol";
import { ServiceRegistry } from "../../core/ServiceRegistry.sol";
import { WithdrawData } from "../../core/types/MorphoBlue.sol";
import { MORPHO_BLUE } from "../../core/constants/MorphoBlue.sol";
import { IMorpho } from "../../interfaces/morpho-blue/IMorpho.sol";
import { UseRegistry } from "../../libs/UseRegistry.sol";

/**
* @title Withdraw | MorphoBlue Action contract
* @notice Withdraw collateral from Morpho Blue's lending pool
* with the amount to withdraw being read from an OperationStorage slot
*/
contract MorphoBlueWithdrawAuto is Executable, UseStorageSlot, UseRegistry {
using Write for StorageSlot.TransactionStorage;
using Read for StorageSlot.TransactionStorage;

constructor(address _registry) UseRegistry(ServiceRegistry(_registry)) {}

/**
* @param data Encoded calldata that conforms to the WithdrawData struct
*/
function execute(bytes calldata data, uint8[] memory paramsMap) external payable override {
WithdrawData memory withdraw = parseInputs(data);

uint256 mappedWithdrawAmount = store().readUint(
bytes32(0),
paramsMap[0]
);

IMorpho morphoBlue = IMorpho(getRegisteredService(MORPHO_BLUE));
morphoBlue.withdrawCollateral(
withdraw.marketParams,
mappedWithdrawAmount,
address(this),
withdraw.to
);

store().write(bytes32(mappedWithdrawAmount));
}

function parseInputs(bytes memory _callData) public pure returns (WithdrawData memory params) {
return abi.decode(_callData, (WithdrawData));
}
}

1 change: 1 addition & 0 deletions packages/dma-contracts/contracts/core/types/MorphoBlue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ struct PaybackData {
MarketParams marketParams;
uint256 amount;
address onBehalf;
bool paybackAll;
}
Loading

0 comments on commit aeb70cb

Please sign in to comment.