-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathPayloadUpdaterLib.sol
65 lines (55 loc) · 1.88 KB
/
PayloadUpdaterLib.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.23;
import { DataLib } from "src/libraries/DataLib.sol";
import { Error } from "src/libraries/Error.sol";
import { PayloadState, CallbackType, LiqRequest } from "src/types/DataTypes.sol";
/// @dev library to validate slippage updation
library PayloadUpdaterLib {
function validateSlippage(
uint256 newAmount_,
uint256 maxAmount_,
uint256 slippage_
)
internal
pure
returns (bool valid_)
{
/// @dev args validation
if (newAmount_ > maxAmount_) {
revert Error.NEGATIVE_SLIPPAGE();
}
uint256 minAmount = (maxAmount_ * (10_000 - slippage_)) / 10_000;
/// @dev amount must fall within the slippage bounds
if (newAmount_ < minAmount) {
return false;
}
return true;
}
function validateLiqReq(LiqRequest memory req_) internal pure {
/// revert if token is address(0) -> user wants settlement without any liq data
/// revert if token is not address(0) and txData is already present
if (req_.token == address(0) || req_.txData.length != 0) {
revert Error.CANNOT_UPDATE_WITHDRAW_TX_DATA();
}
}
function validatePayloadUpdate(
uint256 txInfo_,
uint8 txType_,
PayloadState currentPayloadState_,
uint8 isMulti_
)
internal
pure
{
(uint256 txType, uint256 callbackType, uint8 multi,,,) = DataLib.decodeTxInfo(txInfo_);
if (!(txType == txType_ && callbackType == uint256(CallbackType.INIT))) {
revert Error.INVALID_PAYLOAD_UPDATE_REQUEST();
}
if (currentPayloadState_ != PayloadState.STORED) {
revert Error.PAYLOAD_ALREADY_UPDATED();
}
if (multi != isMulti_) {
revert Error.INVALID_PAYLOAD_UPDATE_REQUEST();
}
}
}