Skip to content

Instantly share code, notes, and snippets.

@gruvin
Last active June 23, 2020 09:27
Show Gist options
  • Save gruvin/95933afee9fd5cdc7fde520d9871de1a to your computer and use it in GitHub Desktop.
Save gruvin/95933afee9fd5cdc7fde520d9871de1a to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.9+commit.a1d534e.js&optimize=false&gist=
pragma solidity ^0.5.9;
contract Ownable {
address public owner;
string test2;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
}
pragma solidity ^0.5.9;
import "./SimpleCoin.sol";
contract ReleasableSimpleCoin is SimpleCoin {
bool public released = false;
modifier isReleased() {
if(!released) {
revert();
}
_;
}
constructor(uint256 _initialSupply)
SimpleCoin(_initialSupply) public {}
function release() onlyOwner public {
released = true;
}
function transfer(address _to, uint256 _amount)
isReleased public {
super.transfer(_to, _amount);
}
function transferFrom(address _from, address _to, uint256 _amount)
isReleased public returns (bool) {
super.transferFrom(_from, _to, _amount);
}
}
pragma solidity ^0.5.9;
contract SimpleCoin {
mapping (address => uint256) public coinBalance;
mapping (address => mapping (address => uint256)) public allowance;
mapping (address => bool) public frozenAccount;
address public owner;
event Transfer(address indexed from, address indexed to, uint256 value);
event FrozenAccount(address target, bool frozen);
modifier onlyOwner {
if (msg.sender != owner) revert();
_;
}
constructor(uint256 _initialSupply) public {
owner = msg.sender;
mint(owner, _initialSupply);
}
function transfer(address _to, uint256 _amount) public {
require(_to != address(0x0));
require(coinBalance[msg.sender] > _amount);
require(coinBalance[_to] + _amount >= coinBalance[_to] );
coinBalance[msg.sender] -= _amount;
coinBalance[_to] += _amount;
emit Transfer(msg.sender, _to, _amount);
}
function authorize(address _authorizedAccount, uint256 _allowance)
public returns (bool success) {
allowance[msg.sender][_authorizedAccount] = _allowance;
return true;
}
function transferFrom(address _from, address _to, uint256 _amount)
public returns (bool success) {
require(_to != address(0x0));
require(coinBalance[_from] > _amount);
require(coinBalance[_to] + _amount >= coinBalance[_to] );
require(_amount <= allowance[_from][msg.sender]);
coinBalance[_from] -= _amount;
coinBalance[_to] += _amount;
allowance[_from][msg.sender] -= _amount;
emit Transfer(_from, _to, _amount);
return true;
}
function mint(address _recipient, uint256 _mintedAmount)
onlyOwner public {
coinBalance[_recipient] += _mintedAmount;
emit Transfer(owner, _recipient, _mintedAmount);
}
function freezeAccount(address target, bool freeze)
onlyOwner public {
frozenAccount[target] = freeze;
emit FrozenAccount(target, freeze);
}
}
pragma solidity ^0.5.9;
import "./ReleasableSimpleCoin.sol";
import "./Ownable.sol";
contract SimpleCrowdsale is Ownable {
uint256 public startTime;
uint256 public endTime;
uint256 public weiTokenPrice;
uint256 public weiInvestmentObjective;
mapping (address => uint256) public investmentAmountOf;
uint256 public investmentReceived;
uint256 public investmentRefunded;
bool public isFinalized;
bool public isRefundingAllowed;
ReleasableSimpleCoin public crowdsaleToken;
constructor(uint256 _startTime, uint256 _endTime,
uint256 _weiTokenPrice,
uint256 _weiInvestmentObjective)
payable public
{
require(_startTime >= now);
require(_endTime >= _startTime);
require(_weiTokenPrice != 0);
require(_weiInvestmentObjective != 0);
startTime = _startTime;
endTime = _endTime;
weiTokenPrice = _weiTokenPrice;
weiInvestmentObjective = _weiInvestmentObjective;
crowdsaleToken = new ReleasableSimpleCoin(0);
isFinalized = false;
}
event LogInvestment(address indexed investor, uint256 value);
event LogTokenAssignment(address indexed investor, uint256 numTokens);
event Refund(address investor, uint256 value);
function invest() public payable {
require(isValidInvestment(msg.value));
address investor = msg.sender;
uint256 investment = msg.value;
investmentAmountOf[investor] += investment;
investmentReceived += investment;
assignTokens(investor, investment);
emit LogInvestment(investor, investment);
}
function isValidInvestment(uint256 _investment)
internal pure returns (bool) {
bool nonZeroInvestment = _investment != 0;
bool withinCrowdsalePeriod = true; // now >= startTime && now <= endTime;
return nonZeroInvestment && withinCrowdsalePeriod;
}
function assignTokens(address _beneficiary,
uint256 _investment) internal {
uint256 _numberOfTokens = calculateNumberOfTokens(_investment);
crowdsaleToken.mint(_beneficiary, _numberOfTokens);
}
function calculateNumberOfTokens(uint256 _investment)
internal returns (uint256) {
return _investment / weiTokenPrice;
}
function finalize() onlyOwner public {
if (isFinalized) revert();
bool isCrowdsaleComplete = true; // now > endTime;
bool investmentObjectiveMet =
investmentReceived >= weiInvestmentObjective;
if (isCrowdsaleComplete)
{
if (investmentObjectiveMet)
crowdsaleToken.release();
else
isRefundingAllowed = true;
isFinalized = true;
}
}
function refund() public {
if (!isRefundingAllowed) revert();
address payable investor = msg.sender;
uint256 investment = investmentAmountOf[investor];
if (investment == 0) revert();
investmentAmountOf[investor] = 0;
investmentRefunded += investment;
emit Refund(msg.sender, investment);
investor.transfer(investment);
}
}
pragma solidity ^0.4.24;
import "./SimpleCrowdsale.sol";
contract TranchePricingCrowdsale
is SimpleCrowdsale {
struct Tranche {
uint256 weiHighLimit;
uint256 weiTokenPrice;
}
mapping(uint256 => Tranche)
public trancheStructure;
uint256 public currentTrancheLevel;
constructor(uint256 _startTime, uint256 _endTime,
uint256 _etherInvestmentObjective)
SimpleCrowdsale(_startTime, _endTime,
1, _etherInvestmentObjective)
payable public
{
trancheStructure[0] = Tranche(3000 ether,
0.002 ether);
trancheStructure[1] = Tranche(10000 ether,
0.003 ether);
trancheStructure[2] = Tranche(15000 ether,
0.004 ether);
trancheStructure[3] = Tranche(1000000000 ether,
0.005 ether);
currentTrancheLevel = 0;
}
// override
function calculateNumberOfTokens(
uint256 investment)
internal returns (uint256) {
updateCurrentTrancheAndPrice();
return investment / weiTokenPrice;
}
function updateCurrentTrancheAndPrice()
internal {
uint256 i = currentTrancheLevel;
while(trancheStructure[i].weiHighLimit < investmentReceived)
++i;
currentTrancheLevel = i;
weiTokenPrice = trancheStructure[currentTrancheLevel].weiTokenPrice;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment