Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved ticket chance code into separate Ticket contract
- Loading branch information
1 parent
2d557b8
commit 9dc3bf6
Showing
12 changed files
with
303 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
pragma solidity 0.6.4; | ||
|
||
import "sortition-sum-tree-factory/contracts/SortitionSumTreeFactory.sol"; | ||
import "@pooltogether/uniform-random-number/contracts/UniformRandomNumber.sol"; | ||
|
||
import "./ControlledToken.sol"; | ||
import "./TicketInterface.sol"; | ||
|
||
contract Ticket is ControlledToken, TicketInterface { | ||
using SortitionSumTreeFactory for SortitionSumTreeFactory.SortitionSumTrees; | ||
|
||
bytes32 constant private TREE_KEY = keccak256("PoolTogether/Ticket"); | ||
uint256 constant private MAX_TREE_LEAVES = 5; | ||
|
||
// Ticket-weighted odds | ||
SortitionSumTreeFactory.SortitionSumTrees internal sortitionSumTrees; | ||
|
||
/// @notice Initializes the Controlled Token with Token Details and the Controller | ||
/// @param _name The name of the Token | ||
/// @param _symbol The symbol for the Token | ||
/// @param _trustedForwarder Address of the Forwarding Contract for GSN Meta-Txs | ||
/// @param _controller Address of the Controller contract for minting & burning | ||
function initialize( | ||
string memory _name, | ||
string memory _symbol, | ||
address _trustedForwarder, | ||
TokenControllerInterface _controller | ||
) | ||
public | ||
virtual | ||
override | ||
initializer | ||
{ | ||
super.initialize(_name, _symbol, _trustedForwarder, _controller); | ||
sortitionSumTrees.createTree(TREE_KEY, MAX_TREE_LEAVES); | ||
} | ||
|
||
/// @notice Returns the user's chance of winning. | ||
function chanceOf(address user) external view returns (uint256) { | ||
return sortitionSumTrees.stakeOf(TREE_KEY, bytes32(uint256(user))); | ||
} | ||
|
||
/// @notice Selects a user using a random number. The random number will be uniformly bounded to the ticket totalSupply. | ||
/// @param randomNumber The random number to use to select a user. | ||
/// @return The winner | ||
function draw(uint256 randomNumber) public view override returns (address) { | ||
uint256 bound = totalSupply(); | ||
address selected; | ||
if (bound == 0) { | ||
selected = address(0); | ||
} else { | ||
uint256 token = UniformRandomNumber.uniform(randomNumber, bound); | ||
selected = address(uint256(sortitionSumTrees.draw(TREE_KEY, token))); | ||
} | ||
return selected; | ||
} | ||
|
||
/// @dev Controller hook to provide notifications & rule validations on token transfers to the controller. | ||
/// This includes minting and burning. | ||
/// May be overridden to provide more granular control over operator-burning | ||
/// @param from Address of the account sending the tokens (address(0x0) on minting) | ||
/// @param to Address of the account receiving the tokens (address(0x0) on burning) | ||
/// @param amount Amount of tokens being transferred | ||
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override { | ||
super._beforeTokenTransfer(from, to, amount); | ||
|
||
if (from != address(0)) { | ||
uint256 fromBalance = balanceOf(from).sub(amount); | ||
sortitionSumTrees.set(TREE_KEY, fromBalance, bytes32(uint256(from))); | ||
} | ||
|
||
if (to != address(0)) { | ||
uint256 toBalance = balanceOf(to).add(amount); | ||
sortitionSumTrees.set(TREE_KEY, toBalance, bytes32(uint256(to))); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
pragma solidity 0.6.4; | ||
|
||
interface TicketInterface { | ||
/// @notice Selects a user using a random number. The random number will be uniformly bounded to the ticket totalSupply. | ||
/// @param randomNumber The random number to use to select a user. | ||
/// @return The winner | ||
function draw(uint256 randomNumber) external view returns (address); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
pragma solidity 0.6.4; | ||
|
||
import "@openzeppelin/contracts-ethereum-package/contracts/Initializable.sol"; | ||
|
||
import "./Ticket.sol"; | ||
import "../external/openzeppelin/ProxyFactory.sol"; | ||
|
||
/// @title Controlled ERC20 Token Factory | ||
/// @notice Minimal proxy pattern for creating new Controlled ERC20 Tokens | ||
contract TicketProxyFactory is ProxyFactory { | ||
|
||
/// @notice Contract template for deploying proxied tokens | ||
Ticket public instance; | ||
|
||
/// @notice Initializes the Factory with an instance of the Controlled ERC20 Token | ||
constructor () public { | ||
instance = new Ticket(); | ||
} | ||
|
||
/// @notice Creates a new Controlled ERC20 Token as a proxy of the template instance | ||
/// @return A reference to the new proxied Controlled ERC20 Token | ||
function create() external returns (Ticket) { | ||
return Ticket(deployMinimal(address(instance), "")); | ||
} | ||
} |
Oops, something went wrong.