forked from lidofinance/lido-l2
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDepositDataCodec.sol
44 lines (34 loc) · 1.34 KB
/
DepositDataCodec.sol
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
// SPDX-FileCopyrightText: 2024 Lido <info@lido.fi>
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
/// @author kovalgek
/// @notice encodes and decodes DepositData for crosschain transfering.
library DepositDataCodec {
uint8 internal constant RATE_FIELD_SIZE = 12;
uint8 internal constant TIMESTAMP_FIELD_SIZE = 5;
struct DepositData {
uint96 rate;
uint40 timestamp;
bytes data;
}
function encodeDepositData(DepositData memory depositData) internal pure returns (bytes memory) {
bytes memory data = bytes.concat(
abi.encodePacked(depositData.rate),
abi.encodePacked(depositData.timestamp),
abi.encodePacked(depositData.data)
);
return data;
}
function decodeDepositData(bytes calldata buffer) internal pure returns (DepositData memory) {
if (buffer.length < RATE_FIELD_SIZE + TIMESTAMP_FIELD_SIZE) {
revert ErrorDepositDataLength();
}
DepositData memory depositData = DepositData({
rate: uint96(bytes12(buffer[0:RATE_FIELD_SIZE])),
timestamp: uint40(bytes5(buffer[RATE_FIELD_SIZE:RATE_FIELD_SIZE + TIMESTAMP_FIELD_SIZE])),
data: buffer[RATE_FIELD_SIZE + TIMESTAMP_FIELD_SIZE:]
});
return depositData;
}
error ErrorDepositDataLength();
}