Created
October 20, 2023 17:55
-
-
Save gyan0890/6ff700114d9a7b98887bc80cb6758e8b to your computer and use it in GitHub Desktop.
Reach platform code for Unfold'23
This file contains 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: UNLICENSED | |
pragma solidity ^0.8.4; | |
import "@openzeppelin/contracts/utils/Counters.sol"; | |
contract DeReach { | |
using Counters for Counters.Counter; | |
Counters.Counter private cIds; //Campaign IDs | |
struct Campaign { | |
bool isActive; | |
uint id; | |
string name; | |
string title; | |
string description; | |
address campaign_owner; | |
uint views; | |
uint clicks; | |
uint startDate; | |
uint endDate; | |
uint createdAt; | |
uint updatedAt; | |
uint cost; | |
} | |
mapping(address => uint[]) campaignMapping; | |
mapping(uint => Campaign) campaignIdMapping; | |
Campaign[] campaigns; | |
function createCampaign(string memory _name, string memory _title, string memory _description, | |
uint _endDate, uint _createdAt, uint _updatedAt) public payable returns(uint) { | |
cIds.increment(); | |
uint id = cIds.current(); | |
Campaign memory campaign = Campaign( | |
true, id, _name, _title, _description, msg.sender, | |
0, 0, block.timestamp, | |
_endDate, _createdAt, _updatedAt, msg.value); | |
campaignMapping[msg.sender].push(id); | |
campaignIdMapping[id] = campaign; | |
campaigns.push(campaign); | |
return id; | |
} | |
function split_amount(uint cId, address[] memory recipients, uint amountPerRecipient) public { | |
Campaign memory campaign = campaignIdMapping[cId]; | |
uint totalAmount = campaign.cost; | |
require(recipients.length * amountPerRecipient <= totalAmount, "Amount is not less than total cost"); | |
require(campaign.isActive , "Campaign is not active"); | |
campaign.isActive = false; | |
campaignIdMapping[cId] = campaign; | |
for(uint i = 0; i < recipients.length; i++) { | |
payable(recipients[i]).transfer(amountPerRecipient); | |
} | |
} | |
function getAllCampaigns() public view returns(Campaign[] memory) { | |
return campaigns; | |
} | |
function getCampaignById(uint id) public view returns(Campaign memory) { | |
return campaignIdMapping[id]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment