Created
March 29, 2023 17:08
-
-
Save trinhvandat/b2b22ab0c14527d2c99c9a38df4f09a2 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.18+commit.87f61d96.js&optimize=false&runs=200&gist=
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
// SPDX-License-Identifier: MIT | |
// compiler version must be greater than or equal to 0.8.17 and less than 0.9.0 | |
pragma solidity ^0.8.17; | |
contract MoneyFund { | |
struct Participant { | |
string fullName; | |
int piority; | |
uint joinedDate; | |
address walletAddress; | |
} | |
struct VoteData { | |
string reason; | |
Voter[] voters; | |
bool isDone; | |
} | |
struct Voter { | |
address voterAddress; | |
bool isVoted; | |
} | |
struct StakingData { | |
Participant user; | |
uint amount; | |
uint date; | |
} | |
mapping(address => Participant) participantMap; | |
address[] participantKeys; | |
mapping(address => StakingData[]) stakingDataMapPerUser; | |
StakingData[] stackingDatas; | |
uint256 totalStacking; | |
mapping(string => VoteData) votes; //key: tên của cái cần vote (withdraw_1000usdt_ios_dev) value: result của vote (60%) | |
function joinGroup(string memory name, int piority, address walletAddress) public { | |
require(isJoined(msg.sender), "User already exist."); | |
Participant storage joiner = participantMap[msg.sender]; | |
joiner.fullName = name; | |
joiner.piority = piority; | |
joiner.joinedDate = block.timestamp; | |
joiner.walletAddress = msg.sender; | |
participantKeys.push(msg.sender); | |
} | |
function outGroup() public { | |
require(!isJoined(msg.sender), "User is not join in the group."); | |
// Remove participant in mapping | |
delete participantMap[msg.sender]; | |
deleteInParticipantKeys(msg.sender); | |
} | |
function listParticipants() public view returns(Participant[] memory) { | |
uint totalParticipant = participantKeys.length; | |
Participant[] memory participants = new Participant[](totalParticipant); | |
for(uint i = 0; i < totalParticipant; i ++) { | |
participants[i] = participantMap[participantKeys[i]]; | |
} | |
return participants; | |
} | |
function stack(uint amount) public { | |
require(!isJoined(msg.sender), "User is not join in the group."); | |
// Get user by address | |
Participant memory user = participantMap[msg.sender]; | |
// add data map for user | |
StakingData[] storage stackingDataPerUser = stakingDataMapPerUser[msg.sender]; | |
stackingDataPerUser.push( | |
StakingData({ | |
user: user, | |
amount: amount, | |
date: block.timestamp | |
}) | |
); | |
// add data map for | |
stackingDatas.push( | |
StakingData({ | |
user: user, | |
amount: amount, | |
date: block.timestamp | |
}) | |
); | |
// increase amount | |
totalStacking += amount; | |
} | |
function withdraw(uint amount, address walletAddress) public { | |
// | |
} | |
function vote(string memory _voteKey) public { | |
Participant memory voter = participantMap[msg.sender]; | |
// require(voter, ) // tồn tại, đã vote chưa | |
VoteData storage voteData = votes[_voteKey]; | |
// require not null | |
voteData.voters.push( | |
Voter({ | |
voterAddress: msg.sender, | |
isVoted: true | |
}) | |
); | |
// internal check vote | |
bool isVoteDone = _checkVotes(); | |
if (isVoteDone) { | |
// tinh phan tram ti dong thuan la bn? | |
voteData.isDone = true; | |
} | |
} | |
function _checkVotes() internal returns(bool) { | |
} | |
function createVote(string memory _voteKey, string memory _reason) public { | |
VoteData storage voteData = votes[_voteKey]; | |
voteData.reason = _reason; | |
voteData.isDone = false; | |
emit CreatedVote(); // khi tạo xong sẽ push event(scaner sẽ bắt được event => bắn notice về user) | |
} | |
function getTotal() public returns(uint256) { | |
require(!isJoined(msg.sender), "You are not in the group."); | |
return totalStacking; | |
} | |
/* Event function */ | |
event CreatedVote(); | |
// khi call withdraw, tất cả part phải vote | |
// 1. Team rút 1000 usdt ra thuê 1 IOS dev | |
// 2. team vote xem có rút không? | |
// 3. Khi người cuối cùng vote xong, smart contract tự động check result (vote * piority) > 50% ==> rút tiền | |
// Create vote function: id | |
/*Private function*/ | |
function isJoined(address walletAddress) internal returns(bool) { | |
Participant memory existParticipant = participantMap[walletAddress]; | |
if(bytes(existParticipant.fullName).length == bytes("").length | |
&& existParticipant.piority == 0 | |
&& existParticipant.joinedDate == 0 | |
) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function deleteInParticipantKeys(address walletAddress) internal { | |
uint totalParticipant = participantKeys.length; | |
if(totalParticipant == 0) { | |
return; | |
} | |
if(totalParticipant == 1 && participantKeys[0] == walletAddress) { | |
participantKeys.pop(); | |
return; | |
} | |
uint removerIndex; | |
for(uint i = 0; i < totalParticipant; i ++) { | |
if(participantKeys[i] == msg.sender) { | |
removerIndex = i; | |
} | |
} | |
if (removerIndex == 0 && participantKeys[0] == walletAddress) { | |
participantKeys[0] = participantKeys[totalParticipant - 1]; | |
} else if(removerIndex < totalParticipant) { | |
participantKeys[removerIndex] = participantKeys[totalParticipant - 1]; | |
} | |
participantKeys.pop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment