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

Fix Allowances #24

Merged
merged 20 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
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
2 changes: 1 addition & 1 deletion .forge-snapshots/ConditionalTokens_mergePositions.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
52410
67211
2 changes: 1 addition & 1 deletion .forge-snapshots/ConditionalTokens_splitPosition.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
99336
114137
2 changes: 1 addition & 1 deletion .forge-snapshots/NegRiskAdapter_convertPositions_32.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4626380
4522762
2 changes: 1 addition & 1 deletion .forge-snapshots/NegRiskAdapter_convertPositions_5.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
687233
717003
2 changes: 1 addition & 1 deletion .forge-snapshots/NegRiskAdapter_convertPositions_64.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9540525
9331964
2 changes: 1 addition & 1 deletion .forge-snapshots/NegRiskAdapter_mergePositions.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
191072
220694
2 changes: 1 addition & 1 deletion .forge-snapshots/NegRiskAdapter_prepareMarket.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
33098
33110
2 changes: 1 addition & 1 deletion .forge-snapshots/NegRiskAdapter_prepareQuestion.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
37479
37634
2 changes: 1 addition & 1 deletion .forge-snapshots/NegRiskAdapter_splitPosition.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
215878
245589
2 changes: 1 addition & 1 deletion .forge-snapshots/Vault_transferERC1155.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32809
34921
2 changes: 1 addition & 1 deletion .forge-snapshots/Vault_transferERC20.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
31009
33121
2 changes: 1 addition & 1 deletion .forge-snapshots/WrappedCollateral_unwrap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
28664
30764
2 changes: 1 addition & 1 deletion .forge-snapshots/WrappedCollateral_wrap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
75728
77828
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ docs/

# python virtualenv
env/

node_modules/
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@
[submodule "lib/forge-gas-snapshot"]
path = lib/forge-gas-snapshot
url = https://github.com/marktoda/forge-gas-snapshot
[submodule "lib/ctf-exchange"]
path = lib/ctf-exchange
url = https://github.com/Polymarket/ctf-exchange
[submodule "lib/exchange-fee-module"]
path = lib/exchange-fee-module
url = https://github.com/Polymarket/exchange-fee-module
6 changes: 3 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"solidity.packageDefaultDependenciesDirectory": "lib",
"solidity.compileUsingRemoteVersion": "v0.8.19+commit.7dd6d404",
"editor.formatOnSave": true,
"[solidity]": {
"editor.defaultFormatter": "JuanBlanco.solidity"
},
// "[solidity]": {
// "editor.defaultFormatter": "JuanBlanco.solidity"
// },
"solidity.formatter": "forge"
}
3 changes: 1 addition & 2 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
[profile.default]
solc = "0.8.19"
src = "src"
out = "out"
libs = ["lib"]

ffi = true
fs_permissions = [{ access = "read-write", path = ".forge-snapshots/"},{ access = "read", path = "./artifacts/"}]
fs_permissions = [{ access = "read-write", path = ".forge-snapshots/"},{ access = "read", path = "./artifacts/"}, { access = "read", path = "./out/"}]

[fmt]
line_length = 120
Expand Down
1 change: 1 addition & 0 deletions lib/ctf-exchange
Submodule ctf-exchange added at 2745c3
1 change: 1 addition & 0 deletions lib/exchange-fee-module
Submodule exchange-fee-module added at 9abb8c
2 changes: 2 additions & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ ds-test/=lib/forge-std/lib/ds-test/src/
forge-std/=lib/forge-std/src/
solmate/=lib/solmate/src/
forge-gas-snapshot/=lib/forge-gas-snapshot/src/

openzeppelin-contracts/=lib/ctf-exchange/lib/openzeppelin-contracts/contracts/
50 changes: 46 additions & 4 deletions src/NegRiskAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ pragma solidity 0.8.19;
import {ERC1155TokenReceiver} from "lib/solmate/src/tokens/ERC1155.sol";
import {ERC20} from "lib/solmate/src/tokens/ERC20.sol";
import {SafeTransferLib} from "lib/solmate/src/utils/SafeTransferLib.sol";

import {WrappedCollateral} from "src/WrappedCollateral.sol";
import {MarketData, MarketStateManager, IMarketStateManagerEE} from "src/modules/MarketDataManager.sol";
import {CTHelpers} from "src/libraries/CTHelpers.sol";
import {Helpers} from "src/libraries/Helpers.sol";
import {NegRiskIdLib} from "src/libraries/NegRiskIdLib.sol";
import {IConditionalTokens} from "src/interfaces/IConditionalTokens.sol";
import {Auth} from "src/modules/Auth.sol";
import {IAuthEE} from "src/modules/interfaces/IAuth.sol";

