Skip to content

Instantly share code, notes, and snippets.

@clar
Created July 17, 2018 07:26
Show Gist options
  • Save clar/88bb60b1b0304e4b32bae98c51bb7b72 to your computer and use it in GitHub Desktop.
Save clar/88bb60b1b0304e4b32bae98c51bb7b72 to your computer and use it in GitHub Desktop.
a.sol
pragma solidity ^0.4.21;
// contract Ownable {
// address public owner;
// constructor() public {
// owner = msg.sender;
// }
// modifier onlyOwner() {
// require(msg.sender == owner);
// _;
// }
// }
contract AccountData {
struct Auth {
address authAddress;
uint weight;
}
modifier onlyOwner() { require(msg.sender == owner); _;}
address public owner;
//address public active;
//Auth[] public auths;
uint public val;
}
contract AccountProxy is AccountData{
address public logic_contract;
function AccountProxy() {
}
// admin to set contract
function setLogicContract(address _c) public returns (bool success){
logic_contract = _c;
return true;
}
// fall back function
function () payable public {
address target = logic_contract;
assembly {
// Copy the data sent to the memory address starting free mem position
let ptr := mload(0x40)
calldatacopy(ptr, 0, calldatasize)
// Proxy the call to the contract address with the provided gas and data
let result := delegatecall(gas, target, ptr, calldatasize, 0, 0)
// Copy the data returned by the proxied call to memory
let size := returndatasize
returndatacopy(ptr, 0, size)
// Check what the result is, return and revert accordingly
switch result
case 0 { revert(ptr, size) }
case 1 { return(ptr, size) }
}
}
}
contract AccountInterface {
function init(address _owner);
function changeOwner(address _owner); // called by owner, with delay
//function changeOwnerWithAuth(address _owner); //called by owner and auths, will be changed immediatly
//function freezeKey(address _key); // by owner
//function unfreezeKey(address _key); // by owner
}
contract AccountLogicOne is AccountData, AccountInterface {
function init(address _owner) public {
owner = _owner;
}
function changeOwner(address _owner) public /*only by onwer*/ {
owner = _owner;
}
function setVal(uint _val) public returns (bool success) {
val = 2 * _val;
return true;
}
}
contract AccountLogicTwo is AccountLogicOne {
function changeOwner(address _owner) public /*only by onwer*/ {
owner = _owner;
}
function setVal(uint _val) public returns (bool success) {
val = 2 + _val;
return true;
}
}
contract AccountCreator {
function AccountCreator() {
m_accountAmounts = 0;
}
function setAccountLogic(address _addr) public {
m_accountLogic = _addr;
}
// TBD. with parameters
function createAccount() public returns(address) {
require( m_accountLogic != address(0x0));
AccountProxy proxy = new AccountProxy();
proxy.setLogicContract(m_accountLogic);
AccountInterface account = AccountInterface(proxy);
account.init(msg.sender);
m_addresses[proxy] = m_accountAmounts;
m_addresses2[m_accountAmounts] = proxy;
m_accountAmounts += 1;
return proxy;
}
mapping(address=>uint) public m_addresses;
mapping(uint=>address) public m_addresses2;
uint public m_accountAmounts;
address public m_accountLogic;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment