Skip to content

Commit

Permalink
Tests using plain solc & mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
0xjac committed May 9, 2019
1 parent c0ea56a commit 540cfa2
Show file tree
Hide file tree
Showing 35 changed files with 1,466 additions and 2,358 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ aliases:
- &save-build-cache
key: v1-build-cache-{{ .BuildNum }}
paths:
- ./build
- ./artifacts

- &restore-build-cache
keys:
Expand Down
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = {
"no-debugger": 0,
"no-undef": "error",
"object-curly-spacing": ["error", "always"],
"max-len": ["error", 80, 2],
"max-len": ["error", 120, 2],
"generator-star-spacing": ["error", "before"],
"promise/avoid-new": 0,
"promise/always-return": 0
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ build
node_modules
coverage
coverage.json
artifacts
File renamed without changes.
File renamed without changes.
48 changes: 27 additions & 21 deletions contracts/examples/ReferenceToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@ contract ReferenceToken is ERC777ERC20BaseToken, Ownable {
event ERC20Enabled();
event ERC20Disabled();

address private mBurnOperator;
mapping(address => bool) private mBurnOperators;

constructor(
string memory _name,
string memory _symbol,
uint256 _granularity,
address[] memory _defaultOperators,
address _burnOperator,
uint256 _initialSupply
)
public ERC777ERC20BaseToken(_name, _symbol, _granularity, _defaultOperators)
{
mBurnOperator = _burnOperator;
doMint(msg.sender, _initialSupply, "", "");
}

Expand All @@ -53,22 +51,12 @@ contract ReferenceToken is ERC777ERC20BaseToken, Ownable {
emit ERC20Enabled();
}

/* -- Mint And Burn Functions (not part of the ERC777 standard, only the Events/tokensReceived call are) -- */
//
/// @notice Generates `_amount` tokens to be assigned to `_tokenHolder`
/// Sample mint function to showcase the use of the `Minted` event and the logic to notify the recipient.
/// @param _tokenHolder The address that will be assigned the new tokens
/// @param _amount The quantity of tokens generated
/// @param _operatorData Data that will be passed to the recipient as a first transfer
function mint(
address _tokenHolder,
uint256 _amount,
bytes calldata _data,
bytes calldata _operatorData
)
external onlyOwner
{
doMint(_tokenHolder, _amount, _data, _operatorData);
function allowBurn(address burnOperator) external onlyOwner {
mBurnOperators[burnOperator] = true;
}

function revokeBurn(address burnOperator) external onlyOwner {
mBurnOperators[burnOperator] = true;
}

/// @notice Burns `_amount` tokens from `msg.sender`
Expand All @@ -92,13 +80,31 @@ contract ReferenceToken is ERC777ERC20BaseToken, Ownable {
bytes calldata _data,
bytes calldata _operatorData
)
external
external
{
require(msg.sender == mBurnOperator, "Not a burn operator");
require(mBurnOperators[msg.sender], "Not a burn operator");
require(isOperatorFor(msg.sender, _tokenHolder), "Not an operator");
doBurn(msg.sender, _tokenHolder, _amount, _data, _operatorData);
}

/* -- Mint Function (not part of the ERC777 standard, only the Events/tokensReceived call are) -- */
//
/// @notice Generates `_amount` tokens to be assigned to `_tokenHolder`
/// Sample mint function to showcase the use of the `Minted` event and the logic to notify the recipient.
/// @param _tokenHolder The address that will be assigned the new tokens
/// @param _amount The quantity of tokens generated
/// @param _operatorData Data that will be passed to the recipient as a first transfer
function mint(
address _tokenHolder,
uint256 _amount,
bytes calldata _data,
bytes calldata _operatorData
)
external onlyOwner
{
doMint(_tokenHolder, _amount, _data, _operatorData);
}

function doMint(address _tokenHolder, uint256 _amount, bytes memory _data, bytes memory _operatorData) private {
requireMultiple(_amount);
mTotalSupply = mTotalSupply.add(_amount);
Expand Down
106 changes: 106 additions & 0 deletions contracts/test/ContractAccount.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* 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.
*/
pragma solidity 0.5.3;

import { ERC1820Client } from "erc1820/contracts/ERC1820Client.sol";
import { ERC777TokensSender } from "../ERC777TokensSender.sol";
import { ERC777TokensRecipient } from "../ERC777TokensRecipient.sol";
import { ERC777Token } from "../ERC777Token.sol";


contract ContractAccount is ERC1820Client, ERC777TokensSender, ERC777TokensRecipient {
bool private allowTokensToSend;
bool private allowTokensReceived;

event NotifiedTokensToSend(
address token,
address operator,
address from,
address to,
uint256 amount,
uint256 balanceFrom,
uint256 balanceTo,
bytes data,
bytes operatorData
);

event NotifiedTokensReceived(
address token,
address operator,
address from,
address to,
uint256 amount,
uint256 balanceFrom,
uint256 balanceTo,
bytes data,
bytes operatorData
);

constructor(bool _allowTokensToSend, bool _allowTokensReceived) public {
setInterfaceImplementation("ERC777TokensSender", address(this));
setInterfaceImplementation("ERC777TokensRecipient", address(this));
allowTokensToSend = _allowTokensToSend;
allowTokensReceived = _allowTokensReceived;
}

function tokensToSend(
address _operator,
address _from,
address _to,
uint256 _amount,
bytes calldata _data,
bytes calldata _operatorData
)
external
{
require(allowTokensToSend, "Send not allowed");
emit NotifiedTokensToSend(
msg.sender,
_operator,
_from,
_to,
_amount,
ERC777Token(msg.sender).balanceOf(_from),
ERC777Token(msg.sender).balanceOf(_to),
_data,
_operatorData
);
}

function tokensReceived(
address _operator,
address _from,
address _to,
uint256 _amount,
bytes calldata _data,
bytes calldata _operatorData
)
external
{
require(allowTokensReceived, "Send not allowed");
emit NotifiedTokensReceived(
msg.sender,
_operator,
_from,
_to,
_amount,
ERC777Token(msg.sender).balanceOf(_from),
ERC777Token(msg.sender).balanceOf(_to),
_data,
_operatorData
);
}

function send(address token, address to, uint256 amount, bytes calldata data) external {
ERC777Token(token).send(to, amount, data);
}

function burn(address token, uint256 amount, bytes calldata data) external {
ERC777Token(token).operatorBurn(address(this), amount, data, "");
}
}
45 changes: 45 additions & 0 deletions js/artifacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const path = require('path');
const DEFAULT_ARTIFACTS_PATH = path.resolve(__dirname, '../artifacts/combined.json');

module.exports = function loadArtifacts(jsonPath = DEFAULT_ARTIFACTS_PATH) {
const solcJson = require(jsonPath);
const artifacts = Object.assign({}, solcJson, {contracts: {}});

Object.keys(solcJson.contracts).forEach(contractPath => {
let path = (contractPath.startsWith('./') ? contractPath.slice(2) : contractPath)
.replace('.sol:', '.').replace(/\//g, '.').split('.');
let contractName = path.pop();

let last = path.reduce((obj, key) => obj[key] = obj[key] || {}, artifacts);
last[contractName] = Object.assign({}, solcJson.contracts[contractPath]);
last[contractName].abi = JSON.parse(last[contractName].abi);

last[contractName].deploy = async (web3, options = {}) => {
if (!last[contractName].bin || last[contractName].bin === '0x') {
throw new Error(`Missing bytecode for ${contractPath}`);
}
if (!last[contractName].abi) {
throw new Error(`Missing abi for ${contractPath}`);
}
if (!options.from) {
options.from = (await web3.eth.getAccounts())[0];
}

let contract = new web3.eth.Contract(last[contractName].abi, null, { from: options.from });
const gas = await contract.deploy({ data: last[contractName].bin, arguments: options.arguments })
.estimateGas({ from: options.from });
if (!options.gas) {
options.gas = gas;
} else if (options.gas < gas) {
console.warn(`Specified gas amount ${options.gas} is lower than the estimated ${gas}.`);
}
return await contract.deploy({ arguments: options.arguments, data: last[contractName].bin })
.send({ from: options.from, gas: options.gas, gasLimit: options.gasLimit });
};

last[contractName]
.instance = (web3, address, options = {}) => new web3.eth.Contract(last[contractName].abi, address, options);
});

return artifacts
};
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
},
"scripts": {
"lint:sol": "solium --reporter pretty --dir contracts",
"lint:js": "eslint --max-warnings 0 --format eslint-formatter-stylish-verbose test migrations truffle-config.js",
"lint:js": "eslint --max-warnings 0 --format eslint-formatter-stylish-verbose test",
"lint": "npm run lint:sol && npm run lint:js",
"build:sol": "truffle compile",
"build": "npm run clean && truffle compile --all",
"test": "scripts/test.sh --network 'test'",
"build:sol": "solc 'erc1820=node_modules/erc1820' 'openzeppelin-solidity=node_modules/openzeppelin-solidity' --allow-paths=',$(pwd)/node_modules' --overwrite --optimize --optimize-runs 200 --metadata-literal --output-dir ./artifacts --combined-json abi,asm,bin,devdoc,hashes,interface,metadata,opcodes,srcmap,userdoc ./contracts/**/*.sol",
"build": "npm run clean && npm run build:sol",
"test": "mocha",
"test-old": "scripts/test.sh --network 'test-old'",
"coverage": "SOLIDITY_COVERAGE=true scripts/test.sh",
"check": "npm run lint && npm run build && npm run test",
"clean": "rm -rf ./build ./coverage ./coverage.json"
Expand Down
63 changes: 0 additions & 63 deletions scripts/test.sh

This file was deleted.

Loading

0 comments on commit 540cfa2

Please sign in to comment.