Skip to content

Commit

Permalink
Massive use of Proxy Delegates for reading, ERC721 Wallet, multiple o…
Browse files Browse the repository at this point in the history
…neTime Proposals and best Proposal Error Catching
  • Loading branch information
vasapower committed Jun 1, 2020
1 parent 3181516 commit 95990d0
Show file tree
Hide file tree
Showing 14 changed files with 407 additions and 222 deletions.
7 changes: 7 additions & 0 deletions IERC721.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pragma solidity ^0.6.0;

interface IERC721 {
function ownerOf(uint256 _tokenId) external view returns (address);
function transferFrom(address _from, address _to, uint256 _tokenId) external payable;
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
}
5 changes: 5 additions & 0 deletions IERC721Receiver.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma solidity ^0.6.0;

interface IERC721Receiver {
function onERC721Received(address operator, address from, uint256 tokenId, bytes calldata data) external returns (bytes4);
}
9 changes: 6 additions & 3 deletions IMVDFunctionalitiesManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ interface IMVDFunctionalitiesManager {
function isAuthorizedFunctionality(address functionality) external view returns(bool);
function setCallingContext(address location) external returns(bool);
function clearCallingContext() external;
function getFunctionalityData(string calldata codeName) external view returns(address, uint256, string memory);
function getFunctionalityData(string calldata codeName) external view returns(address, uint256, string memory, address, uint256);
function hasFunctionality(string calldata codeName) external view returns(bool);
function getFunctionalitiesAmount() external view returns(uint256);
function functionalitiesToJSON() external view returns(string memory functionsJSONArray);
function functionalitiesToJSON() external view returns(string memory);
function functionalitiesToJSON(uint256 start, uint256 l) external view returns(string memory functionsJSONArray);
function functionalityNames() external view returns(string memory);
function functionalityNames(uint256 start, uint256 l) external view returns(string memory functionsJSONArray);
function functionalityToJSON(string calldata codeName) external view returns(string memory);

function preConditionCheck(string calldata codeName, bytes calldata data, uint8 submitable, address sender, uint256 value) external view returns(address location, bytes memory payload);

function setupFunctionality(address proposalAddress) external;
function setupFunctionality(address proposalAddress) external returns (bool);
}
4 changes: 3 additions & 1 deletion IMVDFunctionalityProposal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.6.0;
interface IMVDFunctionalityProposal {

function init(string calldata codeName, address location, string calldata methodSignature, string calldata returnAbiParametersArray, string calldata replaces, address proxy) external;
function setCollateralData(bool emergency, address sourceLocation, uint256 sourceLocationId, bool submitable, bool isInternal, bool needsSender, address proposer) external;
function setCollateralData(bool emergency, address sourceLocation, uint256 sourceLocationId, bool submitable, bool isInternal, bool needsSender, address proposer, uint256 votesHardCap) external;

function getProxy() external view returns(address);
function getCodeName() external view returns(string memory);
Expand All @@ -20,6 +20,8 @@ interface IMVDFunctionalityProposal {
function getProposer() external view returns(address);
function getSurveyEndBlock() external view returns(uint256);
function getSurveyDuration() external view returns(uint256);
function isVotesHardCapReached() external view returns(bool);
function getVotesHardCapToReach() external view returns(uint256);
function toJSON() external view returns(string memory);
function getVote(address addr) external view returns(uint256 accept, uint256 refuse);
function getVotes() external view returns(uint256, uint256);
Expand Down
2 changes: 0 additions & 2 deletions IMVDFunctionalityProposalManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ pragma solidity ^0.6.0;
interface IMVDFunctionalityProposalManager {
function newProposal(string calldata codeName, address location, string calldata methodSignature, string calldata returnAbiParametersArray, string calldata replaces) external returns(address);
function checkProposal(address proposalAddress) external;
function disableProposal(address proposalAddress) external;
function getProxy() external view returns (address);
function setProxy() external;
function isValidProposal(address proposal) external view returns (bool);
function getPendingProposal(string calldata codeName) external view returns(address proposalAddress, bool isReallyPending);
}
32 changes: 10 additions & 22 deletions IMVDProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,39 @@ pragma solidity ^0.6.0;

interface IMVDProxy {

function init(address votingTokenAddress, address stateHolderAddress, address functionalityModelsManagerAddress, address functionalityProposalManagerAddress, address functionalitiesManagerAddress) external;
function init(address votingTokenAddress, address functionalityProposalManagerAddress, address stateHolderAddress, address functionalityModelsManagerAddress, address functionalitiesManagerAddress, address walletAddress) external;

function getDelegates() external view returns(address,address,address,address,address,address);
function getToken() external view returns(address);
function setToken(address newAddress) external;
function getStateHolderAddress() external view returns(address);
function setStateHolderAddress(address newAddress) external;
function getMVDFunctionalityProposalManagerAddress() external view returns(address);
function setMVDFunctionalityProposalManagerAddress(address newAddress) external;
function getStateHolderAddress() external view returns(address);
function getMVDFunctionalityModelsManagerAddress() external view returns(address);
function setMVDFunctionalityModelsManagerAddress(address newAddress) external;
function getMVDFunctionalitiesManagerAddress() external view returns(address);
function setMVDFunctionalitiesManagerAddress(address newAddress) external;
function getMVDWalletAddress() external view returns(address);
function setDelegate(uint256 position, address newAddress) external returns(address oldAddress);
function changeProxy(address newAddress, bytes calldata initPayload) external;
function getFunctionalitiesAmount() external view returns(uint256);
function isValidProposal(address proposal) external view returns (bool);
function isValidFunctionality(address functionality) external view returns(bool);
function isAuthorizedFunctionality(address functionality) external view returns(bool);
function getFunctionalityAddress(string calldata codeName) external view returns(address);
function hasFunctionality(string calldata codeName) external view returns(bool);
function functionalitiesToJSON() external view returns(string memory functionsJSONArray);
function functionalitiesToJSON(uint256 start, uint256 l) external view returns(string memory functionsJSONArray);
function getPendingProposal(string calldata codeName) external view returns(address proposalAddress, bool isPending);
function newProposal(string calldata codeName, bool emergency, address sourceLocation, uint256 sourceLocationId, address location, bool submitable, string calldata methodSignature, string calldata returnParametersJSONArray, bool isInternal, bool needsSender, string calldata replaces) external returns(address proposalAddress);
function startProposal(address proposalAddress) external;
function disableProposal(address proposalAddress) external;
function transfer(address receiver, uint256 value, address token) external;
function transfer721(address receiver, uint256 tokenId, bytes calldata data, bool safe, address token) external;
function setProposal() external;
function read(string calldata codeName, bytes calldata data) external view returns(bytes memory returnData);
function submit(string calldata codeName, bytes calldata data) external payable returns(bytes memory returnData);
function callFromManager(address location, bytes calldata payload) external returns(bool, bytes memory);
function emitFromManager(string calldata codeName, uint256 position, address proposal, string calldata replaced, address location, bool submitable, string calldata methodSignature, bool isInternal, bool needsSender, address proposalAddress, uint256 replacedPosition) external;
function emitFromManager(string calldata codeName, address proposal, string calldata replaced, address replacedSourceLocation, uint256 replacedSourceLocationId, address location, bool submitable, string calldata methodSignature, bool isInternal, bool needsSender, address proposalAddress) external;

function emitEvent(string calldata eventSignature, bytes calldata firstIndex, bytes calldata secondIndex, bytes calldata data) external;

event TokenChanged(address indexed oldAddress, address indexed newAddress);
event MVDFunctionalityProposalManagerChanged(address indexed oldAddress, address indexed newAddress);
event MVDFunctionalityModelsManagerChanged(address indexed oldAddress, address indexed newAddress);
event MVDFunctionalitiesManagerChanged(address indexed oldAddress, address indexed newAddress);
event StateHolderChanged(address indexed oldAddress, address indexed newAddress);
event ProxyChanged(address indexed newAddress);
event DelegateChanged(uint256 position, address indexed oldAddress, address indexed newAddress);

event PaymentReceived(address indexed sender, uint256 value);
event Proposal(address proposal);
event ProposalCheck(address indexed proposal);
event ProposalSet(address indexed proposal, bool success);
event FunctionalitySet(string indexed codeName, uint256 position, address proposal, string indexed replaced, address replacedLocation, bool replacedWasSubmitable, string replacedMethodSignature, bool replacedWasInternal, bool replacedNeededSender, address replacedProposal, uint256 replacedPosition);
event FunctionalitySet(string codeName, address indexed proposal, string replaced, address replacedSourceLocation, uint256 replacedSourceLocationId, address indexed replacedLocation, bool replacedWasSubmitable, string replacedMethodSignature, bool replacedWasInternal, bool replacedNeededSender, address indexed replacedProposal);

event Event(string indexed key, bytes32 indexed firstIndex, bytes32 indexed secondIndex, bytes data);
}
18 changes: 18 additions & 0 deletions IMVDWallet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pragma solidity ^0.6.0;

interface IMVDWallet {

function getProxy() external view returns (address);

function setProxy() external;

function setNewWallet(address payable newWallet, address tokenAddress) external;

function transfer(address receiver, uint256 value, address tokenAddress) external;

function transfer(address receiver, uint256 tokenId, bytes calldata data, bool safe, address token) external;

function flushToNewWallet(address token) external;

function flush721ToNewWallet(uint256 tokenId, bytes calldata data, bool safe, address tokenAddress) external;
}
55 changes: 42 additions & 13 deletions MVDFunctionalitiesManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -171,28 +171,48 @@ contract MVDFunctionalitiesManager is IMVDFunctionalitiesManager, CommonUtilitie
return _callingContext != address(0) && (_functionalityCount[functionality] > 0 || _callingContext == functionality);
}

function getFunctionalityData(string memory codeName) public override view returns(address, uint256, string memory) {
function getFunctionalityData(string memory codeName) public override view returns(address, uint256, string memory, address, uint256) {
Functionality memory functionality = _functionalities[_indexes[codeName]];
return (compareStrings(codeName, functionality.codeName) && functionality.active ? functionality.location : address(0), functionality.position, functionality.methodSignature);
return (compareStrings(codeName, functionality.codeName) && functionality.active ? functionality.location : address(0), functionality.position, functionality.methodSignature, functionality.sourceLocation, functionality.sourceLocationId);
}

function hasFunctionality(string memory codeName) public override view returns(bool) {
Functionality memory functionality = _functionalities[_indexes[codeName]];
return compareStrings(codeName, functionality.codeName) && functionality.active;
}

function functionalitiesToJSON() public override view returns(string memory functionsJSONArray) {
function functionalitiesToJSON() public override view returns(string memory) {
return functionalitiesToJSON(0, _functionalities.length);
}

function functionalitiesToJSON(uint256 start, uint256 l) public override view returns(string memory functionsJSONArray) {
uint256 length = start + l;
functionsJSONArray = "[";
for(uint256 i = start; i < length; i++) {
functionsJSONArray = !_functionalities[i].active ? functionsJSONArray : string(abi.encodePacked(functionsJSONArray, toJSON(_functionalities[i]), i == length - (_functionalities[i].active ? 1 : 0) ? "]" : ","));
functionsJSONArray = !_functionalities[i].active ? functionsJSONArray : string(abi.encodePacked(functionsJSONArray, toJSON(_functionalities[i]), i == length - (_functionalities[i].active ? 1 : 0) ? "" : ","));
length += _functionalities[i].active ? 0 : 1;
length = length > _functionalities.length ? _functionalities.length : length;
}
functionsJSONArray = string(abi.encodePacked(functionsJSONArray, "]"));
}

function functionalityNames() public override view returns(string memory) {
return functionalityNames(0, _functionalities.length);
}

function functionalityNames(uint256 start, uint256 l) public override view returns(string memory functionsJSONArray) {
uint256 length = start + l;
functionsJSONArray = "[";
for(uint256 i = start; i < length; i++) {
functionsJSONArray = !_functionalities[i].active ? functionsJSONArray : string(abi.encodePacked(functionsJSONArray, '"', _functionalities[i].codeName, '"', i == length - (_functionalities[i].active ? 1 : 0) ? "" : ","));
length += _functionalities[i].active ? 0 : 1;
length = length > _functionalities.length ? _functionalities.length : length;
}
functionsJSONArray = string(abi.encodePacked(functionsJSONArray, "]"));
}

function functionalityToJSON(string memory codeName) public override view returns(string memory) {
return string(toJSON(_functionalities[_indexes[codeName]]));
}

function toJSON(Functionality memory func) private pure returns(bytes memory) {
Expand Down Expand Up @@ -229,7 +249,7 @@ contract MVDFunctionalitiesManager is IMVDFunctionalitiesManager, CommonUtilitie
_proxy = _proxy == address(0) ? msg.sender : address(0);
}

function setupFunctionality(address proposalAddress) public override {
function setupFunctionality(address proposalAddress) public override returns(bool result) {

require(_proxy == msg.sender, "Only Proxy can call This!");

Expand All @@ -240,18 +260,21 @@ contract MVDFunctionalitiesManager is IMVDFunctionalitiesManager, CommonUtilitie
string memory replaces = proposal.getReplaces();
bool hasReplaces = !compareStrings(replaces, "");

Functionality memory replacedFunctionality = _functionalities[_indexes[replaces]];

if(!hasCodeName && !hasReplaces) {
IMVDProxy(_proxy).callFromManager(_callingContext = proposal.getLocation(), abi.encodeWithSignature("callOneTime(address)", proposalAddress));
(result,) = IMVDProxy(_proxy).callFromManager(_callingContext = proposal.getLocation(), abi.encodeWithSignature("callOneTime(address)", proposalAddress));
_callingContext = address(0);
return;
return result;
}

Functionality memory replacedFunctionality = _functionalities[_indexes[replaces]];
uint256 position = hasReplaces ? replacedFunctionality.position : _functionalities.length;

if(hasReplaces) {
IMVDProxy(_proxy).callFromManager(_callingContext = replacedFunctionality.location, abi.encodeWithSignature("onStop(address)", proposalAddress));
(result,) = IMVDProxy(_proxy).callFromManager(_callingContext = replacedFunctionality.location, abi.encodeWithSignature("onStop(address)", proposalAddress));
_callingContext = address(0);
if(!result) {
revert("onStop failed!");
}
}

replacedFunctionality.active = hasReplaces ? false : replacedFunctionality.active;
Expand Down Expand Up @@ -291,12 +314,18 @@ contract MVDFunctionalitiesManager is IMVDFunctionalitiesManager, CommonUtilitie
_functionalityCount[newFunctionality.location] = _functionalityCount[newFunctionality.location] + (hasCodeName ? 1 : 0);

if(hasCodeName) {
IMVDProxy(_proxy).callFromManager(_callingContext = newFunctionality.location, abi.encodeWithSignature("onStart(address,address)", proposalAddress, hasReplaces ? replacedFunctionality.location : address(0)));
(result,) = IMVDProxy(_proxy).callFromManager(_callingContext = newFunctionality.location, abi.encodeWithSignature("onStart(address,address)", proposalAddress, hasReplaces ? replacedFunctionality.location : address(0)));
_callingContext = address(0);
if(!result) {
revert("onStart failed!");
}
}
_callingContext = address(0);

if(hasCodeName || hasReplaces) {
IMVDProxy(_proxy).emitFromManager(hasCodeName ? codeName : "", hasCodeName ? position : 0, proposalAddress, hasReplaces ? replacedFunctionality.codeName : "", hasReplaces ? replacedFunctionality.location : address(0), hasReplaces ? replacedFunctionality.submitable : false, hasReplaces ? replacedFunctionality.methodSignature : "", hasReplaces ? replacedFunctionality.isInternal : false, hasReplaces ? replacedFunctionality.needsSender : false, hasReplaces ? replacedFunctionality.proposalAddress : address(0), hasReplaces ? replacedFunctionality.position : 0);
IMVDProxy(_proxy).emitFromManager(hasCodeName ? codeName : "", proposalAddress, hasReplaces ? replacedFunctionality.codeName : "", hasReplaces ? replacedFunctionality.sourceLocation : address(0), hasReplaces ? replacedFunctionality.sourceLocationId : 0, hasReplaces ? replacedFunctionality.location : address(0), hasReplaces ? replacedFunctionality.submitable : false, hasReplaces ? replacedFunctionality.methodSignature : "", hasReplaces ? replacedFunctionality.isInternal : false, hasReplaces ? replacedFunctionality.needsSender : false, hasReplaces ? replacedFunctionality.proposalAddress : address(0));
}
_callingContext = address(0);
return true;
}

function setCallingContext(address location) public override returns(bool changed) {
Expand Down
2 changes: 2 additions & 0 deletions MVDFunctionalityModelsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ contract MVDFunctionalityModelsManager is IMVDFunctionalityModelsManager {

_wellKnownFunctionalityModels["checkSurveyResult"] = Functionality("checkSurveyResult", address(0), 0, address(0), false, "checkSurveyResult(address)", '["bool"]', false, false, address(0), true, 0);

_wellKnownFunctionalityModels["getVotesHardCap"] = Functionality("getVotesHardCap", address(0), 0, address(0), false, "getVotesHardCap()", '["uint256"]', false, false, address(0), false, 0);

_wellKnownFunctionalityModels["onNewProposal"] = Functionality("onNewProposal", address(0), 0, address(0), true, "onNewProposal(address)", '[]', false, false, address(0), false, 0);

_wellKnownFunctionalityModels["startProposal"] = Functionality("startProposal", address(0), 0, address(0), true, "startProposal(address,uint256,address)", '[]', false, true, address(0), false, 0);
Expand Down
Loading

0 comments on commit 95990d0

Please sign in to comment.