Last active
April 27, 2022 23:22
-
-
Save BdVade/56438963e6d543b2788cea2b39e5e00d to your computer and use it in GitHub Desktop.
Reach smart Contract for decho
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const ProjectDetails = Object({ | |
| name:Bytes(32), | |
| url:Bytes(32), | |
| approvalLimit: UInt, | |
| approvalDeadline: UInt, | |
| donationLimit: UInt, | |
| donationDeadline: UInt, | |
| fowardingAccount: Address | |
| }) | |
| export const main = Reach.App(()=>{ | |
| const creator = Participant('Creator', { | |
| projectDetails: ProjectDetails | |
| }) | |
| const voter = API('voter', { | |
| vote: Fun([UInt],Bool), | |
| claimFunds: Fun([], Bool) | |
| }) | |
| const donator = API('donator', { | |
| donate: Fun([UInt], Bool), | |
| reclaimDonations: Fun([], Bool) | |
| }) | |
| init(); | |
| creator.only(()=>{ | |
| const projectDetails = declassify(interact.projectDetails) | |
| }) | |
| creator.publish(projectDetails); | |
| const { | |
| name, | |
| url, | |
| approvalLimit, | |
| approvalDeadline, | |
| donationLimit, | |
| donationDeadline, | |
| fowardingAccount | |
| } = projectDetails | |
| const voters = new Map(UInt) | |
| const donators = new Map(UInt) | |
| const votes_balance = | |
| parallelReduce(balance()) | |
| .invariant(balance()>=0) | |
| .while(balance()<approvalLimit) | |
| .api(voter.vote, | |
| ((amount) => assume(amount>0)), | |
| ((amount) => amount ), | |
| (setResponse) => { | |
| if (voters[this]){ | |
| voters[this]+=amount | |
| } else{ | |
| voters[this]=amount | |
| } | |
| setResponse(true) | |
| return votes_balance+amount | |
| }) | |
| .timeout(approvalDeadline, () => { | |
| const [voters_remaining] = | |
| parallelReduce([ voters.size() ]) // Voters map length | |
| .invariant(0==0) | |
| .while(voters_remaining>0) | |
| // find a way to loop over the map and send to all to avoid using a timeout | |
| .api(voter.claimFunds, | |
| (() => {assume(voters.has(this))}), | |
| (()=> pass), | |
| ((setResponse) => {transfer(voters[this]).to(this) | |
| setResponse(True) | |
| })) | |
| }) | |
| const [donationsBalance] = | |
| parallelReduce([ balance() ]) | |
| .invariant(balance()>=0) | |
| .while(donationsBalance<donationLimit) | |
| .api(donator.donate, | |
| ((amount)=> {assume(amount>0)}), | |
| ((amount)=>{ amount }), | |
| ((amount, setResponse) => { | |
| if (donators[this]){ | |
| donators[this]+=amount | |
| } else { | |
| donators[this]=amount | |
| } | |
| setResponse(true) | |
| if (balance()>=donationLimit){ | |
| transfer(balance()).to(fowardingAccount) | |
| } | |
| return donationsBalance+amount}) | |
| ) | |
| .timeout(donationDeadline, () => { | |
| const donators_remaining = | |
| parallelReduce( donators.size() ) // Voters map length | |
| .invariant(0==0) | |
| .while(donators_remaining>0) | |
| // find a way to loop over the map and send to all to avoid using a timeout | |
| .api(donator.reclaimDonations, | |
| (() => {assume(donators.has(this))}), | |
| (()=> pass), | |
| ((setResponse) => {transfer(donators[this]).to(this) | |
| setResponse(true) | |
| })) | |
| }); | |
| commit(); | |
| exit(); | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment