Skip to content

Commit

Permalink
chore: Reorder (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
deluca-mike committed Feb 18, 2022
1 parent 056fda6 commit 6d8a9b3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 39 deletions.
15 changes: 9 additions & 6 deletions contracts/XDEFIDistribution.sol
Expand Up @@ -14,7 +14,6 @@ contract XDEFIDistribution is IXDEFIDistribution, ERC721Enumerable {

uint256 internal constant ZERO_UINT256 = uint256(0);
uint256 internal constant ONE_UINT256 = uint256(1);
uint256 internal constant TWO_UINT256 = uint256(2);
uint256 internal constant ONE_HUNDRED_UINT256 = uint256(100);
uint256 internal constant ONE_HUNDRED_TWENTY_EIGHT_UINT256 = uint256(128);
uint256 internal constant TWO_HUNDRED_FIFTY_TWO_UINT256 = uint256(252);
Expand Down Expand Up @@ -76,6 +75,10 @@ contract XDEFIDistribution is IXDEFIDistribution, ERC721Enumerable {
baseURI = baseURI_;
}

/*************/
/* Modifiers */
/*************/

modifier onlyOwner() {
if (owner != msg.sender) revert Unauthorized();

Expand Down Expand Up @@ -503,6 +506,11 @@ contract XDEFIDistribution is IXDEFIDistribution, ERC721Enumerable {
}
}

function _getScoreFromTokenId(uint256 tokenId_) internal pure returns (uint256 score_) {
// Shift `tokenId_` right by 128 bits and take the right-most 124 bits (since the first 4 bits are the tier).
score_ = (tokenId_ >> ONE_HUNDRED_TWENTY_EIGHT_UINT256) & ONE_HUNDRED_TWENTY_FOUR_BIT_MASK;
}

function _getTier(uint256 score_) internal pure returns (uint256 tier_) {
if (score_ < TIER_2_THRESHOLD) return uint256(1);

Expand Down Expand Up @@ -531,11 +539,6 @@ contract XDEFIDistribution is IXDEFIDistribution, ERC721Enumerable {
return uint256(13);
}

function _getScoreFromTokenId(uint256 tokenId_) internal pure returns (uint256 score_) {
// Shift `tokenId_` right by 128 bits and take the right-most 124 bits (since the first 4 bits are the tier).
score_ = (tokenId_ >> ONE_HUNDRED_TWENTY_EIGHT_UINT256) & ONE_HUNDRED_TWENTY_FOUR_BIT_MASK;
}

function _lock(
uint256 amount_,
uint256 duration_,
Expand Down
86 changes: 53 additions & 33 deletions contracts/interfaces/IXDEFIDistribution.sol
Expand Up @@ -5,6 +5,22 @@ pragma solidity =0.8.10;
import { IERC721Enumerable } from "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol";

interface IXDEFIDistribution is IERC721Enumerable {
/***********/
/* Structs */
/***********/

struct Position {
uint96 units; // 240,000,000,000,000,000,000,000,000 XDEFI * 2.55x bonus (which fits in a `uint96`).
uint88 depositedXDEFI; // XDEFI cap is 240000000000000000000000000 (which fits in a `uint88`).
uint32 expiry; // block timestamps for the next 50 years (which fits in a `uint32`).
uint32 created;
uint256 pointsCorrection;
}

/**********/
/* Errors */
/**********/

error CannotUnlock();
error EmptyArray();
error IncorrectBonusMultiplier();
Expand All @@ -26,23 +42,16 @@ interface IXDEFIDistribution is IERC721Enumerable {
error TokenDoesNotExist();
error Unauthorized();

struct Position {
uint96 units; // 240,000,000,000,000,000,000,000,000 XDEFI * 2.55x bonus (which fits in a `uint96`).
uint88 depositedXDEFI; // XDEFI cap is 240000000000000000000000000 (which fits in a `uint88`).
uint32 expiry; // block timestamps for the next 50 years (which fits in a `uint32`).
uint32 created;
uint256 pointsCorrection;
}

/// @notice Emitted when owner proposed an account that can accept ownership.
event OwnershipProposed(address indexed owner, address indexed pendingOwner);

/// @notice Emitted when an account has accepted ownership.
event OwnershipAccepted(address indexed previousOwner, address indexed owner);
/**********/
/* Events */
/**********/

/// @notice Emitted when the base URI is set (or re-set).
event BaseURISet(string baseURI);

/// @notice Emitted when a new amount of XDEFI is distributed to all locked positions, by some caller.
event DistributionUpdated(address indexed caller, uint256 amount);

/// @notice Emitted when the contract is no longer allowing locking XDEFI, and is allowing all locked positions to be unlocked effective immediately.
event EmergencyModeActivated();

Expand All @@ -55,26 +64,43 @@ interface IXDEFIDistribution is IERC721Enumerable {
/// @notice Emitted when a locked position is unlocked, withdrawing some amount of XDEFI.
event LockPositionWithdrawn(uint256 indexed tokenId, address indexed owner, uint256 amount);

/// @notice Emitted when a new amount of XDEFI is distributed to all locked positions, by some caller.
event DistributionUpdated(address indexed caller, uint256 amount);
/// @notice Emitted when an account has accepted ownership.
event OwnershipAccepted(address indexed previousOwner, address indexed owner);

/// @notice Emitted when owner proposed an account that can accept ownership.
event OwnershipProposed(address indexed owner, address indexed pendingOwner);

/// @notice Emitted when some score fo a token is consumed, resulting in a new token with a lesser score.
event ScoreConsumed(uint256 indexed tokenId, uint256 amount, uint256 newTokenId);

/// @notice Emitted when unlocked tokens are merged into one.
event TokensMerged(uint256[] mergedTokenIds, uint256 resultingTokenId);

/// @notice The address of the XDEFI token.
function xdefi() external view returns (address XDEFI_);
/*************/
/* Constants */
/*************/

/// @notice The minimum units that can result from a lock of XDEFI.
function MINIMUM_UNITS() external view returns (uint256 minimumUnits_);

/*********/
/* State */
/*********/

/// @notice The base URI for NFT metadata.
function baseURI() external view returns (string memory baseURI_);

/// @notice The multiplier applied to the deposited XDEFI amount to determine the units of a position, and thus its share of future distributions.
function bonusMultiplierOf(uint256 duration_) external view returns (uint256 bonusMultiplier_);

/// @notice The amount of XDEFI that is distributable to all currently locked positions.
function distributableXDEFI() external view returns (uint256 distributableXDEFI_);

/// @notice The amount of XDEFI that was deposited by all currently locked positions.
function totalDepositedXDEFI() external view returns (uint256 totalDepositedXDEFI_);
/// @notice The contract is no longer allowing locking XDEFI, and is allowing all locked positions to be unlocked effective immediately.
function inEmergencyMode() external view returns (bool lockingDisabled_);

/// @notice The amount of locked position units (in some way, it is the denominator use to distribute new XDEFI to each unit).
function totalUnits() external view returns (uint256 totalUnits_);
/// @notice The account that can take ownership of the contract.
function pendingOwner() external view returns (address pendingOwner_);

/// @notice Returns the position details (`pointsCorrection_` is a value used in the amortized work pattern for token distribution).
function positionOf(uint256 tokenId_)
Expand All @@ -88,23 +114,17 @@ interface IXDEFIDistribution is IERC721Enumerable {
uint256 pointsCorrection_
);

/// @notice The multiplier applied to the deposited XDEFI amount to determine the units of a position, and thus its share of future distributions.
function bonusMultiplierOf(uint256 duration_) external view returns (uint256 bonusMultiplier_);

/// @notice The base URI for NFT metadata.
function baseURI() external view returns (string memory baseURI_);

/// @notice The account that can set and unset lock periods and transfer ownership of the contract.
function owner() external view returns (address owner_);

/// @notice The account that can take ownership of the contract.
function pendingOwner() external view returns (address pendingOwner_);
/// @notice The amount of XDEFI that was deposited by all currently locked positions.
function totalDepositedXDEFI() external view returns (uint256 totalDepositedXDEFI_);

/// @notice The contract is no longer allowing locking XDEFI, and is allowing all locked positions to be unlocked effective immediately.
function inEmergencyMode() external view returns (bool lockingDisabled_);
/// @notice The amount of locked position units (in some way, it is the denominator use to distribute new XDEFI to each unit).
function totalUnits() external view returns (uint256 totalUnits_);

/// @notice The minimum units that can result from a lock of XDEFI.
function MINIMUM_UNITS() external view returns (uint256 minimumUnits_);
/// @notice The address of the XDEFI token.
function xdefi() external view returns (address XDEFI_);

/*******************/
/* Admin Functions */
Expand Down

0 comments on commit 6d8a9b3

Please sign in to comment.