remove string error messages

This commit is contained in:
Ricardo Guilherme Schmidt 2023-09-12 15:27:44 -03:00
parent 2636a070b1
commit c8581cd22d
4 changed files with 61 additions and 37 deletions

View File

@ -1,13 +1,13 @@
AllowanceTest:testAllowance() (gas: 248568) AllowanceTest:testAllowance() (gas: 248574)
AllowanceTest:testDeployment() (gas: 52144) AllowanceTest:testDeployment() (gas: 52144)
CreateCloneTokenTest:testCreateCloneToken() (gas: 2311772) CreateCloneTokenTest:testCreateCloneToken() (gas: 2209744)
CreateCloneTokenTest:testDeployment() (gas: 52099) CreateCloneTokenTest:testDeployment() (gas: 52099)
CreateCloneTokenTest:testGenerateTokens() (gas: 2186985) CreateCloneTokenTest:testGenerateTokens() (gas: 2084957)
DestroyTokensTest:testDeployment() (gas: 51928) DestroyTokensTest:testDeployment() (gas: 51928)
DestroyTokensTest:testDestroyTokens() (gas: 127040) DestroyTokensTest:testDestroyTokens() (gas: 127040)
GenerateTokensTest:testDeployment() (gas: 51883) GenerateTokensTest:testDeployment() (gas: 51883)
GenerateTokensTest:testGenerateTokens() (gas: 116764) GenerateTokensTest:testGenerateTokens() (gas: 116764)
GenerateTokensTest:test_RevertWhen_SenderIsNotController() (gas: 15002) GenerateTokensTest:test_RevertWhen_SenderIsNotController() (gas: 14930)
MiniMeTokenTest:testDeployment() (gas: 51928) MiniMeTokenTest:testDeployment() (gas: 51928)
TransferTest:testDeployment() (gas: 52144) TransferTest:testDeployment() (gas: 52144)
TransferTest:testTransfer() (gas: 205602) TransferTest:testTransfer() (gas: 205596)

View File

