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

Erc777 #2

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sol linguist-language=Solidity
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ language: node_js
node_js:
- '8'
- 'node'

before_script:
- npm run start > ganache-unittest-logs.txt &
- npm run pre-deploy
script:
- truffle test
deploy:
provider: npm
email: [email protected]
email: [email protected]
api_key: $NPM_TOKEN
on:
all_branches: true
tags: true
node: '8'
repo: gnosis/pm-apollo-contracts
repo: frontier-token-research/pm-helena-contracts
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[![Solidity version](https://img.shields.io/badge/dynamic/json.svg?style=flat-square&maxAge=3600&label=Solidity&url=https%3A%2F%2Fraw.githubusercontent.com%2F0xjac%2FERC777%2Fmaster%2Fpackage-lock.json&query=%24.dependencies.solc.version&colorB=ff69b4&prefix=v)](https://solidity.readthedocs.io/en/v0.4.21/installing-solidity.html)
[![logo](https://img.shields.io/badge/-logo-C99D66.svg?style=flat-square&maxAge=3600&colorA=grey&logo=data:image/svg+xml;utf8,%253Csvg%2520xmlns='http://www.w3.org/2000/svg'%2520viewBox='0%25200%2520595.3%2520841.9'%253E%253Cpath%2520d='M410.1%2520329.9c20%25200%252039.1.1%252058.3-.1%25204.2%25200%25205.6.4%25204.8%25205.3-2.5%252014.1-4.4%252028.3-6.3%252042.5-.5%25203.6-2%25204.5-5.5%25204.5-28.3-.1-56.7%25200-85-.2-4%25200-6.2%25201.3-8.3%25204.6-9.6%252014.8-18.6%252030-26.7%252045.6-.5%25201-.9%25202.1-1.6%25203.7h5.9c35.5%25200%252071%2520.1%2520106.5-.1%25204.5%25200%25206.3.2%25205.3%25205.8-2.7%252014.2-4.6%252028.6-6.4%252043-.6%25204.4-2.1%25205.6-6.6%25205.6-41.2-.2-82.3-.1-123.5-.2-3.6%25200-5.1%25201.1-6.2%25204.5-13.7%252038.9-22.8%252078.9-28.7%2520119.6-2.8%252019.1-5.9%252038.2-8.6%252057.3-.5%25203.4-1.8%25203.8-4.7%25203.8-31.8-.1-63.7%25200-95.5-.1-1.5%25200-4.1%25201.4-3.5-2.6%25207.5-48.5%252013-97.3%252027.4-144.5%25203.8-12.5%25208-24.9%252012.8-37.9h-21.8c-14.7%25200-29.3-.2-44%2520.1-4.1.1-4.6-1.3-4.1-4.9%25202.5-15.4%25204.9-30.9%25207-46.4.5-3.4%25202.3-3%25204.5-3h79c2.2%25200%25204.1%25200%25205.4-2.5%25209.2-17.5%252020-34.1%252031.5-51.4h-5.8c-33.7%25200-67.3-.1-101%2520.1-4.4%25200-5.3-1.2-4.6-5.2%25202.3-14%25204.6-27.9%25206.3-42%2520.6-4.6%25202.5-5%25206.4-5%252044.2.1%252088.3%25200%2520132.5.2%25203.7%25200%25206-1.1%25208.3-4%252023.2-28.6%252048.2-55.6%252074.4-81.6%25201.2-1.2%25202.9-2%25204.4-3-.3-.5-.7-1-1-1.5h-5.3c-83.5%25200-167%25200-250.5.1-3.9%25200-5.7.1-4.8-5.2%25203.8-23.5%25207.1-47.1%252010.3-70.6.5-4.1%25202.5-4.1%25205.7-4.1%252082.3.1%2520164.7%25200%2520247%25200%252035.2%25200%252070.3.1%2520105.5-.1%25203.8%25200%25205%2520.5%25204.3%25204.8-3.7%252023.7-7.1%252047.4-10.4%252071.1-.4%25203-1.8%25205.2-3.9%25207.2-27.1%252026.8-53%252054.8-77.3%252084.3-.5.6-.9%25201.1-1.9%25202.5z'%2520fill='%2523fff'/%253E%253C/svg%253E)](logo)

# Helena Contracts


Contracts related to Helena Network competitions. Helena uses a sidechain infrastructure, where different instance of tokens are deployed (same as Dai/xDai)


Expand Down
2 changes: 1 addition & 1 deletion contracts/AddressRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.21;
pragma solidity ^0.4.24;

contract AddressRegistry {
mapping (address => address) public mainnetAddressFor;
Expand Down
26 changes: 26 additions & 0 deletions contracts/ERC777/ERC20Token.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This code has not been reviewed.
* Do not use or deploy this code before reviewing it personally first.
*/
// solhint-disable-next-line compiler-fixed
pragma solidity ^0.4.24;


interface ERC20Token {
function name() public view returns (string);
function symbol() public view returns (string);
function decimals() public view returns (uint8);
function totalSupply() public view returns (uint256);
function balanceOf(address owner) public view returns (uint256);
function transfer(address to, uint256 amount) public returns (bool);
function transferFrom(address from, address to, uint256 amount) public returns (bool);
function approve(address spender, uint256 amount) public returns (bool);
function allowance(address owner, address spender) public view returns (uint256);

// solhint-disable-next-line no-simple-event-func-name
event Transfer(address indexed from, address indexed to, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 amount);
}
275 changes: 275 additions & 0 deletions contracts/ERC777/ERC777BaseToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
pragma solidity ^0.4.24;

import { ERC820Implementer } from "eip820/contracts/ERC820Implementer.sol";
import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol";
import { ERC777Token } from "./ERC777Token.sol";
import { ERC777TokensSender } from "./ERC777TokensSender.sol";
import { ERC777TokensRecipient } from "./ERC777TokensRecipient.sol";


contract ERC777BaseToken is ERC820Implementer {
using SafeMath for uint256;

string internal mName;
string internal mSymbol;
uint256 internal mGranularity;
uint256 internal mTotalSupply;


mapping(address => uint) internal mBalances;

address[] internal mDefaultOperators;
mapping(address => bool) internal mIsDefaultOperator;
mapping(address => mapping(address => bool)) internal mRevokedDefaultOperator;
mapping(address => mapping(address => bool)) internal mAuthorizedOperators;

/* -- Constructor -- */
//
/// @notice Constructor to create a ReferenceToken
/// @param _name Name of the new token
/// @param _symbol Symbol of the new token.
/// @param _granularity Minimum transferable chunk.
constructor(string _name, string _symbol, uint256 _granularity, address[] _defaultOperators) internal {
mName = _name;
mSymbol = _symbol;
mTotalSupply = 0;
require(_granularity >= 1, "Granularity must be > 1");
mGranularity = _granularity;

mDefaultOperators = _defaultOperators;
for (uint256 i = 0; i < mDefaultOperators.length; i++) { mIsDefaultOperator[mDefaultOperators[i]] = true; }
setInterfaceImplementation("ERC777Token", this);
}

/* -- ERC777 Interface Implementation -- */
//
/// @return the name of the token
function name() public view returns (string) { return mName; }

/// @return the symbol of the token
function symbol() public view returns (string) { return mSymbol; }

/// @return the granularity of the token
function granularity() public view returns (uint256) { return mGranularity; }

/// @return the total supply of the token
function totalSupply() public view returns (uint256) { return mTotalSupply; }

/// @notice Return the account balance of some account
/// @param _tokenHolder Address for which the balance is returned
/// @return the balance of `_tokenAddress`.
function balanceOf(address _tokenHolder) public view returns (uint256) { return mBalances[_tokenHolder]; }

/// @notice Return the list of default operators
/// @return the list of all the default operators
function defaultOperators() public view returns (address[]) { return mDefaultOperators; }

/// @notice Send `_amount` of tokens to address `_to` passing `_data` to the recipient
/// @param _to The address of the recipient
/// @param _amount The number of tokens to be sent
function send(address _to, uint256 _amount, bytes _data) public {
doSend(msg.sender, msg.sender, _to, _amount, _data, "", true);
}

/// @notice Authorize a third party `_operator` to manage (send) `msg.sender`'s tokens.
/// @param _operator The operator that wants to be Authorized
function authorizeOperator(address _operator) public {
require(_operator != msg.sender, "Cannot authorize yourself as an operator");
if (mIsDefaultOperator[_operator]) {
mRevokedDefaultOperator[_operator][msg.sender] = false;
} else {
mAuthorizedOperators[_operator][msg.sender] = true;
}
emit AuthorizedOperator(_operator, msg.sender);
}

/// @notice Revoke a third party `_operator`'s rights to manage (send) `msg.sender`'s tokens.
/// @param _operator The operator that wants to be Revoked
function revokeOperator(address _operator) public {
require(_operator != msg.sender, "Cannot revoke yourself as an operator");
if (mIsDefaultOperator[_operator]) {
mRevokedDefaultOperator[_operator][msg.sender] = true;
} else {
mAuthorizedOperators[_operator][msg.sender] = false;
}
emit RevokedOperator(_operator, msg.sender);
}

/// @notice Check whether the `_operator` address is allowed to manage the tokens held by `_tokenHolder` address.
/// @param _operator address to check if it has the right to manage the tokens
/// @param _tokenHolder address which holds the tokens to be managed
/// @return `true` if `_operator` is authorized for `_tokenHolder`
function isOperatorFor(address _operator, address _tokenHolder) public view returns (bool) {
return (_operator == _tokenHolder // solium-disable-line operator-whitespace
|| mAuthorizedOperators[_operator][_tokenHolder]
|| (mIsDefaultOperator[_operator] && !mRevokedDefaultOperator[_operator][_tokenHolder]));
}

/// @notice Send `_amount` of tokens on behalf of the address `from` to the address `to`.
/// @param _from The address holding the tokens being sent
/// @param _to The address of the recipient
/// @param _amount The number of tokens to be sent
/// @param _data Data generated by the user to be sent to the recipient
/// @param _operatorData Data generated by the operator to be sent to the recipient
function operatorSend(address _from, address _to, uint256 _amount, bytes _data, bytes _operatorData) public {
require(isOperatorFor(msg.sender, _from), "Not an operator");
doSend(msg.sender, _from, _to, _amount, _data, _operatorData, true);
}

function burn(uint256 _amount, bytes _data) public {
doBurn(msg.sender, msg.sender, _amount, _data, "");
}

function operatorBurn(address _tokenHolder, uint256 _amount, bytes _data, bytes _operatorData) public {
require(isOperatorFor(msg.sender, _tokenHolder), "Not an operator");
doBurn(msg.sender, _tokenHolder, _amount, _data, _operatorData);
}

/* -- Helper Functions -- */
//
/// @notice Internal function that ensures `_amount` is multiple of the granularity
/// @param _amount The quantity that want's to be checked
function requireMultiple(uint256 _amount) internal view {
require(_amount % mGranularity == 0, "Amount is not a multiple of granualrity");
}

/// @notice Check whether an address is a regular address or not.
/// @param _addr Address of the contract that has to be checked
/// @return `true` if `_addr` is a regular address (not a contract)
function isRegularAddress(address _addr) internal view returns(bool) {
if (_addr == 0) { return false; }
uint size;
assembly { size := extcodesize(_addr) } // solium-disable-line security/no-inline-assembly
return size == 0;
}

/// @notice Helper function actually performing the sending of tokens.
/// @param _operator The address performing the send
/// @param _from The address holding the tokens being sent
/// @param _to The address of the recipient
/// @param _amount The number of tokens to be sent
/// @param _data Data generated by the user to be passed to the recipient
/// @param _operatorData Data generated by the operator to be passed to the recipient
/// @param _preventLocking `true` if you want this function to throw when tokens are sent to a contract not
/// implementing `ERC777tokensRecipient`.
/// ERC777 native Send functions MUST set this parameter to `true`, and backwards compatible ERC20 transfer
/// functions SHOULD set this parameter to `false`.
function doSend(
address _operator,
address _from,
address _to,
uint256 _amount,
bytes _data,
bytes _operatorData,
bool _preventLocking
)
internal
{
requireMultiple(_amount);

callSender(_operator, _from, _to, _amount, _data, _operatorData);

require(_to != address(0), "Cannot send to 0x0");
require(mBalances[_from] >= _amount, "Not enough funds");

mBalances[_from] = mBalances[_from].sub(_amount);
mBalances[_to] = mBalances[_to].add(_amount);

callRecipient(_operator, _from, _to, _amount, _data, _operatorData, _preventLocking);

emit Sent(_operator, _from, _to, _amount, _data, _operatorData);
}

/// @notice Helper function actually performing the burning of tokens.
/// @param _operator The address performing the burn
/// @param _tokenHolder The address holding the tokens being burn
/// @param _amount The number of tokens to be burnt
/// @param _data Data generated by the token holder
/// @param _operatorData Data generated by the operator
function doBurn(address _operator, address _tokenHolder, uint256 _amount, bytes _data, bytes _operatorData)
internal
{
callSender(_operator, _tokenHolder, 0x0, _amount, _data, _operatorData);

requireMultiple(_amount);
require(balanceOf(_tokenHolder) >= _amount, "Not enough funds");

mBalances[_tokenHolder] = mBalances[_tokenHolder].sub(_amount);
mTotalSupply = mTotalSupply.sub(_amount);

emit Burned(_operator, _tokenHolder, _amount, _data, _operatorData);
}

/// @notice Helper function that checks for ERC777TokensRecipient on the recipient and calls it.
/// May throw according to `_preventLocking`
/// @param _operator The address performing the send or mint
/// @param _from The address holding the tokens being sent
/// @param _to The address of the recipient
/// @param _amount The number of tokens to be sent
/// @param _data Data generated by the user to be passed to the recipient
/// @param _operatorData Data generated by the operator to be passed to the recipient
/// @param _preventLocking `true` if you want this function to throw when tokens are sent to a contract not
/// implementing `ERC777TokensRecipient`.
/// ERC777 native Send functions MUST set this parameter to `true`, and backwards compatible ERC20 transfer
/// functions SHOULD set this parameter to `false`.
function callRecipient(
address _operator,
address _from,
address _to,
uint256 _amount,
bytes _data,
bytes _operatorData,
bool _preventLocking
)
internal
{
address recipientImplementation = interfaceAddr(_to, "ERC777TokensRecipient");
if (recipientImplementation != 0) {
ERC777TokensRecipient(recipientImplementation).tokensReceived(
_operator, _from, _to, _amount, _data, _operatorData);
} else if (_preventLocking) {
require(isRegularAddress(_to), "Cannot send to contract without ERC777TokensRecipient");
}
}

/// @notice Helper function that checks for ERC777TokensSender on the sender and calls it.
/// May throw according to `_preventLocking`
/// @param _from The address holding the tokens being sent
/// @param _to The address of the recipient
/// @param _amount The amount of tokens to be sent
/// @param _data Data generated by the user to be passed to the recipient
/// @param _operatorData Data generated by the operator to be passed to the recipient
/// implementing `ERC777TokensSender`.
/// ERC777 native Send functions MUST set this parameter to `true`, and backwards compatible ERC20 transfer
/// functions SHOULD set this parameter to `false`.
function callSender(
address _operator,
address _from,
address _to,
uint256 _amount,
bytes _data,
bytes _operatorData
)
internal
{
address senderImplementation = interfaceAddr(_from, "ERC777TokensSender");
if (senderImplementation == 0) { return; }
ERC777TokensSender(senderImplementation).tokensToSend(
_operator, _from, _to, _amount, _data, _operatorData);
}
event Sent(
address indexed operator,
address indexed from,
address indexed to,
uint256 amount,
bytes data,
bytes operatorData
); // solhint-disable-next-line separate-by-one-line-in-contract
event Minted(address indexed operator, address indexed to, uint256 amount, bytes operatorData);
event Burned(address indexed operator, address indexed from, uint256 amount, bytes data, bytes operatorData);
event AuthorizedOperator(address indexed operator, address indexed tokenHolder);
event RevokedOperator(address indexed operator, address indexed tokenHolder);
}
Loading