Skip to content

Instantly share code, notes, and snippets.

@Schaeff
Created May 11, 2016 22:57
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 Schaeff/71382156a602a2d30940b4b1802eed04 to your computer and use it in GitHub Desktop.
Save Schaeff/71382156a602a2d30940b4b1802eed04 to your computer and use it in GitHub Desktop.
contract Campaign {
address public owner;
string public description;
Reward[] public rewards;
uint public endDate;
uint public goal;
uint public current;
bool public success;
struct Reward {
string description;
uint price;
mapping (address => uint) orders;
}
modifier onlyOwner() {
if(msg.sender != owner) throw;
_
}
modifier open() {
if(now > endDate) throw;
_
}
modifier closed() {
if(now <= endDate) throw;
_
}
// Executé une seule fois à la création du contrat
function Campaign(string desc, uint end, uint g) {
owner = msg.sender;
description = desc;
endDate = end;
goal = g;
}
// Changer la description
function setDescription(string desc) onlyOwner() {
description = desc;
}
function addReward(string desc, uint price) onlyOwner() {
rewards.push(Reward(desc, price));
}
function buyReward(uint rId, uint quantity) open() {
if(msg.value < rewards[rId].price * quantity) throw;
rewards[rId].orders[msg.sender] += quantity;
current += rewards[rId].price * quantity;
msg.sender.send(msg.value - rewards[rId].price * quantity);
}
function getResult() {
if(now < endDate) throw;
if(current >= goal) {
success = true;
}
}
function withdrawFunds() onlyOwner() {
if(success) {
msg.sender.send(this.balance);
}
}
function claimFunds() closed() {
if(!success) {
uint funds = 0;
for(uint i = 0; i < rewards.length; i++) {
funds += rewards[i].orders[msg.sender] * rewards[i].price;
}
msg.sender.send(funds);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment