Skip to content

Commit

Permalink
added set token
Browse files Browse the repository at this point in the history
  • Loading branch information
AutoPumpToken authored Apr 1, 2024
1 parent a03d807 commit f2bdfbe
Showing 1 changed file with 28 additions and 42 deletions.
70 changes: 28 additions & 42 deletions src/AutoPumpPresale.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.23;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {IAutoPumpPresale} from "./interfaces/IAutoPumpPresale.sol";
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { ReentrancyGuard } from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import { IAutoPumpPresale } from "./interfaces/IAutoPumpPresale.sol";

/**
* @title AutoPumpPresale
Expand Down Expand Up @@ -72,15 +72,9 @@ contract AutoPumpPresale is Ownable, ReentrancyGuard, IAutoPumpPresale {
* @param fundraisingGoal_ Target fundraising goal for the presale
* @param rate_ Rate for each token
*/
constructor(
address wallet_,
ERC20 token_,
uint256 fundraisingGoal_,
uint256 rate_
) Ownable(msg.sender) {
constructor(address wallet_, ERC20 token_, uint256 fundraisingGoal_, uint256 rate_) Ownable(msg.sender) {
require(fundraisingGoal_ > 0, "Invalid Fundraising Goal");

Check warning on line 76 in src/AutoPumpPresale.sol

View workflow job for this annotation

GitHub Actions / Lint sources (18.x)

GC: Use Custom Errors instead of require statements
require(wallet_ != address(0), "Invalid Wallet");

Check warning on line 77 in src/AutoPumpPresale.sol

View workflow job for this annotation

GitHub Actions / Lint sources (18.x)

GC: Use Custom Errors instead of require statements
require(address(token_) != address(0), "Invalid Token");

treasuryWallet = wallet_;
token = token_;
Expand All @@ -99,6 +93,17 @@ contract AutoPumpPresale is Ownable, ReentrancyGuard, IAutoPumpPresale {
_buyTokens(msg.sender);
}

/**
* @dev See {IAutoPumpPresale-setToken}.
*/
function setToken(address newToken_) external onlyOwner {
require(newToken_ != address(0), "Invalid Token Address");

Check warning on line 100 in src/AutoPumpPresale.sol

View workflow job for this annotation

GitHub Actions / Lint sources (18.x)

GC: Use Custom Errors instead of require statements

emit TreasuryWalletUpdated(address(token), newToken_);

token = ERC20(newToken_);
}

/**
* @dev See {IAutoPumpPresale-setTreasuryWallet}.
*/
Expand Down Expand Up @@ -149,10 +154,7 @@ contract AutoPumpPresale is Ownable, ReentrancyGuard, IAutoPumpPresale {
require(presaleClosed, "Presale not closed yet");

Check warning on line 154 in src/AutoPumpPresale.sol

View workflow job for this annotation

GitHub Actions / Lint sources (18.x)

GC: Use Custom Errors instead of require statements

// Ensure the current time is at least 7 days after the presale closed
require(
block.timestamp >= closedPresaleTime + LOCKUP_PERIOD_DAYS,
"Lockup period not ended"
);
require(block.timestamp >= closedPresaleTime + LOCKUP_PERIOD_DAYS, "Lockup period not ended");

Check warning on line 157 in src/AutoPumpPresale.sol

View workflow job for this annotation

GitHub Actions / Lint sources (18.x)

GC: Use Custom Errors instead of require statements

uint256 eligibleTokens = calculateEligibleTokens(msg.sender);
BuyerInfo storage buyer = buyers[msg.sender];
Expand All @@ -171,34 +173,22 @@ contract AutoPumpPresale is Ownable, ReentrancyGuard, IAutoPumpPresale {
return buyers[buyer_].tokenBalance;
}

function getTotalTokensWithdrawn(
address buyer_
) external view returns (uint256) {
function getTotalTokensWithdrawn(address buyer_) external view returns (uint256) {
return buyers[buyer_].totalTokensWithdrawn;
}

function calculateEligibleTokens(
address buyer_
) public view returns (uint256) {
function calculateEligibleTokens(address buyer_) public view returns (uint256) {
BuyerInfo storage buyer = buyers[buyer_];

if (
!presaleClosed ||
buyer.tokenBalance == 0 ||
closedPresaleTime + LOCKUP_PERIOD_DAYS > block.timestamp
) {
if (!presaleClosed || buyer.tokenBalance == 0 || closedPresaleTime + LOCKUP_PERIOD_DAYS > block.timestamp) {
return 0; // No tokens can be withdrawn if the presale hasn't closed or nothing was purchased.
}

uint256 secondsSinceClosure = block.timestamp -
(closedPresaleTime + LOCKUP_PERIOD_DAYS);
uint256 eligibleTokens = (buyer.tokenBalance * secondsSinceClosure) /
WITHDRAWAL_PERIOD_DAYS;
uint256 secondsSinceClosure = block.timestamp - (closedPresaleTime + LOCKUP_PERIOD_DAYS);
uint256 eligibleTokens = (buyer.tokenBalance * secondsSinceClosure) / WITHDRAWAL_PERIOD_DAYS;

// Ensure we don't exceed the total owned.
uint256 totalEligible = eligibleTokens > buyer.tokenBalance
? buyer.tokenBalance
: eligibleTokens;
uint256 totalEligible = eligibleTokens > buyer.tokenBalance ? buyer.tokenBalance : eligibleTokens;
return totalEligible - buyer.totalTokensWithdrawn;
}

Expand All @@ -220,9 +210,7 @@ contract AutoPumpPresale is Ownable, ReentrancyGuard, IAutoPumpPresale {
acceptedAmount = msg.value - excess; // Adjust acceptedAmount to exclude excess

// Immediately refund excess funds to the sender.
(bool refundSuccess, ) = payable(msg.sender).call{value: excess}(
""
);
(bool refundSuccess, ) = payable(msg.sender).call{ value: excess }("");
require(refundSuccess, "Refund failed");
}

Expand All @@ -238,7 +226,7 @@ contract AutoPumpPresale is Ownable, ReentrancyGuard, IAutoPumpPresale {
emit TokenPurchase(msg.sender, acceptedAmount);

// Forward Funds to treasury wallet
(bool sent, ) = payable(treasuryWallet).call{value: acceptedAmount}("");
(bool sent, ) = payable(treasuryWallet).call{ value: acceptedAmount }("");
require(sent, "Failed to send Accepted Wei");
}

Expand All @@ -257,9 +245,7 @@ contract AutoPumpPresale is Ownable, ReentrancyGuard, IAutoPumpPresale {
* @param weiAmount_ Value in wei to be converted into tokens
* @return Number of tokens that can be purchased with the specified _weiAmount
*/
function _getTokenAmount(
uint256 weiAmount_
) internal view returns (uint256) {
function _getTokenAmount(uint256 weiAmount_) internal view returns (uint256) {
return (weiAmount_ * rate) / PRECISION_MULTIPLIER;
}
}

0 comments on commit f2bdfbe

Please sign in to comment.