mirror of
https://github.com/Instadapp/smart-contract.git
synced 2024-07-29 22:08:07 +00:00
added more comments.
This commit is contained in:
parent
bc40451e91
commit
1ffa90ade8
|
@ -6,15 +6,18 @@ import "./UserProxy.sol";
|
||||||
// checking if the logic proxy is authorised
|
// checking if the logic proxy is authorised
|
||||||
contract SystemAdmin {
|
contract SystemAdmin {
|
||||||
|
|
||||||
address public logicProxyAddr;
|
address public addrRegistry;
|
||||||
|
|
||||||
modifier isAdmin() {
|
modifier isAdmin() {
|
||||||
require(msg.sender == getAdmin(), "permission-denied");
|
require(msg.sender == getAdmin(), "permission-denied");
|
||||||
_;
|
_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev get the system admin
|
||||||
|
*/
|
||||||
function getAdmin() internal view returns (address) {
|
function getAdmin() internal view returns (address) {
|
||||||
AddressRegistryInterface registry = AddressRegistryInterface(logicProxyAddr);
|
AddressRegistryInterface registry = AddressRegistryInterface(addrRegistry);
|
||||||
return registry.getAddress("admin");
|
return registry.getAddress("admin");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,36 +31,50 @@ contract ProxyRegistry is SystemAdmin {
|
||||||
mapping(address => UserProxy) public proxies;
|
mapping(address => UserProxy) public proxies;
|
||||||
bool public guardianEnabled;
|
bool public guardianEnabled;
|
||||||
|
|
||||||
constructor(address _logicProxyAddr) public {
|
constructor(address _addrRegistry) public {
|
||||||
logicProxyAddr = _logicProxyAddr;
|
addrRegistry = _addrRegistry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev deploys a new proxy instance and sets msg.sender as owner of proxy
|
||||||
|
*/
|
||||||
function build() public returns (UserProxy proxy) {
|
function build() public returns (UserProxy proxy) {
|
||||||
proxy = build(msg.sender);
|
proxy = build(msg.sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
// deploys a new proxy instance and sets custom owner of proxy
|
/**
|
||||||
|
* @dev deploys a new proxy instance and sets custom owner of proxy
|
||||||
|
*/
|
||||||
function build(address owner) public returns (UserProxy proxy) {
|
function build(address owner) public returns (UserProxy proxy) {
|
||||||
require(
|
require(
|
||||||
proxies[owner] == UserProxy(0) || proxies[owner].owner() != owner,
|
proxies[owner] == UserProxy(0) || proxies[owner].owner() != owner,
|
||||||
"multiple-proxy-per-user-not-allowed"
|
"multiple-proxy-per-user-not-allowed"
|
||||||
); // Not allow new proxy if the user already has one and remains being the owner
|
); // Not allow new proxy if the user already has one and remains being the owner
|
||||||
proxy = new UserProxy(owner, logicProxyAddr);
|
proxy = new UserProxy(owner, addrRegistry);
|
||||||
emit Created(msg.sender, owner, address(proxy));
|
emit Created(msg.sender, owner, address(proxy));
|
||||||
proxies[owner] = proxy;
|
proxies[owner] = proxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
// msg.sender should always be proxies created via this contract for successful execution
|
/**
|
||||||
|
* @dev update the proxy record whenever owner changed on any proxy
|
||||||
|
* Throws if msg.sender is not a proxy contract created via this contract
|
||||||
|
*/
|
||||||
function updateProxyRecord(address currentOwner, address nextOwner) public {
|
function updateProxyRecord(address currentOwner, address nextOwner) public {
|
||||||
require(msg.sender == address(proxies[currentOwner]), "invalid-proxy-or-owner");
|
require(msg.sender == address(proxies[currentOwner]), "invalid-proxy-or-owner");
|
||||||
proxies[nextOwner] = proxies[currentOwner];
|
proxies[nextOwner] = proxies[currentOwner];
|
||||||
proxies[currentOwner] = UserProxy(0);
|
proxies[currentOwner] = UserProxy(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev enable guardian in overall system
|
||||||
|
*/
|
||||||
function enableGuardian() public isAdmin {
|
function enableGuardian() public isAdmin {
|
||||||
guardianEnabled = true;
|
guardianEnabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev disable guardian in overall system
|
||||||
|
*/
|
||||||
function disableGuardian() public isAdmin {
|
function disableGuardian() public isAdmin {
|
||||||
guardianEnabled = false;
|
guardianEnabled = false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ interface AddressRegistryInterface {
|
||||||
*/
|
*/
|
||||||
contract ProxyRecord {
|
contract ProxyRecord {
|
||||||
|
|
||||||
address public proxyContract;
|
address public proxyRegistryContract;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev this updates the internal proxy ownership on "proxy registry" contract
|
* @dev this updates the internal proxy ownership on "proxy registry" contract
|
||||||
|
@ -39,7 +39,7 @@ contract ProxyRecord {
|
||||||
* @param nextOwner is the new assigned owner
|
* @param nextOwner is the new assigned owner
|
||||||
*/
|
*/
|
||||||
function setProxyRecordOwner(address currentOwner, address nextOwner) internal {
|
function setProxyRecordOwner(address currentOwner, address nextOwner) internal {
|
||||||
ProxyRegistryInterface initCall = ProxyRegistryInterface(proxyContract);
|
ProxyRegistryInterface initCall = ProxyRegistryInterface(proxyRegistryContract);
|
||||||
initCall.updateProxyRecord(currentOwner, nextOwner);
|
initCall.updateProxyRecord(currentOwner, nextOwner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,9 +62,7 @@ contract UserAuth is ProxyRecord {
|
||||||
* @dev defines the "proxy registry" contract and sets the owner
|
* @dev defines the "proxy registry" contract and sets the owner
|
||||||
*/
|
*/
|
||||||
constructor() public {
|
constructor() public {
|
||||||
proxyContract = msg.sender;
|
proxyRegistryContract = msg.sender;
|
||||||
owner = msg.sender;
|
|
||||||
emit LogSetOwner(owner, msg.sender);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -131,7 +129,7 @@ contract UserGuardian is UserAuth {
|
||||||
* @dev Throws if guardians not enabled by system admin
|
* @dev Throws if guardians not enabled by system admin
|
||||||
*/
|
*/
|
||||||
modifier guard() {
|
modifier guard() {
|
||||||
ProxyRegistryInterface initCall = ProxyRegistryInterface(proxyContract);
|
ProxyRegistryInterface initCall = ProxyRegistryInterface(proxyRegistryContract);
|
||||||
require(initCall.guardianEnabled());
|
require(initCall.guardianEnabled());
|
||||||
_;
|
_;
|
||||||
}
|
}
|
||||||
|
@ -161,8 +159,7 @@ contract UserGuardian is UserAuth {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev sets the guardian with assigned number (upto 3)
|
* @dev sets the guardian with assigned number (upto 3)
|
||||||
* @param num is the guardian assigned number
|
* @param _activePeriod is the period when guardians have no rights to dethrone the owner
|
||||||
* @param _guardian is the new guardian address
|
|
||||||
*/
|
*/
|
||||||
function updateActivePeriod(uint _activePeriod) public auth guard {
|
function updateActivePeriod(uint _activePeriod) public auth guard {
|
||||||
activePeriod = _activePeriod;
|
activePeriod = _activePeriod;
|
||||||
|
@ -204,31 +201,66 @@ contract UserNote {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// checking if the logic proxy is authorised
|
/**
|
||||||
|
* @title User Proxy Logic
|
||||||
|
*/
|
||||||
contract UserLogic {
|
contract UserLogic {
|
||||||
address public logicProxyAddr;
|
address public logicRegistryAddr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param logicAddr is the logic proxy contract address
|
||||||
|
* @return the true boolean for logic proxy if authorised otherwise false
|
||||||
|
*/
|
||||||
function isLogicAuthorised(address logicAddr) internal view returns (bool) {
|
function isLogicAuthorised(address logicAddr) internal view returns (bool) {
|
||||||
AddressRegistryInterface logicProxy = AddressRegistryInterface(logicProxyAddr);
|
AddressRegistryInterface logicProxy = AddressRegistryInterface(logicRegistryAddr);
|
||||||
return logicProxy.getLogic(logicAddr);
|
return logicProxy.getLogic(logicAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract UserProxy is UserGuardian, UserNote, UserLogic {
|
contract UserProxy is UserGuardian, UserNote, UserLogic {
|
||||||
|
|
||||||
constructor(address _owner, address _logicProxyAddr) public {
|
event LogExecute(address target, uint srcNum, uint sessionNum);
|
||||||
logicProxyAddr = _logicProxyAddr;
|
|
||||||
|
/**
|
||||||
|
* @dev sets the "address registry", owner's last activity, owner's active period and initial owner
|
||||||
|
* @param _owner initial owner of the contract
|
||||||
|
* @param _logicRegistryAddr address registry address which have logic proxy registry
|
||||||
|
*/
|
||||||
|
constructor(address _owner, address _logicRegistryAddr) public {
|
||||||
|
logicRegistryAddr = _logicRegistryAddr;
|
||||||
|
owner = _owner;
|
||||||
lastActivity = block.timestamp;
|
lastActivity = block.timestamp;
|
||||||
activePeriod = 30 days; // default and changeable
|
activePeriod = 30 days; // default and changeable
|
||||||
owner = _owner;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function() external payable {}
|
function() external payable {}
|
||||||
|
|
||||||
function execute(address _target, bytes memory _data) public payable auth note returns (bytes memory response) {
|
/**
|
||||||
|
* @dev execute authorised calls via delegate call
|
||||||
|
* @param _target logic proxy address
|
||||||
|
* @param _data delegate call data
|
||||||
|
* @param srcNum to find the source
|
||||||
|
* @param sessionNum to find the session
|
||||||
|
*/
|
||||||
|
function execute(
|
||||||
|
address _target,
|
||||||
|
bytes memory _data,
|
||||||
|
uint srcNum,
|
||||||
|
uint sessionNum
|
||||||
|
)
|
||||||
|
public
|
||||||
|
payable
|
||||||
|
auth
|
||||||
|
note
|
||||||
|
returns (bytes memory response)
|
||||||
|
{
|
||||||
require(_target != address(0), "user-proxy-target-address-required");
|
require(_target != address(0), "user-proxy-target-address-required");
|
||||||
require(isLogicAuthorised(_target), "logic-proxy-address-not-allowed");
|
require(isLogicAuthorised(_target), "logic-proxy-address-not-allowed");
|
||||||
lastActivity = block.timestamp;
|
lastActivity = block.timestamp;
|
||||||
|
emit LogExecute(_target, srcNum, sessionNum);
|
||||||
|
|
||||||
// call contract in current context
|
// call contract in current context
|
||||||
assembly {
|
assembly {
|
||||||
let succeeded := delegatecall(sub(gas, 5000), _target, add(_data, 0x20), mload(_data), 0, 0)
|
let succeeded := delegatecall(sub(gas, 5000), _target, add(_data, 0x20), mload(_data), 0, 0)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user