Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/change team #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 91 additions & 7 deletions contracts/contracts/dapphack.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ contract DappHack is ProjectNFTs {
//builders
address[] public s_builders;

mapping(address => uint256) public builderToTeamId;
mapping(address => Team) public builderToTeam;

//teams
Expand All @@ -78,7 +79,10 @@ contract DappHack is ProjectNFTs {
uint256 newPrize
);
event BuilderSignedUp(address indexed builder);
event BuilderWithdrawn(address indexed builder);
event TeamInitialized(string name, address[] participants);
event TeamChanged(uint256 fromTeam, uint256 toTeam);
event TeamWithdrawn(uint256 teamNumber, address indexed participant);
event ProjectSubmitted(uint256 teamNumber, string nftUri);
event WinnerJudged(uint256 trackNumber, uint256 winnerNumber);
event PrizeDistributed(uint256 amount);
Expand Down Expand Up @@ -130,8 +134,7 @@ contract DappHack is ProjectNFTs {

//create mapping for this
modifier NotInTeam() {
// need a mapping from address(of the signer) to team as cant fetch a team that is not created
// require(s_teams[msg.sender] == 0, "Already in a team");
require(builderToTeamId[msg.sender] == 0, "Already in a team");
_;
}

Expand Down Expand Up @@ -311,8 +314,33 @@ contract DappHack is ProjectNFTs {
payable(address(this)).transfer(STAKE);

emit BuilderSignedUp(msg.sender);
}

// builder withdraw ?
function withdrawBuilder() public OnlyBuilder {
Team memory newTeam; // created a empty team object
// Remove from Builders
for (uint256 i = 0; i < s_builders.length; i++) {
if (s_builders[i] == msg.sender) {
s_builders[i] = s_builders[s_builders.length - 1];
s_builders.pop();
break;
}
}
// remove builder from Teams
uint256 Teamid = builderToTeamId[msg.sender];
if (Teamid != 0) {
for (uint256 i = 0; i < s_teams[Teamid].participants.length; i++) {
if (s_teams[Teamid].participants[i] == msg.sender) {
s_teams[Teamid].participants[i] = s_teams[Teamid]
.participants[s_teams[Teamid].participants.length - 1];
s_teams[Teamid].participants.pop();
}
}
builderToTeamId[msg.sender] = 0;
builderToTeam[msg.sender] = newTeam;
}

emit BuilderWithdrawn(msg.sender);
}

/**
Expand All @@ -324,18 +352,74 @@ contract DappHack is ProjectNFTs {
string memory name,
address[] memory participants
) public OnlyValidTeamSize(participants.length) NotInTeam {
// loop to check participants are not already in a team
for (uint256 i = 0; i < participants.length; i++) {
require(
builderToTeamId[participants[i]] == 0,
"participants already in team"
);
}
//add the team to the team array
s_teams.push(Team(name, participants, false, false)); // ATTACK_VECTOR: People can add members already in a team , a Problem

// give the team to builder in mapping
// give team id to participants
// give the builder to team in mapping

for (uint256 i = 0; i < participants.length; i++) {
builderToTeamId[participants[i]] = s_teams.length;
builderToTeam[participants[i]] = s_teams[s_teams.length - 1];
}

emit TeamInitialized(name, participants);
}

/**
* @dev Change the team for the competition.
*/
function changeTeam(
uint256 fromTeamId,
uint256 toTeamId
) public OnlyBuilder {
require(
s_teams[fromTeamId].participants.length < s_teamSizeLimit,
"Invalid team size"
);
require(
builderToTeamId[msg.sender] == fromTeamId + 1,
"Invalid team id"
);

for (uint256 i = 0; i < s_teams[fromTeamId].participants.length; i++) {
if (s_teams[fromTeamId].participants[i] == msg.sender) {
s_teams[fromTeamId].participants[i] = s_teams[fromTeamId]
.participants[s_teams[fromTeamId].participants.length - 1];

//team withdraw ?
s_teams[fromTeamId].participants.pop();
break;
}
}
s_teams[toTeamId].participants.push(msg.sender);
builderToTeam[msg.sender] = s_teams[toTeamId];
builderToTeamId[msg.sender] = toTeamId;
emit TeamChanged(fromTeamId, toTeamId);
}

/**
* @dev Withdraws a team from the competition.
*/

function withdrawTeam() public OnlyBuilder {
Team memory newTeam;
uint256 teamId = builderToTeamId[msg.sender];
for (uint256 i = 0; i < s_teams[teamId].participants.length; i++) {
if (s_teams[teamId].participants[i] == msg.sender) {
s_teams[teamId].participants[i] = s_teams[teamId].participants[
s_teams[teamId].participants.length - 1
];
s_teams[teamId].participants.pop();
}
}
builderToTeamId[msg.sender] = 0;
builderToTeam[msg.sender] = newTeam;
emit TeamWithdrawn(teamId, msg.sender);
}

/**
Expand Down
1 change: 1 addition & 0 deletions contracts/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require("@nomicfoundation/hardhat-toolbox");
require("hardhat-deploy");
require("hardhat-deploy-ethers");
require("@nomicfoundation/hardhat-chai-matchers")
/** @type import('hardhat/config').HardhatUserConfig */
// const PRIVATE_KEY = process.env.PRIVATE_KEY;
module.exports = {
Expand Down
Loading