-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThrift.sol
165 lines (140 loc) · 5.96 KB
/
Thrift.sol
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.15;
import "witnet-solidity-bridge/contracts/interfaces/IWitnetRandomness.sol";
contract Thrift {
struct Club {
string clubName;
}
struct Users {
string userName;
address userAddress;
}
Users[] public user;
mapping(string => Users[]) public clubToUser;
mapping(string => uint256) public nameToAmount;
mapping(string => uint256) public numberOfUsers;
mapping(address => bool) public hasPaid;
mapping(string => uint256) public totalAmountInClub;
mapping(string => uint256) public trackOfUsersThatPaid;
uint32 public randomness;
uint time;
uint256 public latestRandomizingBlock;
IWitnetRandomness public immutable witnet;
/// @param _witnetRandomness Address of the WitnetRandomness contract.
constructor(IWitnetRandomness _witnetRandomness) {
assert(address(_witnetRandomness) != address(0));
witnet = _witnetRandomness;
time = block.timestamp;
}
receive() external payable {}
function requestRandomNumber() external payable {
latestRandomizingBlock = block.number;
uint _usedFunds = witnet.randomize{value: msg.value}();
if (_usedFunds < msg.value) {
payable(msg.sender).transfer(msg.value - _usedFunds);
}
}
function fetchRandomNumber(string memory _clubName) external {
assert(latestRandomizingBlock > 0);
Users[] memory myarr = clubToUser[_clubName];
uint32 num = uint32(myarr.length);
// uint256 num = numberOfUsers[_clubName];
randomness = witnet.random(num, 0, latestRandomizingBlock);
}
function createClub(string memory _clubName, string memory _userName) public {
Users memory clubUser = Users(_userName, msg.sender);
numberOfUsers[_clubName] = 1;
clubToUser[_clubName].push(clubUser);
}
function addUser(string memory _clubName, string memory _userName, address _user) public {
require(clubToUser[_clubName].length > 0, "Club Not Found");
Users memory clubUser = Users(_userName, _user);
numberOfUsers[_clubName] += 1;
clubToUser[_clubName].push(clubUser);
}
// function getAllUsers(string memory _clubName) public returns (Users[] memory){
// Users[] memory all = clubToUser[_clubName];
// return all;
// }
//sd
function proposedContirbutionAmount(string memory _clubName, uint256 _proposedAmount) public {
// require(clubToUser[_clubName].length>0, "wrong");
// amount[_clubName]=_proposedAmount;
uint256 amountInEther = _proposedAmount * 10 ** 18;
nameToAmount[_clubName] = amountInEther;
}
function intervalInWeeks(string memory _clubName) public view returns (uint256) {
return numberOfUsers[_clubName];
}
function getContractBalance() public view returns (uint256) {
return address(this).balance;
}
function getAmountFromClub(string memory _clubName) public view returns (uint256) {
return nameToAmount[_clubName];
}
function sendAmountToContract(string memory _clubName) public payable {
uint256 amount = getAmountFromClub(_clubName);
// uint numberOfUsersInTheClub = numberOfUsers[_clubName];
require(msg.value == amount, "please pay the correct amount");
require(hasPaid[msg.sender] = false, "User has already paid");
hasPaid[msg.sender] = true;
trackOfUsersThatPaid[_clubName] += 1;
totalAmountInClub[_clubName] += msg.value;
}
function hasEveryOnePaid(string memory _clubName) public returns (bool) {
return trackOfUsersThatPaid[_clubName] == numberOfUsers[_clubName];
}
function withdraw(string memory _clubName) public {
require(block.timestamp > time + 600, "Please wait for 10 minutes until next transaction");
require(hasPaid[msg.sender] == true, "User has not paid");
Users storage newArray = clubToUser[_clubName][randomness];
address payable _receiver = payable(newArray.userAddress);
_receiver.transfer(totalAmountInClub[_clubName]);
time = block.timestamp;
}
// uint256 clubId;
// mapping (string => Users) public userToClub;
// mapping(uint256 => Club) public addClub;
// Users[] public user;
// mapping(string => Users[]) public clubUsers;
// function addUser(string memory _clubName, string memory _userName, address _userAddress) public {
// require(clubExists(_clubName)==true,"wrong");
// Users memory newUser = Users(_userName, _userAddress);
// clubUsers[_clubName].push(newUser);
// }
// event done(address _creator, string _clubName);
// function createClub(address _creator, string memory _clubName) public{
// require(_creator==msg.sender, "Please add your own address");
// Club memory cclub = new Club(_clubName,_creator);
// // addClub[clubId].creator=_creator;
// // addClub[clubId].clubName=_clubName;
// clubUsers[_clubName].push(cclub);
// clubId++;
// emit done( _creator, _clubName);
// }
// // function addUser(string memory _clubName,address _userAddress, string memory _userName) public {
// // // Club club = new Club(_clubName,_clubAddress);
// // if()
// // userToClub[_clubName].userName = _userName;
// // userToClub[_clubName].userAddress = _userAddress;
// // }
// function clubExists(string memory _clubName) public view returns (bool) {
// return clubUsers[_clubName].length > 0;
// }
// // function addUser(address _user, string memory _name) public{
// // }
// function getAllClubs() public view returns(Club[] memory){
// Club[] memory result = new Club[](clubId);
// uint256 i = 0;
// for (uint256 key = 0; key <clubId; key++) {
// if (bytes(addClub[key].clubName).length != 0) {
// result[i] = addClub[key];
// i++;
// }
// }
// return result;
// }
// function getClubWithId(uint256 _id) public view returns(string memory, address){
// return(addClub[_id].clubName, addClub[_id].creator);
// }
}