Skip to content

Latest commit

 

History

History
275 lines (214 loc) · 7.43 KB

File metadata and controls

275 lines (214 loc) · 7.43 KB

//Counter.sol

// SPDX-License-Identifier: MIT pragma solidity ^0.8.26;

contract Counter { uint256 public count;

function get() public view returns (uint256) {
    return count;
}

function inc() public {
    count += 1;
}

function dec() public {
    count -= 1;
}

}

//EtherWallet.sol

// SPDX-License-Identifier: MIT pragma solidity ^0.8.26;

contract EtherWallet { address payable public owner;

constructor() {
    owner = payable(msg.sender);
}

receive() external payable {}

function withdraw(uint256 _amount) external {
    require(msg.sender == owner, "caller is not owner");
    payable(msg.sender).transfer(_amount);
}

function getBalance() external view returns (uint256) {
    return address(this).balance;
}

}

//MyToken.sol

// SPDX-License-Identifier: MIT pragma solidity ^0.8.26;

interface IERC20 { function totalSupply() external view returns (uint256); function balanceOf(address account) external view returns (uint256); function transfer(address recipient, uint256 amount) external returns (bool); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 amount) external returns (bool); function transferFrom(address sender, address recipient, uint256 amount) external returns (bool); }

contract ERC20 is IERC20 { event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value );

uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
string public name;
string public symbol;
uint8 public decimals;

constructor(string memory _name, string memory _symbol, uint8 _decimals) {
    name = _name;
    symbol = _symbol;
    decimals = _decimals;
}

function transfer(address recipient, uint256 amount)
    external
    returns (bool)
{
    balanceOf[msg.sender] -= amount;
    balanceOf[recipient] += amount;
    emit Transfer(msg.sender, recipient, amount);
    return true;
}

function approve(address spender, uint256 amount) external returns (bool) {
    allowance[msg.sender][spender] = amount;
    emit Approval(msg.sender, spender, amount);
    return true;
}

function transferFrom(address sender, address recipient, uint256 amount)
    external
    returns (bool)
{
    allowance[sender][msg.sender] -= amount;
    balanceOf[sender] -= amount;
    balanceOf[recipient] += amount;
    emit Transfer(sender, recipient, amount);
    return true;
}

function _mint(address to, uint256 amount) internal {
    balanceOf[to] += amount;
    totalSupply += amount;
    emit Transfer(address(0), to, amount);
}

function _burn(address from, uint256 amount) internal {
    balanceOf[from] -= amount;
    totalSupply -= amount;
    emit Transfer(from, address(0), amount);
}

function mint(address to, uint256 amount) external {
    _mint(to, amount);
}

function burn(address from, uint256 amount) external {
    _burn(from, amount);
}

}

    // Mint 100 tokens to msg.sender
    // Similar to how
    // 1 dollar = 100 cents
    // 1 token = 1 * (10 ** decimals)

contract MyToken is ERC20 { constructor(string memory name, string memory symbol, uint8 decimals) ERC20(name, symbol, decimals) {} }

//CrowdFund.sol

// SPDX-License-Identifier: MIT pragma solidity ^0.8.26;

interface IERC20 { function transfer(address, uint256) external returns (bool); function transferFrom(address, address, uint256) external returns (bool); }

contract CrowdFund { event Launch( uint256 id, address indexed creator, uint256 goal, uint32 startAt, uint32 endAt ); event Cancel(uint256 id); event Pledge(uint256 indexed id, address indexed caller, uint256 amount); event Unpledge(uint256 indexed id, address indexed caller, uint256 amount); event Claim(uint256 id); event Refund(uint256 id, address indexed caller, uint256 amount);

struct Campaign {
    address creator;
    
    uint256 goal;

    uint256 pledged;

    uint32 startAt;

    uint32 endAt;

    bool claimed;
}

IERC20 public immutable token;
// Total count of campaigns created.
// It is also used to generate id for new campaigns.
uint256 public count;
// Mapping from id to Campaign
mapping(uint256 => Campaign) public campaigns;
// Mapping from campaign id => pledger => amount pledged
mapping(uint256 => mapping(address => uint256)) public pledgedAmount;

constructor(address _token) {
    token = IERC20(_token);
}

function launch(uint256 _goal, uint32 _startAt, uint32 _endAt) external {
    require(_startAt >= block.timestamp, "start at < now");
    require(_endAt >= _startAt, "end at < start at");
    require(_endAt <= block.timestamp + 90 days, "end at > max duration");

    count += 1;
    campaigns[count] = Campaign({
        creator: msg.sender,
        goal: _goal,
        pledged: 0,
        startAt: _startAt,
        endAt: _endAt,
        claimed: false
    });

    emit Launch(count, msg.sender, _goal, _startAt, _endAt);
}

function cancel(uint256 _id) external {
    Campaign memory campaign = campaigns[_id];
    require(campaign.creator == msg.sender, "not creator");
    require(block.timestamp < campaign.startAt, "started");

    delete campaigns[_id];
    emit Cancel(_id);
}

function pledge(uint256 _id, uint256 _amount) external {
    Campaign storage campaign = campaigns[_id];
    require(block.timestamp >= campaign.startAt, "not started");
    require(block.timestamp <= campaign.endAt, "ended");

    campaign.pledged += _amount;
    pledgedAmount[_id][msg.sender] += _amount;
    token.transferFrom(msg.sender, address(this), _amount);

    emit Pledge(_id, msg.sender, _amount);
}

function unpledge(uint256 _id, uint256 _amount) external {
    Campaign storage campaign = campaigns[_id];
    require(block.timestamp <= campaign.endAt, "ended");

    campaign.pledged -= _amount;
    pledgedAmount[_id][msg.sender] -= _amount;
    token.transfer(msg.sender, _amount);

    emit Unpledge(_id, msg.sender, _amount);
}

function claim(uint256 _id) external {
    Campaign storage campaign = campaigns[_id];
    require(campaign.creator == msg.sender, "not creator");
    require(block.timestamp > campaign.endAt, "not ended");
    require(campaign.pledged >= campaign.goal, "pledged < goal");
    require(!campaign.claimed, "claimed");

    campaign.claimed = true;
    token.transfer(campaign.creator, campaign.pledged);

    emit Claim(_id);
}

function refund(uint256 _id) external {
    Campaign memory campaign = campaigns[_id];
    require(block.timestamp > campaign.endAt, "not ended");
    require(campaign.pledged < campaign.goal, "pledged >= goal");

    uint256 bal = pledgedAmount[_id][msg.sender];
    pledgedAmount[_id][msg.sender] = 0;
    token.transfer(msg.sender, bal);

    emit Refund(_id, msg.sender, bal);
}

}