-
Notifications
You must be signed in to change notification settings - Fork 115
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
Lezek123
wants to merge
3
commits into
Joystream:development
Choose a base branch
from
Lezek123:content-directory-smart-contracts
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules/ | ||
/build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"singleQuote": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
smart-contracts/contracts/bridge/ContentWorkingGroupBridge.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
_; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
/** | ||
* @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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 useSafeMath
(256bit) in combination with OpenZeppelin'sSafeCast
. Maintaining multiple math libraries that are not supported byOpenZeppelin
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)There was a problem hiding this comment.
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.