Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/restructure market creation incentives #62

Open
wants to merge 60 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
9b32ef2
Distribute rewards based on gas price and bonus bPLOT
udkreddySomish Oct 17, 2020
2e5ceef
Restrict setting initial creation incentives multiple times
udkreddySomish Oct 17, 2020
ba9a4aa
Handle rewards of market creators before the contract was upgraded
udkreddySomish Oct 18, 2020
b749437
corrected gas used variable
udkreddySomish Oct 19, 2020
1cb74a0
Added buffer gas for incentive calculation
udkreddySomish Oct 19, 2020
e5379b7
Added new contract for new creation incentive calculation
udkreddySomish Oct 19, 2020
256759b
Fetch the current gas price from chainlink and updated calculations
udkreddySomish Oct 19, 2020
ee8f9a9
Removed bPLOT incentives for market creation
udkreddySomish Oct 19, 2020
f860d92
Fixed price conversion issue
udkreddySomish Oct 19, 2020
6a97208
Updated testcases
udkreddySomish Oct 19, 2020
a655293
Updated buffer gas amount, and minor fixes
udkreddySomish Oct 20, 2020
4495eec
Updated testcases for new market creation incentives
udkreddySomish Oct 20, 2020
899d465
Updated incentive formulae and added authorized address check for set…
udkreddySomish Oct 20, 2020
9a5c7ad
Added code comments
udkreddySomish Oct 20, 2020
91000c9
Corrected comments
udkreddySomish Oct 20, 2020
5cfa0d8
Increased default balance in coverage script
udkreddySomish Oct 20, 2020
a450e1f
Increased default balance in coverage script
udkreddySomish Oct 20, 2020
5b40913
Removed commented code
udkreddySomish Oct 20, 2020
afec03d
Removed unused argument
udkreddySomish Oct 21, 2020
bb552f8
Covergae: Fixed issue with high gas while running coverage
udkreddySomish Oct 21, 2020
5d22c42
Removed redundant checks and corrected indentation
udkreddySomish Oct 21, 2020
ae4a176
Covergae: Fixed issue with low balance while running coverage
udkreddySomish Oct 21, 2020
902cd4f
Updated testcases
udkreddySomish Oct 21, 2020
e45297d
Added feature to distribute percent of reward pool share for market c…
udkreddySomish Oct 30, 2020
084863a
Fixed compile issues
udkreddySomish Oct 31, 2020
41ab257
Corrected event while claimingmarket creation incentives
udkreddySomish Oct 31, 2020
64c01e1
Corrected formulae
udkreddySomish Oct 31, 2020
1f4810b
Updated plot stake required for reward pool share percent
udkreddySomish Oct 31, 2020
cb76e15
Updated plot stake required for reward pool share percent
udkreddySomish Oct 31, 2020
d8849e8
Corrected formulae for reward pool share percent
udkreddySomish Oct 31, 2020
37ce6f5
Corrected formulae
udkreddySomish Nov 1, 2020
ab5dbd0
Removed unused variables
udkreddySomish Nov 1, 2020
390af34
Added code comments
udkreddySomish Nov 1, 2020
9ddc41e
Updated MarketCreationReward eventdata
udkreddySomish Nov 2, 2020
bcf1c30
Fixed minor issues
udkreddySomish Nov 2, 2020
0600070
Updated testcases for Market creation incentives
udkreddySomish Nov 2, 2020
c56df09
Fix broken test cases
udkreddySomish Nov 2, 2020
6020c5a
Implement old Market contract instead of rewriting whole code for new…
udkreddySomish Nov 2, 2020
4e39b02
Added new function for calling market result event instead of updatin…
udkreddySomish Nov 2, 2020
95d6cb7
Added more testcases
udkreddySomish Nov 2, 2020
fe9b407
Added a market leveraged participation threshold for reward pool shar…
udkreddySomish Nov 5, 2020
c059a93
Fixed minor issue
udkreddySomish Nov 5, 2020
b685a72
Updated testcases
udkreddySomish Nov 5, 2020
bc50cd3
Fixed broken testcases in coverage run
udkreddySomish Nov 6, 2020
abd3e1b
Fixed broken testcases
udkreddySomish Nov 6, 2020
7ce214f
Fixed broken testcases
udkreddySomish Nov 6, 2020
0e74370
Fixed issue while user claiming market creation rewards
udkreddySomish Nov 8, 2020
00adefc
Added a function to get users pending market creation rewards
udkreddySomish Nov 10, 2020
e2a0e84
Added event market creator reward pool share details
udkreddySomish Nov 11, 2020
f8fbe7a
Added user address argument to check pending market creation rewards
udkreddySomish Nov 12, 2020
c21755d
Updated testcases
udkreddySomish Nov 12, 2020
cf68f6b
Added a functionality to pause market creation of particular type thr…
udkreddySomish Nov 17, 2020
81e4714
Updated comments
udkreddySomish Nov 17, 2020
90ac621
Corrected event data
udkreddySomish Nov 18, 2020
bfc5622
Fixed bug in claim reward
udkreddySomish Nov 19, 2020
78c8be6
Updated testcases
udkreddySomish Nov 19, 2020
64cb3e2
Merge branch 'feature/restructure-marketCreationIncentives' into feat…
udkreddySomish Nov 19, 2020
ccedbf1
Merge pull request #119 from plotx/feature/pauseMarketCreationOfType
udkreddySomish Nov 19, 2020
e71c3cf
Updated test cases
udkreddySomish Nov 19, 2020
4edc39b
Fixed dependencies
udkreddySomish Nov 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions contracts/MarketRegistryNew.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* Copyright (C) 2020 PlotX.io

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/ */

