-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: multiple creditAccounts per user
- Loading branch information
Showing
24 changed files
with
1,228 additions
and
1,300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
// Gearbox Protocol. Generalized leverage for DeFi protocols | ||
// (c) Gearbox Holdings, 2022 | ||
pragma solidity ^0.8.17; | ||
|
||
import {ContractsRegisterTrait} from "../traits/ContractsRegisterTrait.sol"; | ||
import {Clones} from "@openzeppelin/contracts/proxy/Clones.sol"; | ||
|
||
import {CreditAccount} from "../credit/CreditAccount.sol"; | ||
import {ACLTrait} from "../traits/ACLTrait.sol"; | ||
|
||
import {IAccountFactory} from "../interfaces/IAccountFactory.sol"; | ||
|
||
// EXCEPTIONS | ||
import "../interfaces/IExceptions.sol"; | ||
|
||
/// @title Disposable credit accounts factory | ||
contract AccountFactoryV2 is IAccountFactory, ACLTrait, ContractsRegisterTrait { | ||
/// @dev Address of master credit account for cloning | ||
mapping(address => address) public masterCreditAccounts; | ||
|
||
/// @dev Contract version | ||
uint256 public constant version = 3_00; | ||
|
||
error MasterCreditAccountAlreadyDeployed(); | ||
|
||
/// @param addressProvider Address of address repository | ||
constructor(address addressProvider) ACLTrait(addressProvider) ContractsRegisterTrait(addressProvider) {} | ||
|
||
/// @dev Provides a new credit account to a Credit Manager | ||
/// @return creditAccount Address of credit account | ||
function takeCreditAccount(uint256, uint256) external override returns (address creditAccount) { | ||
address masterCreditAccount = _getMasterCreditAccountOrRevert(); | ||
// Create a new credit account if there are none in stock | ||
creditAccount = Clones.clone(masterCreditAccount); // T:[AF-2] | ||
|
||
// emit InitializeCreditAccount(result, msg.sender); // T:[AF-5] | ||
} | ||
|
||
function returnCreditAccount(address usedAccount) external override { | ||
// Do nothing for disposable CA | ||
} | ||
|
||
// CONFIGURATION | ||
|
||
function addCreditManager(address creditManager) | ||
external | ||
configuratorOnly | ||
registeredCreditManagerOnly(creditManager) | ||
{ | ||
if (masterCreditAccounts[msg.sender] != address(0)) { | ||
revert MasterCreditAccountAlreadyDeployed(); | ||
} | ||
|
||
masterCreditAccounts[msg.sender] = address(new CreditAccount(creditManager)); | ||
} | ||
|
||
function _getMasterCreditAccountOrRevert() internal view returns (address masterCA) { | ||
masterCA = masterCreditAccounts[msg.sender]; | ||
if (masterCA == address(0)) { | ||
revert CallerNotCreditManagerException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
// Gearbox Protocol. Generalized leverage for DeFi protocols | ||
// (c) Gearbox Holdings, 2022 | ||
pragma solidity ^0.8.10; | ||
|
||
import {Address} from "@openzeppelin/contracts/utils/Address.sol"; | ||
|
||
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | ||
|
||
import "../interfaces/IExceptions.sol"; | ||
|
||
/// @title Credit Account | ||
/// @notice Implements generic credit account logic: | ||
/// - Holds collateral assets | ||
/// - Stores general parameters: borrowed amount, cumulative index at open and block when it was initialized | ||
/// - Transfers assets | ||
/// - Executes financial orders by calling connected protocols on its behalf | ||
/// | ||
/// More: https://dev.gearbox.fi/developers/credit/credit_account | ||
contract CreditAccount { | ||
using SafeERC20 for IERC20; | ||
using Address for address; | ||
/// @dev Address of the currently connected Credit Manager | ||
|
||
address public immutable creditManager; | ||
|
||
// Contract version | ||
uint256 public constant version = 3_00; | ||
|
||
/// @dev Restricts operations to the connected Credit Manager only | ||
modifier creditManagerOnly() { | ||
if (msg.sender != creditManager) { | ||
revert CallerNotCreditManagerException(); | ||
} | ||
_; | ||
} | ||
|
||
constructor(address _creditManager) { | ||
creditManager = _creditManager; | ||
} | ||
|
||
/// @dev Transfers tokens from the credit account to a provided address. Restricted to the current Credit Manager only. | ||
/// @param token Token to be transferred from the Credit Account. | ||
/// @param to Address of the recipient. | ||
/// @param amount Amount to be transferred. | ||
function safeTransfer(address token, address to, uint256 amount) | ||
external | ||
creditManagerOnly // T:[CA-2] | ||
{ | ||
IERC20(token).safeTransfer(to, amount); // T:[CA-6] | ||
} | ||
|
||
/// @dev Executes a call to a 3rd party contract with provided data. Restricted to the current Credit Manager only. | ||
/// @param destination Contract address to be called. | ||
/// @param data Data to call the contract with. | ||
function execute(address destination, bytes memory data) external creditManagerOnly returns (bytes memory) { | ||
return destination.functionCall(data); // T: [CM-48] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.