@@ -14,6 +14,7 @@ import "./library/typesHelper/AccountTypeHelper.sol";
1414import "./library/typesHelper/AccountTypePositionHelper.sol " ;
1515import "./library/typesHelper/SafeCastHelper.sol " ;
1616import "./interface/ILedgerImplA.sol " ;
17+ import "./interface/ILedgerImplB.sol " ;
1718
1819/// @title Ledger contract
1920/// @author Orderly_Rubick
@@ -29,6 +30,7 @@ contract Ledger is ILedger, OwnableUpgradeable, LedgerDataLayout {
2930 struct LedgerStorage {
3031 // Because of EIP170 size limit, the implementation should be split to impl contracts
3132 address ledgerImplA;
33+ address ledgerImplB;
3234 }
3335
3436 // keccak256(abi.encode(uint256(keccak256("orderly.Ledger")) - 1)) & ~bytes32(uint256(0xff))
@@ -72,6 +74,12 @@ contract Ledger is ILedger, OwnableUpgradeable, LedgerDataLayout {
7274 _getLedgerStorage ().ledgerImplA = _ledgerImplA;
7375 }
7476
77+ /// @notice Set the address of ledgerImplB contract
78+ function setLedgerImplB (address _ledgerImplB ) external override onlyOwner nonZeroAddress (_ledgerImplB) {
79+ emit ChangeLedgerImplB (_getLedgerStorage ().ledgerImplB, _ledgerImplB);
80+ _getLedgerStorage ().ledgerImplB = _ledgerImplB;
81+ }
82+
7583 /// @notice Set the address of operatorManager contract
7684 /// @param _operatorManagerAddress new operatorManagerAddress
7785 function setOperatorManagerAddress (address _operatorManagerAddress )
@@ -216,83 +224,126 @@ contract Ledger is ILedger, OwnableUpgradeable, LedgerDataLayout {
216224 /// @notice The cross chain manager will call this function to notify the deposit event to the Ledger contract
217225 /// @param data account deposit data
218226 function accountDeposit (AccountTypes.AccountDeposit calldata data ) external override onlyCrossChainManager {
219- _delegatecall (abi.encodeWithSelector (ILedgerImplA.accountDeposit.selector , data));
227+ _delegatecall (
228+ abi.encodeWithSelector (ILedgerImplA.accountDeposit.selector , data), _getLedgerStorage ().ledgerImplA
229+ );
220230 }
221231
222232 function executeProcessValidatedFutures (PerpTypes.FuturesTradeUpload calldata trade )
223233 external
224234 override
225235 onlyOperatorManager
226236 {
227- _delegatecall (abi.encodeWithSelector (ILedgerImplA.executeProcessValidatedFutures.selector , trade));
237+ _delegatecall (
238+ abi.encodeWithSelector (ILedgerImplA.executeProcessValidatedFutures.selector , trade),
239+ _getLedgerStorage ().ledgerImplA
240+ );
241+ }
242+
243+ function executeProcessValidatedFuturesBatch (PerpTypes.FuturesTradeUpload[] calldata trades )
244+ external
245+ override
246+ onlyOperatorManager
247+ {
248+ _delegatecall (
249+ abi.encodeWithSelector (ILedgerImplB.executeProcessValidatedFuturesBatch.selector , trades),
250+ _getLedgerStorage ().ledgerImplB
251+ );
228252 }
229253
230254 function executeWithdrawAction (EventTypes.WithdrawData calldata withdraw , uint64 eventId )
231255 external
232256 override
233257 onlyOperatorManager
234258 {
235- _delegatecall (abi.encodeWithSelector (ILedgerImplA.executeWithdrawAction.selector , withdraw, eventId));
259+ _delegatecall (
260+ abi.encodeWithSelector (ILedgerImplA.executeWithdrawAction.selector , withdraw, eventId),
261+ _getLedgerStorage ().ledgerImplA
262+ );
236263 }
237264
238265 function accountWithdrawFail (AccountTypes.AccountWithdraw memory withdraw ) external override onlyOwner {
239- _delegatecall (abi.encodeWithSelector (ILedgerImplA.accountWithdrawFail.selector , withdraw));
266+ _delegatecall (
267+ abi.encodeWithSelector (ILedgerImplA.accountWithdrawFail.selector , withdraw), _getLedgerStorage ().ledgerImplA
268+ );
240269 }
241270
242271 function accountWithDrawFinish (AccountTypes.AccountWithdraw calldata withdraw )
243272 external
244273 override
245274 onlyCrossChainManager
246275 {
247- _delegatecall (abi.encodeWithSelector (ILedgerImplA.accountWithDrawFinish.selector , withdraw));
276+ _delegatecall (
277+ abi.encodeWithSelector (ILedgerImplA.accountWithDrawFinish.selector , withdraw),
278+ _getLedgerStorage ().ledgerImplA
279+ );
248280 }
249281
250282 function executeSettlement (EventTypes.Settlement calldata settlement , uint64 eventId )
251283 external
252284 override
253285 onlyOperatorManager
254286 {
255- _delegatecall (abi.encodeWithSelector (ILedgerImplA.executeSettlement.selector , settlement, eventId));
287+ _delegatecall (
288+ abi.encodeWithSelector (ILedgerImplA.executeSettlement.selector , settlement, eventId),
289+ _getLedgerStorage ().ledgerImplA
290+ );
256291 }
257292
258293 function executeLiquidation (EventTypes.Liquidation calldata liquidation , uint64 eventId )
259294 external
260295 override
261296 onlyOperatorManager
262297 {
263- _delegatecall (abi.encodeWithSelector (ILedgerImplA.executeLiquidation.selector , liquidation, eventId));
298+ _delegatecall (
299+ abi.encodeWithSelector (ILedgerImplA.executeLiquidation.selector , liquidation, eventId),
300+ _getLedgerStorage ().ledgerImplA
301+ );
264302 }
265303
266304 function executeLiquidationV2 (EventTypes.LiquidationV2 calldata liquidation , uint64 eventId )
267305 external
268306 override
269307 onlyOperatorManager
270308 {
271- _delegatecall (abi.encodeWithSelector (ILedgerImplA.executeLiquidationV2.selector , liquidation, eventId));
309+ _delegatecall (
310+ abi.encodeWithSelector (ILedgerImplA.executeLiquidationV2.selector , liquidation, eventId),
311+ _getLedgerStorage ().ledgerImplA
312+ );
272313 }
273314
274315 function executeAdl (EventTypes.Adl calldata adl , uint64 eventId ) external override onlyOperatorManager {
275- _delegatecall (abi.encodeWithSelector (ILedgerImplA.executeAdl.selector , adl, eventId));
316+ _delegatecall (
317+ abi.encodeWithSelector (ILedgerImplA.executeAdl.selector , adl, eventId), _getLedgerStorage ().ledgerImplA
318+ );
276319 }
277320
278321 function executeAdlV2 (EventTypes.AdlV2 calldata adl , uint64 eventId ) external override onlyOperatorManager {
279- _delegatecall (abi.encodeWithSelector (ILedgerImplA.executeAdlV2.selector , adl, eventId));
322+ _delegatecall (
323+ abi.encodeWithSelector (ILedgerImplA.executeAdlV2.selector , adl, eventId), _getLedgerStorage ().ledgerImplA
324+ );
280325 }
281326
282327 function executeFeeDistribution (EventTypes.FeeDistribution calldata feeDistribution , uint64 eventId )
283328 external
284329 override
285330 onlyOperatorManager
286331 {
287- _delegatecall (abi.encodeWithSelector (ILedgerImplA.executeFeeDistribution.selector , feeDistribution, eventId));
332+ _delegatecall (
333+ abi.encodeWithSelector (ILedgerImplA.executeFeeDistribution.selector , feeDistribution, eventId),
334+ _getLedgerStorage ().ledgerImplA
335+ );
288336 }
289337
290338 function executeDelegateSigner (EventTypes.DelegateSigner calldata delegateSigner , uint64 eventId )
291339 external
292340 override
293341 onlyOperatorManager
294342 {
295- _delegatecall (abi.encodeWithSelector (ILedgerImplA.executeDelegateSigner.selector , delegateSigner, eventId));
343+ _delegatecall (
344+ abi.encodeWithSelector (ILedgerImplA.executeDelegateSigner.selector , delegateSigner, eventId),
345+ _getLedgerStorage ().ledgerImplA
346+ );
296347 }
297348
298349 function executeDelegateWithdrawAction (EventTypes.WithdrawData calldata delegateWithdraw , uint64 eventId )
@@ -301,7 +352,8 @@ contract Ledger is ILedger, OwnableUpgradeable, LedgerDataLayout {
301352 onlyOperatorManager
302353 {
303354 _delegatecall (
304- abi.encodeWithSelector (ILedgerImplA.executeDelegateWithdrawAction.selector , delegateWithdraw, eventId)
355+ abi.encodeWithSelector (ILedgerImplA.executeDelegateWithdrawAction.selector , delegateWithdraw, eventId),
356+ _getLedgerStorage ().ledgerImplA
305357 );
306358 }
307359
@@ -364,8 +416,8 @@ contract Ledger is ILedger, OwnableUpgradeable, LedgerDataLayout {
364416 }
365417
366418 // inner function for delegatecall
367- function _delegatecall (bytes memory data ) private {
368- (bool success , bytes memory returnData ) = _getLedgerStorage ().ledgerImplA .delegatecall (data);
419+ function _delegatecall (bytes memory data , address impl ) private {
420+ (bool success , bytes memory returnData ) = impl .delegatecall (data);
369421 if (! success) {
370422 if (returnData.length > 0 ) {
371423 assembly {
0 commit comments