-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathPaymentHelperExtn.sol
195 lines (167 loc) · 8.04 KB
/
PaymentHelperExtn.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.23;
import { IPaymentHelperV2 as IPaymentHelper } from "src/interfaces/IPaymentHelperV2.sol";
import { IPaymentHelperExtn } from "src/interfaces/IPaymentHelperExtn.sol";
import { ISuperRegistry } from "src/interfaces/ISuperRegistry.sol";
import { Error } from "src/libraries/Error.sol";
import { DataLib } from "src/libraries/DataLib.sol";
import {
SingleDirectSingleVaultStateReq,
SingleXChainSingleVaultStateReq,
SingleDirectMultiVaultStateReq,
SingleXChainMultiVaultStateReq,
SingleVaultSFData,
MultiVaultSFData
} from "src/types/DataTypes.sol";
/// @title PaymentHelperExtn (Payment Helper Extension)
/// @dev Helps estimate the cost for the entire transaction lifecycle when using router wrapper
/// @author ZeroPoint Labs
contract PaymentHelperExtn is IPaymentHelperExtn {
using DataLib for uint256;
//////////////////////////////////////////////////////////////
// CONSTANTS //
//////////////////////////////////////////////////////////////
ISuperRegistry public immutable superRegistry;
uint64 public immutable CHAIN_ID;
/// @dev uses payment helper to estimate the router calls
IPaymentHelper public paymentHelper;
//////////////////////////////////////////////////////////////
// CONSTRUCTOR //
//////////////////////////////////////////////////////////////
constructor(address superRegistry_, address paymentHelper_) {
if (superRegistry_ == address(0) || paymentHelper_ == address(0)) {
revert Error.ZERO_ADDRESS();
}
if (block.chainid > type(uint64).max) {
revert Error.BLOCK_CHAIN_ID_OUT_OF_BOUNDS();
}
CHAIN_ID = uint64(block.chainid);
superRegistry = ISuperRegistry(superRegistry_);
paymentHelper = IPaymentHelper(paymentHelper_);
}
//////////////////////////////////////////////////////////////
// EXTERNAL FUNCTIONS //
//////////////////////////////////////////////////////////////
function estimateRebalanceSinglePosition(
bytes calldata callData_,
bytes calldata rebalanceCallData_
)
external
view
override
returns (uint256 msgValue)
{
/// @dev estimate withdrawal
SingleDirectSingleVaultStateReq memory withdrawReq =
SingleDirectSingleVaultStateReq({ superformData: _decodeSingleVaultData(callData_) });
(,, uint256 withdrawCost) = paymentHelper.estimateSingleDirectSingleVault(withdrawReq, false);
/// @dev estimate deposit
SingleDirectSingleVaultStateReq memory depositReq =
SingleDirectSingleVaultStateReq({ superformData: _decodeSingleVaultData(rebalanceCallData_) });
(,, uint256 depositCost) = paymentHelper.estimateSingleDirectSingleVault(depositReq, true);
msgValue = withdrawCost + depositCost;
}
function estimateRebalanceMultiPositions(
bytes calldata callData_,
bytes calldata rebalanceCallData_
)
external
view
override
returns (uint256 msgValue)
{
/// @dev estimate withdrawal
SingleDirectMultiVaultStateReq memory withdrawReq =
SingleDirectMultiVaultStateReq({ superformData: _decodeMultiVaultData(callData_) });
(,, uint256 withdrawCost) = paymentHelper.estimateSingleDirectMultiVault(withdrawReq, false);
/// @dev estimate deposit
SingleDirectMultiVaultStateReq memory depositReq =
SingleDirectMultiVaultStateReq({ superformData: _decodeMultiVaultData(rebalanceCallData_) });
(,, uint256 depositCost) = paymentHelper.estimateSingleDirectMultiVault(depositReq, true);
msgValue = withdrawCost + depositCost;
}
function estimateCrossChainRebalance(
bytes calldata callData_,
bytes calldata rebalanceCallData_
)
external
view
override
returns (uint256 msgValue)
{
/// @dev estimate withdrawal
SingleXChainSingleVaultStateReq memory withdrawReq = SingleXChainSingleVaultStateReq({
superformData: _decodeSingleVaultData(callData_),
dstChainId: _extractDstChainId(callData_),
ambIds: _extractAmbIds(callData_)
});
(,,, uint256 withdrawalCost) = paymentHelper.estimateSingleXChainSingleVault(withdrawReq, false);
/// @dev estimate deposit
SingleXChainSingleVaultStateReq memory depositReq = SingleXChainSingleVaultStateReq({
superformData: _decodeSingleVaultData(rebalanceCallData_),
dstChainId: _extractDstChainId(rebalanceCallData_),
ambIds: _extractAmbIds(rebalanceCallData_)
});
(,,, uint256 depositCost) = paymentHelper.estimateSingleXChainSingleVault(depositReq, true);
msgValue = withdrawalCost + depositCost;
}
function estimateCrossChainRebalanceMulti(
bytes calldata callData_,
bytes calldata rebalanceCallData_
)
external
view
override
returns (uint256 msgValue)
{
/// @dev estimate withdrawal
SingleXChainMultiVaultStateReq memory withdrawReq = SingleXChainMultiVaultStateReq({
superformsData: _decodeMultiVaultData(callData_),
dstChainId: _extractDstChainId(callData_),
ambIds: _extractAmbIds(callData_)
});
(,,, uint256 withdrawalCost) = paymentHelper.estimateSingleXChainMultiVault(withdrawReq, false);
/// @dev estimate deposit
SingleXChainMultiVaultStateReq memory depositReq = SingleXChainMultiVaultStateReq({
superformsData: _decodeMultiVaultData(rebalanceCallData_),
dstChainId: _extractDstChainId(rebalanceCallData_),
ambIds: _extractAmbIds(rebalanceCallData_)
});
(,,, uint256 depositCost) = paymentHelper.estimateSingleXChainMultiVault(depositReq, true);
msgValue = withdrawalCost + depositCost;
}
function estimateDeposit4626(bytes calldata callData_) external view override returns (uint256 msgValue) {
SingleDirectSingleVaultStateReq memory req =
SingleDirectSingleVaultStateReq({ superformData: _decodeSingleVaultData(callData_) });
(,, msgValue) = paymentHelper.estimateSingleDirectSingleVault(req, true);
}
function estimateDeposit(bytes calldata callData_) external view override returns (uint256 msgValue) {
SingleDirectSingleVaultStateReq memory req =
SingleDirectSingleVaultStateReq({ superformData: _decodeSingleVaultData(callData_) });
(,, msgValue) = paymentHelper.estimateSingleDirectSingleVault(req, true);
}
function estimateBatchDeposit(bytes[] calldata callData_) external view override returns (uint256 msgValue) {
uint256 len = callData_.length;
for (uint256 i = 0; i < len; i++) {
SingleDirectSingleVaultStateReq memory req =
SingleDirectSingleVaultStateReq({ superformData: _decodeSingleVaultData(callData_[i]) });
(,, uint256 depositCost) = paymentHelper.estimateSingleDirectSingleVault(req, true);
msgValue += depositCost;
}
}
//////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS //
//////////////////////////////////////////////////////////////
function _decodeSingleVaultData(bytes calldata data_) internal pure returns (SingleVaultSFData memory) {
// Implement decoding logic based on your data structure
}
function _decodeMultiVaultData(bytes calldata data_) internal pure returns (MultiVaultSFData memory) {
// Implement decoding logic based on your data structure
}
function _extractDstChainId(bytes calldata data_) internal pure returns (uint64) {
// Implement logic to extract destination chain ID from calldata
}
function _extractAmbIds(bytes calldata data_) internal pure returns (uint8[] memory) {
// Implement logic to extract AMB IDs from calldata
}
}