Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Sep 12, 2023
1 parent 62562dc commit 8dd1509
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 937 deletions.
33 changes: 20 additions & 13 deletions omnichain/staking/contracts/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ contract Staking is ERC20, zContract {
error SenderNotSystemContract();
error WrongChain();
error NotAuthorizedToClaim();
error UnknownAction();

SystemContract public immutable systemContract;
uint256 public immutable chainID;
Expand Down Expand Up @@ -52,14 +53,22 @@ contract Staking is ERC20, zContract {

address staker = BytesHelperLib.bytesToAddress(context.origin, 0);
address beneficiary;
uint32 action;

if (context.chainID == 18332) {
beneficiary = BytesHelperLib.bytesToAddress(message, 0);
action = BytesHelperLib.bytesToUint32(message, 20);
} else {
beneficiary = abi.decode(message, (address));
(beneficiary, action) = abi.decode(message, (address, uint32));
}

stakeZRC(staker, beneficiary, amount);
if (action == 1) {
stakeZRC(staker, beneficiary, amount);
} else if (action == 2) {
unstakeZRC(staker, amount);
} else {
revert UnknownAction();
}
}

function stakeZRC(
Expand Down Expand Up @@ -103,10 +112,10 @@ contract Staking is ERC20, zContract {
emit RewardsClaimed(staker, rewardAmount);
}

function unstakeZRC(uint256 amount) external {
require(stakes[msg.sender] >= amount, "Insufficient staked balance");
function unstakeZRC(address staker, uint256 amount) internal {
require(stakes[staker] >= amount, "Insufficient staked balance");

updateRewards(msg.sender);
updateRewards(staker);

address zrc20 = systemContract.gasCoinZRC20ByChainId(chainID);
(address gasZRC20, uint256 gasFee) = IZRC20(zrc20).withdrawGasFee();
Expand All @@ -117,20 +126,18 @@ contract Staking is ERC20, zContract {

bytes memory recipient;
if (chainID == 18332) {
recipient = abi.encodePacked(
BytesHelperLib.addressToBytes(msg.sender)
);
recipient = abi.encodePacked(BytesHelperLib.addressToBytes(staker));
} else {
recipient = abi.encodePacked(msg.sender);
recipient = abi.encodePacked(staker);
}

IZRC20(zrc20).withdraw(recipient, amount - gasFee);
stakes[msg.sender] -= amount;
require(stakes[msg.sender] <= amount, "Underflow detected");
stakes[staker] -= amount;
require(stakes[staker] <= amount, "Underflow detected");

lastStakeTime[msg.sender] = block.timestamp;
lastStakeTime[staker] = block.timestamp;

emit Unstaked(msg.sender, amount);
emit Unstaked(staker, amount);
}

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

0 comments on commit 8dd1509

Please sign in to comment.