-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathVaultExample.sol
More file actions
104 lines (86 loc) · 4.03 KB
/
VaultExample.sol
File metadata and controls
104 lines (86 loc) · 4.03 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {CoreWriterLib, HLConversions} from "@hyper-evm-lib/src/CoreWriterLib.sol";
import {PrecompileLib} from "@hyper-evm-lib/src/PrecompileLib.sol";
/**
* @title VaultExample
* @dev This contract demonstrates CoreWriterLib vault functionality.
*/
contract VaultExample {
using CoreWriterLib for *;
uint64 public constant USDC_TOKEN_ID = 0;
/*//////////////////////////////////////////////////////////////
Basic Vault Operations
//////////////////////////////////////////////////////////////*/
/**
* @notice Deposits USDC to a specified vault
* @param vault Address of the vault to deposit to
* @param usdcAmount Amount of USDC to deposit
*/
function depositToVault(address vault, uint64 usdcAmount) external {
CoreWriterLib.vaultTransfer(vault, true, usdcAmount);
}
/**
* @notice Withdraws USDC from a specified vault
* @param vault Address of the vault to withdraw from
* @param usdcAmount Amount of USDC to withdraw
*/
function withdrawFromVault(address vault, uint64 usdcAmount) external {
CoreWriterLib.vaultTransfer(vault, false, usdcAmount);
}
/*//////////////////////////////////////////////////////////////
Advanced Vault Operations
//////////////////////////////////////////////////////////////*/
/**
* @notice Withdraws USDC from vault and sends to recipient
* @dev vaultTransfer checks if funds are withdrawable and reverts if locked
* @param vault Address of the vault to withdraw from
* @param recipient Address to send the withdrawn USDC to
* @param coreAmount Amount of USDC to withdraw and send
*/
function withdrawFromVaultAndSend(address vault, address recipient, uint64 coreAmount) external {
uint64 usdcPerpAmount = HLConversions.weiToPerp(coreAmount);
CoreWriterLib.vaultTransfer(vault, false, usdcPerpAmount);
CoreWriterLib.transferUsdClass(usdcPerpAmount, false);
CoreWriterLib.spotSend(recipient, USDC_TOKEN_ID, coreAmount);
}
/**
* @notice Transfers USDC from spot to perp and deposits to vault
* @param vault Address of the vault to deposit USDC to
* @param coreAmount Amount of USDC to transfer and deposit to vault
*/
function transferUsdcToPerpAndDepositToVault(address vault, uint64 coreAmount) external {
uint64 usdcPerpAmount = HLConversions.weiToPerp(coreAmount);
CoreWriterLib.transferUsdClass(usdcPerpAmount, true);
CoreWriterLib.vaultTransfer(vault, true, usdcPerpAmount);
}
/*//////////////////////////////////////////////////////////////
Vault Information
//////////////////////////////////////////////////////////////*/
/**
* @notice Gets the vault equity for a user in a specific vault
* @param user Address of the user to check
* @param vault Address of the vault to check
* @return equity Amount of equity the user has in the vault
* @return lockedUntilTimestamp Timestamp until which the equity is locked
*/
function getVaultEquity(address user, address vault)
public
view
returns (uint64 equity, uint64 lockedUntilTimestamp)
{
PrecompileLib.UserVaultEquity memory vaultEquity = PrecompileLib.userVaultEquity(user, vault);
return (vaultEquity.equity, vaultEquity.lockedUntilTimestamp);
}
/**
* @notice Checks if funds are withdrawable from a vault for a user
* @param user Address of the user to check
* @param vault Address of the vault to check
* @return withdrawable True if funds can be withdrawn, false if locked
*/
function isWithdrawable(address user, address vault) public view returns (bool withdrawable) {
PrecompileLib.UserVaultEquity memory vaultEquity = PrecompileLib.userVaultEquity(user, vault);
return CoreWriterLib.toMilliseconds(uint64(block.timestamp)) >= vaultEquity.lockedUntilTimestamp;
}
receive() external payable {}
}