-
Notifications
You must be signed in to change notification settings - Fork 0
/
HaloHalo.sol
67 lines (58 loc) · 2.34 KB
/
HaloHalo.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.6.12;
import {ERC20} from '@openzeppelin/contracts/token/ERC20/ERC20.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
contract HaloHalo is ERC20('Rainbow Pool', 'xRNBW') {
using SafeMath for uint256;
IERC20 public halo;
uint256 public constant DECIMALS = 1e18;
uint256 public genesisTimestamp;
// Define the Halo token contract
constructor(IERC20 _halo) public {
halo = _halo;
genesisTimestamp = 0;
}
//TODO: add test before entering the genesis == 0, after enter >0
// Stake HALOs for HALOHALOs.
// Locks Halo and mints HALOHALO
function enter(uint256 _amount) public {
if (genesisTimestamp == 0) {
genesisTimestamp = now;
}
// Gets the amount of Halo locked in the contract
uint256 totalHalo = halo.balanceOf(address(this));
// Gets the amount of HALOHALO in existence
uint256 totalShares = totalSupply();
// If no HALOHALO exists, mint it 1:1 to the amount put in
if (totalShares == 0 || totalHalo == 0) {
_mint(msg.sender, _amount);
} else {
// Calculate and mint the amount of HALOHALO the Halo is worth. The ratio will change overtime, as HALOHALO is burned/minted and Halo deposited from LP rewards.
uint256 haloHaloAmount = _amount.mul(totalShares).div(totalHalo);
_mint(msg.sender, haloHaloAmount);
}
// Lock the Halo in the contract
halo.transferFrom(msg.sender, address(this), _amount);
}
// Claim HALOs from HALOHALOs.
// Unlocks the staked + gained Halo and burns HALOHALO
function leave(uint256 _share) public {
// Gets the amount of HALOHALO in existence
uint256 totalShares = totalSupply();
// Calculates the amount of Halo the HALOHALO is worth
uint256 haloHaloAmount =
_share.mul(halo.balanceOf(address(this))).div(totalShares);
_burn(msg.sender, _share);
halo.transfer(msg.sender, haloHaloAmount);
}
function getCurrentHaloHaloPrice() public view returns (uint256) {
uint256 totalShares = totalSupply();
require(totalShares > 0, 'No HALOHALO supply');
// convert to decimals to get answer in wei
uint256 haloHaloPrice =
halo.balanceOf(address(this)).mul(DECIMALS).div(totalShares);
// ratio in wei
return haloHaloPrice;
}
}