Skip to content

Commit

Permalink
Merge 1b87072 into f3bd551
Browse files Browse the repository at this point in the history
  • Loading branch information
kamescg committed Dec 1, 2021
2 parents f3bd551 + 1b87072 commit 760ccd2
Show file tree
Hide file tree
Showing 16 changed files with 457 additions and 45 deletions.
58 changes: 58 additions & 0 deletions contracts/BeaconTimelockAndPushRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.6;
import "@pooltogether/v4-core/contracts/interfaces/IDrawBeacon.sol";
import "@pooltogether/v4-core/contracts/interfaces/IDrawBuffer.sol";
import "@pooltogether/owner-manager-contracts/contracts/Manageable.sol";
import "./interfaces/IBeaconTimelockAndPushRouter.sol";
import "./interfaces/IPrizeDistributionFactory.sol";
import "./interfaces/IDrawCalculatorTimelock.sol";

/**
* @title PoolTogether V4 BeaconTimelockAndPushRouter
* @author PoolTogether Inc Team
* @notice The BeaconTimelockAndPushRouter smart contract is an upgrade of the L1TimelockTimelock smart contract.
Reducing protocol risk by eliminating off-chain computation of PrizeDistribution parameters. The timelock will
only pass the total supply of all tickets in a "PrizePool Network" to the prize distribution factory contract.
*/
contract BeaconTimelockAndPushRouter is IBeaconTimelockAndPushRouter, Manageable {
/* ============ Global Variables ============ */

/// @notice PrizeDistributionFactory reference.
IPrizeDistributionFactory public immutable prizeDistributionFactory;

/// @notice DrawCalculatorTimelock reference.
IDrawCalculatorTimelock public immutable timelock;

/* ============ Constructor ============ */

/**
* @notice Initialize BeaconTimelockAndPushRouter smart contract.
* @param _owner The smart contract owner
* @param _prizeDistributionFactory PrizeDistributionFactory address
* @param _timelock DrawCalculatorTimelock address
*/
constructor(
address _owner,
IPrizeDistributionFactory _prizeDistributionFactory,
IDrawCalculatorTimelock _timelock
) Ownable(_owner) {
prizeDistributionFactory = _prizeDistributionFactory;
timelock = _timelock;
emit Deployed(_prizeDistributionFactory, _timelock);
}

/// @inheritdoc IBeaconTimelockAndPushRouter
function push(IDrawBeacon.Draw memory _draw, uint256 _totalNetworkTicketSupply)
external
override
onlyManagerOrOwner
{
timelock.lock(_draw.drawId, _draw.timestamp + _draw.beaconPeriodSeconds);
prizeDistributionFactory.pushPrizeDistribution(_draw.drawId, _totalNetworkTicketSupply);
emit DrawLockedAndTotalNetworkTicketSupplyPushed(
_draw.drawId,
_draw,
_totalNetworkTicketSupply
);
}
}
12 changes: 7 additions & 5 deletions contracts/DrawCalculatorTimelock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ contract DrawCalculatorTimelock is IDrawCalculatorTimelock, Manageable {
* @param _owner Address of the DrawCalculator owner.
* @param _calculator DrawCalculator address.
*/
constructor(
address _owner,
IDrawCalculator _calculator
) Ownable(_owner) {
constructor(address _owner, IDrawCalculator _calculator) Ownable(_owner) {
calculator = _calculator;

emit Deployed(_calculator);
Expand All @@ -69,7 +66,12 @@ contract DrawCalculatorTimelock is IDrawCalculatorTimelock, Manageable {
}

/// @inheritdoc IDrawCalculatorTimelock
function lock(uint32 _drawId, uint64 _timestamp) external override onlyManagerOrOwner returns (bool) {
function lock(uint32 _drawId, uint64 _timestamp)
external
override
onlyManagerOrOwner
returns (bool)
{
Timelock memory _timelock = timelock;
require(_drawId == _timelock.drawId + 1, "OM/not-drawid-plus-one");

Expand Down
14 changes: 8 additions & 6 deletions contracts/L1TimelockTrigger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ contract L1TimelockTrigger is Manageable {
* @param drawId Draw ID
* @param prizeDistribution PrizeDistribution
*/
event PrizeDistributionPushed(uint32 indexed drawId, IPrizeDistributionBuffer.PrizeDistribution prizeDistribution);

event PrizeDistributionPushed(
uint32 indexed drawId,
IPrizeDistributionBuffer.PrizeDistribution prizeDistribution
);

/* ============ Global Variables ============ */

Expand Down Expand Up @@ -65,10 +67,10 @@ contract L1TimelockTrigger is Manageable {
* @param _draw Draw struct
* @param _prizeDistribution PrizeDistribution struct
*/
function push(IDrawBeacon.Draw calldata _draw, IPrizeDistributionBuffer.PrizeDistribution memory _prizeDistribution)
external
onlyManagerOrOwner
{
function push(
IDrawBeacon.Draw calldata _draw,
IPrizeDistributionBuffer.PrizeDistribution memory _prizeDistribution
) external onlyManagerOrOwner {
// Locks the new PrizeDistribution according to the Draw endtime.
timelock.lock(_draw.drawId, _draw.timestamp + _draw.beaconPeriodSeconds);
prizeDistributionBuffer.pushPrizeDistribution(_draw.drawId, _prizeDistribution);
Expand Down
15 changes: 9 additions & 6 deletions contracts/L2TimelockTrigger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import "./interfaces/IDrawCalculatorTimelock.sol";
malicously set Draw in the unfortunate event an Owner is compromised.
*/
contract L2TimelockTrigger is Manageable {

/// @notice Emitted when the contract is deployed.
event Deployed(
IDrawBuffer indexed drawBuffer,
Expand All @@ -29,7 +28,11 @@ contract L2TimelockTrigger is Manageable {
* @param drawId Draw ID
* @param prizeDistribution PrizeDistribution
*/
event DrawAndPrizeDistributionPushed(uint32 indexed drawId, IDrawBeacon.Draw draw, IPrizeDistributionBuffer.PrizeDistribution prizeDistribution);
event DrawAndPrizeDistributionPushed(
uint32 indexed drawId,
IDrawBeacon.Draw draw,
IPrizeDistributionBuffer.PrizeDistribution prizeDistribution
);

/* ============ Global Variables ============ */

Expand Down Expand Up @@ -72,10 +75,10 @@ contract L2TimelockTrigger is Manageable {
* @param _draw Draw struct from IDrawBeacon
* @param _prizeDistribution PrizeDistribution struct from IPrizeDistributionBuffer
*/
function push(IDrawBeacon.Draw memory _draw, IPrizeDistributionBuffer.PrizeDistribution memory _prizeDistribution)
external
onlyManagerOrOwner
{
function push(
IDrawBeacon.Draw memory _draw,
IPrizeDistributionBuffer.PrizeDistribution memory _prizeDistribution
) external onlyManagerOrOwner {
timelock.lock(_draw.drawId, _draw.timestamp + _draw.beaconPeriodSeconds);
drawBuffer.pushDraw(_draw);
prizeDistributionBuffer.pushPrizeDistribution(_draw.drawId, _prizeDistribution);
Expand Down
65 changes: 65 additions & 0 deletions contracts/ReceiverTimelockAndPushRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.6;
import "@pooltogether/v4-core/contracts/interfaces/IDrawBeacon.sol";
import "@pooltogether/v4-core/contracts/interfaces/IDrawBuffer.sol";
import "@pooltogether/owner-manager-contracts/contracts/Manageable.sol";
import "./interfaces/IReceiverTimelockAndPushRouter.sol";
import "./interfaces/IPrizeDistributionFactory.sol";
import "./interfaces/IDrawCalculatorTimelock.sol";

/**
* @title PoolTogether V4 ReceiverTimelockAndPushRouter
* @author PoolTogether Inc Team
* @notice The ReceiverTimelockAndPushRouter smart contract is an upgrade of the L2TimelockTimelock smart contract.
Reducing protocol risk by eliminating off-chain computation of PrizeDistribution parameters. The timelock will
only pass the total supply of all tickets in a "PrizePool Network" to the prize distribution factory contract.
*/
contract ReceiverTimelockAndPushRouter is IReceiverTimelockAndPushRouter, Manageable {
/* ============ Global Variables ============ */

/// @notice The DrawBuffer contract address.
IDrawBuffer public immutable drawBuffer;

/// @notice Internal PrizeDistributionFactory reference.
IPrizeDistributionFactory public immutable prizeDistributionFactory;

/// @notice Timelock struct reference.
IDrawCalculatorTimelock public immutable timelock;

/* ============ Constructor ============ */

/**
* @notice Initialize ReceiverTimelockAndPushRouter smart contract.
* @param _owner The smart contract owner
* @param _drawBuffer DrawBuffer address
* @param _prizeDistributionFactory PrizeDistributionFactory address
* @param _timelock DrawCalculatorTimelock address
*/
constructor(
address _owner,
IDrawBuffer _drawBuffer,
IPrizeDistributionFactory _prizeDistributionFactory,
IDrawCalculatorTimelock _timelock
) Ownable(_owner) {
drawBuffer = _drawBuffer;
prizeDistributionFactory = _prizeDistributionFactory;
timelock = _timelock;
emit Deployed(_drawBuffer, _prizeDistributionFactory, _timelock);
}

/// @inheritdoc IReceiverTimelockAndPushRouter
function push(IDrawBeacon.Draw memory _draw, uint256 _totalNetworkTicketSupply)
external
override
onlyManagerOrOwner
{
timelock.lock(_draw.drawId, _draw.timestamp + _draw.beaconPeriodSeconds);
drawBuffer.pushDraw(_draw);
prizeDistributionFactory.pushPrizeDistribution(_draw.drawId, _totalNetworkTicketSupply);
emit DrawLockedPushedAndTotalNetworkTicketSupplyPushed(
_draw.drawId,
_draw,
_totalNetworkTicketSupply
);
}
}
38 changes: 38 additions & 0 deletions contracts/interfaces/IBeaconTimelockAndPushRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.6;
import "@pooltogether/v4-core/contracts/interfaces/IDrawBeacon.sol";
import "./IPrizeDistributionFactory.sol";
import "./IDrawCalculatorTimelock.sol";

/**
* @title PoolTogether V4 IBeaconTimelockAndPushRouter
* @author PoolTogether Inc Team
* @notice The IBeaconTimelockAndPushRouter smart contract interface...
*/
interface IBeaconTimelockAndPushRouter {
/// @notice Emitted when the contract is deployed.
event Deployed(
IPrizeDistributionFactory indexed prizeDistributionFactory,
IDrawCalculatorTimelock indexed timelock
);

/**
* @notice Emitted when Draw is locked and totalNetworkTicketSupply is pushed to PrizeDistributionFactory
* @param drawId Draw ID
* @param draw Draw
* @param totalNetworkTicketSupply totalNetworkTicketSupply
*/
event DrawLockedAndTotalNetworkTicketSupplyPushed(
uint32 indexed drawId,
IDrawBeacon.Draw draw,
uint256 totalNetworkTicketSupply
);

/**
* @notice Locks next Draw and pushes totalNetworkTicketSupply to PrizeDistributionFactory
* @dev Restricts new draws for N seconds by forcing timelock on the next target draw id.
* @param draw Draw
* @param totalNetworkTicketSupply totalNetworkTicketSupply
*/
function push(IDrawBeacon.Draw memory draw, uint256 totalNetworkTicketSupply) external;
}
9 changes: 4 additions & 5 deletions contracts/interfaces/IDrawCalculatorTimelock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ pragma solidity 0.8.6;
import "@pooltogether/v4-core/contracts/interfaces/IDrawCalculator.sol";

interface IDrawCalculatorTimelock {

/**
* @notice Emitted when target draw id is locked.
* @param timestamp The epoch timestamp to unlock the current locked Draw
* @param drawId The Draw to unlock
/**
* @notice Emitted when target draw id is locked.
* @param timestamp The epoch timestamp to unlock the current locked Draw
* @param drawId The Draw to unlock
*/
struct Timelock {
uint64 timestamp;
Expand Down
6 changes: 6 additions & 0 deletions contracts/interfaces/IPrizeDistributionFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.6;

interface IPrizeDistributionFactory {
function pushPrizeDistribution(uint32 _drawId, uint256 _totalNetworkTicketSupply) external;
}
40 changes: 40 additions & 0 deletions contracts/interfaces/IReceiverTimelockAndPushRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.6;
import "@pooltogether/v4-core/contracts/interfaces/IDrawBeacon.sol";
import "@pooltogether/v4-core/contracts/interfaces/IDrawBuffer.sol";
import "./IPrizeDistributionFactory.sol";
import "./IDrawCalculatorTimelock.sol";

/**
* @title PoolTogether V4 IReceiverTimelockAndPushRouter
* @author PoolTogether Inc Team
* @notice The IReceiverTimelockAndPushRouter smart contract interface...
*/
interface IReceiverTimelockAndPushRouter {
/// @notice Emitted when the contract is deployed.
event Deployed(
IDrawBuffer indexed drawBuffer,
IPrizeDistributionFactory indexed prizeDistributionFactory,
IDrawCalculatorTimelock indexed timelock
);

/**
* @notice Emitted when Draw is locked, pushed to Draw DrawBuffer and totalNetworkTicketSupply is pushed to PrizeDistributionFactory
* @param drawId Draw ID
* @param draw Draw
* @param totalNetworkTicketSupply totalNetworkTicketSupply
*/
event DrawLockedPushedAndTotalNetworkTicketSupplyPushed(
uint32 indexed drawId,
IDrawBeacon.Draw draw,
uint256 totalNetworkTicketSupply
);

/**
* @notice Locks next Draw, pushes Draw to DraWBuffer and pushes totalNetworkTicketSupply to PrizeDistributionFactory.
* @dev Restricts new draws for N seconds by forcing timelock on the next target draw id.
* @param draw Draw
* @param totalNetworkTicketSupply totalNetworkTicketSupply
*/
function push(IDrawBeacon.Draw memory draw, uint256 totalNetworkTicketSupply) external;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"distribute-ether": "hardhat --network localhost fork:distribute-ether-from-binance",
"create-prize-pool": "hardhat --network localhost fork:create-aave-prize-pool",
"run-fork": "yarn impersonate-accounts && yarn distribute-ether && yarn create-prize-pool",
"test": "HIDE_DEPLOY_LOG=true OPTIMIZER_DISABLED=true hardhat test",
"test": "HIDE_DEPLOY_LOG=true hardhat test",
"prepack": "yarn compile"
},
"peerDependencies": {
Expand Down

0 comments on commit 760ccd2

Please sign in to comment.