|
| 1 | +// SPDX-License-Identifier: GPL-3.0 |
| 2 | + |
| 3 | +pragma solidity 0.8.6; |
| 4 | + |
| 5 | +import { IDrawBeacon } from "@pooltogether/v4-core/contracts/interfaces/IDrawBeacon.sol"; |
| 6 | +import { IDrawBuffer } from "@pooltogether/v4-core/contracts/interfaces/IDrawBuffer.sol"; |
| 7 | + |
| 8 | +import { ISingleMessageDispatcher } from "./interfaces/ISingleMessageDispatcher.sol"; |
| 9 | + |
| 10 | +/** |
| 11 | + * @title PoolTogether V4 DrawDispatcher |
| 12 | + * @author PoolTogether Inc Team |
| 13 | + * @notice The DrawDispatcher smart contract relies on ERC-5164 to dispatch draws from Ethereum to another L1 or L2 |
| 14 | + * where Chainlink VRF 2.0 may not be available to compute draws. |
| 15 | + */ |
| 16 | +contract DrawDispatcher { |
| 17 | + /** |
| 18 | + * @notice Emitted when the `draw` has been dispatched. |
| 19 | + * @param dispatcher Address of the dispatcher on Ethereum that dispatched the draw |
| 20 | + * @param toChainId ID of the receiving chain |
| 21 | + * @param drawExecutor Address of the DrawExecutor on the receiving chain that will push the draw onto the DrawBuffer |
| 22 | + * @param draw Draw that was dispatched |
| 23 | + */ |
| 24 | + event DrawDispatched( |
| 25 | + ISingleMessageDispatcher indexed dispatcher, |
| 26 | + uint256 indexed toChainId, |
| 27 | + address indexed drawExecutor, |
| 28 | + IDrawBeacon.Draw draw |
| 29 | + ); |
| 30 | + |
| 31 | + /** |
| 32 | + * @notice Emitted when the `draws` have been dispatched. |
| 33 | + * @param dispatcher Address of the dispatcher on Ethereum that dispatched the draws |
| 34 | + * @param toChainId ID of the receiving chain |
| 35 | + * @param drawExecutor Address of the DrawExecutor on the receiving chain that will push the draws onto the DrawBuffer |
| 36 | + * @param draws Draws that were dispatched |
| 37 | + */ |
| 38 | + event DrawsDispatched( |
| 39 | + ISingleMessageDispatcher indexed dispatcher, |
| 40 | + uint256 indexed toChainId, |
| 41 | + address indexed drawExecutor, |
| 42 | + IDrawBeacon.Draw[] draws |
| 43 | + ); |
| 44 | + |
| 45 | + /// @notice DrawBuffer from which draws are retrieved. |
| 46 | + IDrawBuffer public immutable drawBuffer; |
| 47 | + |
| 48 | + /** |
| 49 | + * @notice DrawDispatcher constructor. |
| 50 | + * @param _drawBuffer Address of the DrawBuffer from which draws are retrieved |
| 51 | + */ |
| 52 | + constructor(IDrawBuffer _drawBuffer) { |
| 53 | + require(address(_drawBuffer) != address(0), "DD/drawBuffer-not-zero-address"); |
| 54 | + |
| 55 | + drawBuffer = _drawBuffer; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @notice Retrieves and dispatch the newest recorded draw. |
| 60 | + * @param _dispatcher Address of the dispatcher on Ethereum that will be used to dispatch the draw |
| 61 | + * @param _toChainId ID of the receiving chain |
| 62 | + * @param _drawExecutor Address of the DrawExecutor on the receiving chain that will push the draw onto the DrawBuffer |
| 63 | + */ |
| 64 | + function dispatchNewestDraw( |
| 65 | + ISingleMessageDispatcher _dispatcher, |
| 66 | + uint256 _toChainId, |
| 67 | + address _drawExecutor |
| 68 | + ) external { |
| 69 | + IDrawBeacon.Draw memory _newestDraw = drawBuffer.getNewestDraw(); |
| 70 | + _dispatchDraw(_dispatcher, _toChainId, _drawExecutor, _newestDraw); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @notice Retrieves and dispatch draw. |
| 75 | + * @dev Will revert if the draw does not exist. |
| 76 | + * @param _dispatcher Address of the dispatcher on Ethereum that will be used to dispatch the draw |
| 77 | + * @param _toChainId ID of the receiving chain |
| 78 | + * @param _drawExecutor Address of the DrawExecutor on the receiving chain that will push the draw onto the DrawBuffer |
| 79 | + * @param _drawId Id of the draw to dispatch |
| 80 | + */ |
| 81 | + function dispatchDraw( |
| 82 | + ISingleMessageDispatcher _dispatcher, |
| 83 | + uint256 _toChainId, |
| 84 | + address _drawExecutor, |
| 85 | + uint32 _drawId |
| 86 | + ) external { |
| 87 | + require(_drawId > 0, "DD/drawId-gt-zero"); |
| 88 | + |
| 89 | + IDrawBeacon.Draw memory _draw = drawBuffer.getDraw(_drawId); |
| 90 | + _dispatchDraw(_dispatcher, _toChainId, _drawExecutor, _draw); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @notice Retrieves and dispatch draws. |
| 95 | + * @dev `_drawIds` must be ordered in ascending and contiguous order. |
| 96 | + * @dev Will revert if one of the draw does not exist. |
| 97 | + * @param _dispatcher Address of the dispatcher on Ethereum that will be used to dispatch the draw |
| 98 | + * @param _toChainId ID of the receiving chain |
| 99 | + * @param _drawExecutor Address of the DrawExecutor on the receiving chain that will push the draw onto the DrawBuffer |
| 100 | + * @param _drawIds Array of draw ids to dispatch |
| 101 | + */ |
| 102 | + function dispatchDraws( |
| 103 | + ISingleMessageDispatcher _dispatcher, |
| 104 | + uint256 _toChainId, |
| 105 | + address _drawExecutor, |
| 106 | + uint32[] calldata _drawIds |
| 107 | + ) external { |
| 108 | + IDrawBeacon.Draw[] memory _draws = drawBuffer.getDraws(_drawIds); |
| 109 | + |
| 110 | + _dispatchMessage( |
| 111 | + _dispatcher, |
| 112 | + _toChainId, |
| 113 | + _drawExecutor, |
| 114 | + abi.encodeWithSignature("pushDraws((uint256,uint32,uint64,uint64,uint32)[])", _draws) |
| 115 | + ); |
| 116 | + |
| 117 | + emit DrawsDispatched(_dispatcher, _toChainId, _drawExecutor, _draws); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @notice Dispatch the passed `draw`. |
| 122 | + * @param _dispatcher Address of the dispatcher on Ethereum that will be used to dispatch the draw |
| 123 | + * @param _toChainId ID of the receiving chain |
| 124 | + * @param _drawExecutor Address of the DrawExecutor on the receiving chain that will push the draw onto the DrawBuffer |
| 125 | + * @param _draw Draw to dispatch |
| 126 | + */ |
| 127 | + function _dispatchDraw( |
| 128 | + ISingleMessageDispatcher _dispatcher, |
| 129 | + uint256 _toChainId, |
| 130 | + address _drawExecutor, |
| 131 | + IDrawBeacon.Draw memory _draw |
| 132 | + ) internal { |
| 133 | + _dispatchMessage( |
| 134 | + _dispatcher, |
| 135 | + _toChainId, |
| 136 | + _drawExecutor, |
| 137 | + abi.encodeWithSignature("pushDraw((uint256,uint32,uint64,uint64,uint32))", _draw) |
| 138 | + ); |
| 139 | + |
| 140 | + emit DrawDispatched(_dispatcher, _toChainId, _drawExecutor, _draw); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @notice Dispatch encoded call. |
| 145 | + * @param _dispatcher Address of the dispatcher on Ethereum that will dispatch the call |
| 146 | + * @param _toChainId ID of the receiving chain |
| 147 | + * @param _drawExecutor Address of the DrawExecutor on the receiving chain that will receive the call |
| 148 | + * @param _data Calldata to dispatch |
| 149 | + */ |
| 150 | + function _dispatchMessage( |
| 151 | + ISingleMessageDispatcher _dispatcher, |
| 152 | + uint256 _toChainId, |
| 153 | + address _drawExecutor, |
| 154 | + bytes memory _data |
| 155 | + ) internal { |
| 156 | + require(address(_dispatcher) != address(0), "DD/dispatcher-not-zero-address"); |
| 157 | + require(_drawExecutor != address(0), "DD/drawExecutor-not-zero-address"); |
| 158 | + |
| 159 | + _dispatcher.dispatchMessage(_toChainId, _drawExecutor, _data); |
| 160 | + } |
| 161 | +} |
0 commit comments