@ -1,11 +1,13 @@
// SPDX-License-Identifier: GPL-3.0 // SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0; pragma solidity ^0.8.0;
error NotAuthorized();
contract Controlled { contract Controlled {
/// @notice The address of the controller is the only address that can call /// @notice The address of the controller is the only address that can call
/// a function with this modifier /// a function with this modifier
modifier onlyController() { modifier onlyController() {
require(msg.sender == controller, "Not authorized"); if (msg.sender != controller) revert NotAuthorized();
_; _;
} }

View File

@ -1,6 +1,17 @@
// SPDX-License-Identifier: GPL-3.0 // SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0; pragma solidity ^0.8.0;
error TransfersDisabled();
error ParentSnapshotNotReached();
error NotEnoughBalance();
error NotEnoughAllowance();
error NotEnoughSupply();
error InvalidDestination();
error ControllerRejected();
error Overflow();
error AllowanceAlreadySet();
error OperationFailed();
error ControllerNotSet();
/* /*
Copyright 2016, Jordi Baylina Copyright 2016, Jordi Baylina
@ -125,7 +136,7 @@ contract MiniMeToken is Controlled {
/// @param _amount The amount of tokens to be transferred /// @param _amount The amount of tokens to be transferred
/// @return success Whether the transfer was successful or not /// @return success Whether the transfer was successful or not
function transfer(address _to, uint256 _amount) public returns (bool success) { function transfer(address _to, uint256 _amount) public returns (bool success) {
require(transfersEnabled, "Transfers are not enabled"); if (!transfersEnabled) revert TransfersDisabled();
doTransfer(msg.sender, _to, _amount); doTransfer(msg.sender, _to, _amount);
return true; return true;
} }
@ -142,10 +153,10 @@ contract MiniMeToken is Controlled {
// controller of this contract, which in most situations should be // controller of this contract, which in most situations should be
// another open source smart contract or 0x0 // another open source smart contract or 0x0
if (msg.sender != controller) { if (msg.sender != controller) {
require(transfersEnabled, "Transfers are not enabled"); if (!transfersEnabled) revert TransfersDisabled();
// The standard ERC 20 transferFrom functionality // The standard ERC 20 transferFrom functionality
require(allowed[_from][msg.sender] >= _amount, "Not enough allowed balance"); if (allowed[_from][msg.sender] < _amount) revert NotEnoughAllowance();
allowed[_from][msg.sender] -= _amount; allowed[_from][msg.sender] -= _amount;
} }
doTransfer(_from, _to, _amount); doTransfer(_from, _to, _amount);
@ -163,20 +174,22 @@ contract MiniMeToken is Controlled {
return; return;
} }
require(parentSnapShotBlock < block.number, "Parent snapshot not reached"); if (parentSnapShotBlock >= block.number) revert ParentSnapshotNotReached();
// Do not allow transfer to 0x0 or the token contract itself // Do not allow transfer to 0x0 or the token contract itself
require((_to != address(0)) && (_to != address(this)), "Invalid destination"); if ((_to == address(0)) || (_to == address(this))) revert InvalidDestination();
// If the amount being transfered is more than the balance of the // If the amount being transfered is more than the balance of the
// account the transfer throws // account the transfer throws
uint256 previousBalanceFrom = balanceOfAt(_from, block.number); uint256 previousBalanceFrom = balanceOfAt(_from, block.number);
require(previousBalanceFrom >= _amount, "Not enough balance"); if (previousBalanceFrom < _amount) revert NotEnoughBalance();
// Alerts the token controller of the transfer // Alerts the token controller of the transfer
if (isContract(controller)) { if (isContract(controller)) {
require(TokenController(controller).onTransfer(_from, _to, _amount), "Controller rejected transfer"); if (!TokenController(controller).onTransfer(_from, _to, _amount)) {
revert ControllerRejected();
}
} }
// First update the balance array with the new value for the address // First update the balance array with the new value for the address
@ -186,7 +199,7 @@ contract MiniMeToken is Controlled {
// Then update the balance array with the new value for the address // Then update the balance array with the new value for the address
// receiving the tokens // receiving the tokens
uint256 previousBalanceTo = balanceOfAt(_to, block.number); uint256 previousBalanceTo = balanceOfAt(_to, block.number);
require(previousBalanceTo + _amount >= previousBalanceTo, "Balance overflow"); // Check for overflow if (previousBalanceTo + _amount < previousBalanceTo) revert Overflow(); // Check for overflow
updateValueAtNow(balances[_to], previousBalanceTo + _amount); updateValueAtNow(balances[_to], previousBalanceTo + _amount);
// An event to make the transfer easy to find on the blockchain // An event to make the transfer easy to find on the blockchain
@ -206,19 +219,19 @@ contract MiniMeToken is Controlled {
/// @param _amount The amount of tokens to be approved for transfer /// @param _amount The amount of tokens to be approved for transfer
/// @return success True if the approval was successful /// @return success True if the approval was successful
function approve(address _spender, uint256 _amount) public returns (bool success) { function approve(address _spender, uint256 _amount) public returns (bool success) {
require(transfersEnabled, "Transfers are not enabled"); if (!transfersEnabled) revert TransfersDisabled();
// To change the approve amount you first have to reduce the addresses` // To change the approve amount you first have to reduce the addresses`
// allowance to zero by calling `approve(_spender,0)` if it is not // allowance to zero by calling `approve(_spender,0)` if it is not
// already 0 to mitigate the race condition described here: // already 0 to mitigate the race condition described here:
// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 // https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
require((_amount == 0) || (allowed[msg.sender][_spender] == 0), "First reset allowance"); if ((_amount != 0) && (allowed[msg.sender][_spender] != 0)) revert AllowanceAlreadySet();
// Alerts the token controller of the approve function call // Alerts the token controller of the approve function call
if (isContract(controller)) { if (isContract(controller)) {
require( if (!TokenController(controller).onApprove(msg.sender, _spender, _amount)) {
TokenController(controller).onApprove(msg.sender, _spender, _amount), "Controller rejected allowance" revert ControllerRejected();
); }
} }
allowed[msg.sender][_spender] = _amount; allowed[msg.sender][_spender] = _amount;
@ -243,7 +256,7 @@ contract MiniMeToken is Controlled {
/// @param _amount The amount of tokens to be approved for transfer /// @param _amount The amount of tokens to be approved for transfer
/// @return success True if the function call was successful /// @return success True if the function call was successful
function approveAndCall(address _spender, uint256 _amount, bytes memory _extraData) public returns (bool success) { function approveAndCall(address _spender, uint256 _amount, bytes memory _extraData) public returns (bool success) {
require(approve(_spender, _amount), "Approve failed"); if (!approve(_spender, _amount)) revert OperationFailed();
ApproveAndCallFallBack(_spender).receiveApproval(msg.sender, _amount, address(this), _extraData); ApproveAndCallFallBack(_spender).receiveApproval(msg.sender, _amount, address(this), _extraData);
@ -352,9 +365,9 @@ contract MiniMeToken is Controlled {
/// @return True if the tokens are generated correctly /// @return True if the tokens are generated correctly
function generateTokens(address _owner, uint256 _amount) public onlyController returns (bool) { function generateTokens(address _owner, uint256 _amount) public onlyController returns (bool) {
uint256 curTotalSupply = totalSupply(); uint256 curTotalSupply = totalSupply();
require(curTotalSupply + _amount >= curTotalSupply, "Total supply overflow"); // Check for overflow if (curTotalSupply + _amount < curTotalSupply) revert Overflow(); // Check for overflow
uint256 previousBalanceTo = balanceOf(_owner); uint256 previousBalanceTo = balanceOf(_owner);
require(previousBalanceTo + _amount >= previousBalanceTo, "Balance overflow"); // Check for overflow if (previousBalanceTo + _amount < previousBalanceTo) revert Overflow(); // Check for overflow
updateValueAtNow(totalSupplyHistory, curTotalSupply + _amount); updateValueAtNow(totalSupplyHistory, curTotalSupply + _amount);
updateValueAtNow(balances[_owner], previousBalanceTo + _amount); updateValueAtNow(balances[_owner], previousBalanceTo + _amount);
emit Transfer(address(0), _owner, _amount); emit Transfer(address(0), _owner, _amount);
@ -367,9 +380,9 @@ contract MiniMeToken is Controlled {
/// @return True if the tokens are burned correctly /// @return True if the tokens are burned correctly
function destroyTokens(address _owner, uint256 _amount) public onlyController returns (bool) { function destroyTokens(address _owner, uint256 _amount) public onlyController returns (bool) {
uint256 curTotalSupply = totalSupply(); uint256 curTotalSupply = totalSupply();
require(curTotalSupply >= _amount, "Not enough supply"); if (curTotalSupply < _amount) revert NotEnoughSupply();
uint256 previousBalanceFrom = balanceOf(_owner); uint256 previousBalanceFrom = balanceOf(_owner);
require(previousBalanceFrom >= _amount, "Not enough balance"); if (previousBalanceFrom < _amount) revert NotEnoughBalance();
updateValueAtNow(totalSupplyHistory, curTotalSupply - _amount); updateValueAtNow(totalSupplyHistory, curTotalSupply - _amount);
updateValueAtNow(balances[_owner], previousBalanceFrom - _amount); updateValueAtNow(balances[_owner], previousBalanceFrom - _amount);
emit Transfer(_owner, address(0), _amount); emit Transfer(_owner, address(0), _amount);
@ -454,8 +467,10 @@ contract MiniMeToken is Controlled {
/// set to 0, then the `proxyPayment` method is called which relays the /// set to 0, then the `proxyPayment` method is called which relays the
/// ether and creates tokens as described in the token controller contract /// ether and creates tokens as described in the token controller contract
receive() external payable { receive() external payable {
require(isContract(controller), "Controller not set"); if (!isContract(controller)) revert ControllerNotSet();
require(TokenController(controller).proxyPayment{ value: msg.value }(msg.sender), "Controller rejected payment"); if (!TokenController(controller).proxyPayment{ value: msg.value }(msg.sender)) {
revert ControllerRejected();
}
} }
////////// //////////

View File

@ -28,13 +28,20 @@ pragma solidity ^0.8.0;
import { MiniMeToken } from "./MiniMeToken.sol"; import { MiniMeToken } from "./MiniMeToken.sol";
import { TokenController } from "./TokenController.sol"; import { TokenController } from "./TokenController.sol";
error NotAuthorized();
error InvalidParameters();
error PaymentRejected();
error TransferFailed(address destination);
error TokenMintFailed();
error FundingPeriodNotOver();
/// @dev `Owned` is a base level contract that assigns an `owner` that can be /// @dev `Owned` is a base level contract that assigns an `owner` that can be
/// later changed /// later changed
contract Owned { contract Owned {
/// @dev `owner` is the only address that can call a function with this /// @dev `owner` is the only address that can call a function with this
/// modifier /// modifier
modifier onlyOwner() { modifier onlyOwner() {
require(msg.sender == owner, "Not authorized"); if (msg.sender != owner) revert NotAuthorized();
_; _;
} }
@ -137,25 +144,25 @@ contract Campaign is TokenController, Owned {
/// contract receives to the `vault` and creates tokens in the address of the /// contract receives to the `vault` and creates tokens in the address of the
/// `_owner` assuming the Campaign is still accepting funds /// `_owner` assuming the Campaign is still accepting funds
/// @param _owner The address that will hold the newly created tokens /// @param _owner The address that will hold the newly created tokens
function doPayment(address _owner) internal { function doPayment(address _owner) internal {
// First check that the Campaign is allowed to receive this donation // First check that the Campaign is allowed to receive this donation
require( if (
(block.timestamp >= startFundingTime) && (block.timestamp <= endFundingTime) (block.timestamp > startFundingTime) || (block.timestamp > endFundingTime)
&& (tokenContract.controller() != address(0)) // Extra check || (tokenContract.controller() == address(0)) // Extra check
&& (msg.value != 0) && (totalCollected + msg.value <= maximumFunding), || (msg.value == 0) || (totalCollected + msg.value > maximumFunding)
"Payment rejected" ) {
); revert PaymentRejected();
}
//Track how much the Campaign has collected //Track how much the Campaign has collected
totalCollected += msg.value; totalCollected += msg.value;
//Send the ether to the vault //Send the ether to the vault
require(vaultAddress.send(msg.value), "Vault transfer failed"); if (!vaultAddress.send(msg.value)) revert TransferFailed(vaultAddress);
// Creates an equal amount of tokens as ether sent. The new tokens are created // Creates an equal amount of tokens as ether sent. The new tokens are created
// in the `_owner` address // in the `_owner` address
require(tokenContract.generateTokens(_owner, msg.value), "Token mint failed"); if (!tokenContract.generateTokens(_owner, msg.value)) revert TokenMintFailed();
return; return;
} }
@ -166,7 +173,7 @@ contract Campaign is TokenController, Owned {
/// @dev `finalizeFunding()` can only be called after the end of the funding period. /// @dev `finalizeFunding()` can only be called after the end of the funding period.
function finalizeFunding() external { function finalizeFunding() external {
require(block.timestamp >= endFundingTime, "Funding period not over"); if (block.timestamp > endFundingTime) revert FundingPeriodNotOver();
tokenContract.changeController(payable(address(0))); tokenContract.changeController(payable(address(0)));
} }