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

Small fixes to the Staking tutorial #177

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 14 additions & 18 deletions docs/developers/omnichain/tutorials/staking.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ contract Staking is ERC20, zContract {
error UnknownAction(uint8 action);
error Overflow();
error Underflow();
error WrongAmount();
error AmountTooLow();
error NotAuthorized();
error NoRewardsToClaim();

Expand Down Expand Up @@ -271,11 +271,10 @@ deposited tokens.

```solidity title="contracts/Staking.sol"
function stakeZRC(address staker, uint256 amount) internal {
updateRewards(staker);

stake[staker] += amount;
if (stake[staker] < amount) revert Overflow();

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

function updateRewards(address staker) internal {
Expand All @@ -286,15 +285,17 @@ deposited tokens.
}

function queryRewards(address staker) public view returns (uint256) {
if (lastStakeTime[staker] == 0) {
return 0;
}
uint256 timeDifference = block.timestamp - lastStakeTime[staker];
uint256 rewardAmount = timeDifference * stake[staker] * rewardRate;
return rewardAmount;
}
```

`stakeZRC` increases the staker's balance in the contract. The function also
updates the timestamp of when the staking happened last, and calls the
`updateRewards` function to update the rewards for the staker.
The calls the `updateRewards` function to update the rewards for the staker.
`stakeZRC` increases the staker's balance in the contract.

`updateRewards` calculates the rewards for the staker and mints them to the
beneficiary address. The function also updates the timestamp of when the staking
Expand All @@ -306,31 +307,25 @@ The `unstakeZRC` function begins by updating any outstanding rewards due to the
user. It then checks that the user has a sufficient staked balance.
Subsequently, it identifies the ZRC20 token associated with the contract's
`chainID` and determines the gas fee for the unstaking operation. This fee is
then approved. The user's tokens, minus the gas fee, are withdrawn to the
encoded recipient address. Finally, the contract updates the user's staking
balance and the timestamp of their last stake action.
then approved. The function sets the staked balance to 0. Finally, the user's
tokens, minus the gas fee, are withdrawn to the encoded recipient address.

```solidity title="contracts/Staking.sol"
function unstakeZRC(address staker) internal {
uint256 amount = stake[staker];

updateRewards(staker);

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

if (amount < gasFee) revert WrongAmount();
uint256 amount = stake[staker];
if (amount < gasFee) revert AmountTooLow();

bytes memory recipient = withdraw[staker];

stake[staker] = 0;

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

if (stake[staker] > amount) revert Underflow();

lastStakeTime[staker] = block.timestamp;
}
```

Expand Down Expand Up @@ -485,7 +480,8 @@ project:

https://github.com/zeta-chain/example-contracts/tree/main/omnichain/staking/tasks

When copying these files make sure to also copy the helper function `convertToHexAddress.ts` in the `lib` directory.
When copying these files make sure to also copy the helper function
`convertToHexAddress.ts` in the `lib` directory.

To perform the actions above, the tasks simply send transactions to the TSS
address on the connected chain with the encoded data in the `data` field of the
Expand Down
Loading