Skip to content

Commit

Permalink
Merge pull request #29 from yomanthunder/feature/ChangePrizePoolAndPr…
Browse files Browse the repository at this point in the history
…izeArray

changePrizePool and changePrizeArray implimented
  • Loading branch information
lordshashank authored Mar 9, 2024
2 parents 762f789 + cd93128 commit ba85c24
Show file tree
Hide file tree
Showing 5 changed files with 8,248 additions and 794 deletions.
147 changes: 112 additions & 35 deletions contracts/contracts/dapphack.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ contract DappHack is ProjectNFTs {
uint256 oldPrize,
uint256 newPrize
);
event PrizeArrayChanged(
address indexed sponsor,
uint256[] oldPrizeArray,
uint256[] newPrizeArray
);
event BuilderSignedUp(address indexed builder);
event TeamInitialized(string name, address[] participants);
event ProjectSubmitted(uint256 teamNumber, string nftUri);
Expand Down Expand Up @@ -235,7 +240,9 @@ contract DappHack is ProjectNFTs {
}

//give sponsor a id
sponsorToId[msg.sender] = s_sponsors.length - 1;
for (uint256 i = 0; i < _sponsors.length; i++) {
sponsorToId[_sponsors[i]] = s_sponsors.length - 1;
}

//add in sponsor prizepool array
sponsorPrizePool[s_sponsors.length - 1] = msg.value; //safe when sponsor cant be deleted.
Expand All @@ -245,52 +252,113 @@ contract DappHack is ProjectNFTs {

/**
* @dev Allows a sponsor to change the prize pool for their sponsorship.
* @param sponsorNumber The index of the sponsor in the `s_sponsors` array.
* @param newPrize The new prize amount for the sponsor.
* @param newPrizePool The new prize pool amount for the sponsor.
*/

function changePrizePool(
uint256 sponsorNumber,
uint256 newPrize
) public payable OnlySponsor {
require(newPrize > 0, "Invalid prize amount");

//sponsorid is same as sponsor number as far as code is structured
function calculatePoolPrizeChangePayment(
uint256 newPrizePool
) public view OnlySponsor returns (int256 amt) {
// Update the sponsor prize pool
require(newPrizePool > 0, "Invalid prize amount");
uint256 sponsorNumber = sponsorToId[msg.sender];
Sponsor memory sponsor = s_sponsors[sponsorNumber];

uint256 temp_sum = 0;
uint256[] memory temp_array = sponsor.prizeArray;
for (uint i = 0; i < temp_array.length; ++i) {
temp_sum += temp_array[i];
for (uint i = 0; i < sponsor.prizeArray.length; ++i) {
temp_sum += sponsor.prizeArray[i];
}

// check that the newPrize fund is greater than the sum of all the prizes
require(
newPrize >= temp_sum + sponsor.poolPrize,
"Invalid prize amount"
);

uint256 oldPrize = sponsorPrizePool[sponsorNumber];

int amt = int(newPrize - oldPrize);

if (amt >= 0) {
payable(address(this)).transfer(uint256(amt));
//sponsorPrizePool[sponsorNumber]
if (temp_sum + newPrizePool > sponsorPrizePool[sponsorNumber]) {
amt = int256(
temp_sum + newPrizePool - sponsorPrizePool[sponsorNumber]
);
return amt;
} else {
payable(msg.sender).transfer(uint256(-amt));
amt = int256(
sponsorPrizePool[sponsorNumber] - temp_sum - newPrizePool
);
return -amt;
}
//update total prize pool
s_totalPrizePool += uint256(amt);
}

//prizePoolStructChange to be called
function changePrizePool(uint256 newPrize) public payable OnlySponsor {
uint256 sponsorNumber = sponsorToId[msg.sender];
int256 paymentRequired = calculatePoolPrizeChangePayment(newPrize);
if (paymentRequired > 0) {
require(
msg.value >= uint256(paymentRequired),
"Insufficient payment"
);
}

sponsorPrizePool[sponsorNumber] = newPrize;
if (paymentRequired > 0) {
payable(address(this)).transfer(uint256(paymentRequired));
sponsorPrizePool[sponsorNumber] += uint256(paymentRequired);
} else {
payable(msg.sender).transfer(uint256(-paymentRequired));
sponsorPrizePool[sponsorNumber] =
sponsorPrizePool[sponsorNumber] -
uint256(-paymentRequired);
}
uint256 oldPoolPrize = s_sponsors[sponsorNumber].poolPrize;
s_sponsors[sponsorNumber].poolPrize = newPrize;

emit PrizePoolChanged(msg.sender, oldPrize, newPrize);
emit PrizePoolChanged(msg.sender, oldPoolPrize, newPrize);
}

/**
* @dev Allows a sponsor to change the prize pool for their sponsorship.
* @param newPrizeArray The new prize Array of the sponsor.
*/
function calculatePrizeArrayChangePayment(
uint256[] memory newPrizeArray
) public view OnlySponsor returns (int256 amt) {
//update the sponsor prize pool
uint256 temp_sum = 0;
for (uint i = 0; i < newPrizeArray.length; ++i) {
temp_sum += newPrizeArray[i];
}
require(temp_sum > 0, "Invalid prize amount");
uint256 sponsorNumber = sponsorToId[msg.sender];
Sponsor memory sponsor = s_sponsors[sponsorNumber];
if (temp_sum + sponsor.poolPrize > sponsorPrizePool[sponsorNumber]) {
amt = int256(
temp_sum + sponsor.poolPrize - sponsorPrizePool[sponsorNumber]
);
return amt;
} else {
amt = int256(
sponsorPrizePool[sponsorNumber] - temp_sum - sponsor.poolPrize
);
return -amt;
}
}

function changePrizePoolSTruct() public payable {
/// TODO
function changePrizeArray(
uint256[] memory newPrizeArray
) public payable OnlySponsor {
uint256 sponsorNumber = sponsorToId[msg.sender];
int256 paymentRequired = calculatePrizeArrayChangePayment(
newPrizeArray
);
if (paymentRequired > 0) {
require(
msg.value >= uint256(paymentRequired),
"Insufficient payment"
);
}

uint256[] memory oldPrizeArray = s_sponsors[sponsorNumber].prizeArray;
s_sponsors[sponsorNumber].prizeArray = newPrizeArray;
if (paymentRequired > 0) {
payable(address(this)).transfer(uint256(paymentRequired));
sponsorPrizePool[sponsorNumber] += uint256(paymentRequired);
} else {
payable(msg.sender).transfer(uint256(-paymentRequired));
sponsorPrizePool[sponsorNumber] =
sponsorPrizePool[sponsorNumber] -
uint256(-paymentRequired);
}
emit PrizeArrayChanged(msg.sender, oldPrizeArray, newPrizeArray);
}

/**
Expand Down Expand Up @@ -569,6 +637,15 @@ contract DappHack is ProjectNFTs {
return s_sponsors[sponsorNumber].poolPrize;
}

/**
* @dev Returns the prize Array for the sponsor .
* @return The prize Array for the sponsor.
*/
function getSponsorPrizeArray() public view returns (uint256[] memory) {
uint256 sponsorNumber = sponsorToId[msg.sender];
return s_sponsors[sponsorNumber].prizeArray;
}

/**
* @dev Returns the number of builders.
* @return The number of builders.
Expand Down
Loading

0 comments on commit ba85c24

Please sign in to comment.