Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Sep 11, 2023
1 parent b507eeb commit 7ff9eed
Show file tree
Hide file tree
Showing 2 changed files with 955 additions and 9 deletions.
40 changes: 31 additions & 9 deletions omnichain/staking/contracts/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ contract Staking is ERC20, zContract {
mapping(address => uint256) public lastStakeTime;
uint256 public rewardRate = 1;

event Staked(
address indexed staker,
address indexed beneficiary,
uint256 amount
);
event RewardsClaimed(address indexed staker, uint256 rewardAmount);
event Unstaked(address indexed staker, uint256 amount);

constructor(
string memory name_,
string memory symbol_,
Expand Down Expand Up @@ -60,43 +68,54 @@ contract Staking is ERC20, zContract {
uint256 amount
) internal {
stakes[staker] += amount;
require(stakes[staker] >= amount, "Overflow detected"); // Check for overflows

if (beneficiaries[staker] == address(0)) {
beneficiaries[staker] = beneficiary;
}
lastStakeTime[staker] = block.timestamp;

lastStakeTime[staker] = block.timestamp;
updateRewards(staker);

emit Staked(staker, beneficiary, amount); // Emitting Staked event
}

function updateRewards(address staker) internal {
uint256 timeDifference = block.timestamp - lastStakeTime[staker];
uint256 rewardAmount = timeDifference * stakes[staker] * rewardRate;
require(rewardAmount >= timeDifference, "Overflow detected"); // Check for overflows

_mint(beneficiaries[staker], rewardAmount);
lastStakeTime[staker] = block.timestamp;
}

function claimRewards(address staker) external {
if (beneficiaries[staker] != msg.sender) {
revert NotAuthorizedToClaim();
}
require(
beneficiaries[staker] == msg.sender,
"Not authorized to claim rewards"
);

uint256 rewardAmount = queryRewards(staker);
require(rewardAmount > 0, "No rewards to claim");

updateRewards(staker);

emit RewardsClaimed(staker, rewardAmount); // Emitting RewardsClaimed event
}

function unstakeZRC(uint256 amount) external {
updateRewards(msg.sender);

require(stakes[msg.sender] >= amount, "Insufficient staked balance");

address zrc20 = systemContract.gasCoinZRC20ByChainId(chainID);
updateRewards(msg.sender);

address zrc20 = systemContract.gasCoinZRC20ByChainId(chainID);
(address gasZRC20, uint256 gasFee) = IZRC20(zrc20).withdrawGasFee();

require(amount >= gasFee, "Amount should be greater than the gas fee");

IZRC20(zrc20).approve(zrc20, gasFee);

bytes memory recipient;

if (chainID == 18332) {
recipient = abi.encodePacked(
BytesHelperLib.addressToBytes(msg.sender)
Expand All @@ -106,9 +125,12 @@ contract Staking is ERC20, zContract {
}

IZRC20(zrc20).withdraw(recipient, amount - gasFee);

stakes[msg.sender] -= amount;
require(stakes[msg.sender] <= amount, "Underflow detected"); // Check for underflows

lastStakeTime[msg.sender] = block.timestamp;

emit Unstaked(msg.sender, amount); // Emitting Unstaked event
}

function queryRewards(address account) public view returns (uint256) {
Expand Down
Loading

0 comments on commit 7ff9eed

Please sign in to comment.