Skip to content

Encode x Polkadot #25

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

Open
wants to merge 3 commits into
base: main
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.qodo
2 changes: 2 additions & 0 deletions challenge-1-vesting/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ node_modules

# Hardhat Ignition default folder for deployments against a local node
ignition/deployments/chain-31337
.qodo
.env
99 changes: 91 additions & 8 deletions challenge-1-vesting/contracts/TokenVesting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,33 @@ Here's your starter code:
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Pausable.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";

contract TokenVesting is Ownable(msg.sender), Pausable, ReentrancyGuard {
struct VestingSchedule {
// TODO: Define the vesting schedule struct
// TODO: Define the vesting schedule struct
uint256 totalAmount;
uint256 startTime;
uint256 cliffDuration;
uint256 vestingDuration;
uint256 amountClaimed;
bool revoked;
}

// Token being vested
// TODO: Add state variables

using SafeERC20 for IERC20;
IERC20 public token;

// Mapping from beneficiary to vesting schedule
// TODO: Add state variables
mapping(address => VestingSchedule) public vestingSchedules;

// Whitelist of beneficiaries
// TODO: Add state variables
mapping(address => bool) public whitelist;

// Events
event VestingScheduleCreated(address indexed beneficiary, uint256 amount);
Expand All @@ -51,8 +60,9 @@ contract TokenVesting is Ownable(msg.sender), Pausable, ReentrancyGuard {
event BeneficiaryRemovedFromWhitelist(address indexed beneficiary);

constructor(address tokenAddress) {
// TODO: Initialize the contract

// TODO: Initialize the contract
require(tokenAddress != address(0), "Invalid token address");
token = IERC20(tokenAddress);
}

// Modifier to check if beneficiary is whitelisted
Expand Down Expand Up @@ -80,21 +90,94 @@ contract TokenVesting is Ownable(msg.sender), Pausable, ReentrancyGuard {
uint256 startTime
) external onlyOwner onlyWhitelisted(beneficiary) whenNotPaused {
// TODO: Implement vesting schedule creation
require(
startTime > block.timestamp,
"Start time must be in the future"
);
require(cliffDuration > 0, "Cliff duration must be greater than 0");
require(vestingDuration > 0, "Vesting duration must be greater than 0");
require(amount > 0, "Amount must be greater than 0");
require(
vestingDuration > cliffDuration,
"Vesting duration must be greater than cliff duration"
);

VestingSchedule memory schedule = VestingSchedule(
amount,
startTime,
cliffDuration,
vestingDuration,
0,
false
);
vestingSchedules[beneficiary] = schedule;

token.safeTransferFrom(owner(), address(this), amount);

emit VestingScheduleCreated(beneficiary, amount);
}

function calculateVestedAmount(
address beneficiary
) public view returns (uint256) {
// TODO: Implement vested amount calculation
VestingSchedule memory schedule = vestingSchedules[beneficiary];
require(!schedule.revoked, "Vesting schedule revoked");

uint256 currentTime = block.timestamp;
if(currentTime < schedule.startTime + schedule.cliffDuration) {
return 0;
}

if(currentTime >= schedule.startTime + schedule.vestingDuration) {
return schedule.totalAmount;
}

uint256 vestedDuration = currentTime - schedule.startTime;
uint256 vestedAmount = (schedule.totalAmount * vestedDuration) / schedule.vestingDuration;

return vestedAmount;
}

function claimVestedTokens() external nonReentrant whenNotPaused {
// TODO: Implement token claiming
// TODO: Implement token claiming
address beneficiary = msg.sender;
VestingSchedule storage schedule = vestingSchedules[beneficiary];

require(schedule.startTime + schedule.cliffDuration < block.timestamp, "No tokens to claim");
require(!schedule.revoked, "Vesting schedule revoked");
require(schedule.totalAmount > 0, "Total amount must be larger than 0");

uint256 vestedAmount = calculateVestedAmount(msg.sender);
uint256 receivableAmount = vestedAmount - schedule.amountClaimed;
schedule.amountClaimed += vestedAmount;

token.safeTransfer(beneficiary, receivableAmount);

emit TokensClaimed(beneficiary, vestedAmount);

}

function revokeVesting(address beneficiary) external onlyOwner {
// TODO: Implement vesting revocation

VestingSchedule storage schedule = vestingSchedules[beneficiary];
require(!schedule.revoked, "Vesting schedule revoked");


uint256 vestedAmount = calculateVestedAmount(beneficiary);
uint256 unclaimedAmount = vestedAmount - schedule.amountClaimed;
uint256 unvestedAmount = schedule.totalAmount - vestedAmount;

schedule.revoked = true;

if (unclaimedAmount > 0) {
token.safeTransfer(beneficiary, unclaimedAmount);
}
if (unvestedAmount > 0) {
token.safeTransfer(owner(), unvestedAmount);
}

emit VestingRevoked(beneficiary);
}

function pause() external onlyOwner {
Expand Down Expand Up @@ -145,4 +228,4 @@ Solution template (key points to implement):
- Calculate and transfer unvested tokens back
- Mark schedule as revoked
- Emit event
*/
*/

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../build-info/98f1554be46227c65584aeb4365f9bd8.json"
"buildInfo": "../build-info/babaac433f2eb48d04e0121b0389f5c3.json"
}

Large diffs are not rendered by default.

This file was deleted.

Large diffs are not rendered by default.

Loading