# Pool Factory V2

Contract Code: <https://github.com/0xBets/mvp/blob/upgradable-contracts/packages/hardhat/contracts/PoolFactoryV2.sol>

### Functions:

* [`initialize()`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`createPool(uint256 _game, uint256 _gameMode, uint256 _currentPrize, uint256 _entryFee, uint256 _startOn, uint256 _finishOn, uint256 _firstFee, uint256 _secondFee, uint256 _thirdFee, address _tokenAddr)`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`joinPool(uint256 _poolId)`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`leavePool(uint256 _poolId)`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`viewPools(address _addr)`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`viewPoolPlayers(uint256 _poolId)`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`viewActivePools()`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`getTreasury()`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`finishPool(uint256 _poolId, address payable _winner1, address payable _winner2, address payable _winner3)`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`cancelPool(uint256 _poolId)`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`updateFee(uint256 _newFee)`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`updateTreasury(address payable _newTreasury)`](https://docs.blast.gg/smart-contracts/broken-reference)

### Events:

* [`NewPool(uint256 poolId, uint256 _game, uint256 _gameMode, uint256 _currentPrize, uint256 _entryFee, uint256 _startOn, uint256 _finishOn, uint256 _firstFee, uint256 _secondFee, uint256 _thirdFee, address _tokenAddr)`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`CancelPool(uint256 _poolId)`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`PoolLeftBy(uint256 _poolId, address _player)`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`PoolJoinedBy(uint256 _poolId, address _player)`](https://docs.blast.gg/smart-contracts/broken-reference)
* [`PoolFinished(uint256 _poolId, address _winner)`](https://docs.blast.gg/smart-contracts/broken-reference)

### Function `initialize()` <a href="#poolfactoryv2-initialize" id="poolfactoryv2-initialize"></a>

Initializes pool count and bbFee

### Function `createPool(uint256 _game, uint256 _gameMode, uint256 _currentPrize, uint256 _entryFee, uint256 _startOn, uint256 _finishOn, uint256 _firstFee, uint256 _secondFee, uint256 _thirdFee, address _tokenAddr) → uint256` <a href="#poolfactoryv2-createpool-uint256-uint256-uint256-uint256-uint256-uint256-uint256-uint256-uint256-add" id="poolfactoryv2-createpool-uint256-uint256-uint256-uint256-uint256-uint256-uint256-uint256-uint256-add"></a>

Creates a new Pool providing entryFee, CurrentPrize, start and finish times.\
The transaction value must be equal to initial pool prize.\
Prizes and fees are in uint256, start and finish times are Unix Epochs in miliseconds.\
Fees are uint expressed in percentages i.e. a value of 10 means 10% fee, the sum of the 3 fees must equal a 100

#### Parameters:

* `_game`: game Id
* `_gameMode`: game Mode Id
* `_currentPrize`: initial pool prize
* `_entryFee`: entry fee to join the pool
* `_startOn`: unix start epoch in ms
* `_finishOn`: unix finish epoch in ms
* `_firstFee`: percentage fee of 1st place
* `_secondFee`: percentage fee of 2nd place
* `_thirdFee`: percentage fee of 3d place
* `_tokenAddr`: token used for pool prize and fees, if tokenAddr is address(0), native coin will be used

#### Return Values:

* PoolId (uint256)

### Function `joinPool(uint256 _poolId)` <a href="#poolfactoryv2-joinpool-uint256" id="poolfactoryv2-joinpool-uint256"></a>

Allows players to join a pool if the pool has an entry fee it should be equal to msg.value

### Function `leavePool(uint256 _poolId)` <a href="#poolfactoryv2-leavepool-uint256" id="poolfactoryv2-leavepool-uint256"></a>

Allows players to leave the pool if they change their mind before it starts Already started pools can't be left

### Function `viewPools(address _addr) → uint256[] _userPools` <a href="#poolfactoryv2-viewpools-address" id="poolfactoryv2-viewpools-address"></a>

View function to retrieve joined pools for a given player(address)

#### Parameters:

* `_addr`: address of player

### Function `viewPoolPlayers(uint256 _poolId) → address payable[] _users` <a href="#poolfactoryv2-viewpoolplayers-uint256" id="poolfactoryv2-viewpoolplayers-uint256"></a>

View function to retrieve players of a given pool

#### Parameters:

* `_poolId`: pool id number

#### Return Values:

* \_users an array of players addresses

### Function `viewActivePools() → uint256[] _activePools` <a href="#poolfactoryv2-viewactivepools" id="poolfactoryv2-viewactivepools"></a>

View function to retrieve current active Pools

#### Return Values:

* \_activePools an array of pool Ids

### Function `getTreasury() → address _treasury` <a href="#poolfactoryv2-gettreasury" id="poolfactoryv2-gettreasury"></a>

View function to retrieve current treasury address

### Function `finishPool(uint256 _poolId, address payable _winner1, address payable _winner2, address payable _winner3)` <a href="#poolfactoryv2-finishpool-uint256-address-payable-address-payable-address-payable" id="poolfactoryv2-finishpool-uint256-address-payable-address-payable-address-payable"></a>

Admin function to finish a pool

#### Parameters:

* `_poolId`: id of the pool to finish
* `_winner1`: address of the first place
* `_winner2`: address of the second place
* `_winner3`: address of the third place

### Function `cancelPool(uint256 _poolId)` <a href="#poolfactoryv2-cancelpool-uint256" id="poolfactoryv2-cancelpool-uint256"></a>

Admin function to cancel a pool it returns the entry fee to all the players who joined, and the prize pool to treasury

#### Parameters:

* `_poolId`: id of the pool to finish

### Function `updateFee(uint256 _newFee)` <a href="#poolfactoryv2-updatefee-uint256" id="poolfactoryv2-updatefee-uint256"></a>

Admin function to update protocol fee

#### Parameters:

* `_newFee`: new fee to set

### Event `NewPool(uint256 poolId, uint256 _game, uint256 _gameMode, uint256 _currentPrize, uint256 _entryFee, uint256 _startOn, uint256 _finishOn, uint256 _firstFee, uint256 _secondFee, uint256 _thirdFee, address _tokenAddr)` <a href="#poolfactoryv2-newpool-uint256-uint256-uint256-uint256-uint256-uint256-uint256-uint256-uint256-uint25" id="poolfactoryv2-newpool-uint256-uint256-uint256-uint256-uint256-uint256-uint256-uint256-uint256-uint25"></a>

Event emitted on new pool created

#### Parameters:

* `poolId`: pool Id
* `_game`: game Id
* `_gameMode`: game Mode Id
* `_currentPrize`: initial pool prize
* `_entryFee`: entry fee to join the pool
* `_startOn`: unix start epoch in ms
* `_finishOn`: unix finish epoch in ms
* `_firstFee`: percentage fee of 1st place
* `_secondFee`: percentage fee of 2nd place
* `_thirdFee`: percentage fee of 3d place
* `_tokenAddr`: address of token used for pool

### Event `CancelPool(uint256 _poolId)` <a href="#poolfactoryv2-cancelpool-uint256" id="poolfactoryv2-cancelpool-uint256"></a>

Event emitted on cancelled pool

#### Parameters:

* `_poolId`: pool Id of cancelled pool

### Event `PoolLeftBy(uint256 _poolId, address _player)` <a href="#poolfactoryv2-poolleftby-uint256-address" id="poolfactoryv2-poolleftby-uint256-address"></a>

Event emitted when a player leaves a pool

#### Parameters:

* `_player`: player who leaves the pool
* `_poolId`: pool Id

### Event `PoolJoinedBy(uint256 _poolId, address _player)` <a href="#poolfactoryv2-pooljoinedby-uint256-address" id="poolfactoryv2-pooljoinedby-uint256-address"></a>

Event emitted when a player joins a pool

#### Parameters:

* `_player`: player who joins the pool
* `_poolId`: pool Id

### Event `PoolFinished(uint256 _poolId, address _winner)` <a href="#poolfactoryv2-poolfinished-uint256-address" id="poolfactoryv2-poolfinished-uint256-address"></a>

Event emitted on a finished pool

#### Parameters:

* `_poolId`: pool Id
* `_winner`: winner of the pool tournament
