diff --git a/contracts/Referral.sol b/contracts/Referral.sol index 9f9e1987..065e804a 100644 --- a/contracts/Referral.sol +++ b/contracts/Referral.sol @@ -20,29 +20,35 @@ import "./external/proxy/OwnedUpgradeabilityProxy.sol"; import "./external/NativeMetaTransaction.sol"; import "./interfaces/IMaster.sol"; import "./interfaces/IAllMarkets.sol"; +import "./interfaces/IbPLOTToken.sol"; import "./interfaces/IToken.sol"; import "./interfaces/IAuth.sol"; contract Referral is IAuth, NativeMetaTransaction { - event ReferralLog(address indexed referrer, address indexed referee, uint256 referredOn); - event ClaimedReferralReward(address indexed user, address token, uint256 amount); + event ReferralLog(address indexed referrer, address indexed referee, uint256 referredOn, uint256 validity); + event ReferralFeeLog(address indexed referrer, address indexed referee, address token, uint256 referrerFee, uint256 refereeFee); + event ClaimedReferralReward(address indexed user, address token, uint256 referrerFee, uint256 refereeFee); struct UserData { mapping(address => uint256) referrerFee; // Fee earned by referring another user for a given token mapping(address => uint256) refereeFee; // Fee earned after being referred by another user for a given token - address referrer; // Address of the referrer + address referrer; // Address of the referrer + uint validity; } IAllMarkets internal allMarkets; + IbPLOTToken internal bPLOTToken; address internal masterAddress; + address internal plotToken; uint internal predictionDecimalMultiplier; + uint public referralValidity; mapping (address => UserData) public userData; modifier onlyInternal { - IMaster(masterAddress).isInternal(msg.sender); + require(IMaster(masterAddress).isInternal(msg.sender)); _; } @@ -55,10 +61,37 @@ contract Referral is IAuth, NativeMetaTransaction { authorized = ms.authorized(); masterAddress = _masterAddress; allMarkets = IAllMarkets(ms.getLatestAddress("AM")); + bPLOTToken = IbPLOTToken(ms.getLatestAddress("BL")); + plotToken = ms.dAppToken(); predictionDecimalMultiplier = 10; + referralValidity = 90 days; _initializeEIP712("RF"); } + /** + * @dev Approves Plot tokens to bPLOT contract to use PLOT for minting bPLOT + * @param _amount amount of plot tokens + */ + function approveToBPLOT(uint _amount) external onlyAuthorized { + require(IToken(plotToken).approve((address(bPLOTToken)),_amount),"ERC20 call Failed"); + } + + /** + * @dev Renounce this contract as minter + */ + function renounceAsMinter() public onlyAuthorized { + bPLOTToken.renounceMinter(); + } + + /** + * @dev Set time upto which both referrer and referee can earn fee for refferral + * @param _referralValidity Time in seconds + */ + function setReferralValidity(uint _referralValidity) public onlyAuthorized { + require(_referralValidity > 0); + referralValidity = _referralValidity; + } + /** * @dev Set referrer address of a user, can be set only by the authorized users * @param _referrer User who is referring new user @@ -70,7 +103,8 @@ contract Referral is IAuth, NativeMetaTransaction { require(allMarkets.getTotalStakedByUser(_referee) == 0); require(_userData.referrer == address(0)); _userData.referrer = _referrer; - emit ReferralLog(_referrer, _referee, now); + _userData.validity = referralValidity.add(now); + emit ReferralLog(_referrer, _referee, now, _userData.validity); } /** @@ -84,12 +118,13 @@ contract Referral is IAuth, NativeMetaTransaction { function setReferralRewardData(address _referee, address _token, uint _referrerFee, uint _refereeFee) external onlyInternal returns(bool _isEligible) { UserData storage _userData = userData[_referee]; address _referrer = _userData.referrer; - if(_referrer != address(0)) { + if(_referrer != address(0) && _userData.validity >= now) { _isEligible = true; //Commission for referee _userData.refereeFee[_token] = _userData.refereeFee[_token].add(_refereeFee); //Commission for referrer userData[_referrer].referrerFee[_token] = userData[_referrer].referrerFee[_token].add(_referrerFee); + emit ReferralFeeLog(_referrer, _referee, _token, _referrerFee, _refereeFee); } } @@ -115,8 +150,9 @@ contract Referral is IAuth, NativeMetaTransaction { uint256 _refereeFee = _userData.refereeFee[_token]; delete _userData.refereeFee[_token]; uint _tokenToTransfer = (_refereeFee.add(_referrerFee)).mul(10**predictionDecimalMultiplier); + require(_tokenToTransfer > 0); _transferAsset(_token, _user, _tokenToTransfer); - emit ClaimedReferralReward(_user, _token, _tokenToTransfer); + emit ClaimedReferralReward(_user, _token, _referrerFee, _refereeFee); } /** @@ -125,8 +161,10 @@ contract Referral is IAuth, NativeMetaTransaction { * @param _amount The amount which is transfer. */ function _transferAsset(address _asset, address _recipient, uint256 _amount) internal { - if(_amount > 0) { - require(IToken(_asset).transfer(_recipient, _amount)); + if(_asset == plotToken) { + require(bPLOTToken.mint(_recipient, _amount)); + } else { + require(IToken(_asset).transfer(_recipient, _amount)); } } diff --git a/contracts/interfaces/IbPLOTToken.sol b/contracts/interfaces/IbPLOTToken.sol index f6a5f082..51622d1b 100644 --- a/contracts/interfaces/IbPLOTToken.sol +++ b/contracts/interfaces/IbPLOTToken.sol @@ -4,4 +4,5 @@ contract IbPLOTToken { function convertToPLOT(address _of, address _to, uint256 amount) public; function transfer(address recipient, uint256 amount) public returns (bool); function renounceMinter() public; + function mint(address account, uint256 amount) public returns (bool); } \ No newline at end of file diff --git a/test/10_plotusMetaTx.js b/test/10_plotusMetaTx.js index bb63858d..3310f2a3 100644 --- a/test/10_plotusMetaTx.js +++ b/test/10_plotusMetaTx.js @@ -3,6 +3,7 @@ const { assert } = require("chai"); const OwnedUpgradeabilityProxy = artifacts.require("OwnedUpgradeabilityProxy"); const Master = artifacts.require("Master"); const PlotusToken = artifacts.require("MockPLOT"); +const BLOT = artifacts.require("BPLOT"); const AllMarkets = artifacts.require("MockAllMarkets"); const CyclicMarkets = artifacts.require("MockCyclicMarkets"); const Referral = artifacts.require("Referral"); @@ -50,6 +51,7 @@ contract("Rewards-Market", async function(users) { plotusToken = await PlotusToken.deployed(); timeNow = await latestTime(); + bPlotInstance = await BLOT.at(await masterInstance.getLatestAddress(web3.utils.toHex("BL"))); allMarkets = await AllMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("AM"))); cyclicMarkets = await CyclicMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("CM"))); referral = await Referral.deployed(); @@ -209,7 +211,8 @@ contract("Rewards-Market", async function(users) { it("Check referral fee", async () => { let referralRewardPlot = [9.932, 0.8, 0.42, 0.246, 1, 1.4, 0.4, 0.1, 0.6, 0]; - + await bPlotInstance.addMinter(referral.address); + await referral.approveToBPLOT(toWei(100000000)); for(i=1;i<11;i++) { let reward = await referral.getReferralFees(users[i], plotusToken.address); @@ -219,8 +222,9 @@ contract("Rewards-Market", async function(users) { reward = reward[1]; } assert.equal(reward/1,referralRewardPlot[i-1]*1e8); - let plotBalBefore = await plotusToken.balanceOf(users[i]); + let plotBalBefore = await bPlotInstance.balanceOf(users[i]); functionSignature = encode3("claimReferralFee(address,address)", users[i], plotusToken.address); + if(reward > 0) await signAndExecuteMetaTx( privateKeyList[i], users[i], @@ -228,7 +232,7 @@ contract("Rewards-Market", async function(users) { referral, "RF" ); - let plotBalAfter = await plotusToken.balanceOf(users[i]); + let plotBalAfter = await bPlotInstance.balanceOf(users[i]); assert.equal(Math.round((plotBalAfter/1e13-plotBalBefore/1e13)),reward/1e3); } }) @@ -348,6 +352,7 @@ contract("Rewards-Market Raise dispute and pass the proposal ", async function(u plotusToken = await PlotusToken.deployed(); timeNow = await latestTime(); + bPlotInstance = await BLOT.at(await masterInstance.getLatestAddress(web3.utils.toHex("BL"))); cyclicMarkets = await CyclicMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("CM"))); allMarkets = await AllMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("AM"))); disputeResolution = await DisputeResolution.at(await masterInstance.getLatestAddress(web3.utils.toHex("DR"))); @@ -499,6 +504,8 @@ contract("Rewards-Market Raise dispute and pass the proposal ", async function(u it("Check referral fee", async () => { let referralRewardPlot = [10.532, 0.8, 0.42, 0.246, 1, 1.4, 0.4, 0.1, 0.6, 0.3]; + await bPlotInstance.addMinter(referral.address); + await referral.approveToBPLOT(toWei(100000000)); for(i=1;i<11;i++) { @@ -509,8 +516,9 @@ contract("Rewards-Market Raise dispute and pass the proposal ", async function(u reward = reward[1]; } assert.equal(reward/1,referralRewardPlot[i-1]*1e8); - let plotBalBefore = await plotusToken.balanceOf(users[i]); + let plotBalBefore = await bPlotInstance.balanceOf(users[i]); functionSignature = encode3("claimReferralFee(address,address)", users[i], plotusToken.address); + if(reward > 0) await signAndExecuteMetaTx( privateKeyList[i], users[i], @@ -518,7 +526,7 @@ contract("Rewards-Market Raise dispute and pass the proposal ", async function(u referral, "RF" ); - let plotBalAfter = await plotusToken.balanceOf(users[i]); + let plotBalAfter = await bPlotInstance.balanceOf(users[i]); assert.equal(Math.round((plotBalAfter/1e13-plotBalBefore/1e13)),reward/1e3); } }) diff --git a/test/18_Acyclic_testcase.js b/test/18_Acyclic_testcase.js index af486508..8ff27a70 100644 --- a/test/18_Acyclic_testcase.js +++ b/test/18_Acyclic_testcase.js @@ -54,6 +54,7 @@ contract("Rewards-Market", async function(users) { BLOTInstance = await BLOT.deployed(); timeNow = await latestTime(); + bPlotInstance = await BLOT.at(await masterInstance.getLatestAddress(web3.utils.toHex("BL"))); allMarkets = await AllMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("AM"))); acyclicMarkets = await AcyclicMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("AC"))); referral = await Referral.deployed(); @@ -222,6 +223,8 @@ contract("Rewards-Market", async function(users) { it("Check referral fee", async () => { let referralRewardPlot = [10.532, 0.8, 0.42, 0.246, 1, 1.4, 0.4, 0.1, 0.6, 0.3]; + await bPlotInstance.addMinter(referral.address); + await referral.approveToBPLOT(toWei(100000000)); for(i=1;i<11;i++) { @@ -232,8 +235,9 @@ contract("Rewards-Market", async function(users) { reward = reward[1]; } assert.equal(reward/1,referralRewardPlot[i-1]*1e8); - let plotBalBefore = await plotusToken.balanceOf(users[i]); + let plotBalBefore = await bPlotInstance.balanceOf(users[i]); functionSignature = encode3("claimReferralFee(address,address)", users[i], plotusToken.address); + if(reward > 0) await signAndExecuteMetaTx( privateKeyList[i], users[i], @@ -241,7 +245,7 @@ contract("Rewards-Market", async function(users) { referral, "RF" ); - let plotBalAfter = await plotusToken.balanceOf(users[i]); + let plotBalAfter = await bPlotInstance.balanceOf(users[i]); assert.equal(Math.round((plotBalAfter/1e13-plotBalBefore/1e13)),reward/1e3); } }) @@ -391,6 +395,7 @@ contract("Rewards-Market Raise dispute and pass the proposal ", async function(u BLOTInstance = await BLOT.deployed(); timeNow = await latestTime(); + bPlotInstance = await BLOT.at(await masterInstance.getLatestAddress(web3.utils.toHex("BL"))); acyclicMarkets = await AcyclicMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("AC"))); allMarkets = await AllMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("AM"))); disputeResolution = await DisputeResolution.at(await masterInstance.getLatestAddress(web3.utils.toHex("DR"))); @@ -543,6 +548,8 @@ contract("Rewards-Market Raise dispute and pass the proposal ", async function(u it("Check referral fee", async () => { let referralRewardPlot = [10.532, 0.8, 0.42, 0.246, 1, 1.4, 0.4, 0.1, 0.6, 0.3]; + await bPlotInstance.addMinter(referral.address); + await referral.approveToBPLOT(toWei(100000000)); for(i=1;i<11;i++) { @@ -553,8 +560,9 @@ contract("Rewards-Market Raise dispute and pass the proposal ", async function(u reward = reward[1]; } assert.equal(reward/1,referralRewardPlot[i-1]*1e8); - let plotBalBefore = await plotusToken.balanceOf(users[i]); + let plotBalBefore = await bPlotInstance.balanceOf(users[i]); functionSignature = encode3("claimReferralFee(address,address)", users[i], plotusToken.address); + if(reward > 0) await signAndExecuteMetaTx( privateKeyList[i], users[i], @@ -562,7 +570,7 @@ contract("Rewards-Market Raise dispute and pass the proposal ", async function(u referral, "RF" ); - let plotBalAfter = await plotusToken.balanceOf(users[i]); + let plotBalAfter = await bPlotInstance.balanceOf(users[i]); assert.equal(Math.round((plotBalAfter/1e13-plotBalBefore/1e13)),reward/1e3); } }) diff --git a/test/Referral.test.js b/test/Referral.test.js new file mode 100644 index 00000000..45f387a9 --- /dev/null +++ b/test/Referral.test.js @@ -0,0 +1,272 @@ +const { assert } = require("chai"); + +const OwnedUpgradeabilityProxy = artifacts.require("OwnedUpgradeabilityProxy"); +const Master = artifacts.require("Master"); +const PlotusToken = artifacts.require("MockPLOT"); +const AllMarkets = artifacts.require("MockAllMarkets"); +const CyclicMarkets = artifacts.require("MockCyclicMarkets"); +const BLOT = artifacts.require("BPLOT"); +const Referral = artifacts.require("Referral"); +const DisputeResolution = artifacts.require('DisputeResolution'); +const BigNumber = require("bignumber.js"); +const { increaseTimeTo } = require("./utils/increaseTime.js"); +const encode1 = require('./utils/encoder.js').encode1; +const encode3 = require("./utils/encoder.js").encode3; +const signAndExecuteMetaTx = require("./utils/signAndExecuteMetaTx.js").signAndExecuteMetaTx; +const BN = require('bn.js'); + +const assertRevert = require("./utils/assertRevert.js").assertRevert; +const increaseTime = require("./utils/increaseTime.js").increaseTime; +const latestTime = require("./utils/latestTime.js").latestTime; +const encode = require("./utils/encoder.js").encode; +const gvProposal = require("./utils/gvProposal.js").gvProposalWithIncentiveViaTokenHolder; +const { toHex, toWei, toChecksumAddress } = require("./utils/ethTools"); +// get etherum accounts +// swap ether with LOT +let timeNow, + marketData, + expireTme, + priceOption1, + priceOption2, + priceOption3, + option1RangeMIN, + option1RangeMAX, + option2RangeMIN, + option2RangeMAX, + option3RangeMIX, + marketStatus, + option3RangeMAX, + disputeResolution, + marketETHBalanceBeforeDispute, + marketIncentives; + +let privateKeyList = ["fb437e3e01939d9d4fef43138249f23dc1d0852e69b0b5d1647c087f869fabbd","7c85a1f1da3120c941b83d71a154199ee763307683f206b98ad92c3b4e0af13e","ecc9b35bf13bd5459350da564646d05c5664a7476fe5acdf1305440f88ed784c","f4470c3fca4dbef1b2488d016fae25978effc586a1f83cb29ac8cb6ab5bc2d50","141319b1a84827e1046e93741bf8a9a15a916d49684ab04925ac4ce4573eea23","d54b606094287758dcf19064a8d91c727346aadaa9388732e73c4315b7c606f9","49030e42ce4152e715a7ddaa10e592f8e61d00f70ef11f48546711f159d985df","b96761b1e7ebd1e8464a78a98fe52f53ce6035c32b4b2b12307a629a551ff7cf","d4786e2581571c863c7d12231c3afb6d4cef390c0ac9a24b243293721d28ea95","ed28e3d3530544f1cf2b43d1956b7bd13b63c612d963a8fb37387aa1a5e11460","05b127365cf115d4978a7997ee98f9b48f0ddc552b981c18aa2ee1b3e6df42c6","9d11dd6843f298b01b34bd7f7e4b1037489871531d14b58199b7cba1ac0841e6","f79e90fa4091de4fc2ec70f5bf67b24393285c112658e0d810e6bd711387fbb9","99f1fc0f09230ce745b6a256ba7082e6e51a2907abda3d9e735a5c8188bb4ba1","477f86cce983b9c91a36fdcd4a7ce21144a08dee9b1aafb91b9c70e57f717ce6","b03d2e6bb4a7d71c66a66ff9e9c93549cae4b593f634a4ea2a1f79f94200f5b4","9ddc0f53a81e631dcf39d5155f41ec12ed551b731efc3224f410667ba07b37dc","cf087ff9ae7c9954ad8612d071e5cdf34a6024ee1ae477217639e63a802a53dd","b64f62b94babb82cc78d3d1308631ae221552bb595202fc1d267e1c29ce7ba60","a91e24875f8a534497459e5ccb872c4438be3130d8d74b7e1104c5f94cdcf8c2","4f49f3d029eeeb3fed14d59625acd088b6b34f3b41c527afa09d29e4a7725c32","179795fd7ac7e7efcba3c36d539a1e8659fb40d77d0a3fab2c25562d99793086","4ba37d0b40b879eceaaca2802a1635f2e6d86d5c31e3ff2d2fd13e68dd2a6d3d","6b7f5dfba9cd3108f1410b56f6a84188eee23ab48a3621b209a67eea64293394","870c540da9fafde331a3316cee50c17ad76ddb9160b78b317bef2e6f6fc4bac0","470b4cccaea895d8a5820aed088357e380d66b8e7510f0a1ea9b575850160241","8a55f8942af0aec1e0df3ab328b974a7888ffd60ded48cc6862013da0f41afbc","2e51e8409f28baf93e665df2a9d646a1bf9ac8703cbf9a6766cfdefa249d5780","99ef1a23e95910287d39493d8d9d7d1f0b498286f2b1fdbc0b01495f10cf0958","6652200c53a4551efe2a7541072d817562812003f9d9ef0ec17995aa232378f8","39c6c01194df72dda97da2072335c38231ced9b39afa280452afcca901e73643","12097e411d948f77b7b6fa4656c6573481c1b4e2864c1fca9d5b296096707c45","cbe53bf1976aee6cec830a848c6ac132def1503cffde82ccfe5bd15e75cbaa72","eeab5dcfff92dbabb7e285445aba47bd5135a4a3502df59ac546847aeb5a964f","5ea8279a578027abefab9c17cef186cccf000306685e5f2ee78bdf62cae568dd","0607767d89ad9c7686dbb01b37248290b2fa7364b2bf37d86afd51b88756fe66","e4fd5f45c08b52dae40f4cdff45e8681e76b5af5761356c4caed4ca750dc65cd","145b1c82caa2a6d703108444a5cf03e9cb8c3cd3f19299582a564276dbbba734","736b22ec91ae9b4b2b15e8d8c220f6c152d4f2228f6d46c16e6a9b98b4733120","ac776cb8b40f92cdd307b16b83e18eeb1fbaa5b5d6bd992b3fda0b4d6de8524c","65ba30e2202fdf6f37da0f7cfe31dfb5308c9209885aaf4cef4d572fd14e2903","54e8389455ec2252de063e83d3ce72529d674e6d2dc2070661f01d4f76b63475","fbbbfb525dd0255ee332d51f59648265aaa20c2e9eff007765cf4d4a6940a849","8de5e418f34d04f6ea947ce31852092a24a705862e6b810ca9f83c2d5f9cda4d","ea6040989964f012fd3a92a3170891f5f155430b8bbfa4976cde8d11513b62d9","14d94547b5deca767137fbd14dae73e888f3516c742fad18b83be333b38f0b88","47f05203f6368d56158cda2e79167777fc9dcb0c671ef3aabc205a1636c26a29"]; + + +contract("Rewards-Market", async function(users) { + describe("Scenario1", async () => { + it("0.0", async () => { + masterInstance = await OwnedUpgradeabilityProxy.deployed(); + masterInstance = await Master.at(masterInstance.address); + plotusToken = await PlotusToken.deployed(); + timeNow = await latestTime(); + + bPlotInstance = await BLOT.at(await masterInstance.getLatestAddress(web3.utils.toHex("BL"))); + allMarkets = await AllMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("AM"))); + cyclicMarkets = await CyclicMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("CM"))); + referral = await Referral.deployed(); + disputeResolution = await DisputeResolution.at(await masterInstance.getLatestAddress(web3.utils.toHex("DR"))); + await increaseTime(5 * 3600); + await plotusToken.transfer(users[12],toWei(100000)); + await plotusToken.transfer(users[11],toWei(100000)); + // await plotusToken.transfer(marketIncentives.address,toWei(500)); + + let nullAddress = "0x0000000000000000000000000000"; + + await plotusToken.transfer(users[11],toWei(100)); + await plotusToken.approve(allMarkets.address,toWei(200000),{from:users[11]}); + await cyclicMarkets.setNextOptionPrice(18); + await cyclicMarkets.claimRelayerRewards(); + await cyclicMarkets.whitelistMarketCreator(users[11]); + await cyclicMarkets.createMarket(0, 0, 0,{from:users[11],gasPrice:500000}); + // await assertRevert(marketIncentives.setMasterAddress(users[0], users[0])); + await assertRevert(allMarkets.setMasterAddress(users[0], users[0])); + await assertRevert(allMarkets.setMarketStatus(6, 1)); + await assertRevert(cyclicMarkets.setReferralContract(users[0])); + var date = Date.now(); + date = Math.round(date/1000); + await assertRevert(cyclicMarkets.addInitialMarketTypesAndStart(date, users[0], users[0], {from:users[10]})); + await assertRevert(cyclicMarkets.handleFee(100, 1, users[0], users[0], {from:users[10]})); + // await marketIncentives.claimCreationReward(100,{from:users[11]}); + }); + + it("Should be able to set referral validity", async() => { + await assertRevert(referral.setReferralValidity(0)); + await referral.setReferralValidity(24 * 60*60);//24 hours + }); + + it("Scenario 1: Few user wins", async () => { + let i; + let totalDepositedPlot = toWei(1000); + let predictionVal = [0,100, 400, 210, 123, 500, 700, 200, 50, 300, 150]; + let options=[0,2,2,2,3,1,1,2,3,3,2,1]; + let daoCommissions = [0, 1.8, 6.4, 3.36, 1.968, 8, 11.2, 3.2, 0.8, 4.8, 2.4]; + const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; + await assertRevert(referral.setReferrer(ZERO_ADDRESS, ZERO_ADDRESS)); + for(i=1; i<11;i++){ + if(i>1) { + //Should not allow unauthorized address to set referrer + await assertRevert(referral.setReferrer(users[1], users[i], {from:users[i]})); + await referral.setReferrer(users[1], users[i]); + //SHould not add referrer if already set + await assertRevert(referral.setReferrer(users[1], users[i])); + } + if(i == 10) { + await cyclicMarkets.removeReferralContract(); + await assertRevert(cyclicMarkets.removeReferralContract()); + } + await plotusToken.transfer(users[i], toWei(2000)); + await plotusToken.approve(allMarkets.address, toWei(1000000), { from: users[i] }); + let functionSignature = encode3("depositAndPlacePrediction(uint,uint,address,uint64,uint256)", totalDepositedPlot, 7, plotusToken.address, predictionVal[i]*1e8, options[i]); + await cyclicMarkets.setNextOptionPrice(options[i]*9); + // let daoBalanceBefore = await plotusToken.balanceOf(marketIncentives.address); + // await allMarkets.depositAndPlacePrediction(totalDepositedPlot, 7, plotusToken.address, predictionVal[i]*1e8, options[i]); + await signAndExecuteMetaTx( + privateKeyList[i], + users[i], + functionSignature, + allMarkets, + "AM" + ); + } + + //SHould not add referrer if already placed prediction + await assertRevert(referral.setReferrer(users[1], users[2])); + let relayerBalBefore = await plotusToken.balanceOf(users[0]); + await cyclicMarkets.claimRelayerRewards(); + let relayerBalAfter = await plotusToken.balanceOf(users[0]); + + // assert.equal(Math.round((relayerBalAfter-relayerBalBefore)/1e15),11.532*1e3); + + + let betpoints = [0,5444.44444, 21777.77777, 11433.33333, 4464.44444, 54444.44444, 76222.22222, 10888.88888, 1814.81481, 10888.88888, 8166.66666, 1814.81481, 1814.81481, 1814.81481]; + + + for(i=1;i<=11;i++) + { + let betPointUser = (await allMarkets.getUserPredictionPoints(users[i],7,options[i]))/1e5; + if(i == 11) { + let betPointUser1 = (await allMarkets.getUserPredictionPoints(users[i],7,2))/1e5; + assert.equal(betPointUser1,betpoints[i+1]); + let betPointUser2 = (await allMarkets.getUserPredictionPoints(users[i],7,3))/1e5; + assert.equal(betPointUser2,betpoints[i+1]); + } + assert.equal(betPointUser,betpoints[i]); + let unusedBal = await allMarkets.getUserUnusedBalance(users[i]); + if(i != 11) + assert.equal(totalDepositedPlot/1e18-unusedBal[0]/1e18,predictionVal[i]); + } + + await cyclicMarkets.settleMarket(7,0); + await increaseTime(8*60*60); + + let daoBalanceBefore = await plotusToken.balanceOf(masterInstance.address); + await cyclicMarkets.settleMarket(7,0); + let daoFee = 5.666; + let daoBalanceAfter = await plotusToken.balanceOf(masterInstance.address); + assert.equal((daoBalanceAfter/1e18).toFixed(2), (daoBalanceBefore/1e18 + daoFee).toFixed(2)); + + + let marketCreatorReward = await cyclicMarkets.getPendingMarketCreationRewards(users[11]); + assert.equal(226640000,Math.round(marketCreatorReward/1e11)); + + // let creationReward = 14.3999; + let marketCreatoFee = 22.664; + let balanceBefore = await plotusToken.balanceOf(users[11]); + await cyclicMarkets.claimCreationReward({ from: users[11] }); + let balanceAfter = await plotusToken.balanceOf(users[11]); + assert.equal(~~(balanceAfter/1e15), ~~((balanceBefore/1e14 + marketCreatoFee*1e4)/10)); + }); + + it("Check referral fee", async () => { + let referralRewardPlot = [9.932, 0.8, 0.42, 0.246, 1, 1.4, 0.4, 0.1, 0.6, 0]; + await bPlotInstance.addMinter(referral.address); + await referral.approveToBPLOT(toWei(100000000)); + + for(i=1;i<11;i++) + { + let reward = await referral.getReferralFees(users[i], plotusToken.address); + if(i == 1) { + reward = reward[0]; + } else { + reward = reward[1]; + } + assert.equal(reward/1,referralRewardPlot[i-1]*1e8); + let plotBalBefore = await bPlotInstance.balanceOf(users[i]); + functionSignature = encode3("claimReferralFee(address,address)", users[i], plotusToken.address); + if(reward > 0) + await signAndExecuteMetaTx( + privateKeyList[i], + users[i], + functionSignature, + referral, + "RF" + ); + let plotBalAfter = await bPlotInstance.balanceOf(users[i]); + assert.equal(Math.round((plotBalAfter/1e13-plotBalBefore/1e13)),reward/1e3); + } + }); + + it("Increase time > referral validity", async() => { + await increaseTime(24*60*60); + }); + + it("Above scenario with referral validity being expired", async () => { + await cyclicMarkets.createMarket(0,0,0, {from:users[11]}); + let i; + let totalDepositedPlot = toWei(1000); + let predictionVal = [0,100, 400, 210, 123, 500, 700, 200, 50, 300, 150]; + let options=[0,2,2,2,3,1,1,2,3,3,2,1]; + const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; + for(i=1; i<11;i++){ + await plotusToken.transfer(users[i], toWei(2000)); + await plotusToken.approve(allMarkets.address, toWei(1000000), { from: users[i] }); + let functionSignature = encode3("depositAndPlacePrediction(uint,uint,address,uint64,uint256)", totalDepositedPlot, 8, plotusToken.address, predictionVal[i]*1e8, options[i]); + await cyclicMarkets.setNextOptionPrice(options[i]*9); + // let daoBalanceBefore = await plotusToken.balanceOf(marketIncentives.address); + // await allMarkets.depositAndPlacePrediction(totalDepositedPlot, 7, plotusToken.address, predictionVal[i]*1e8, options[i]); + await signAndExecuteMetaTx( + privateKeyList[i], + users[i], + functionSignature, + allMarkets, + "AM" + ); + } + + await cyclicMarkets.settleMarket(8,0); + await increaseTime(8*60*60); + + let daoBalanceBefore = await plotusToken.balanceOf(masterInstance.address); + await cyclicMarkets.settleMarket(8,0); + let daoFee = 5.666; + let daoBalanceAfter = await plotusToken.balanceOf(masterInstance.address); + assert.equal((daoBalanceAfter/1e18).toFixed(2), (daoBalanceBefore/1e18 + daoFee).toFixed(2)); + let marketCreatorReward = await cyclicMarkets.getPendingMarketCreationRewards(users[11]); + assert.equal(226640000,Math.round(marketCreatorReward/1e11)); + + // let creationReward = 14.3999; + let marketCreatoFee = 22.664; + let balanceBefore = await plotusToken.balanceOf(users[11]); + await cyclicMarkets.claimCreationReward({ from: users[11] }); + let balanceAfter = await plotusToken.balanceOf(users[11]); + assert.equal(~~(balanceAfter/1e15), ~~((balanceBefore/1e14 + marketCreatoFee*1e4)/10)); + }); + + it("Users should not get referral fee", async () => { + await referral.approveToBPLOT(toWei(100000000)); + + for(i=1;i<11;i++) + { + let reward = await referral.getReferralFees(users[i], plotusToken.address); + if(i == 1) { + reward = reward[0]; + } else { + reward = reward[1]; + } + assert.equal(reward/1,0); + let plotBalBefore = await bPlotInstance.balanceOf(users[i]); + functionSignature = encode3("claimReferralFee(address,address)", users[i], plotusToken.address); + await assertRevert(signAndExecuteMetaTx( + privateKeyList[i], + users[i], + functionSignature, + referral, + "RF" + )); + let plotBalAfter = await bPlotInstance.balanceOf(users[i]); + assert.equal(Math.round((plotBalAfter/1e13-plotBalBefore/1e13)),reward/1e3); + } + }); + }); +}); diff --git a/test/RewardPoolShare.js b/test/RewardPoolShare.js index b6ecf763..e08e0759 100644 --- a/test/RewardPoolShare.js +++ b/test/RewardPoolShare.js @@ -5,6 +5,7 @@ const Master = artifacts.require("Master"); const PlotusToken = artifacts.require("MockPLOT"); const AllMarkets = artifacts.require("AllPlotMarkets_2"); const AllMarkets_V3 = artifacts.require("AllPlotMarkets_3"); +const BLOT = artifacts.require("BPLOT"); const CyclicMarkets = artifacts.require("MockCyclicMarkets"); const CyclicMarkets_V2 = artifacts.require("MockCyclicMarkets_2"); const MockUniswapRouter = artifacts.require('MockUniswapRouter'); @@ -56,6 +57,7 @@ contract("Rewards-Market", async function(users) { plotusToken = await PlotusToken.deployed(); timeNow = await latestTime(); router = await MockUniswapRouter.deployed(); + bPlotInstance = await BLOT.at(await masterInstance.getLatestAddress(web3.utils.toHex("BL"))); allMarkets = await AllMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("AM"))); cyclicMarkets = await CyclicMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("CM"))); referral = await Referral.deployed(); @@ -282,6 +284,8 @@ contract("Rewards-Market", async function(users) { it("Check referral fee", async () => { let referralRewardPlot = [9.932, 0.8, 0.42, 0.246, 1, 1.4, 0.4, 0.1, 0.6, 0]; + await bPlotInstance.addMinter(referral.address); + await referral.approveToBPLOT(toWei(100000000)); for(i=1;i<11;i++) { @@ -292,8 +296,9 @@ contract("Rewards-Market", async function(users) { reward = reward[1]; } assert.equal(reward/1,referralRewardPlot[i-1]*1e8); - let plotBalBefore = await plotusToken.balanceOf(users[i]); + let plotBalBefore = await bPlotInstance.balanceOf(users[i]); functionSignature = encode3("claimReferralFee(address,address)", users[i], plotusToken.address); + if(reward > 0) await signAndExecuteMetaTx( privateKeyList[i], users[i], @@ -301,7 +306,7 @@ contract("Rewards-Market", async function(users) { referral, "RF" ); - let plotBalAfter = await plotusToken.balanceOf(users[i]); + let plotBalAfter = await bPlotInstance.balanceOf(users[i]); assert.equal(Math.round((plotBalAfter/1e13-plotBalBefore/1e13)),reward/1e3); } }) @@ -500,6 +505,7 @@ contract("Market", async function(users) { plotusToken = await PlotusToken.deployed(); timeNow = await latestTime(); router = await MockUniswapRouter.deployed(); + bPlotInstance = await BLOT.at(await masterInstance.getLatestAddress(web3.utils.toHex("BL"))); allMarkets = await AllMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("AM"))); cyclicMarkets = await CyclicMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("CM"))); referral = await Referral.deployed(); @@ -725,6 +731,8 @@ contract("Market", async function(users) { it("Check referral fee", async () => { let referralRewardPlot = [9.932, 0.8, 0.42, 0.246, 1, 1.4, 0.4, 0.1, 0.6, 0]; + await bPlotInstance.addMinter(referral.address); + await referral.approveToBPLOT(toWei(100000000)); for(i=1;i<11;i++) { @@ -735,8 +743,9 @@ contract("Market", async function(users) { reward = reward[1]; } assert.equal(reward/1,referralRewardPlot[i-1]*1e8); - let plotBalBefore = await plotusToken.balanceOf(users[i]); + let plotBalBefore = await bPlotInstance.balanceOf(users[i]); functionSignature = encode3("claimReferralFee(address,address)", users[i], plotusToken.address); + if(reward > 0) await signAndExecuteMetaTx( privateKeyList[i], users[i], @@ -744,7 +753,7 @@ contract("Market", async function(users) { referral, "RF" ); - let plotBalAfter = await plotusToken.balanceOf(users[i]); + let plotBalAfter = await bPlotInstance.balanceOf(users[i]); assert.equal(Math.round((plotBalAfter/1e13-plotBalBefore/1e13)),reward/1e3); } }) diff --git a/test/SwapAndPredict.js b/test/SwapAndPredict.js index db1aeff6..6e9bff37 100644 --- a/test/SwapAndPredict.js +++ b/test/SwapAndPredict.js @@ -3,6 +3,7 @@ const { assert } = require("chai"); const OwnedUpgradeabilityProxy = artifacts.require("OwnedUpgradeabilityProxy"); const Master = artifacts.require("Master"); const PlotusToken = artifacts.require("MockPLOT"); +const BLOT = artifacts.require("BPLOT"); const AllMarkets = artifacts.require("AllPlotMarkets_2"); const CyclicMarkets = artifacts.require("MockCyclicMarkets"); const MockUniswapRouter = artifacts.require('MockUniswapRouter'); @@ -53,6 +54,7 @@ contract("Rewards-Market", async function(users) { plotusToken = await PlotusToken.deployed(); timeNow = await latestTime(); router = await MockUniswapRouter.deployed(); + bPlotInstance = await BLOT.at(await masterInstance.getLatestAddress(web3.utils.toHex("BL"))); allMarkets = await AllMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("AM"))); cyclicMarkets = await CyclicMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("CM"))); referral = await Referral.deployed(); @@ -259,6 +261,8 @@ contract("Rewards-Market", async function(users) { it("Check referral fee", async () => { let referralRewardPlot = [9.932, 0.8, 0.42, 0.246, 1, 1.4, 0.4, 0.1, 0.6, 0]; + await bPlotInstance.addMinter(referral.address); + await referral.approveToBPLOT(toWei(100000000)); for(i=1;i<11;i++) { @@ -269,8 +273,9 @@ contract("Rewards-Market", async function(users) { reward = reward[1]; } assert.equal(reward/1,referralRewardPlot[i-1]*1e8); - let plotBalBefore = await plotusToken.balanceOf(users[i]); + let plotBalBefore = await bPlotInstance.balanceOf(users[i]); functionSignature = encode3("claimReferralFee(address,address)", users[i], plotusToken.address); + if(reward > 0) await signAndExecuteMetaTx( privateKeyList[i], users[i], @@ -278,7 +283,7 @@ contract("Rewards-Market", async function(users) { referral, "RF" ); - let plotBalAfter = await plotusToken.balanceOf(users[i]); + let plotBalAfter = await bPlotInstance.balanceOf(users[i]); assert.equal(Math.round((plotBalAfter/1e13-plotBalBefore/1e13)),reward/1e3); } }) diff --git a/test/SwapAndPredict_Acyclic.js b/test/SwapAndPredict_Acyclic.js index e40e2477..f3266948 100644 --- a/test/SwapAndPredict_Acyclic.js +++ b/test/SwapAndPredict_Acyclic.js @@ -57,6 +57,7 @@ contract("Rewards-Market", async function(users) { BLOTInstance = await BLOT.deployed(); timeNow = await latestTime(); + bPlotInstance = await BLOT.at(await masterInstance.getLatestAddress(web3.utils.toHex("BL"))); allMarkets = await AllMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("AM"))); acyclicMarkets = await AcyclicMarkets.at(await masterInstance.getLatestAddress(web3.utils.toHex("AC"))); referral = await Referral.deployed(); @@ -248,6 +249,8 @@ contract("Rewards-Market", async function(users) { it("Check referral fee", async () => { let referralRewardPlot = [10.532, 0.8, 0.42, 0.246, 1, 1.4, 0.4, 0.1, 0.6, 0.3]; + await bPlotInstance.addMinter(referral.address); + await referral.approveToBPLOT(toWei(100000000)); for(i=1;i<11;i++) { @@ -258,8 +261,9 @@ contract("Rewards-Market", async function(users) { reward = reward[1]; } assert.equal(reward/1,referralRewardPlot[i-1]*1e8); - let plotBalBefore = await plotusToken.balanceOf(users[i]); + let plotBalBefore = await bPlotInstance.balanceOf(users[i]); functionSignature = encode3("claimReferralFee(address,address)", users[i], plotusToken.address); + if(reward > 0) await signAndExecuteMetaTx( privateKeyList[i], users[i], @@ -267,7 +271,7 @@ contract("Rewards-Market", async function(users) { referral, "RF" ); - let plotBalAfter = await plotusToken.balanceOf(users[i]); + let plotBalAfter = await bPlotInstance.balanceOf(users[i]); assert.equal(Math.round((plotBalAfter/1e13-plotBalBefore/1e13)),reward/1e3); } })