-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from gnosis/help-extend
Exposed ID helper functions even more
- Loading branch information
Showing
4 changed files
with
228 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
pragma solidity ^0.5.1; | ||
|
||
import { IERC20 } from "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; | ||
|
||
library CTHelpers { | ||
/// @dev Constructs a condition ID from an oracle, a question ID, and the outcome slot count for the question. | ||
/// @param oracle The account assigned to report the result for the prepared condition. | ||
/// @param questionId An identifier for the question to be answered by the oracle. | ||
/// @param outcomeSlotCount The number of outcome slots which should be used for this condition. Must not exceed 256. | ||
function getConditionId(address oracle, bytes32 questionId, uint outcomeSlotCount) internal pure returns (bytes32) { | ||
return keccak256(abi.encodePacked(oracle, questionId, outcomeSlotCount)); | ||
} | ||
|
||
uint constant P = 21888242871839275222246405745257275088696311157297823662689037894645226208583; | ||
uint constant B = 3; | ||
uint constant SQRT_EXP = 5472060717959818805561601436314318772174077789324455915672259473661306552146; | ||
|
||
/// @dev Constructs an outcome collection ID from a parent collection and an outcome collection. | ||
/// @param parentCollectionId Collection ID of the parent outcome collection, or bytes32(0) if there's no parent. | ||
/// @param conditionId Condition ID of the outcome collection to combine with the parent outcome collection. | ||
/// @param indexSet Index set of the outcome collection to combine with the parent outcome collection. | ||
function getCollectionId(bytes32 parentCollectionId, bytes32 conditionId, uint indexSet) internal view returns (bytes32) { | ||
uint x1 = uint(keccak256(abi.encodePacked(conditionId, indexSet))); | ||
bool odd = x1 >> 255 != 0; | ||
uint y1; | ||
uint yy; | ||
do { | ||
x1 = addmod(x1, 1, P); | ||
yy = addmod(mulmod(x1, mulmod(x1, x1, P), P), B, P); | ||
(bool success, bytes memory ret) = address(5).staticcall(abi.encode(uint(0x20), uint(0x20), uint(0x20), yy, SQRT_EXP, P)); | ||
if(success) | ||
y1 = abi.decode(ret, (uint)); | ||
} while(mulmod(y1, y1, P) != yy); | ||
if(odd && y1 % 2 == 0 || !odd && y1 % 2 == 1) | ||
y1 = P - y1; | ||
|
||
uint x2 = uint(parentCollectionId); | ||
if(x2 != 0) { | ||
odd = x2 >> 254 != 0; | ||
x2 = (x2 << 2) >> 2; | ||
yy = addmod(mulmod(x2, mulmod(x2, x2, P), P), B, P); | ||
(bool success, bytes memory ret) = address(5).staticcall(abi.encode(uint(0x20), uint(0x20), uint(0x20), yy, SQRT_EXP, P)); | ||
require(success, "can't find quadratic residue"); | ||
uint y2 = abi.decode(ret, (uint)); | ||
if(odd && y2 % 2 == 0 || !odd && y2 % 2 == 1) | ||
y2 = P - y2; | ||
require(mulmod(y2, y2, P) == yy, "invalid parent collection ID"); | ||
|
||
(success, ret) = address(6).staticcall(abi.encode(x1, y1, x2, y2)); | ||
require(success, "ecadd failed"); | ||
(x1, y1) = abi.decode(ret, (uint, uint)); | ||
} | ||
|
||
if(y1 % 2 == 1) | ||
x1 ^= 1 << 254; | ||
|
||
return bytes32(x1); | ||
} | ||
|
||
/// @dev Constructs a position ID from a collateral token and an outcome collection. These IDs are used as the ERC-1155 ID for this contract. | ||
/// @param collateralToken Collateral token which backs the position. | ||
/// @param collectionId ID of the outcome collection associated with this position. | ||
function getPositionId(IERC20 collateralToken, bytes32 collectionId) internal pure returns (uint) { | ||
return uint(keccak256(abi.encodePacked(collateralToken, collectionId))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.