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

update smart contract validateProm/getGains and refactor differet functions #315

Merged
merged 8 commits into from
Sep 7, 2023

Conversation

HamdiBenK
Copy link
Contributor

Description:

This pull request introduces a series of enhancements and optimizations to the contract logic, addressing the "Stack too deep" error and improving code readability. The changes have been made in response to the issueand focus on ensuring a cleaner codebase while maintaining gas efficiency.

Changes Made:

Modularization of Logic: The contract's logic has been modularized by breaking down the validateProm function into smaller, specialized functions. This modular approach enhances code organization, readability, and maintainability.

Function Reusability: To improve code reusability, we've introduced separate functions for generating promotion IDs, checking promotion eligibility, and storing promotion details. This approach streamlines the code and reduces duplication.

Influencer Promotion Count: A new function, incrementPromotionCount, has been added to manage the influencer's promotion count for a specific campaign. This function helps enforce the participation limit without exceeding the stack limit.

Gas Efficiency Consideration: While introducing additional functions can increase gas costs due to function call overhead, the changes have been designed to strike a balance between gas efficiency and code readability. The benefits of improved organization and readability often outweigh the minimal increase in gas costs.

Code Commenting: Existing and newly added functions have been thoroughly commented to explain their purpose, inputs, and outputs. This enhances the contract's understandability for developers and auditors.

Enhanced getGains Function: [If applicable] The getGains function has been optimized to [briefly describe the optimization or change made, e.g., reduce storage reads, gas cost reduction]. This optimization enhances the efficiency of the function while maintaining accurate results.

@waelhanfi04 waelhanfi04 self-requested a review August 29, 2023 15:08
@@ -83,20 +113,20 @@ contract oracle is limited {
emit AskRequestBounty( typeSN, idPost, idUser, idProm);
}

function answer(address campaignContract,bytes32 idRequest,uint64 likes,uint64 shares, uint64 views) public onlyOwner {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combiner les deux fonctions "answer" et "answerBounty".
Tu peux faire comme ça.

function answerOrBounty(address campaignContract, bytes32 id, uint64 likes, uint64 shares, uint64 views, uint256 nbAbos) public onlyOwner validCampaignAddress(campaignContract) {
ICampaign campaign = ICampaign(campaignContract);

if (nbAbos > 0) {
    campaign.updateBounty(id, nbAbos);
    emit AnswerRequestBounty(id, nbAbos);
    return;
}

campaign.update(id, likes, shares, views);
emit AnswerRequest(id, likes, shares, views);

}

* @param idProm The unique identifier of the campaign promotion.
* @param nbAbos The number of subscribers for the promotion.
* @return ok True if the operation is successful.
*/
function updateBounty(bytes32 idProm, uint256 nbAbos)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combiner les deux fonctions "updateBounty" et "update".

function updateBountyAndPost(
bytes32 idProm,
uint256 nbAbos,
bytes32 idRequest,
uint64 likes,
uint64 shares,
uint64 views
) external notPaused returns (bool ok) {
require(msg.sender == oracle, "oracle mismatch");

// Store the prom element for efficient access
SocialMediaPostData storage post = proms[idProm];
require(!post.isPayed, "link already paid");
post.isPayed = true;

// Store the campaign element for efficient access
AdPool storage campaignData = campaigns[post.idCampaign];
post.funds.token = campaignData.funds.token;

uint256 bountyGain = 0;
for (uint256 i = 0; i < campaignData.bounties.length; i++) {
    if (
        nbAbos >= campaignData.bounties[i].minRange &&
        nbAbos < campaignData.bounties[i].maxRange &&
        post.typeSN == campaignData.bounties[i].typeSN
    ) {
        bountyGain = campaignData.bounties[i].amount;
    }
}

uint256 postGain = 0;

if (likes > results[post.prevResult].likes) {
    postGain +=
        (likes - results[post.prevResult].likes) *
        campaigns[post.idCampaign].ratios[post.typeSN].likeRatio;
}
if (shares > results[post.prevResult].shares) {
    postGain +=
        (shares - results[post.prevResult].shares) *
        campaigns[post.idCampaign].ratios[post.typeSN].shareRatio;
}
if (views > results[post.prevResult].views) {
    postGain +=
        (views - results[post.prevResult].views) *
        campaigns[post.idCampaign].ratios[post.typeSN].viewRatio;
}
post.prevResult = idRequest;

if (campaignData.funds.amount <= bountyGain) {
    post.funds.amount += campaignData.funds.amount;
    campaignData.funds.amount = 0;
    emit CampaignFundsSpent(post.idCampaign);
} else {
    campaignData.funds.amount -= bountyGain;
    post.funds.amount += bountyGain;
}

if (post.funds.token == address(0)) {
    post.funds.token = campaigns[post.idCampaign].funds.token;
}
if (campaigns[post.idCampaign].funds.amount <= postGain) {
    post.funds.amount += campaigns[post.idCampaign].funds.amount;
    campaigns[post.idCampaign].funds.amount = 0;
    emit CampaignFundsSpent(post.idCampaign);
} else {
    campaigns[post.idCampaign].funds.amount -= postGain;
    post.funds.amount += postGain;
}

return true;

}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combining the two functions like you did won't work in this way 'cause on is used for bouty campaign & the other for performance

@HamdiBenK HamdiBenK merged commit f790ec0 into develop Sep 7, 2023
2 of 3 checks passed
@HamdiBenK HamdiBenK deleted the smart-changes branch September 7, 2023 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants