Skip to content

Instantly share code, notes, and snippets.

@gyan0890
Last active June 24, 2024 11:47
Show Gist options
  • Save gyan0890/cf3e270058097cf29d3557f8078ed5c0 to your computer and use it in GitHub Desktop.
Save gyan0890/cf3e270058097cf29d3557f8078ed5c0 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;
/**
* @title Accountability
* @dev Create tasks with timeline and assign an accountability partner for it
*/
contract Accountability {
uint id;
address platform_address;
struct Task {
uint taskId;
address owner;
string task;
uint startTime;
uint endTime; //Do we need an end time? What if they complete it before the end time?
address accountability_addr;
uint price;
uint donationAmount;
address donationAddress;
Status taskStatus;
bool accepted;
}
enum Status {
Created,
Completed
}
mapping(uint => Task) userTaskMap;
Task[] tasks;
constructor(address _platform_address) {
platform_address = _platform_address;
}
/**
* @dev
* @param task: taskName, startTime: starting time of the task.
* endTime: ending time of the task,
* accountability_addr: address of the accountability person
*/
function createTask(string memory task, uint startTime, uint endTime,
address accountability_addr, uint donationAmount, address donationAddress) public payable{
id = id + 1;
uint price = msg.value;
userTaskMap[id] = Task(id, msg.sender, task, startTime, endTime,
accountability_addr, price, donationAmount, donationAddress,
Status.Created, false);
tasks.push(userTaskMap[id]);
}
/**
* @dev
* @param tId: taskName, result: whether the accountability person has accepted or not
*/
function markTaskComplete(uint tId, bool result) public {
address accountability_address = msg.sender;
require(accountability_address == userTaskMap[tId].accountability_addr, "Only the accountability partner can approve the completion of the task");
require(block.timestamp > userTaskMap[tId].startTime, "It's not start time yet!");
userTaskMap[tId].accepted = result;
userTaskMap[tId].taskStatus = Status.Completed;
uint totalFunds = userTaskMap[tId].price;
uint userFunds = totalFunds - userTaskMap[tId].donationAmount;
//Transfer funds to user and donation address if the accountability person accepts
//Add 0 address check
if(userTaskMap[tId].accepted == true) {
payable(userTaskMap[tId].owner).transfer(userFunds);
if(userTaskMap[tId].donationAddress != address(0)){
payable(userTaskMap[tId].donationAddress).transfer(userTaskMap[tId].donationAmount);
}
}
else {
//If it is false, funds go to the platform address
payable(platform_address).transfer(userFunds);
if(userTaskMap[tId].donationAddress != address(0)){
payable(userTaskMap[tId].donationAddress).transfer(userTaskMap[tId].donationAmount);
}
}
}
/**
* @dev
* @param tId: Task ID, ID of the task you want to donate to.
**/
function donate(uint tId) public payable {
require(block.timestamp > userTaskMap[tId].startTime, "It's not start time yet!");
require(userTaskMap[tId].taskStatus == Status.Created, "The task is already completed.");
require(userTaskMap[tId].donationAddress != address(0), "Donation address cannot be 0");
userTaskMap[tId].donationAmount += msg.value;
}
/* -------GET FUNCTIONS------ */
/**
* @dev
* @param taskId: Task ID, ID of the task you want to get the details of.
**/
function getTask(uint taskId) public view returns(Task memory) {
return userTaskMap[taskId];
}
/**
* @dev
**/
function getAllTasks() public view returns(Task[] memory) {
return tasks;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment