// Sources flattened with hardhat v2.8.0 https://hardhat.org
// File @openzeppelin/contracts/utils/Context.sol@v4.4.1
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
pragma solidity ^0.8.0;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
// File @openzeppelin/contracts/access/Ownable.sol@v4.4.1
// OpenZeppelin Contracts v4.4.1 (access/Ownable.sol)
pragma solidity ^0.8.0;
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
// File @chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol@v0.6.1
pragma solidity ^0.8.0;
interface AggregatorV3Interface {
function decimals() external view returns (uint8);
function description() external view returns (string memory);
function version() external view returns (uint256);
function getRoundData(uint80 _roundId)
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
function latestRoundData()
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
}
// File contracts/ETHSeedSale.sol
pragma solidity ^0.8.9;
/// @title Seed Sale
contract ETHSeedSale is Ownable {
mapping(address => uint256) public participants;
mapping(address => int256) public participantTokens;
int256 internal constant PRECISION = 1 ether;
int256 internal constant DECIMALS = 10**8;
int256 public BUY_PRICE; //buy price in format 1 base token = amount of sell token, 1 ETH = 0.01 Token
uint256 public SOFTCAP; //soft cap
uint256 public HARDCAP; //hard cap
uint256 public MIN_ETH_PER_WALLET; //min base token per wallet
uint256 public MAX_ETH_PER_WALLET; //max base token per wallet
uint256 public SALE_LENGTH; //sale length in seconds
enum STATUS {
QUED,
ACTIVE,
SUCCESS,
FAILED
}
uint256 public totalCollected; //total ETH collected
int256 public totalSold; //total sold tokens
uint256 public startTime; //start time for presale
uint256 public endTime; //end time for presale
bool forceFailed; //force failed, emergency
AggregatorV3Interface internal priceFeed;
event SellToken(address recipient, int256 tokensSold, uint256 value, int256 amountInUSD, int256 price);
event Refund(address recipient, uint256 ETHToRefund);
event ForceFailed();
event Withdraw(address recipient, uint256 amount);
event SaleTokenChanged(address saleToken);
constructor(
int256 _buyPrice,
uint256 _softCap,
uint256 _hardCap,
uint256 _minETHPerWallet,
uint256 _maxETHPerWallet,
uint256 _startTime,
uint256 _sellLengh,
address _priceFeed
) {
BUY_PRICE = _buyPrice;
SOFTCAP = _softCap;
HARDCAP = _hardCap;
MIN_ETH_PER_WALLET = _minETHPerWallet;
MAX_ETH_PER_WALLET = _maxETHPerWallet;
SALE_LENGTH = _sellLengh; //2 days, 48 hours
startTime = _startTime;
endTime = _startTime + SALE_LENGTH;
priceFeed = AggregatorV3Interface(
_priceFeed
);
}
receive() external payable {
sell();
}
/// @notice sell
/// @dev before this, need approve
function sell() public payable {
uint256 _amount = msg.value;
require(status() == STATUS.ACTIVE, "SeedSale: sale is not started yet or ended");
require(_amount >= MIN_ETH_PER_WALLET, "SeedSale: insufficient purchase amount");
require(_amount <= MAX_ETH_PER_WALLET, "SeedSale: reached purchase amount");
require(participants[_msgSender()] < MAX_ETH_PER_WALLET, "SeedSale: the maximum amount of purchases has been reached");
uint256 newTotalCollected = totalCollected + _amount;
if (HARDCAP < newTotalCollected) {
// Refund anything above the hard cap
uint256 diff = newTotalCollected - HARDCAP;
_amount = _amount - diff;
}
if (_amount >= MAX_ETH_PER_WALLET - participants[_msgSender()]) {
_amount = MAX_ETH_PER_WALLET - participants[_msgSender()];
}
// Save participants eth
participants[_msgSender()] = participants[_msgSender()] + _amount;
// 2* 10^18 * 182221 * 10^6 / 10^8 = 364442 * 10^16
int256 price = getLatestPrice();
int256 amountInUSD = int256(_amount) * price / DECIMALS;
int256 tokensSold = amountInUSD * PRECISION / BUY_PRICE;
// Save participant tokens
participantTokens[_msgSender()] = participantTokens[_msgSender()] + tokensSold;
// Update total ETH
totalCollected = totalCollected + _amount;
// Update tokens sold
totalSold = totalSold + tokensSold;
if (_amount < msg.value) {
//refund
_deliverFunds(_msgSender(), msg.value - _amount, "Cant send ETH");
}
emit SellToken(_msgSender(), tokensSold, _amount, amountInUSD, price);
}
function getLatestPrice() public view returns (int) {
// prettier-ignore
(
/* uint80 roundID */,
int price,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = priceFeed.latestRoundData();
return price;
}
/// @notice refund base tokens
/// @dev only if sale status is failed
function refund() external {
require(status() == STATUS.FAILED, "SeedSale: sale is failed");
require(participants[_msgSender()] > 0, "SeedSale: no tokens for refund");
uint256 ETHToRefund = participants[_msgSender()];
participants[_msgSender()] = 0;
_withdraw(_msgSender(), ETHToRefund);
emit Refund(_msgSender(), ETHToRefund);
}
///@notice withdraw all ETH
///@param _recipient address
///@dev from owner
function withdraw(address _recipient) external virtual onlyOwner {
require(status() == STATUS.SUCCESS, "SeedSale: failed or active");
_withdraw(_recipient, address(this).balance);
}
/// @notice force fail contract
/// @dev in other world, emergency exit
function forceFail() external onlyOwner {
forceFailed = true;
emit ForceFailed();
}
/// sale status
function status() public view returns (STATUS) {
if (forceFailed) {
return STATUS.FAILED;
}
if ((block.timestamp > endTime) && (totalCollected < SOFTCAP)) {
return STATUS.FAILED; // FAILED - SOFTCAP not met by end time
}
if (totalCollected >= HARDCAP) {
return STATUS.SUCCESS; // SUCCESS - HARDCAP met
}
if ((block.timestamp > endTime) && (totalCollected >= SOFTCAP)) {
return STATUS.SUCCESS; // SUCCESS - endblock and soft cap reached
}
if ((block.timestamp >= startTime) && (block.timestamp <= endTime)) {
return STATUS.ACTIVE; // ACTIVE - deposits enabled
}
return STATUS.QUED; // QUED - awaiting start time
}
///@notice get token amount
///@param _account account
function getTokenAmount(address _account) public view returns (int256 tokenAmount) {
tokenAmount = participantTokens[_account];
}
function _withdraw(address _recipient, uint256 _amount) internal virtual {
require(_recipient != address(0x0), "SeedSale: address is zero");
require(_amount <= address(this).balance, "SeedSale: not enought ETH balance");
_deliverFunds(_recipient, _amount, "SeedSale: Cant send ETH");
}
function _deliverFunds(
address _recipient,
uint256 _value,
string memory _message
) internal {
if (_value > address(this).balance) {
_value = address(this).balance;
}
(bool sent, ) = payable(_recipient).call{value: _value}("");
require(sent, _message);
emit Withdraw(_recipient, _value);
}
}
{
"compilationTarget": {
"ETHSeedSale.sol": "ETHSeedSale"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"int256","name":"_buyPrice","type":"int256"},{"internalType":"uint256","name":"_softCap","type":"uint256"},{"internalType":"uint256","name":"_hardCap","type":"uint256"},{"internalType":"uint256","name":"_minETHPerWallet","type":"uint256"},{"internalType":"uint256","name":"_maxETHPerWallet","type":"uint256"},{"internalType":"uint256","name":"_startTime","type":"uint256"},{"internalType":"uint256","name":"_sellLengh","type":"uint256"},{"internalType":"address","name":"_priceFeed","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[],"name":"ForceFailed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"ETHToRefund","type":"uint256"}],"name":"Refund","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"saleToken","type":"address"}],"name":"SaleTokenChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"int256","name":"tokensSold","type":"int256"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"},{"indexed":false,"internalType":"int256","name":"amountInUSD","type":"int256"},{"indexed":false,"internalType":"int256","name":"price","type":"int256"}],"name":"SellToken","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Withdraw","type":"event"},{"inputs":[],"name":"BUY_PRICE","outputs":[{"internalType":"int256","name":"","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"HARDCAP","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"MAX_ETH_PER_WALLET","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"MIN_ETH_PER_WALLET","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"SALE_LENGTH","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"SOFTCAP","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"endTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"forceFail","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"getLatestPrice","outputs":[{"internalType":"int256","name":"","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"getTokenAmount","outputs":[{"internalType":"int256","name":"tokenAmount","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"participantTokens","outputs":[{"internalType":"int256","name":"","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"participants","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"refund","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"sell","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"startTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"status","outputs":[{"internalType":"enum ETHSeedSale.STATUS","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalCollected","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSold","outputs":[{"internalType":"int256","name":"","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_recipient","type":"address"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]