-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloodHelp.sol
More file actions
82 lines (65 loc) · 1.93 KB
/
FloodHelp.sol
File metadata and controls
82 lines (65 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
struct Request {
uint id;
address author;
string title;
string description;
string contact;
uint timestamp;
uint goal;
uint balance;
bool open;
}
contract FloodHelp {
uint public lastId = 0;
mapping(uint => Request) public requests;
function openRequest(string memory title, string memory description, string memory contact, uint goal) public {
lastId++;
requests[lastId] = Request({
id: lastId,
title: title,
description: description,
contact: contact,
goal: goal,
balance: 0,
timestamp: block.timestamp,
author: msg.sender,
open: true
});
}
function closeRequest(uint id) private {
address author = requests[id].author;
uint balance = requests[id].balance;
requests[id].open = false;
if (balance > 0) {
requests[id].balance = 0;
payable(author).transfer(balance);
}
}
function closeRequestByAuthor(uint id) public {
address author = requests[id].author;
require(requests[id].open && msg.sender == author, unicode"Você não pode fechar este pedido.");
closeRequest(id);
}
function donate(uint id) public payable {
requests[id].balance += msg.value;
if (requests[id].balance >= requests[id].goal) {
closeRequest(id);
}
}
function getOpenRequests(uint startId, uint quantity) public view returns(Request[] memory) {
Request[] memory result = new Request[](quantity);
uint id = startId;
uint count = 0;
do {
if (requests[id].open) {
result[count] = requests[id];
count++;
}
id++;
}
while(count < quantity && id <= lastId);
return result;
}
}