Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
69 changes: 26 additions & 43 deletions contracts/Vault/VaultDiamond.sol
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;

/******************************************************************************\
* Author: Nick Mudge <nick@perfectabstractions.com> (https://twitter.com/mudgen)
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
*
* Implementation of a diamond.
/******************************************************************************/
/**
* Implementation of a diamond.
* /*****************************************************************************
*/

import {LibDiamond} from "./libraries/LibDiamond.sol";
import {IDiamondCut} from "../interfaces/IDiamondCut.sol";

import {FacetAndSelectorData,DMSData} from "./libraries/LibLayoutSilo.sol";
import {FacetAndSelectorData, DMSData} from "./libraries/LibLayoutSilo.sol";

import {LibStorageBinder} from "./libraries/LibStorageBinder.sol";

Expand All @@ -20,42 +18,31 @@ import {LibStorageBinder} from "./libraries/LibStorageBinder.sol";
import "../interfaces/IVaultDiamond.sol";

contract VaultDiamond {
bool _init;
address public vaultFactoryDiamond;

constructor() payable {
address _contractOwner = tx.origin;
LibDiamond.setVaultOwner(_contractOwner);
}

function init(address _diamondCutFacet, address _backup) public {
VaultData storage vaultData = LibStorageBinder
._bindAndReturnVaultStorage();
assert(!_init);
assert(
msg.sender == LibDiamond.vaultOwner() ||
tx.origin == LibDiamond.vaultOwner()
);
// Add the diamondCut external function from the diamondCutFacet
IDiamondCut.FacetCut[] memory cut = new IDiamondCut.FacetCut[](1);
bytes4[] memory functionSelectors = new bytes4[](2);
functionSelectors[0] = IDiamondCut.diamondCut.selector;
functionSelectors[1] = IVaultDiamond.tempOwner.selector;
cut[0] = IDiamondCut.FacetCut({
facetAddress: _diamondCutFacet,
action: IDiamondCut.FacetCutAction.Add,
functionSelectors: functionSelectors
});

LibDiamond.diamondCut(cut, address(0), "");
vaultData.backupAddress = _backup;
_init = true;
/// @notice sets selector and token module at deployment
constructor(
IDiamondCut.FacetCut[] memory _selectorModule,
IDiamondCut.FacetCut[] memory _tokenModule,
address _vaultOwner
) payable {
LibDiamond.diamondCut(_selectorModule, address(0), "");
LibDiamond.diamondCut(_tokenModule, address(0), "");
//set module installation record to true
FacetAndSelectorData storage fsData = LibStorageBinder._bindAndReturnFacetStorage();
fsData.activeModule["Selector"] = true;
fsData.activeModule["Token"] = true;
fsData.activeModules.push("Selector");
fsData.activeModules.push("Token");
LibDiamond.setVaultOwner(_vaultOwner);
vaultFactoryDiamond = msg.sender;
}

// Find facet for function that is called and execute the
// function if a facet is found and return any value.

fallback() external payable {
FacetAndSelectorData storage fsData = LibStorageBinder
._bindAndReturnFacetStorage();
FacetAndSelectorData storage fsData = LibStorageBinder._bindAndReturnFacetStorage();

// get facet from function selector
address facet = fsData.selectorToFacetAndPosition[msg.sig].facetAddress;
Expand All @@ -70,12 +57,8 @@ contract VaultDiamond {
returndatacopy(0, 0, returndatasize())
// return any return value or error back to the caller
switch result
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
case 0 { revert(0, returndatasize()) }
default { return(0, returndatasize()) }
}
}

Expand Down
32 changes: 26 additions & 6 deletions contracts/Vault/facets/DMSFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ contract DMSFacet {
uint256 amount;
}



/// @notice checks throgh the vault and return dataa associated with it
function inspectVault() public view returns (VaultInfo memory info) {
DMSData storage vaultData = LibStorageBinder._bindAndReturnDMSStorage();
FacetAndSelectorData storage fsData = LibStorageBinder._bindAndReturnFacetStorage();
Expand All @@ -63,6 +62,7 @@ contract DMSFacet {
info.inheritors = vaultData.inheritors;
}

/// @notice returns all ethers that has been allocated from the vault
function allEtherAllocations() public view returns (AllInheritorEtherAllocs[] memory eAllocs) {
DMSData storage vaultData = LibStorageBinder._bindAndReturnDMSStorage();
uint256 count = vaultData.inheritors.length;
Expand All @@ -73,6 +73,7 @@ contract DMSFacet {
}
}

/// @notice returns all ether allocates to _inheritor
function inheritorEtherAllocation(address _inheritor) public view returns (uint256 _allocatedEther) {
DMSData storage vaultData = LibStorageBinder._bindAndReturnDMSStorage();
if (!LibDMSGuards._anInheritor(_inheritor)) {
Expand All @@ -81,21 +82,25 @@ contract DMSFacet {
_allocatedEther = vaultData.inheritorWeishares[_inheritor];
}

// @notice retuns the amount of all ether that has been allocated in the vault
function getAllocatedEther() public view returns (uint256) {
return LibDMS.getCurrentAllocatedEth();
}

/// @notice returns the amoounf of free(unallocated) ether in the vault
function getUnallocatedEther() public view returns (uint256 unallocated_) {
uint256 currentBalance = address(this).balance;
if (currentBalance > 0) {
unallocated_ = currentBalance - LibDMS.getCurrentAllocatedEth();
}
}

/// @notice returns the current ether balance of the vault
function etherBalance() public view returns (uint256) {
return address(this).balance;
}

/// @notice gets the aloocated ERC20 tokens to inheritor
function getAllocatedERC20Tokens(address _inheritor) public view returns (AllocatedERC20Tokens[] memory tAllocs) {
LibDMSGuards._activeInheritor(_inheritor);
DMSData storage vaultData = LibStorageBinder._bindAndReturnDMSStorage();
Expand All @@ -110,19 +115,21 @@ contract DMSFacet {
}
}

/// @notice returns the amount of a token aloocated to an inheritor
function inheritorERC20TokenAllocation(address _inheritor, address _token) public view returns (uint256) {
DMSData storage vaultData = LibStorageBinder._bindAndReturnDMSStorage();
return vaultData.inheritorTokenShares[_inheritor][_token];
}

/// @notice returns free(unallocated) ERC20 tokens in the vault
function getUnallocatedTokens(address _token) public view returns (uint256 unallocated_) {
uint256 bal = IERC20(_token).balanceOf(address(this));
uint256 allocated = LibDMS.getCurrentAllocatedTokens(_token);
if (bal > allocated) {
unallocated_ = bal - allocated;
}
}

/// @notice returns allocated ERC721 tokens to an inheritor
function getAllocatedERC721Tokens(address _inheritor)
public
view
Expand All @@ -141,18 +148,21 @@ contract DMSFacet {
}
}

/// @notice returns the amount of an ERC721 token allocated to an inheritor
function getAllocatedERC721TokenIds(address _inheritor, address _token) external view returns (uint256[] memory) {
DMSData storage vaultData = LibStorageBinder._bindAndReturnDMSStorage();
LibDMSGuards._activeInheritor(_inheritor);
return vaultData.inheritorAllocatedTokenIds[_inheritor][_token];
}

/// @notice returns ERC721 token addresses allocated to an inheritor
function getAllocatedERC721TokenAddresses(address _inheritor) public view returns (address[] memory) {
DMSData storage vaultData = LibStorageBinder._bindAndReturnDMSStorage();
LibDMSGuards._activeInheritor(_inheritor);
return vaultData.inheritorAllocatedERC721TokenAddresses[_inheritor];
}

/// @notice returns the amount of an ERC1155 token allocated to an inheritor
function getAllocatedERC1155Tokens(address _token, address _inheritor)
public
view
Expand All @@ -171,6 +181,7 @@ contract DMSFacet {
}
}

/// @notice returns all ERC1155 tokens allocated to an inheritor
function getAllAllocatedERC1155Tokens(address _inheritor)
public
view
Expand All @@ -192,6 +203,7 @@ contract DMSFacet {
}
}

/// @notice retuns free(unallocated) ERC1155 tokens in the vault
function getUnallocatedERC115Tokens(address _token, uint256 _tokenId) public view returns (uint256 remaining_) {
uint256 allocated = LibDMS.getCurrentAllocated1155tokens(_token, _tokenId);
uint256 available = IERC1155(_token).balanceOf(address(this), _tokenId);
Expand All @@ -204,33 +216,38 @@ contract DMSFacet {
///WRITE FUNCTIONS///
////////////////////
//note: owner restriction is in external fns
/// @notice adds inheritors and weishares to the vault
function addInheritors(address[] calldata _newInheritors, uint256[] calldata _weiShare) external {
LibGuards._onlyVaultOwner();
LibDMS._addInheritors(_newInheritors, _weiShare);
}

/// @notice removes inheritors from the vault
function removeInheritors(address[] calldata _inheritors) external {
LibGuards._onlyVaultOwner();
LibDMS._removeInheritors(_inheritors);
}

/// @notice allocate ether to inheritors
function allocateEther(address[] calldata _inheritors, uint256[] calldata _ethShares) external {
LibGuards._onlyVaultOwner();
LibDMS._allocateEther(_inheritors, _ethShares);
}

/// @notice allocate ERC20 tokens to inheritors
function allocateERC20Tokens(address token, address[] calldata _inheritors, uint256[] calldata _shares) external {
LibGuards._onlyVaultOwner();
LibDMS._allocateERC20Tokens(token, _inheritors, _shares);
}

/// @notice allocate ERC721 tokens to inheritors
function allocateERC721Tokens(address token, address[] calldata _inheritors, uint256[] calldata _tokenIDs)
external
{
LibGuards._onlyVaultOwner();
LibDMS._allocateERC721Tokens(token, _inheritors, _tokenIDs);
}

/// @notice allocate ERC1155 tokens to inheritors
function allocateERC1155Tokens(
address token,
address[] calldata _inheritors,
Expand All @@ -241,26 +258,29 @@ contract DMSFacet {
LibDMS._allocateERC1155Tokens(token, _inheritors, _tokenIDs, _amounts);
}

/// @notice transfer vault ownership to _newVaultOwner
function transferOwnership(address _newVaultOwner) public {
LibGuards._onlyVaultOwner();
LibDMS._transferOwnerShip(_newVaultOwner);
}

/// @notice transfer vault backup to _newBackupAddress
function transferBackup(address _newBackupAddress) public {
LibDMSGuards._onlyVaultOwnerOrBackup();
LibDMS._transferBackup(_newBackupAddress);
}

//CLAIMS
/// @notice allow backup address to claim ownership
function claimOwnership(address _newBackupAddress) public {
LibDMSGuards._enforceIsBackupAddress();
LibDMS._claimOwnership(_newBackupAddress);
}

/// @notice allow inheritor to claim all allocated assets
function claimAllAllocations() external {
LibDMS._claimAll();
}

/// @notice pings vault and resets inactivity timer
function ping() external {
LibGuards._onlyVaultOwner();
LibDMS._ping();
Expand Down
50 changes: 22 additions & 28 deletions contracts/Vault/facets/DiamondCutFacet.sol
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;

/******************************************************************************\
* Author: Nick Mudge <nick@perfectabstractions.com> (https://twitter.com/mudgen)
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
/******************************************************************************/
/**
* \
* Author: Nick Mudge <nick@perfectabstractions.com> (https://twitter.com/mudgen)
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
* /*****************************************************************************
*/

import { IDiamondCut } from "../../interfaces/IDiamondCut.sol";
import {IDiamondCut} from "../../interfaces/IDiamondCut.sol";

import { LibErrors } from "../libraries/LibErrors.sol";
import { LibDiamond } from "../libraries/LibDiamond.sol";
import {LibErrors} from "../libraries/LibErrors.sol";
import {LibDiamond} from "../libraries/LibDiamond.sol";
import "../libraries/LibLayoutSilo.sol";
import "../libraries/LibStorageBinder.sol";

contract DiamondCutFacet is IDiamondCut {
/// @notice Add/replace/remove any number of functions and optionally execute
/// a function with delegatecall
/// @param _diamondCut Contains the facet addresses and function selectors
/// @param _init The address of the contract or facet to execute _calldata
/// @param _calldata A function call, including function selector and arguments
/// _calldata is executed with delegatecall on _init
function diamondCut(
FacetCut[] calldata _diamondCut,
address _init,
bytes calldata _calldata
) external override {
// restrict upgrades to VaultFactoryDiamond only
if (msg.sender !=IVaultDiamond(address(this)).vaultFactoryDiamond()) revert LibErrors.NoPermissions();
LibDiamond.diamondCut(_diamondCut, _init, _calldata);
}
import {IVaultDiamond} from "../../interfaces/IVaultDiamond.sol";

//temp call made from factory to confirm ownership
function tempOwner() public view returns (address owner_) {
VaultData storage vaultData=LibStorageBinder._bindAndReturnVaultStorage();
owner_ = vaultData.vaultOwner;
}
contract DiamondCutFacet is IDiamondCut {
/// @notice Add/replace/remove any number of functions and optionally execute
/// a function with delegatecall
/// @param _diamondCut Contains the facet addresses and function selectors
/// @param _init The address of the contract or facet to execute _calldata
/// @param _calldata A function call, including function selector and arguments
/// _calldata is executed with delegatecall on _init
function diamondCut(FacetCut[] calldata _diamondCut, address _init, bytes calldata _calldata) external override {
// restrict upgrades to VaultFactoryDiamond only
// if (msg.sender !=IVaultDiamond(address(this)).vaultFactoryDiamond()) revert LibErrors.NoPermissions();
LibDiamond.diamondCut(_diamondCut, _init, _calldata);
}
}
Loading