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

Challenge Registration #2

Open
wants to merge 2 commits into
base: master
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
36 changes: 21 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ The **OpenGuild Labs** makes the repository to introduce OpenHack workshop parti

Add your information to the below list to officially participate in the workshop challenge (This is the first mission of the whole workshop)

| Emoji | Name | Github Username | Occupations |
| ----- | --------------- | ----------------------------------------------------- | ------------------------ |
| 🎅 | Ippo | [NTP-996](https://github.com/NTP-996) | DevRel |
| Emoji | Name | Github Username | Occupations |
| ----- | ----------- | ------------------------------------------------- | ----------- |
| 🎅 | Ippo | [NTP-996](https://github.com/NTP-996) | DevRel |
| 🦛 | HarryRiddle | [0xharryriddle](https://github.com/0xharryriddle) | Student |

## 💻 Local development environment setup

### 1. Install Volta (Node.js Version Manager)

#### Windows

1. Download the Windows installer from https://docs.volta.sh/guide/getting-started
2. Run the installer and follow the prompts
3. Open a new terminal to activate Volta

#### macOS/Linux

```bash
# Install Volta
curl https://get.volta.sh | bash
Expand All @@ -32,6 +35,7 @@ source ~/.zshrc # for zsh
```

### 2. Install Node.js and npm using Volta

```bash
# Install Node.js LTS version
volta install node
Expand All @@ -43,7 +47,6 @@ npm --version

## 🚀 Getting Started


```bash
git clone [email protected]:openguild-labs/open-hack-yield-farm.git
cd open-hack-yield-farm
Expand All @@ -68,6 +71,7 @@ At the time writing this challenge, Hardhat haven't support deployment on Westen
### 🦊 Installing and Setting Up MetaMask

1. 💿 Install Metahttps://remix.polkadot.io/Mask

- Visit the [MetaMask website](https://metamask.io)
- Click "Download" and add the extension to your browser
- Create a new wallet by following the setup wizard
Expand All @@ -94,35 +98,37 @@ At the time writing this challenge, Hardhat haven't support deployment on Westen

🎯 Access Remix

- Go to https://remix.polkadot.io
- Simply copy/paste your yeild.sol contract
- Go to https://remix.polkadot.io
- Simply copy/paste your yeild.sol contract

🔨 Compile the Contract

- Select the "Solidity Compiler" tab
- Choose compiler version (e.g., 0.8.0)
- Click "Compile"
- Select the "Solidity Compiler" tab
- Choose compiler version (e.g., 0.8.0)
- Click "Compile"

📤 Deploy the Contract

- Go to the "Deploy & Run Transactions" tab
- Set the environment to "Injected Provider - MetaMask"
- Ensure your MetaMask is connected to Asset-Hub Westend
- Click "Deploy"
- Confirm
- Go to the "Deploy & Run Transactions" tab
- Set the environment to "Injected Provider - MetaMask"
- Ensure your MetaMask is connected to Asset-Hub Westend
- Click "Deploy"
- Confirm

![image](./public/assets/deployed.png)

---

### 🙋‍♂️ How to claim the bounty?
Complete the challenge on your fork repository <br/>

Complete the challenge on your fork repository <br/>
⭐ Star Open Guild repository <br/>
👥 Follow OpenGuild Lab Github <br/>
💬 Join OpenGuild Discord <br/>
📝 Submit the proof-of-work (your challenge repository) to OpenGuild Discord <br/>

---

# 🤝 How to contribute to the community?

To submit a proposal, ideas, or any questions, please submit them here: [OpenGuild Discussion 💭](https://github.com/orgs/openguild-labs/discussions)
Expand Down
77 changes: 76 additions & 1 deletion contracts/yeild.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "hardhat/console.sol";

/**
* @title YieldFarm
Expand Down Expand Up @@ -71,6 +72,9 @@ contract YieldFarm is ReentrancyGuard, Ownable {
uint256 _rewardRate
) Ownable(msg.sender) {
// TODO: Initialize contract state
lpToken = IERC20(_lpToken);
rewardToken = IERC20(_rewardToken);
rewardRate = _rewardRate;
}

function updateReward(address _user) internal {
Expand All @@ -90,6 +94,11 @@ contract YieldFarm is ReentrancyGuard, Ownable {
// 1. Calculate rewards since last update
// 2. Apply boost multiplier
// 3. Return total pending rewards
if (totalStaked == 0) {
return rewardPerTokenStored;
}
uint256 totalRewards = (block.timestamp - lastUpdateTime) * rewardRate;
return rewardPerTokenStored + ((totalRewards * 1e18) / totalStaked);
}

function earned(address _user) public view returns (uint256) {
Expand All @@ -98,7 +107,17 @@ contract YieldFarm is ReentrancyGuard, Ownable {
// 1. Calculate rewards since last update
// 2. Apply boost multiplier
// 3. Return total pending rewards
}
UserInfo storage user = userInfo[_user];
if (user.amount == 0) {
return 0;
}
uint256 currentRewardPerToken = rewardPerToken();
uint256 boostMultiplier = calculateBoostMultiplier(_user);
uint256 userRewards = ((user.amount * currentRewardPerToken) / 1e18) - user.rewardDebt;
uint256 boostedRewards = (userRewards * boostMultiplier) / 100;
return user.pendingRewards + boostedRewards;
}


/**
* @notice Stake LP tokens into the farm
Expand All @@ -107,10 +126,19 @@ contract YieldFarm is ReentrancyGuard, Ownable {
function stake(uint256 _amount) external nonReentrant {
// TODO: Implement staking logic
// Requirements:
require(_amount > 0, "Cannot stake 0");
address sender = msg.sender;
// 1. Update rewards
updateReward(sender);
// 2. Transfer LP tokens from user
lpToken.transferFrom(sender, address(this), _amount);
// 3. Update user info and total staked amount
UserInfo storage user = userInfo[sender];
user.amount += _amount;
user.startTime = block.timestamp;
totalStaked += _amount;
// 4. Emit Staked event
emit Staked(sender, _amount);
}

/**
Expand All @@ -120,10 +148,19 @@ contract YieldFarm is ReentrancyGuard, Ownable {
function withdraw(uint256 _amount) external nonReentrant {
// TODO: Implement withdrawal logic
// Requirements:
address sender = msg.sender;
UserInfo storage user = userInfo[sender];
require(user.amount >= _amount, "Insufficient balance");
// 1. Update rewards
updateReward(sender);
// 2. Transfer LP tokens to user
lpToken.transfer(sender, _amount);
// 3. Update user info and total staked amount
user.amount -= _amount;
user.rewardDebt = (user.amount * rewardPerTokenStored) / 1e18;
totalStaked -= _amount;
// 4. Emit Withdrawn event
emit Withdrawn(sender, _amount);
}

/**
Expand All @@ -136,6 +173,17 @@ contract YieldFarm is ReentrancyGuard, Ownable {
// 2. Transfer rewards to user
// 3. Update user reward debt
// 4. Emit RewardsClaimed event
address sender = msg.sender;
updateReward(sender);
UserInfo storage user = userInfo[sender];
uint256 pendingRewards = earned(sender);
require(pendingRewards > 0, "No rewards to claim");
rewardToken.transfer(sender, pendingRewards);

user.pendingRewards = 0;
user.rewardDebt = (user.amount * rewardPerTokenStored) / 1e18;

emit RewardsClaimed(sender, pendingRewards);
}

/**
Expand All @@ -147,6 +195,21 @@ contract YieldFarm is ReentrancyGuard, Ownable {
// 1. Transfer all LP tokens back to user
// 2. Reset user info
// 3. Emit EmergencyWithdrawn event
address sender = msg.sender;
updateReward(sender);
UserInfo storage user = userInfo[sender];
uint256 amount = user.amount;
if (amount > 0) {
// Transfer LP tokens back to user
lpToken.transfer(sender, amount);
// Reset user info
user.amount = 0;
user.startTime = 0;
user.rewardDebt = 0;
user.pendingRewards = 0;
totalStaked -= amount;
emit EmergencyWithdrawn(sender, amount);
}
}

/**
Expand All @@ -161,6 +224,16 @@ contract YieldFarm is ReentrancyGuard, Ownable {
// Requirements:
// 1. Calculate staking duration
// 2. Return appropriate multiplier based on duration thresholds
uint256 startTime = userInfo[_user].startTime;
uint256 stakingDuration = block.timestamp - startTime;
if (stakingDuration >= BOOST_THRESHOLD_3) {
return 200;
} else if (stakingDuration >= BOOST_THRESHOLD_2) {
return 150;
} else if (stakingDuration >= BOOST_THRESHOLD_1) {
return 125;
}
return 100;
}

/**
Expand All @@ -172,6 +245,8 @@ contract YieldFarm is ReentrancyGuard, Ownable {
// Requirements:
// 1. Update rewards before changing rate
// 2. Set new reward rate
updateReward(address(0));
rewardRate = _newRate;
}

/**
Expand Down
Loading