Skip to content

Commit

Permalink
removed invalid market check on sell price view, fixed sell()
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidityDrone committed Oct 30, 2024
1 parent 05e93aa commit ffe04ef
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/MultiOutcomePredictionMarket.sol
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ contract MultiOutcomePredictionMarket is IMultiOutcomePredictionMarket {
require(userShares.shares[optionId] >= quantity, "Not enough shares to sell");
require(!market.resolved, "Market is resolved");
require(marketId <= marketCount, "Market dosen't exists");
market.options[optionId].shares -= quantity;

uint sellReturn = calculateSellReturn(marketId, optionId, quantity);
market.options[optionId].shares -= quantity;
uint sellReturnAfterFees = (sellReturn * 90 / 100);

userVolume[msg.sender] -= sellReturnAfterFees;
Expand Down Expand Up @@ -258,7 +259,6 @@ contract MultiOutcomePredictionMarket is IMultiOutcomePredictionMarket {
* @return totalReturn Total return from the sale
*/
function calculateSellReturn(uint256 marketId, uint256 optionId, uint256 quantity) public view returns (uint256 totalReturn) {
require(marketId < marketCount, "Invalid market ID");
Market storage market = markets[marketId];
require(optionId < market.options.length, "Invalid option ID");
require(quantity > 0, "Quantity must be greater than zero");
Expand Down
2 changes: 1 addition & 1 deletion src/flattened/MutiOutcomePredictionMarket.flattened.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2279,7 +2279,7 @@ contract MultiOutcomePredictionMarket is IMultiOutcomePredictionMarket {
* @return totalReturn Total return from the sale
*/
function calculateSellReturn(uint256 marketId, uint256 optionId, uint256 quantity) public view returns (uint256 totalReturn) {
require(marketId < marketCount, "Invalid market ID");

Market storage market = markets[marketId];
require(optionId < market.options.length, "Invalid option ID");
require(quantity > 0, "Quantity must be greater than zero");
Expand Down
18 changes: 18 additions & 0 deletions test/MultiOutcomePredictionMarket.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,28 @@ contract MultiOutcomePredictionMarketTest is Test {
predictionMarket.buy(1, 0, 10);



}

function testSellOptions() public {
singleMarketCreation();

// Buy option 1
uint256 costOfNextShare = predictionMarket.calculateBuyCost(1, 0, 1);
// cost of the first share should be equal to initial price (125000);
assertEq(costOfNextShare, 125000);

deal(address(usdc), address(this), costOfNextShare);
usdc.approve(address(predictionMarket), costOfNextShare);

// Log the actual approval
uint256 actualApproval = usdc.allowance(address(this), address(predictionMarket));
// buy from market 1, option 0, quantity 1
predictionMarket.buy(1, 0, 1);

uint256 rewardForNextShare = predictionMarket.calculateSellReturn(1, 0, 1);

predictionMarket.sell(1,0,1);
}

function testOptionMarketResolutoin() public {
Expand Down

0 comments on commit ffe04ef

Please sign in to comment.