diff --git a/contracts/contract/deposit/RocketDepositPool.sol b/contracts/contract/deposit/RocketDepositPool.sol index 46577e59..fa57ab57 100644 --- a/contracts/contract/deposit/RocketDepositPool.sol +++ b/contracts/contract/deposit/RocketDepositPool.sol @@ -333,7 +333,7 @@ contract RocketDepositPool is RocketBase, RocketDepositPoolInterface, RocketVaul //uint256 ethSupplied = RocketNodeManagerInterface.getEthSupplied(nodeAddress) + msg.value; //uint256 ethMatched = RocketNodeManagerInterface.getEthMatched(nodeAddress) + amount; - // Check that the ratio of ethSupplied and ethMatched meets minimum requirement + // TODO: Check that the ratio of ethSupplied and ethMatched meets minimum requirement // If useExpressTicket, subtract an express ticket DepositQueueValue memory value = DepositQueueValue({ @@ -343,12 +343,8 @@ contract RocketDepositPool is RocketBase, RocketDepositPoolInterface, RocketVaul requestedValue: uint32(amount / 10 ** 15) // Amount being requested }); - bytes32 namespace; - if (_expressQueue) { - namespace = keccak256("deposit.queue.express"); - } else { - namespace = keccak256("deposit.queue.standard"); - } + bytes32 namespace = getQueueNamespace(_expressQueue); + linkedListStorage.enqueueItem(namespace, value); } @@ -360,12 +356,46 @@ contract RocketDepositPool is RocketBase, RocketDepositPoolInterface, RocketVaul suppliedValue: 0, requestedValue: 0 }); - bytes32 namespace; - if (_expressQueue) { - namespace = keccak256("deposit.queue.express"); - } else { - namespace = keccak256("deposit.queue.standard"); - } + bytes32 namespace = getQueueNamespace(_expressQueue); + linkedListStorage.removeItem(namespace, key); } + + function assignMegapools() external { + LinkedListStorageInterface linkedListStorage = LinkedListStorageInterface(getContractAddress("linkedListStorage")); + + uint256 expressQueueLength = linkedListStorage.getLength(keccak256("deposit.queue.express")); + uint256 standardQueueLength = linkedListStorage.getLength(keccak256("deposit.queue.standard")); + + require(expressQueueLength != 0 || standardQueueLength != 0, "Empty queues"); + + uint256 queueIndex = getUint(keccak256(abi.encodePacked("megapoolQueueIndex"))); + bool express = (queueIndex++) % 3 != 0; + + if (express && expressQueueLength == 0) { + express = false; + } + if (!express && standardQueueLength == 0) { + express = true; + } + bytes32 namespace = getQueueNamespace(express); + + DepositQueueValue memory head = linkedListStorage.dequeueItem(namespace); + + // Convert from milliEther to wei + uint256 ethRequired = head.suppliedValue * 10**15 + head.requestedValue * 10**15; + if (address(this).balance >= ethRequired) { + // If there's enough ETH to assign + RocketMegapoolInterface(head.receiver).assignFunds{value: ethRequired}(head.validatorId); + setUint(keccak256(abi.encodePacked("megapoolQueueIndex")), queueIndex); + } + } + + /// @notice + function getQueueNamespace(bool _expressQueue) internal pure returns (bytes32) { + if (_expressQueue) { + return keccak256("deposit.queue.express"); + } + return keccak256("deposit.queue.standard"); + } } diff --git a/contracts/contract/megapool/RocketMegapool.sol b/contracts/contract/megapool/RocketMegapool.sol index 0157ffc2..84c0d5a0 100644 --- a/contracts/contract/megapool/RocketMegapool.sol +++ b/contracts/contract/megapool/RocketMegapool.sol @@ -55,6 +55,7 @@ contract RocketMegapool is RocketBase, RocketMegapoolInterface { // Events event MegapoolValidatorEnqueued(address indexed megapool, uint256 indexed validatorId, uint256 time); event MegapoolValidatorDequeued(address indexed megapool, uint256 indexed validatorId, uint256 time); + event MegapoolValidatorAssigned(address indexed megapool, uint256 indexed validatorId, uint256 time); // Immutables @@ -124,6 +125,8 @@ contract RocketMegapool is RocketBase, RocketMegapoolInterface { validators[validatorId].status = Status.Assigned; assignedValue += msg.value; validators[validatorId].assignmentTime = uint32(block.timestamp); + + emit MegapoolValidatorAssigned(address(this), validatorId, block.timestamp); } /// @notice Executes the first 1 ETH deposit on the Beacon Chain