pragma solidity 0.5.7;
import "./MarketRegistry.sol";
import "./interfaces/IChainLinkOracle.sol";
import "./external/openzeppelin-solidity/math/Math.sol";

contract MarketRegistryNew is MarketRegistry {

uint256 internal maxGasPrice;
IChainLinkOracle public clGasPriceAggregator;

mapping(address => uint256) userIncentives;
event MarketCreationReward(address indexed createdBy, uint256 plotIncentive, uint256 gasUsed, uint256 gasCost, uint256 gasPriceConsidered, uint256 gasPriceGiven, uint256 maxGasCap);
event ClaimedMarketCreationReward(address indexed user, uint256 plotIncentive);

/**
* @dev Set initial market creation incentive params.
*/
function setGasPriceAggAndMaxGas(address _clGasPriceAggregator) external {
require(address(clGasPriceAggregator) == address(0));
require(msg.sender == marketInitiater);
clGasPriceAggregator = IChainLinkOracle(_clGasPriceAggregator);
maxGasPrice = 100 * 10**9;
maxsam4 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @dev Creates the new market
* @param _marketType The type of the market.
* @param _marketCurrencyIndex the index of market currency.
*/
function createMarket(uint256 _marketType, uint256 _marketCurrencyIndex) public payable{
uint256 gasProvided = gasleft();
address penultimateMarket = marketCreationData[_marketType][_marketCurrencyIndex].penultimateMarket;
if(penultimateMarket != address(0)) {
IMarket(penultimateMarket).settleMarket();
}
if(marketCreationData[_marketType][_marketCurrencyIndex].marketAddress != address(0)) {
(,,,,,,,, uint _status) = getMarketDetails(marketCreationData[_marketType][_marketCurrencyIndex].marketAddress);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making marketStatus() public and then directly using that instead of this generic function will save a decent chunk of gas. However, that'll only work for new markets.

require(_status >= uint(IMarket.PredictionStatus.InSettlement));
}
(uint8 _roundOfToNearest, bytes32 _currencyName, address _priceFeed) = IMarket(marketCurrencies[_marketCurrencyIndex].marketImplementation).getMarketFeedData();
marketUtility.update();
uint64 _marketStartTime = calculateStartTimeForMarket(_marketType, _marketCurrencyIndex);
uint64 _optionRangePerc = marketTypes[_marketType].optionRangePerc;
uint currentPrice = marketUtility.getAssetPriceUSD(_priceFeed);
_optionRangePerc = uint64(currentPrice.mul(_optionRangePerc.div(2)).div(10000));
maxsam4 marked this conversation as resolved.
Show resolved Hide resolved
uint64 _decimals = marketCurrencies[_marketCurrencyIndex].decimals;
uint64 _minValue = uint64((ceil(currentPrice.sub(_optionRangePerc).div(_roundOfToNearest), 10**_decimals)).mul(_roundOfToNearest));
uint64 _maxValue = uint64((ceil(currentPrice.add(_optionRangePerc).div(_roundOfToNearest), 10**_decimals)).mul(_roundOfToNearest));
maxsam4 marked this conversation as resolved.
Show resolved Hide resolved
_createMarket(_marketType, _marketCurrencyIndex, _minValue, _maxValue, _marketStartTime, _currencyName);
uint256 gasUsed = gasProvided - gasleft();
_calculateIncentive(gasUsed);
}

/**
* @dev internal function to calculate user incentive for market creation
*/
function _calculateIncentive(uint256 gasUsed) internal{
//Adding buffer gas for below calculations
gasUsed = gasUsed + 38500;
uint256 gasPrice = _checkGasPrice();
uint256 gasCost = gasUsed.mul(gasPrice);
(, uint256 incentive) = marketUtility.getValueAndMultiplierParameters(ETH_ADDRESS, gasCost);
userIncentives[msg.sender] = userIncentives[msg.sender].add(incentive);
emit MarketCreationReward(msg.sender, incentive, gasUsed, gasCost, gasPrice, tx.gasprice, maxGasPrice);
}

/**
* @dev internal function to calculate gas price for market creation incentives
*/
function _checkGasPrice() internal view returns(uint256) {
uint fastGas = uint(clGasPriceAggregator.latestAnswer());
uint fastGasWithMaxDeviation = fastGas.mul(125).div(100);
return Math.min(Math.min(tx.gasprice,fastGasWithMaxDeviation), maxGasPrice);
}


/**
* @dev function to reward user for initiating market creation calls as per the new incetive calculations
*/
function claimCreationRewardV2() external {
uint256 pendingPLOTReward = userIncentives[msg.sender];
require(pendingPLOTReward > 0);
delete userIncentives[msg.sender];
_transferAsset(address(plotToken), msg.sender, pendingPLOTReward);
emit ClaimedMarketCreationReward(msg.sender, pendingPLOTReward);
}

/**
* @dev function to update address parameters of market
*/
function updateConfigAddressParameters(bytes8 code, address payable value) external onlyAuthorizedToGovern {
if(code == "GASAGG") { // Incentive to be distributed to user for market creation
clGasPriceAggregator = IChainLinkOracle(value);
} else {
marketUtility.updateAddressParameters(code, value);
}
}

/**
* @dev function to update integer parameters of market
*/
function updateUintParameters(bytes8 code, uint256 value) external onlyAuthorizedToGovern {
if(code == "MCRINC") { // Incentive to be distributed to user for market creation
marketCreationIncentive = value;
} else if(code == "MAXGAS") { // Maximum gas upto which is considered while calculating market creation incentives
maxGasPrice = value;
} else {
marketUtility.updateUintParameters(code, value);
}
}

/**
* @dev Get uint config parameters
*/
function getUintParameters(bytes8 code) external view returns(bytes8 codeVal, uint256 value) {
codeVal = code;
if(code == "MCRINC") {
value = marketCreationIncentive;
} else if(code == "MAXGAS") {
value = maxGasPrice;
}
}

}
11 changes: 11 additions & 0 deletions contracts/interfaces/IMarketUtility.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,15 @@ contract IMarketUtility {
address _currencyFeedAddress,
uint256 _settleTime
) public view returns (uint256 latestAnswer, uint256 roundId);

/**
* @dev Get value of provided currency address in ETH
* @param _currencyAddress Address of currency
* @param _amount Amount of provided currency
* @return Value of provided amount in ETH
**/
function getAssetValueETH(address _currencyAddress, uint256 _amount)
public
view
returns (uint256 tokenEthValue);
}
80 changes: 41 additions & 39 deletions contracts/mock/MockChainLinkAggregator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,49 @@ pragma solidity 0.5.7;
import "../interfaces/IChainLinkOracle.sol";
contract MockChainLinkAggregator is IChainLinkOracle{

int256 latestAns = 934999802346;
uint256 updatedAt = now;
int256 latestAns = 934999802346;
uint256 updatedAt = now;

struct RoundData {
uint80 roundId;
int256 answer;
uint256 startedAt;
uint256 updatedAt;
uint80 answeredInRound;
}
struct RoundData {
uint80 roundId;
int256 answer;
uint256 startedAt;
uint256 updatedAt;
uint80 answeredInRound;
}

mapping(uint80 => RoundData) public roundData;
uint80 public currentRound;
mapping(uint80 => RoundData) public roundData;
uint80 public currentRound;

constructor() public {
currentRound = 0;
roundData[0] = RoundData(uint80(0),latestAns, updatedAt, updatedAt, uint80(0));
}
constructor() public {
currentRound = 0;
roundData[0] = RoundData(uint80(0),latestAns, updatedAt, updatedAt, uint80(0));
}

function decimals() external view returns (uint8) {
return uint8(8);
}
/**
* @dev Gets the latest answer of chainLink oracle.
* @return int256 representing the latest answer of chainLink oracle.
*/
function latestAnswer() external view returns (int256)
{
return roundData[currentRound].answer;

}
/**
* @dev Gets the latest answer of chainLink oracle.
* @return int256 representing the latest answer of chainLink oracle.
*/
function latestAnswer() external view returns (int256)
{
return roundData[currentRound].answer;
}

/**
* @dev Set the latest answer of chainLink oracle.
* @param _latestAnswer The latest anser of chainLink oracle.
*/
function setLatestAnswer(int256 _latestAnswer) public
{
* @dev Set the latest answer of chainLink oracle.
* @param _latestAnswer The latest anser of chainLink oracle.
*/
function setLatestAnswer(int256 _latestAnswer) public
{
currentRound = currentRound + uint80(1);
roundData[currentRound] = RoundData(currentRound,_latestAnswer, now, now, currentRound);
}
}

function getRoundData(uint80 _roundId)
function getRoundData(uint80 _roundId)
external
view
returns (
Expand All @@ -54,12 +54,13 @@ contract MockChainLinkAggregator is IChainLinkOracle{
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
) {
return (roundData[_roundId].roundId, roundData[_roundId].answer, roundData[_roundId].startedAt,
roundData[_roundId].updatedAt,roundData[_roundId].answeredInRound);
}
)
{
return (roundData[_roundId].roundId, roundData[_roundId].answer, roundData[_roundId].startedAt,
roundData[_roundId].updatedAt,roundData[_roundId].answeredInRound);
}

function latestRoundData()
function latestRoundData()
external
view
returns (
Expand All @@ -68,9 +69,10 @@ contract MockChainLinkAggregator is IChainLinkOracle{
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
) {
return (roundData[currentRound].roundId, roundData[currentRound].answer, roundData[currentRound].startedAt,
roundData[currentRound].updatedAt,roundData[currentRound].answeredInRound);
}
)
{
return (roundData[currentRound].roundId, roundData[currentRound].answer, roundData[currentRound].startedAt,
roundData[currentRound].updatedAt,roundData[currentRound].answeredInRound);
}

}
76 changes: 76 additions & 0 deletions contracts/mock/MockChainLinkGasPriceAgg.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
pragma solidity 0.5.7;

