Skip to content

Instantly share code, notes, and snippets.

@ericoporto
Last active November 1, 2019 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericoporto/3dbc698097b3b6855f3adfacffdf2aec to your computer and use it in GitHub Desktop.
Save ericoporto/3dbc698097b3b6855f3adfacffdf2aec to your computer and use it in GitHub Desktop.
A game like Ethereum fund in Solidity
pragma solidity ^0.5.11;
contract Fund {
address payable manager = msg.sender;
uint reserve = 0;
uint redemptionFee = 100;
struct Account{
uint balance;
bool isValid;
}
uint perAccountExtra;
uint accountCount;
mapping(address => Account) account;
modifier onlyManager() {
require(manager == msg.sender);
_;
}
function Invest() public payable {
if(account[msg.sender].isValid == false){
account[msg.sender].isValid = true;
accountCount = accountCount + 1;
}
account[msg.sender].balance = account[msg.sender].balance + msg.value;
}
function Redeem() public {
require(account[msg.sender].balance > redemptionFee);
uint redemption = account[msg.sender].balance - redemptionFee + perAccountExtra;
reserve = reserve + redemptionFee;
msg.sender.transfer(redemption);
account[msg.sender].balance = 0;
account[msg.sender].isValid = false;
accountCount = accountCount - 1;
}
function getBalance() public view returns (uint) {
return account[msg.sender].balance;
}
function ChangeRedemptionFee(uint _redemptionFee) public onlyManager {
redemptionFee = _redemptionFee;
}
function RedeemReserve() public onlyManager {
manager.transfer(reserve);
}
function DistributeReserve() public onlyManager {
require(reserve/accountCount > 0);
uint _perAccount = reserve/accountCount;
perAccountExtra = perAccountExtra + _perAccount;
reserve = reserve - _perAccount * accountCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment