-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathStakingExample.sol
More file actions
70 lines (57 loc) · 2.3 KB
/
StakingExample.sol
File metadata and controls
70 lines (57 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {CoreWriterLib, HLConstants, HLConversions} from "@hyper-evm-lib/src/CoreWriterLib.sol";
/**
* @title StakingExample
* @dev This contract demonstrates CoreWriterLib staking functionality.
*/
contract StakingExample {
using CoreWriterLib for *;
error NoHypeBalance();
/**
* @notice Transfers HYPE tokens to core, stakes them, and delegates to a validator
*/
function bridgeHypeAndStake(uint256 evmAmount, address validator) external payable {
// Transfer HYPE tokens to core
uint64 hypeTokenIndex = HLConstants.hypeTokenIndex();
hypeTokenIndex.bridgeToCore(evmAmount);
// Using data from the `TokenInfo` precompile, convert EVM amount to core decimals for staking operations
uint64 coreAmount = HLConversions.evmToWei(hypeTokenIndex, evmAmount);
// Transfer tokens to staking account
CoreWriterLib.depositStake(coreAmount);
// Delegate the tokens to a validator
CoreWriterLib.delegateToken(validator, coreAmount, false);
}
/**
* @notice Undelegates tokens from a validator
*/
function undelegateTokens(address validator, uint64 coreAmount) external {
// Undelegate tokens by setting the bool `undelegate` parameter to true
CoreWriterLib.delegateToken(validator, coreAmount, true);
}
/**
* @notice Undelegates tokens from a validator and withdraws them to the spot balance
*/
function undelegateAndWithdrawStake(address validator, uint64 coreAmount) external {
// Undelegate tokens from the validator
CoreWriterLib.delegateToken(validator, coreAmount, true);
// Withdraw the tokens from staking
CoreWriterLib.withdrawStake(coreAmount);
}
/**
* @notice Withdraws tokens from the staking balance
*/
function withdrawStake(uint64 coreAmount) external {
// Withdraw the tokens from the staking balance
CoreWriterLib.withdrawStake(coreAmount);
}
/**
* @notice Transfers all HYPE balance to the sender
*/
function transferAllHypeToSender() external {
uint256 balance = address(this).balance;
if (balance == 0) revert NoHypeBalance();
payable(msg.sender).transfer(balance);
}
receive() external payable {}
}