import "../interfaces/IChainLinkOracle.sol";
contract MockChainLinkGasPriceAgg is IChainLinkOracle{

int256 latestAns = 45000000000;
uint256 updatedAt = now;

struct RoundData {
uint80 roundId;
int256 answer;
uint256 startedAt;
uint256 updatedAt;
uint80 answeredInRound;
}

mapping(uint80 => RoundData) public roundData;
uint80 public currentRound;

constructor() public {
currentRound = 0;
roundData[0] = RoundData(uint80(0),latestAns, updatedAt, updatedAt, uint80(0));
}

function decimals() external view returns (uint8) {
return uint8(8);
}
/**
* @dev Gets the latest answer of chainLink oracle.
* @return int256 representing the latest answer of chainLink oracle.
*/
function latestAnswer() external view returns (int256)
{
return roundData[currentRound].answer;

}

/**
* @dev Set the latest answer of chainLink oracle.
* @param _latestAnswer The latest anser of chainLink oracle.
*/
function setLatestAnswer(int256 _latestAnswer) public
{
currentRound = currentRound + uint80(1);
roundData[currentRound] = RoundData(currentRound,_latestAnswer, now, now, currentRound);
}

function getRoundData(uint80 _roundId)
external
maxsam4 marked this conversation as resolved.
Show resolved Hide resolved
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
) {
return (roundData[_roundId].roundId, roundData[_roundId].answer, roundData[_roundId].startedAt,
roundData[_roundId].updatedAt,roundData[_roundId].answeredInRound);
}

function latestRoundData()
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
) {
return (roundData[currentRound].roundId, roundData[currentRound].answer, roundData[currentRound].startedAt,
roundData[currentRound].updatedAt,roundData[currentRound].answeredInRound);
}

}
4 changes: 4 additions & 0 deletions migrations/2_deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ module.exports = function(deployer, network, accounts){
// console.log(await plotus.getOpenMarkets());
await plotusToken.transfer(uniswapRouter.address, "100000000000000000000");
await plotusToken.transfer(plotus.address, "10000000000000000000000");
// await blotToken.addMinter(plotusAddress);
// await plotusToken.approve(blotToken.address, "1000000000000000000000000000");
// await blotToken.mint(plotus.address, "10000000000000000000000");
// await plotus.setInitialCreationIncentives();
});
};

Loading