Skip to content

Commit

Permalink
verifiable proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
okwme committed May 2, 2021
1 parent c0ae5f7 commit 4555062
Show file tree
Hide file tree
Showing 18 changed files with 32,360 additions and 35,104 deletions.
16,145 changes: 7,522 additions & 8,623 deletions build/contracts/Doneth.json

Large diffs are not rendered by default.

15,716 changes: 7,198 additions & 8,518 deletions build/contracts/ERC20.json

Large diffs are not rendered by default.

15,716 changes: 7,198 additions & 8,518 deletions build/contracts/ERC20Basic.json

Large diffs are not rendered by default.

1,344 changes: 1,344 additions & 0 deletions build/contracts/IDoneth.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/contracts/Migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,7 @@
}
},
"schemaVersion": "3.3.2",
"updatedAt": "2021-05-01T11:16:10.932Z",
"updatedAt": "2021-05-02T12:42:44.239Z",
"networkType": "ethereum",
"devdoc": {
"methods": {}
Expand Down
872 changes: 872 additions & 0 deletions build/contracts/Proxy.json

Large diffs are not rendered by default.

1,584 changes: 849 additions & 735 deletions build/contracts/ProxyFactory.json

Large diffs are not rendered by default.

15,726 changes: 7,203 additions & 8,523 deletions build/contracts/SafeMath.json

Large diffs are not rendered by default.

57 changes: 14 additions & 43 deletions contracts/Doneth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ pragma solidity ^0.4.15;
* the member can withdraw from the contract.
*/

/*
* Ownable
*
* Base contract with an owner.
* Provides onlyOwner modifier, which prevents function from running
* if it is called by anyone other than the owner.
*/

contract Ownable {
contract Doneth {

address public implementation;
// constructor(address _implementation) public {}

address public owner;

function Ownable() public {
Expand All @@ -35,10 +32,6 @@ contract Ownable {
owner = newOwner;
}
}
}

contract Doneth is Ownable {
address public implementation;

using SafeMath for uint256;

Expand Down Expand Up @@ -76,42 +69,24 @@ contract Doneth is Ownable {
}

// Used to keep track of ERC20 tokens used and how much withdrawn
mapping(address => Token) public tokens;
address[] public tokenKeys;
struct Token {
bool exists;
uint256 totalWithdrawn;
}

// constructor() public {
// genesisBlockNumber = block.number;
// addMember(tx.origin, 1, true, "");
// }

mapping(address => uint256) public tokens;

function init(string _contractName, string _founderName) public {
require(!initialized, "ALREADY INITIALIZED");
function init() public {
require(!initialized);
initialized = true;

owner = tx.origin;
genesisBlockNumber = block.number;
if (bytes(_contractName).length > 21) revert();
if (bytes(_founderName).length > 21) revert();
name = _contractName;
// addMember(founder, 1, true, _founderName);

Member memory newMember;
newMember.exists = true;
newMember.admin = true;
newMember.memberName = _founderName;

members[tx.origin] = newMember;
memberKeys.push(tx.origin);
// addShare(tx.origin, shares);

totalShares = totalShares.add(1);
members[tx.origin].shares = members[tx.origin].shares.add(1);
AddShare(tx.origin, 1, members[tx.origin].shares);
// AddShare(tx.origin, 1, members[tx.origin].shares);
// AddShare(msg.sender, 1, members[tx.origin].shares);
}

event Deposit(address from, uint value);
Expand Down Expand Up @@ -164,10 +139,6 @@ contract Doneth is Ownable {

function checkERC20Balance(address token) public constant returns(uint256) {
return ERC20(token).balanceOf(address(this));
// if (!tokens[token].exists && balance > 0) {
// tokens[token].exists = true;
// }
// return balance;
}

// Function to add members to the contract
Expand Down Expand Up @@ -263,15 +234,15 @@ contract Doneth is Ownable {
Withdraw(msg.sender, amount, totalWithdrawn);
}

// Withdrawal function for ERC20 tokens
// Withdrawal function for ERC20 tokensx
function withdrawToken(uint256 amount, address token) public onlyExisting(msg.sender) {
uint256 newTotal = calculateTotalWithdrawableTokenAmount(msg.sender, token);
if (amount > newTotal.sub(members[msg.sender].tokensWithdrawn[token])) revert();

members[msg.sender].tokensWithdrawn[token] = members[msg.sender].tokensWithdrawn[token].add(amount);
tokens[token].totalWithdrawn = tokens[token].totalWithdrawn.add(amount);
tokens[token] = tokens[token].add(amount);
ERC20(token).transfer(msg.sender, amount);
TokenWithdraw(msg.sender, amount, token, tokens[token].totalWithdrawn);
TokenWithdraw(msg.sender, amount, token, tokens[token]);
}

// Withdraw from shared expense allocation. Total withdrawable is calculated as
Expand Down Expand Up @@ -303,7 +274,7 @@ contract Doneth is Ownable {


function calculateTotalWithdrawableTokenAmount(address who, address token) public constant returns(uint256) {
uint256 balanceSum = checkERC20Balance(token).add(tokens[token].totalWithdrawn);
uint256 balanceSum = checkERC20Balance(token).add(tokens[token]);

// Need to use parts-per notation to compute percentages for lack of floating point division
uint256 tokPerSharePPN = balanceSum.percent(totalShares, PRECISION);
Expand Down
126 changes: 63 additions & 63 deletions contracts/Factory.sol
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
pragma solidity ^0.4.19;
// pragma solidity ^0.4.19;

import "./Proxy.sol";
// import "./Proxy.sol";

/**
* @dev Proxy Factory library to create multiple proxy instances at runtime
*
* @notice i modified this to use a contract instead of a library as it was causing errors
*
* These functions implement a simple Factory pattern that can be used to deploy
* multiple proxy instances when the implementation contract needs to be reused.
*/
contract Proxies {
// Contains the address of the implementation used for the proxies it creates.
struct ProxyFactory { address implementation; }
// /**
// * @dev Proxy Factory library to create multiple proxy instances at runtime
// *
// * @notice i modified this to use a contract instead of a library as it was causing errors
// *
// * These functions implement a simple Factory pattern that can be used to deploy
// * multiple proxy instances when the implementation contract needs to be reused.
// */
// contract Proxies {
// // Contains the address of the implementation used for the proxies it creates.
// struct ProxyFactory { address implementation; }

/**
* @dev Returns the ProxyFactory struct with the impementation contract address.
*
* Initializes the factory struct by deploying the implementation contract and
* storing its address.
*/
function newFactory(bytes memory creationCode) internal returns (ProxyFactory memory) {
address implementationAddr = _deployImplementation(creationCode);
ProxyFactory memory factory = ProxyFactory(implementationAddr);
return factory;
}
// /**
// * @dev Returns the ProxyFactory struct with the impementation contract address.
// *
// * Initializes the factory struct by deploying the implementation contract and
// * storing its address.
// */
// function newFactory(bytes memory creationCode) internal returns (ProxyFactory memory) {
// address implementationAddr = _deployImplementation(creationCode);
// ProxyFactory memory factory = ProxyFactory(implementationAddr);
// return factory;
// }

/**
* @dev Deploys a Proxy instance with `admin` and initialized with `initializerData
*
*/
function deploy(ProxyFactory storage self,address admin, bytes memory initializerData) internal returns (address) {
Proxy prox = new Proxy(self.implementation, admin, initializerData);
return address(prox);
}
// /**
// * @dev Deploys a Proxy instance with `admin` and initialized with `initializerData
// *
// */
// function deploy(ProxyFactory storage self,address admin, bytes memory initializerData) internal returns (address) {
// Proxy prox = new Proxy(self.implementation, admin, initializerData);
// return address(prox);
// }

/**
* @dev Returns the address of the deployed implementation contract.
*
* Creates the implementation contract with `creationCode`.
*/
function _deployImplementation(bytes memory creationCode) private returns (address) {
address payable addr;
// solhint-disable-next-line no-inline-assembly
assembly {
addr := create(0, add(creationCode, 0x20), mload(creationCode))
if iszero(extcodesize(addr)) {
revert(0, 0)
}
}
return addr;
}
}
// /**
// * @dev Returns the address of the deployed implementation contract.
// *
// * Creates the implementation contract with `creationCode`.
// */
// function _deployImplementation(bytes memory creationCode) private returns (address) {
// address payable addr;
// // solhint-disable-next-line no-inline-assembly
// assembly {
// addr := create(0, add(creationCode, 0x20), mload(creationCode))
// if iszero(extcodesize(addr)) {
// revert(0, 0)
// }
// }
// return addr;
// }
// }

contract Factory is Proxies {
// contract Factory is Proxies {

// This struct contains the address of the implementation that is
// used for the proxies it creates.
ProxyFactory factory;
// // This struct contains the address of the implementation that is
// // used for the proxies it creates.
// ProxyFactory factory;

constructor(bytes memory _creationCode) {
// Initializes the factory struct by deploying the implementation
// and storing its address.
factory = Proxies.newFactory(_creationCode);
}
// constructor(bytes memory _creationCode) {
// // Initializes the factory struct by deploying the implementation
// // and storing its address.
// factory = Proxies.newFactory(_creationCode);
// }

// initializerData is the encoded call to the initializer function.
function deployInstance(address admin, bytes memory initializerData) public returns (address) {
return deploy(factory, admin, initializerData);
}
}
// // initializerData is the encoded call to the initializer function.
// function deployInstance(address admin, bytes memory initializerData) public returns (address) {
// return deploy(factory, admin, initializerData);
// }
// }
12 changes: 5 additions & 7 deletions contracts/Proxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ pragma solidity ^0.4.18;
* @dev Gives the possibility to delegate any call to a foreign implementation.
*/
contract Proxy {

/**
* @dev Tells the address of the implementation where every call will be delegated.
* @return address of the implementation to which it will be delegated
*/
function implementation() public view returns (address);
address public implementation;
constructor(address _implementation) {
implementation = _implementation;
}

/**
* @dev Fallback function allowing to perform a delegatecall to the given implementation.
* This function will return whatever the implementation call returns
*/
function () payable public {
address _impl = implementation();
address _impl = implementation;
require(_impl != address(0));

assembly {
Expand Down
46 changes: 12 additions & 34 deletions contracts/ProxyFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,53 +28,31 @@
*
***/

import "./Proxy.sol";

pragma solidity ^0.4.19;


interface IDoneth {
function init() public;
}

/* solhint-disable no-inline-assembly, indent, state-visibility, avoid-low-level-calls */

contract ProxyFactory {
// address public target;

function ProxyFactory() public {
}
address public target;
function ProxyFactory() public {}

event ProxyDeployed(address proxyAddress, address targetAddress);

function createProxy(address _target, bytes _data)
function createProxy(address _target)
public
payable
returns (address proxyContract)
{
// address _target = ControllerPointer(target).getERC20Main();
proxyContract = createProxyImpl(_target, _data);

ProxyDeployed(proxyContract, _target);
}

function createProxyImpl(address _target, bytes _data)
internal
returns (address proxyContract)
{
assembly {
let contractCode := mload(0x40) // Find empty storage location using "free memory pointer"

mstore(add(contractCode, 0x0b), _target) // Add target address, with a 11 bytes [i.e. 23 - (32 - 20)] offset to later accomodate first part of the bytecode
mstore(sub(contractCode, 0x09), 0x000000000000000000603160008181600b9039f3600080808080368092803773) // First part of the bytecode, shifted left by 9 bytes, overwrites left padding of target address
mstore(add(contractCode, 0x2b), 0x5af43d828181803e808314602f57f35bfd000000000000000000000000000000) // Final part of bytecode, offset by 43 bytes

proxyContract := create(0, contractCode, 60) // total length 60 bytes
if iszero(extcodesize(proxyContract)) {
revert(0, 0)
}

// check if the _data.length > 0 and if it is forward it to the newly created contract
let dataLength := mload(_data)
if iszero(iszero(dataLength)) {
if iszero(call(gas, proxyContract, callvalue, add(_data, 0x20), dataLength, 0, 0)) {
revert(0, 0)
}
}
}
Proxy prox = new Proxy(_target);
IDoneth(address(prox)).init();
ProxyDeployed(address(prox), _target);
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"web3modal": "^1.9.3"
},
"devDependencies": {
"@openzeppelin/contracts": "^3.0.0",
"autoprefixer": "^7.1.2",
"babel-core": "^6.22.1",
"babel-eslint": "^8.2.1",
Expand Down
Loading

0 comments on commit 4555062

Please sign in to comment.