/// @title INegRiskAdapterEE
/// @notice NegRiskAdapter Errors and Events
interface INegRiskAdapterEE is IMarketStateManagerEE {
interface INegRiskAdapterEE is IMarketStateManagerEE, IAuthEE {
error InvalidIndexSet();
error LengthMismatch();
error UnexpectedCollateralToken();
error NoConvertiblePositions();
error NotApprovedForAll();

event MarketPrepared(bytes32 indexed marketId, address indexed oracle, uint256 feeBips, bytes data);
event QuestionPrepared(bytes32 indexed marketId, bytes32 indexed questionId, uint256 index, bytes data);
Expand All @@ -37,7 +39,7 @@ interface INegRiskAdapterEE is IMarketStateManagerEE {
/// @notice And the adapter allows for the conversion of a set of no positions, to collateral plus the set of
/// complementary yes positions
/// @author Mike Shrieve ([email protected])
contract NegRiskAdapter is ERC1155TokenReceiver, MarketStateManager, INegRiskAdapterEE {
contract NegRiskAdapter is ERC1155TokenReceiver, MarketStateManager, INegRiskAdapterEE, Auth {
using SafeTransferLib for ERC20;

/*//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -164,6 +166,46 @@ contract NegRiskAdapter is ERC1155TokenReceiver, MarketStateManager, INegRiskAda
emit PositionsMerge(msg.sender, _conditionId, _amount);
}

/*//////////////////////////////////////////////////////////////
ERC1155 OPERATIONS
//////////////////////////////////////////////////////////////*/

/// @notice Proxies ERC1155 balanceOf to the CTF
/// @param _owner - the owner of the tokens
/// @param _id - the positionId
/// @return balance - the owner's balance
function balanceOf(address _owner, uint256 _id) external view returns (uint256) {
return ctf.balanceOf(_owner, _id);
}

/// @notice Proxies ERC1155 balanceOfBatch to the CTF
/// @param _owners - the owners of the tokens
/// @param _ids - the positionIds
/// @return balances - the owners' balances
function balanceOfBatch(address[] memory _owners, uint256[] memory _ids) external view returns (uint256[] memory) {
return ctf.balanceOfBatch(_owners, _ids);
}

/// @notice Proxies ERC1155 safeTransferFrom to the CTF
/// @notice Can only be called by an admin
/// @notice Requires this contract to be approved for all
/// @notice Requires the sender to be approved for all
/// @param _from - the owner of the tokens
/// @param _to - the recipient of the tokens
/// @param _id - the positionId
/// @param _value - the amount of tokens to transfer
/// @param _data - the data to pass to the recipient
function safeTransferFrom(address _from, address _to, uint256 _id, uint256 _value, bytes calldata _data)
external
onlyAdmin
{
if (!ctf.isApprovedForAll(_from, msg.sender)) {
revert NotApprovedForAll();
}

return ctf.safeTransferFrom(_from, _to, _id, _value, _data);
}

/*//////////////////////////////////////////////////////////////
REDEEM POSITION
//////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -373,7 +415,7 @@ contract NegRiskAdapter is ERC1155TokenReceiver, MarketStateManager, INegRiskAda
INTERNAL
//////////////////////////////////////////////////////////////*/

/// @dev internal function to avoid stack to deep in convertPositions
/// @dev internal function to avoid stack too deep in convertPositions
function _splitPosition(bytes32 _conditionId, uint256 _amount) internal {
ctf.splitPosition(address(wcol), bytes32(0), _conditionId, Helpers.partition(), _amount);
}
Expand Down
14 changes: 14 additions & 0 deletions src/NegRiskCtfExchange.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;

import {CTFExchange} from "../lib/ctf-exchange/src/exchange/CTFExchange.sol";
import {IConditionalTokens} from "./interfaces/IConditionalTokens.sol";

contract NegRiskCtfExchange is CTFExchange {
constructor(address _collateral, address _ctf, address _negRiskAdapter, address _proxyFactory, address _safeFactory)
CTFExchange(_collateral, _negRiskAdapter, _proxyFactory, _safeFactory)
{
IConditionalTokens(_ctf).setApprovalForAll(_negRiskAdapter, true);
IConditionalTokens(_ctf).setApprovalForAll(address(this), true);
L-Kov marked this conversation as resolved.
Show resolved Hide resolved
}
}
13 changes: 13 additions & 0 deletions src/NegRiskFeeModule.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;

import {FeeModule, IExchange} from "../lib/exchange-fee-module/src/FeeModule.sol";
import {IConditionalTokens} from "./interfaces/IConditionalTokens.sol";

contract NegRiskFeeModule is FeeModule {
constructor(address _negRiskCtfExchange, address _negRiskAdapter, address _ctf) FeeModule(_negRiskCtfExchange) {
IConditionalTokens(_ctf).setApprovalForAll(_negRiskAdapter, true);
L-Kov marked this conversation as resolved.
Show resolved Hide resolved
IConditionalTokens(_ctf).setApprovalForAll(_negRiskCtfExchange, true);
IConditionalTokens(_ctf).setApprovalForAll(address(this), true);
}
}
2 changes: 1 addition & 1 deletion src/NegRiskOperator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ contract NegRiskOperator is INegRiskOperatorEE, Auth {

NegRiskAdapter public immutable nrAdapter;
address public oracle;
uint256 public constant DELAY_PERIOD = 12 hours;
uint256 public constant DELAY_PERIOD = 1 hours;

mapping(bytes32 _requestId => bytes32) public questionIds;
mapping(bytes32 _questionId => bool) public results;
Expand Down
55 changes: 55 additions & 0 deletions src/dev/OrderHelper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;

import {Script} from "../../lib/forge-std/src/Script.sol";
import {stdStorage, StdStorage} from "../../lib/forge-std/src/StdStorage.sol";
import {Side, SignatureType} from "../../lib/ctf-exchange/src/exchange/libraries/OrderStructs.sol";

import {vm} from "./libraries/Vm.sol";
import {ICTFExchange} from "../interfaces/index.sol";

using stdStorage for StdStorage;

contract OrderHelper is Script {
function _createAndSignOrder(
address _exchange,
uint256 _pk,
uint256 _tokenId,
uint256 _makerAmount,
uint256 _takerAmount,
Side _side
) internal view returns (ICTFExchange.Order memory) {
address maker = vm.addr(_pk);
ICTFExchange.Order memory order = _createOrder(maker, _tokenId, _makerAmount, _takerAmount, _side);
order.signature = _signMessage(_pk, ICTFExchange(_exchange).hashOrder(order));
return order;
}

function _signMessage(uint256 _pk, bytes32 _message) internal pure returns (bytes memory) {
(uint8 v, bytes32 r, bytes32 s) = vm.sign(_pk, _message);
return abi.encodePacked(r, s, v);
}

function _createOrder(address _maker, uint256 _tokenId, uint256 _makerAmount, uint256 _takerAmount, Side _side)
internal
pure
returns (ICTFExchange.Order memory)
{
ICTFExchange.Order memory order = ICTFExchange.Order({
salt: 1,
signer: _maker,
maker: _maker,
taker: address(0),
tokenId: _tokenId,
makerAmount: _makerAmount,
takerAmount: _takerAmount,
expiration: 0,
nonce: 0,
feeRateBps: 0,
signatureType: uint8(SignatureType.EOA),
side: uint8(_side),
signature: new bytes(0)
});
return order;
}
}
35 changes: 35 additions & 0 deletions src/dev/StorageHelper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;

import {Script} from "../../lib/forge-std/src/Script.sol";
import {stdStorage, StdStorage} from "../../lib/forge-std/src/StdStorage.sol";
import {vm} from "./libraries/Vm.sol";

using stdStorage for StdStorage;

contract StorageHelper is Script {
function getStorageSlot(address _target, string memory _sig, address _param) public returns (uint256) {
return stdstore.target(_target).sig(_sig).with_key(_param).find();
}

function _dealERC1155(address _erc1155, address _account, uint256 _id, uint256 _amount) internal {
stdstore.target(_erc1155).sig("balanceOf(address,uint256)").with_key(_account).with_key(_id).checked_write(
_amount
);
}

function _dealERC20(address _erc20, address _account, uint256 _amount) internal {
uint256 storageSlot = getStorageSlot(_erc20, "balanceOf(address)", address(_account));
vm.store(_erc20, bytes32(storageSlot), bytes32(_amount));
}

function _setOperator(address _exchange, address _operator) internal {
uint256 storageSlot = getStorageSlot(_exchange, "operators(address)", _operator);
vm.store(_exchange, bytes32(storageSlot), bytes32(uint256(1)));
}

function _setAdmin(address _exchange, address _admin) internal {
uint256 storageSlot = getStorageSlot(_exchange, "admins(address)", _admin);
vm.store(_exchange, bytes32(storageSlot), bytes32(uint256(1)));
}
}
31 changes: 9 additions & 22 deletions src/dev/TestHelper.sol
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;

import {
Test,
console2 as console,
stdJson,
stdStorage,
StdStorage,
stdError
} from "lib/forge-std/src/Test.sol";
import {Test, console2 as console, stdJson, stdStorage, StdStorage, stdError} from "lib/forge-std/src/Test.sol";

abstract contract TestHelper is Test {
using stdJson for string;

address public immutable alice;
address public immutable brian;
address public immutable carly;
address public immutable devin;
address public alice;
address public brian;
address public carly;
address public devin;

constructor() {
alice = _getAndLabelAddress("alice");
brian = _getAndLabelAddress("brian");
carly = _getAndLabelAddress("carly");
devin = _getAndLabelAddress("devin");
}

function _getAndLabelAddress(string memory _name) internal returns (address) {
address addr = address(bytes20(keccak256(abi.encode(_name))));
vm.label(addr, _name);
return addr;
alice = vm.createWallet("alice").addr;
brian = vm.createWallet("brian").addr;
carly = vm.createWallet("carly").addr;
devin = vm.createWallet("devin").addr;
}
}
2 changes: 1 addition & 1 deletion src/dev/libraries/AddressLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {stdJson} from "forge-std/Test.sol";
library AddressLib {
using stdJson for string;

function getAddress(string memory _name) internal returns (address) {
function getAddress(string memory _name) internal view returns (address) {
string memory json = vm.readFile("./addresses.json");

uint256 chainId = block.chainid;
Expand Down
Loading