Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xferit committed Oct 7, 2024
1 parent 702e30d commit dbbbf02
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 107 deletions.
8 changes: 5 additions & 3 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.25 <0.9.0;

import { Foo } from "../src/Foo.sol";
import { MarketFactory } from "../src/MarketFactory.sol";

import { BaseScript } from "./Base.s.sol";

/// @dev See the Solidity Scripting tutorial: https://book.getfoundry.sh/tutorials/solidity-scripting
contract Deploy is BaseScript {
function run() public broadcast returns (Foo foo) {
foo = new Foo();
function run() public broadcast returns (MarketFactory marketFactory) {
address realityETHAddress = 0x1234567890123456789012345678901234567890; // Replace with actual RealityETH
// address
marketFactory = new MarketFactory(realityETHAddress);
}
}
8 changes: 0 additions & 8 deletions src/Foo.sol

This file was deleted.

9 changes: 3 additions & 6 deletions src/LPToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";


contract LPToken is ERC20, Ownable {
constructor(string memory name, string memory symbol) ERC20(name, symbol) Ownable(msg.sender) {

}
constructor(string memory name, string memory symbol) ERC20(name, symbol) Ownable(msg.sender) { }

function mint(address account, uint256 amount) onlyOwner external {
function mint(address account, uint256 amount) external onlyOwner {
_mint(account, amount);
}

function burn(address account, uint256 amount) onlyOwner external {
function burn(address account, uint256 amount) external onlyOwner {
_burn(account, amount);
}
}
22 changes: 13 additions & 9 deletions src/Market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,33 @@ contract Market {
Oracle public oracle;
bool public isResolved;
bool public outcome;
OutcomeToken longToken;
OutcomeToken shortToken;
OutcomeToken public longToken;
OutcomeToken public shortToken;

constructor(bytes32 _questionId, bytes32 _tokenName, Oracle _oracle) {
constructor(bytes32 _questionId, string memory _tokenName, Oracle _oracle) {
questionId = _questionId;

longToken = new OutcomeToken(string(abi.encodePacked("Long ", _tokenName)), string(abi.encodePacked("L", _tokenName)));
shortToken = new OutcomeToken(string(abi.encodePacked("Short ", _tokenName)), string(abi.encodePacked("S", _tokenName)));

longToken =
new OutcomeToken(string(abi.encodePacked("Long ", _tokenName)), string(abi.encodePacked("L", _tokenName)));
shortToken =
new OutcomeToken(string(abi.encodePacked("Short ", _tokenName)), string(abi.encodePacked("S", _tokenName)));

marketMaker = new MarketMaker(longToken, shortToken);
oracle = _oracle;
}

function split() external payable {
uint256 amount = msg.value;

longToken.mint(msg.sender, amount);
shortToken.mint(msg.sender, amount);
}

function merge(uint256 amount) external {
require(longToken.balanceOf(msg.sender) >= amount && shortToken.balanceOf(msg.sender) >= amount, "Insufficient balance");
require(
longToken.balanceOf(msg.sender) >= amount && shortToken.balanceOf(msg.sender) >= amount,
"Insufficient balance"
);

longToken.burn(msg.sender, amount);
shortToken.burn(msg.sender, amount);
Expand All @@ -46,4 +51,3 @@ contract Market {
isResolved = true;
}
}

5 changes: 2 additions & 3 deletions src/MarketFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ contract MarketFactory {
oracle = new Oracle(_realityETHAddress);
}

function createMarket(bytes32 _tokenName) external returns (Market) {
function createMarket(string memory _tokenName) external returns (Market) {
// Ask the question to Reality contract
bytes32 _questionId = oracle.askQuestionWithMinBond(
0, // template_id (0 for binary questions)
string(abi.encodePacked("Market question for ", _tokenName)),
address(0), // arbitrator (set to zero address for no arbitration)
uint32(86400), // 24 hours timeout
uint32(86_400), // 24 hours timeout
uint32(block.timestamp), // opening_ts (current block timestamp)
0, // nonce
0 // min_bond
);


require(address(markets[_questionId]) == address(0), "Market already exists");

Market newMarket = new Market(_questionId, _tokenName, oracle);
Expand Down
26 changes: 17 additions & 9 deletions src/MarketMaker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract MarketMaker {
OutcomeToken public shortToken;

LPToken public liquidityToken;
uint public totalShares;
uint256 public totalShares;

constructor(OutcomeToken _longToken, OutcomeToken _shortToken) {
longToken = _longToken;
Expand All @@ -23,7 +23,7 @@ contract MarketMaker {

function deposit(uint256 _longAmount) external {
require(longToken.allowance(msg.sender, address(this)) >= _longAmount, "Insufficient long token allowance");
uint shortAmount = shortToken.balanceOf(address(this)) * _longAmount / longToken.balanceOf(address(this));
uint256 shortAmount = shortToken.balanceOf(address(this)) * _longAmount / longToken.balanceOf(address(this));
require(shortToken.allowance(msg.sender, address(this)) >= shortAmount, "Insufficient short token allowance");

longToken.transferFrom(msg.sender, address(this), _longAmount);
Expand All @@ -32,7 +32,6 @@ contract MarketMaker {
uint256 shares;
if (longToken.balanceOf(address(this)) == 0) {
shares = _longAmount;

} else {
shares = (_longAmount / longToken.balanceOf(address(this))) * totalShares;
}
Expand All @@ -44,8 +43,8 @@ contract MarketMaker {

function withdraw(uint256 _shares) external {
require(_shares < totalShares);
uint longAmount = longToken.balanceOf(address(this)) * _shares / totalShares;
uint shortAmount = shortToken.balanceOf(address(this)) * longAmount / longToken.balanceOf(address(this));
uint256 longAmount = longToken.balanceOf(address(this)) * _shares / totalShares;
uint256 shortAmount = shortToken.balanceOf(address(this)) * longAmount / longToken.balanceOf(address(this));

liquidityToken.burn(msg.sender, _shares);
totalShares -= _shares;
Expand All @@ -54,23 +53,32 @@ contract MarketMaker {
shortToken.transfer(msg.sender, shortAmount);
}


function swap(bool buyLong, uint256 amountIn) external {
require(amountIn > 0, "Amount must be greater than 0");

uint256 amountOut;
if (buyLong) {
amountOut = calculateSwapAmount(shortToken.balanceOf(address(this)), longToken.balanceOf(address(this)), amountIn);
amountOut =
calculateSwapAmount(shortToken.balanceOf(address(this)), longToken.balanceOf(address(this)), amountIn);
shortToken.transferFrom(msg.sender, address(this), amountIn);
longToken.transfer(msg.sender, amountOut);
} else {
amountOut = calculateSwapAmount(longToken.balanceOf(address(this)), shortToken.balanceOf(address(this)), amountIn);
amountOut =
calculateSwapAmount(longToken.balanceOf(address(this)), shortToken.balanceOf(address(this)), amountIn);
longToken.transferFrom(msg.sender, address(this), amountIn);
shortToken.transfer(msg.sender, amountOut);
}
}

function calculateSwapAmount(uint256 reserveIn, uint256 reserveOut, uint256 amountIn) internal pure returns (uint256) {
function calculateSwapAmount(
uint256 reserveIn,
uint256 reserveOut,
uint256 amountIn
)
internal
pure
returns (uint256)
{
uint256 k = reserveIn * reserveOut;
uint256 amountOut = reserveOut - (k / (reserveIn + amountIn));
return amountOut;
Expand Down
20 changes: 13 additions & 7 deletions src/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ interface IRealityETHAdapter {
uint32 opening_ts,
uint256 nonce,
uint256 min_bond
) external payable returns (bytes32);
)
external
payable
returns (bytes32);

function resultForOnceSettled(bytes32 question_id) external view returns (bytes32);

Expand All @@ -21,7 +24,6 @@ interface IRealityETHAdapter {
function submitAnswer(bytes32 question_id, bytes32 answer, uint256 max_previous) external payable;

function getBestAnswer(bytes32 question_id) external view returns (bytes32);

}

contract Oracle is IRealityETHAdapter {
Expand All @@ -45,13 +47,18 @@ contract Oracle is IRealityETHAdapter {
uint32 opening_ts,
uint256 nonce,
uint256 min_bond
) external payable override returns (bytes32) {
)
external
payable
override
returns (bytes32)
{
bytes32 questionId = keccak256(abi.encodePacked(question, nonce));
require(questions[questionId] == bytes32(0), "Question already exists");

questions[questionId] = keccak256(abi.encodePacked(question));
timeouts[questionId] = timeout;

return questionId;
}

Expand All @@ -71,13 +78,12 @@ contract Oracle is IRealityETHAdapter {
function submitAnswer(bytes32 question_id, bytes32 answer, uint256 max_previous) external payable override {
require(questions[question_id] != bytes32(0), "Question does not exist");
require(!settled[question_id], "Question already settled");

answers[question_id] = answer;
settled[question_id] = true;
}

function getBestAnswer(bytes32 question_id) external view override returns (bytes32) {
return answers[question_id];
}

}
9 changes: 3 additions & 6 deletions src/OutcomeToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";


contract OutcomeToken is ERC20, Ownable {
constructor(string memory name, string memory symbol) ERC20(name, symbol) Ownable(msg.sender) {

}
constructor(string memory name, string memory symbol) ERC20(name, symbol) Ownable(msg.sender) { }

function mint(address account, uint256 amount) onlyOwner external {
function mint(address account, uint256 amount) external onlyOwner {
_mint(account, amount);
}

function burn(address account, uint256 amount) onlyOwner external {
function burn(address account, uint256 amount) external onlyOwner {
_burn(account, amount);
}
}
56 changes: 0 additions & 56 deletions test/Foo.t.sol

This file was deleted.

Loading

0 comments on commit dbbbf02

Please sign in to comment.