Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content directory - initial smart contracts #1752

Draft
wants to merge 3 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions smart-contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/build/
3 changes: 3 additions & 0 deletions smart-contracts/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": false
}
8 changes: 8 additions & 0 deletions smart-contracts/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": ["solhint:recommended"],
"rules": {
"prettier/prettier": "error",
"compiler-version": ["error", "^0.6.0"]
},
"plugins": ["prettier"]
}
475 changes: 475 additions & 0 deletions smart-contracts/contracts/ContentDirectory.sol

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions smart-contracts/contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract Migrations {
address public owner;
uint256 public last_completed_migration;

constructor() public {
owner = msg.sender;
}

modifier restricted() {
if (msg.sender == owner) _;
}

function setCompleted(uint256 completed) public restricted {
last_completed_migration = completed;
}

function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}
41 changes: 41 additions & 0 deletions smart-contracts/contracts/bridge/ContentWorkingGroupBridge.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

import "./auth.sol";

contract ContentWorkingGroupBridge is RuntimeManageable {
// A map of curatorId => evmAddress(roleKey)
mapping(uint64 => address) private addressByCuratorId;

// evmAddress(roleKey) of current lead
address public currentLeadAddress;

// Lead status managed by the council
bool public isLeadActive = true;

constructor(RuntimeAddressProvider _provider) public RuntimeManageable(_provider) {}

function setCuratorAddress(uint64 _curatorId, address _address) public onlyRuntime {
addressByCuratorId[_curatorId] = _address;
}

function setLeadAddress(address _address) public onlyRuntime {
currentLeadAddress = _address;
}

function setLeadStatus(bool _status) public onlyCouncil {
isLeadActive = _status;
}

function isCurator(address _address, uint64 _curatorId) public view returns (bool) {
return (curatorExists(_curatorId) && addressByCuratorId[_curatorId] == _address);
}

function curatorExists(uint64 _curatorId) public view returns (bool) {
return addressByCuratorId[_curatorId] != address(0);
}

function isActiveLead(address _address) public view returns (bool) {
return (isLeadActive && currentLeadAddress != address(0) && currentLeadAddress == _address);
}
}
22 changes: 22 additions & 0 deletions smart-contracts/contracts/bridge/MembershipBridge.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

import "./auth.sol";

contract MembershipBridge is RuntimeManageable {
mapping(uint64 => address) private controllerAddressByMemberId;

constructor(RuntimeAddressProvider _provider) public RuntimeManageable(_provider) {}

function setMemberAddress(uint64 _memberId, address _address) public onlyRuntime {
controllerAddressByMemberId[_memberId] = _address;
}

function isMemberController(address _address, uint64 _memberId) public view returns (bool) {
return (memberExists(_memberId) && controllerAddressByMemberId[_memberId] == _address);
}

function memberExists(uint64 _memberId) public view returns (bool) {
return controllerAddressByMemberId[_memberId] != address(0);
}
}
43 changes: 43 additions & 0 deletions smart-contracts/contracts/bridge/auth.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

// Provider of runtime-hardcoded addresses
// (can be customized for testing purposes)
contract RuntimeAddressProvider {
address public runtimeAddress;
address public councilAddress;

constructor(address _runtimeAddress, address _councilAddress) public {
// The address hardcoded in the runtime, used to update bridges
// (and possibly other operations that shouldn't be done via proposal)
runtimeAddress = _runtimeAddress;
// This is the address hardcoded in the runtime proposal module
// (can only be used in context of proposal execution)
councilAddress = _councilAddress;
}
}

// Abstract contract providing modifiers for contracts that can be managed through runtime/proposals
abstract contract RuntimeManageable {
RuntimeAddressProvider public runtimeAddressProvider;

constructor(RuntimeAddressProvider _runtimeAddressProvider) public {
runtimeAddressProvider = _runtimeAddressProvider;
}

modifier onlyRuntime {
require(
msg.sender == runtimeAddressProvider.runtimeAddress(),
"This function can only be executed from the runtime"
);
_;
}

modifier onlyCouncil {
require(
msg.sender == runtimeAddressProvider.councilAddress(),
"This function can only be executed through proposal system"
);
_;
}
}
158 changes: 158 additions & 0 deletions smart-contracts/contracts/lib/SafeMath16.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

// uint16 version of OpenZeppelin's SafeMath
library SafeMath16 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of type-specific implementations of SafeMath (SafeMath16, SafeMath32, ...) not originating from OpenZeppelin, we should use SafeMath(256bit) in combination with OpenZeppelin's SafeCast. Maintaining multiple math libraries that are not supported by OpenZeppelin would create for us an unnecessary maintenance burden and security weak point, especially when (eventually) updating the Solidity version. See OpenZeppelin/openzeppelin-contracts#1625 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stumbled upon this link when trying to figure out the best solution, but it seemed to add a lot of burden to perform this casting after every operation. Since this is related to the general u256 comment, I'll address it further below.

/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(uint16 a, uint16 b) internal pure returns (uint16) {
uint16 c = a + b;
require(c >= a, "SafeMath: addition overflow");

return c;
}

/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint16 a, uint16 b) internal pure returns (uint16) {
return sub(a, b, "SafeMath: subtraction overflow");
}

/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(
uint16 a,
uint16 b,
string memory errorMessage
) internal pure returns (uint16) {
require(b <= a, errorMessage);
uint16 c = a - b;

return c;
}

/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint16 a, uint16 b) internal pure returns (uint16) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
return 0;
}

uint16 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");

return c;
}

/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint16 a, uint16 b) internal pure returns (uint16) {
return div(a, b, "SafeMath: division by zero");
}

/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(
uint16 a,
uint16 b,
string memory errorMessage
) internal pure returns (uint16) {
require(b > 0, errorMessage);
uint16 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold

return c;
}

/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint16 a, uint16 b) internal pure returns (uint16) {
return mod(a, b, "SafeMath: modulo by zero");
}

/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(
uint16 a,
uint16 b,
string memory errorMessage
) internal pure returns (uint16) {
require(b != 0, errorMessage);
return a % b;
}
}