编译器
0.8.20+commit.a1b79de6
文件 1 的 5:AggregatorV3Interface.sol
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);
}
文件 2 的 5:Context.sol
pragma solidity ^0.8.20;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
文件 3 的 5:IERC20.sol
pragma solidity ^0.8.20;
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
文件 4 的 5:Ownable.sol
pragma solidity ^0.8.20;
import {Context} from "../utils/Context.sol";
abstract contract Ownable is Context {
address private _owner;
error OwnableUnauthorizedAccount(address account);
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
modifier onlyOwner() {
_checkOwner();
_;
}
function owner() public view virtual returns (address) {
return _owner;
}
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
文件 5 的 5:PorkPresale.sol
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
pragma solidity ^0.8.20;
interface IUSDT {
function transferFrom(address _from, address _to, uint _value) external;
function balanceOf(address who) external returns (uint);
function transfer(address _to, uint _value) external;
}
contract PORKPresale is Ownable {
uint256 public initialPrice = 0.00001 ether;
uint256 public fixedPrice;
uint256 public finalPrice;
uint256 public tokensSold = 0;
uint256 public presaleSupply = 0;
uint256 public startTime;
bool public staticPrice = false;
bool public initialize = false;
bool public halfSupplySold = false;
struct userBalance {
uint256 balance;
uint256 claimed;
}
mapping(address => userBalance) public userBalances;
AggregatorV3Interface private dataFeed;
IUSDT usdt;
IERC20 token;
constructor(address _token, uint256 _presaleSupply) Ownable(msg.sender) {
dataFeed = AggregatorV3Interface(
0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419
);
usdt = IUSDT(0xdAC17F958D2ee523a2206206994597C13D831ec7);
token = IERC20(_token);
presaleSupply = _presaleSupply;
}
function buyPORK(uint256 _amount, uint8 _id) external payable {
require(initialize, "Presale has not been initialized.");
uint256 halfSupply = presaleSupply / 2;
if(tokensSold >= halfSupply && !halfSupplySold){
finalPrice = getCurrentRate();
halfSupplySold = true;
}
if (_id == 0) {
uint256 valueSent = msg.value;
require(valueSent > 0, "Cannot buy PORK with zero ether/usd");
uint256 _amountToReceiveInUSD = getConversionRate(valueSent);
uint256 _amountToSend = getPORKSaleAmountForETH(
_amountToReceiveInUSD
);
tokensSold += _amountToSend;
userBalances[msg.sender].balance += _amountToSend;
(bool success, ) = owner().call{value: valueSent}("");
require(success);
} else if (_id == 1) {
require(_amount > 0, "Cannot buy PORK with zero ether/usd");
uint256 _amountToReceive = _amount;
uint256 _amountToSend = getPORKSaleAmountForUSD(_amount);
require(
usdt.balanceOf(msg.sender) >= _amountToReceive,
"PORK: USDT Transfer exceeds balance"
);
tokensSold += _amountToSend;
userBalances[msg.sender].balance += _amountToSend;
usdt.transferFrom(msg.sender, address(this), _amountToReceive);
usdt.transfer(owner(), _amountToReceive);
}
}
function claim() external {
uint256 _amountToSend = userBalances[msg.sender].balance - userBalances[msg.sender].claimed;
require(_amountToSend > 0, "You've already claimed your PORK Tokens.");
require(token.balanceOf(address(this)) >= _amountToSend, "PORK: Transfer exceeds contract balance of PORK.");
userBalances[msg.sender].claimed +=_amountToSend;
token.transfer(msg.sender, _amountToSend);
}
function getPORKSaleAmountForETH(uint256 baseAmount)
public
view
returns (uint256)
{
uint256 price = getCurrentRate();
return (baseAmount * 10**18) / price;
}
function getPORKSaleAmountForUSD(uint256 baseAmount)
public
view
returns (uint256)
{
uint256 price = getCurrentRate();
return (baseAmount * 10**30) / price;
}
function getETHPrice() internal view returns (uint256) {
(, int256 answer, , , ) = dataFeed.latestRoundData();
return uint256(answer * 10000000000);
}
function getConversionRate(uint256 ethAmount)
public
view
returns (uint256)
{
uint256 ethPrice = getETHPrice();
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
return ethAmountInUsd;
}
function getCurrentRate()
public
view
returns (uint256)
{
if (staticPrice){
return fixedPrice;
}
if (halfSupplySold) {
return finalPrice;
}
uint256 daysPassed = (block.timestamp - startTime) / 1 days;
uint256 currentPrice = initialPrice;
for (uint256 i = 0; i < daysPassed; i++) {
currentPrice = currentPrice + (currentPrice * 5) / 100;
}
return currentPrice;
}
function getSecondsUntilNextHike() public view returns (uint256) {
uint256 daysPassed = (block.timestamp - startTime) / 1 days;
uint256 nextHikeTime = startTime + ((daysPassed + 1) * 1 days);
if (block.timestamp < nextHikeTime) {
return nextHikeTime - block.timestamp;
} else {
return 0;
}
}
function setPresaleSupply(uint256 _amount) public onlyOwner {
presaleSupply = _amount;
}
function withdrawPreSaleTokens(uint256 _amount) external onlyOwner {
require(
token.balanceOf(address(this)) >= _amount,
"PORK: Transfer exceeds contract balance"
);
token.transfer(msg.sender, _amount);
}
function changeInitialPrice(uint256 _price) public onlyOwner {
initialPrice = _price;
}
function setFixedPrice(uint256 _price, bool _status) public onlyOwner {
staticPrice = _status;
fixedPrice = _price;
}
function correctStartTime(uint256 _time) public onlyOwner {
startTime = _time;
}
function initializePresale(uint256 _startTime, bool _initialize) public onlyOwner {
startTime = _startTime;
initialize = _initialize;
}
function withdrawEther() external onlyOwner {
(bool success, ) = msg.sender.call{value: address(this).balance}("");
require(success);
}
function withdrawToken(address _tokenAddress) external onlyOwner {
IERC20(_tokenAddress).transfer(
msg.sender,
IERC20(_tokenAddress).balanceOf(address(this))
);
}
}
{
"compilationTarget": {
"contracts/PorkPresale.sol": "PORKPresale"
},
"evmVersion": "shanghai",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_token","type":"address"},{"internalType":"uint256","name":"_presaleSupply","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"OwnableInvalidOwner","type":"error"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"OwnableUnauthorizedAccount","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"},{"internalType":"uint8","name":"_id","type":"uint8"}],"name":"buyPORK","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_price","type":"uint256"}],"name":"changeInitialPrice","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"claim","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_time","type":"uint256"}],"name":"correctStartTime","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"finalPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"fixedPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"ethAmount","type":"uint256"}],"name":"getConversionRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCurrentRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"baseAmount","type":"uint256"}],"name":"getPORKSaleAmountForETH","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"baseAmount","type":"uint256"}],"name":"getPORKSaleAmountForUSD","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getSecondsUntilNextHike","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"halfSupplySold","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"initialPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"initialize","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_startTime","type":"uint256"},{"internalType":"bool","name":"_initialize","type":"bool"}],"name":"initializePresale","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"presaleSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_price","type":"uint256"},{"internalType":"bool","name":"_status","type":"bool"}],"name":"setFixedPrice","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"setPresaleSupply","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"startTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"staticPrice","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"tokensSold","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"userBalances","outputs":[{"internalType":"uint256","name":"balance","type":"uint256"},{"internalType":"uint256","name":"claimed","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"withdrawEther","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"withdrawPreSaleTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_tokenAddress","type":"address"}],"name":"withdrawToken","outputs":[],"stateMutability":"nonpayable","type":"function"}]