Skip to content

Commit

Permalink
Add min and max range for Sale and deploy script (#190)
Browse files Browse the repository at this point in the history
Why:
* The Sale contract needs to have a minimum and maximum amount to raise.
  When the maximum is reached, Rising Tide triggers for the final time
* We want to have a mock Sale contract deployed in Sepolia so we can
  start testing the integration with the UI

How:
* Adding the `minTarget` and `maxTarget` variables to the Sale contract
* Adding a `Deploy.s.sol` script to deploy mock Sale and MockERC20
contracts
* Updating the tests and the foundry config
  • Loading branch information
DavideSilva authored Apr 8, 2024
1 parent 7463893 commit d0e38d6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
14 changes: 13 additions & 1 deletion packages/contracts/contracts/token/Sale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ contract Sale is ISale, RisingTide, ERC165, AccessControl, ReentrancyGuard {

uint256 public immutable totalTokensForSale;

uint256 public immutable minTarget;
uint256 public immutable maxTarget;

/// Token allocations committed by each buyer
mapping(address => Account) accounts;

Expand All @@ -98,19 +101,28 @@ contract Sale is ISale, RisingTide, ERC165, AccessControl, ReentrancyGuard {
uint256 _rate,
uint256 _start,
uint256 _end,
uint256 _totalTokensForSale
uint256 _totalTokensForSale,
uint256 _minTarget,
uint256 _maxTarget
) {
require(_rate > 0, "can't be zero");
require(_paymentToken != address(0), "can't be zero");
require(_start > 0, "can't be zero");
require(_end > _start, "end must be after start");
require(_totalTokensForSale > 0, "total cannot be 0");
require(_minTarget > 0, "_minTarget cannot be 0");
require(
_maxTarget > _minTarget,
"_maxTarget cannot be lower than _minTarget"
);

paymentToken = _paymentToken;
rate = _rate;
start = _start;
end = _end;
totalTokensForSale = _totalTokensForSale;
minTarget = _minTarget;
maxTarget = _maxTarget;

_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
_grantRole(CAP_VALIDATOR_ROLE, msg.sender);
Expand Down
6 changes: 6 additions & 0 deletions packages/contracts/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ out = 'out'
libs = ['lib', 'node_modules']
test = 'test'
cache_path = 'cache_forge'

[rpc_endpoints]
sepolia = "${ETH_RPC_URL}"

[etherscan]
sepolia = { key = "${ETHERSCAN_API_KEY}"}
29 changes: 29 additions & 0 deletions packages/contracts/script/Deploy.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

import {Script} from "lib/forge-std/src/Script.sol";

import {MockERC20} from "contracts/test/MockERC20.sol";
import {Sale} from "contracts/token/Sale.sol";

contract DeployScript is Script {
function run() public {
vm.startBroadcast();

uint start = vm.unixTime();
uint end = start + 60 * 60 * 24 * 7;

MockERC20 token = new MockERC20("USDC", "USDC", 18);
Sale sale = new Sale(
address(token),
1 ** 18,
start,
end,
1000,
1000000,
2000000
);

vm.stopBroadcast();
}
}
10 changes: 9 additions & 1 deletion packages/contracts/script/DevDeploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ contract DevDeployScript is Script {
end = start + 60 * 60 * 24;

MockERC20 token = new MockERC20("USDC", "USDC", 18);
Sale sale = new Sale(address(token), 1 ** 18, start, end, 1000);
Sale sale = new Sale(
address(token),
1 ** 18,
start,
end,
1000,
1000000,
2000000
);

bool success = token.approve(address(sale), 1000 ether);
require(success, "approve failed");
Expand Down
10 changes: 9 additions & 1 deletion packages/contracts/test/contracts/token/Sale.d.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ contract SaleTest is Test {
end = start + 60 * 60 * 24;

paymentToken = new MockERC20("USDC", "USDC", 18);
sale = new Sale(address(paymentToken), 1 ** 18, start, end, 100);
sale = new Sale(
address(paymentToken),
1 ** 18,
start,
end,
100,
1000000,
2000000
);

vm.stopPrank();

Expand Down

0 comments on commit d0e38d6

Please sign in to comment.