Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ShareXKeysStaking

Git Source

Inherits: Initializable, AccessControlUpgradeable, PausableUpgradeable, ReentrancyGuardUpgradeable, IERC1155Receiver, IShareXKeysStaking

State Variables

ESSENTIA

Token type constants

uint256 public constant ESSENTIA = 0

MAGNA

uint256 public constant MAGNA = 1

NOVA

uint256 public constant NOVA = 2

PRECISION_FACTOR

Precision factor for reward calculations

uint256 public constant PRECISION_FACTOR = 1e18

shareXKeysContract

ShareX Keys Series contract for token operations

IShareXKeysSeries public shareXKeysContract

_stakes

User staking information: user => tokenId => StakeInfo

mapping(address user => mapping(uint256 tokenId => StakeInfo)) private _stakes

_pools

Pool information for each token type: tokenId => InternalPoolInfo

mapping(uint256 tokenId => InternalPoolInfo poolInfo) private _pools

__gap

Storage gap for upgrade safety

uint256[45] private __gap

Functions

validTokenId

Modifier to validate token IDs (0, 1, 2)

modifier validTokenId(uint256 tokenId) ;

validAmount

Modifier to validate non-zero amounts

modifier validAmount(uint256 amount) ;

initialize

Initialize the staking contract

Replaces constructor for upgradeable pattern

function initialize(address _shareXKeysContract, address _admin) external initializer;

Parameters

NameTypeDescription
_shareXKeysContractaddressAddress of the ShareX Keys Series ERC1155 contract
_adminaddressAddress to grant admin role to

stake

Stake tokens for rewards

function stake(uint256 tokenId, uint256 amount)
    external
    override
    nonReentrant
    whenNotPaused
    validTokenId(tokenId)
    validAmount(amount);

Parameters

NameTypeDescription
tokenIduint256Token type to stake (0=ESSENTIA, 1=MAGNA, 2=NOVA)
amountuint256Number of tokens to stake

unstake

Unstake tokens and claim rewards

function unstake(uint256 tokenId, uint256 amount)
    external
    override
    nonReentrant
    whenNotPaused
    validTokenId(tokenId)
    validAmount(amount);

Parameters

NameTypeDescription
tokenIduint256Token type to unstake (0=ESSENTIA, 1=MAGNA, 2=NOVA)
amountuint256Number of tokens to unstake

stakeAll

Stake all available tokens across all types

Gas-optimized batch operation

function stakeAll() external override nonReentrant whenNotPaused;

unstakeAll

Unstake all staked tokens across all types

Gas-optimized batch operation with reward claiming

function unstakeAll() external override nonReentrant whenNotPaused;

claimRewards

Claim accumulated rewards from all staked positions

function claimRewards() external override nonReentrant whenNotPaused;

emergencyWithdraw

Emergency withdrawal without rewards

function emergencyWithdraw(uint256 tokenId)
    external
    override
    nonReentrant
    validTokenId(tokenId);

Parameters

NameTypeDescription
tokenIduint256Token type to withdraw

burnAllTokens

Burn all tokens (staked and owned) for a specific address

Admin function to burn all tokens belonging to a user

function burnAllTokens(address account)
    external
    override
    onlyRole(DEFAULT_ADMIN_ROLE)
    nonReentrant;

Parameters

NameTypeDescription
accountaddressThe address whose tokens should be burned

updatePoolRewards

Update pool reward parameters (admin only)

function updatePoolRewards(uint256 tokenId, uint256 rewardPerBlock)
    external
    override
    onlyRole(DEFAULT_ADMIN_ROLE)
    validTokenId(tokenId);

Parameters

NameTypeDescription
tokenIduint256Token type to update
rewardPerBlockuint256New reward rate per block

pause

Pause the contract (admin only)

function pause() external onlyRole(DEFAULT_ADMIN_ROLE);

unpause

Unpause the contract (admin only)

function unpause() external onlyRole(DEFAULT_ADMIN_ROLE);

getStakingInfo

Get staking information for a user and token type

function getStakingInfo(address account, uint256 tokenId)
    external
    view
    override
    validTokenId(tokenId)
    returns (StakingInfo memory info);

Parameters

NameTypeDescription
accountaddressUser address
tokenIduint256Token type

Returns

NameTypeDescription
infoStakingInfoComplete staking information

getPoolInfo

Get pool information for a token type

function getPoolInfo(uint256 tokenId)
    external
    view
    override
    validTokenId(tokenId)
    returns (PoolInfo memory info);

Parameters

NameTypeDescription
tokenIduint256Token type

Returns

NameTypeDescription
infoPoolInfoPool configuration and statistics

getTotalPendingRewards

Get total pending rewards for a user

function getTotalPendingRewards(address account)
    external
    view
    override
    returns (uint256 totalRewards);

Parameters

NameTypeDescription
accountaddressUser address

Returns

NameTypeDescription
totalRewardsuint256Sum of pending rewards across all token types

shareXKeys

Get ShareX Keys Series contract address

function shareXKeys() external view override returns (address);

onERC1155Received

ERC1155 token receiver implementation

function onERC1155Received(address, address, uint256, uint256, bytes calldata)
    external
    pure
    override
    returns (bytes4);

onERC1155BatchReceived

ERC1155 batch token receiver implementation

function onERC1155BatchReceived(
    address,
    address,
    uint256[] calldata,
    uint256[] calldata,
    bytes calldata
) external pure override returns (bytes4);

supportsInterface

ERC165 interface support

function supportsInterface(bytes4 interfaceId)
    public
    view
    virtual
    override(AccessControlUpgradeable, IERC165)
    returns (bool);

_updatePool

Update pool accumulated rewards

function _updatePool(uint256 tokenId) internal;

Parameters

NameTypeDescription
tokenIduint256Token type to update

_updateUserRewards

Update user reward debt

function _updateUserRewards(address account, uint256 tokenId) internal;

Parameters

NameTypeDescription
accountaddressUser address
tokenIduint256Token type

_validateStakeParameters

Internal function to validate staking parameters

function _validateStakeParameters(address to, uint256 tokenId, uint256 amount) internal view;

_calculatePendingReward

Calculate pending rewards for a user (internal state-changing)

function _calculatePendingReward(address account, uint256 tokenId)
    internal
    view
    returns (uint256);

Parameters

NameTypeDescription
accountaddressUser address
tokenIduint256Token type

Returns

NameTypeDescription
<none>uint256Pending reward amount

_calculatePendingRewardView

Calculate pending rewards for view function

function _calculatePendingRewardView(address account, uint256 tokenId)
    internal
    view
    returns (uint256);

Parameters

NameTypeDescription
accountaddressUser address
tokenIduint256Token type

Returns

NameTypeDescription
<none>uint256Pending reward amount

Structs

StakeInfo

Internal struct for user staking data

Optimized storage layout for gas efficiency

struct StakeInfo {
    uint128 stakedAmount; // Current staked amount (fits in 128 bits)
    uint128 rewardDebt; // Reward debt for calculations (fits in 128 bits)
    uint64 lastStakedTime; // Last staking timestamp (fits in 64 bits)
}

InternalPoolInfo

Internal struct for pool configuration

Optimized storage layout for gas efficiency

struct InternalPoolInfo {
    uint128 rewardPerBlock; // Rewards per block (fits in 128 bits)
    uint128 accRewardPerShare; // Accumulated reward per share (fits in 128 bits)
    uint64 lastRewardBlock; // Last reward block (fits in 64 bits)
    uint128 totalStaked; // Total staked in pool (fits in 128 bits)
}