minime/contracts/sample/SampleCampaign-TokenControl...

163 lines
6.8 KiB
Solidity
Raw Normal View History

2023-09-12 14:22:43 +00:00
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
/*
Copyright 2017, Jordi Baylina
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/// @title MilestoneTracker Contract
/// @author Jordi Baylina
/// @dev This contract controls the issuance of tokens for the MiniMe Token
2017-01-11 12:20:47 +00:00
/// Contract. This version specifically acts as a Campaign manager for raising
/// funds for non-profit causes, but it can be customized for any variety of
/// purposes.
import { MiniMeToken } from "../MiniMeToken.sol";
import { TokenController } from "../TokenController.sol";
import { Owned } from "./Owned.sol";
2023-09-12 18:27:44 +00:00
error NotAuthorized();
error InvalidParameters();
error PaymentRejected();
error TransferFailed(address destination);
error TokenMintFailed();
error FundingPeriodNotOver();
/// @dev This is designed to control the issuance of a MiniMe Token for a
2017-01-11 12:20:47 +00:00
/// non-profit Campaign. This contract effectively dictates the terms of the
/// funding round.
contract Campaign is TokenController, Owned {
uint256 public startFundingTime; // In UNIX Time Format
uint256 public endFundingTime; // In UNIX Time Format
uint256 public maximumFunding; // In wei
uint256 public totalCollected; // In wei
MiniMeToken public tokenContract; // The new token for this Campaign
address payable public vaultAddress; // The address to hold the funds donated
/// @notice 'Campaign()' initiates the Campaign by setting its funding
/// parameters
/// @dev There are several checks to make sure the parameters are acceptable
/// @param _startFundingTime The UNIX time that the Campaign will be able to
/// start receiving funds
/// @param _endFundingTime The UNIX time that the Campaign will stop being able
/// to receive funds
/// @param _maximumFunding In wei, the Maximum amount that the Campaign can
/// receive (currently the max is set at 10,000 ETH for the beta)
/// @param _vaultAddress The address that will store the donated funds
/// @param _tokenAddress Address of the token contract this contract controls
2023-09-12 14:22:43 +00:00
constructor(
uint256 _startFundingTime,
uint256 _endFundingTime,
uint256 _maximumFunding,
2023-09-12 14:22:43 +00:00
address payable _vaultAddress,
MiniMeToken _tokenAddress
) {
require(
(_endFundingTime >= block.timestamp) // Cannot end in the past
&& (_endFundingTime > _startFundingTime) && (_maximumFunding <= 10_000 ether) // The Beta is limited
2023-09-12 18:25:29 +00:00
&& (_vaultAddress != address(0)),
"Invalid parameters"
); // To prevent burning ETH
startFundingTime = _startFundingTime;
endFundingTime = _endFundingTime;
maximumFunding = _maximumFunding;
tokenContract = _tokenAddress; // The Deployed Token Contract
vaultAddress = _vaultAddress;
}
/// @dev The fallback function is called when ether is sent to the contract, it
/// simply calls `doPayment()` with the address that sent the ether as the
/// `_owner`. Payable is a required solidity modifier for functions to receive
/// ether, without this modifier functions will throw if ether is sent to them
2023-09-12 14:22:43 +00:00
receive() external payable {
doPayment(msg.sender);
}
/////////////////
// TokenController interface
/////////////////
/// @notice `proxyPayment()` allows the caller to send ether to the Campaign and
/// have the tokens created in an address of their choosing
/// @param _owner The address that will hold the newly created tokens
function proxyPayment(address _owner) public payable override returns (bool) {
doPayment(_owner);
return true;
}
/// @notice Notifies the controller about a transfer, for this Campaign all
/// transfers are allowed by default and no extra notifications are needed
/// @return False if the controller does not authorize the transfer
2023-09-12 18:25:29 +00:00
function onTransfer(address, address, uint256) public pure override returns (bool) {
return true;
}
/// @notice Notifies the controller about an approval, for this Campaign all
/// approvals are allowed by default and no extra notifications are needed
/// @return False if the controller does not authorize the approval
2023-09-12 18:25:29 +00:00
function onApprove(address, address, uint256) public pure override returns (bool) {
return true;
}
/// @dev `doPayment()` is an internal function that sends the ether that this
/// contract receives to the `vault` and creates tokens in the address of the
/// `_owner` assuming the Campaign is still accepting funds
/// @param _owner The address that will hold the newly created tokens
function doPayment(address _owner) internal {
// First check that the Campaign is allowed to receive this donation
2023-09-12 18:27:44 +00:00
if (
(block.timestamp > startFundingTime) || (block.timestamp > endFundingTime)
|| (tokenContract.controller() == address(0)) // Extra check
|| (msg.value == 0) || (totalCollected + msg.value > maximumFunding)
) {
revert PaymentRejected();
}
//Track how much the Campaign has collected
totalCollected += msg.value;
//Send the ether to the vault
2023-09-12 18:27:44 +00:00
if (!vaultAddress.send(msg.value)) revert TransferFailed(vaultAddress);
// Creates an equal amount of tokens as ether sent. The new tokens are created
// in the `_owner` address
2023-09-12 18:27:44 +00:00
if (!tokenContract.generateTokens(_owner, msg.value)) revert TokenMintFailed();
return;
}
/// @notice `finalizeFunding()` ends the Campaign by calling setting the
/// controller to 0, thereby ending the issuance of new tokens and stopping the
/// Campaign from receiving more ether
/// @dev `finalizeFunding()` can only be called after the end of the funding period.
2023-09-12 14:22:43 +00:00
function finalizeFunding() external {
2023-09-12 18:27:44 +00:00
if (block.timestamp > endFundingTime) revert FundingPeriodNotOver();
2023-09-12 14:22:43 +00:00
tokenContract.changeController(payable(address(0)));
}
/// @notice `onlyOwner` changes the location that ether is sent
/// @param _newVaultAddress The address that will receive the ether sent to this
/// Campaign
2023-09-12 14:22:43 +00:00
function setVault(address payable _newVaultAddress) external onlyOwner {
vaultAddress = _newVaultAddress;
}
}