📟Computing
Last updated
Last updated
/// @dev Compute the mine upgrade price.
/// @param nextLevel Next level of the mine.
/// @return result Price of the mine upgrade.
function _computeUpgradePrice(uint8 nextLevel)
internal
view
returns (uint256 result)
{
assembly {
/// @dev // Load the value of `baseMinePrice` from its designated storage slot into `basePrice`
let basePrice := sload(baseMinePrice.slot)
/// @dev Subtract 1 from `nextLevel` to get the adjusted level, storing it in `levelAdjusted`
let levelAdjusted := sub(nextLevel, 0x01)
/// @dev Shift left operation: calculates 2^(levelAdjusted), effectively computing 2^(nextLevel - 1), and store it in `exponent`
let exponent := shl(levelAdjusted, 0x01)
/// @dev Multiply the `basePrice` by the `exponent` to get the upgrade price at the specified level
let price := mul(basePrice, exponent)
/// @dev Assign the calculated price to the `result` variable, which will be returned from the function
result := price
}
}
/// @dev Compute the mine join price.
/// @param level Level of the mine to join.
/// @return result Price of the mine join.
function _computeJoinPrice(uint8 level)
internal
view
returns (uint256 result)
{
assembly {
/// @dev Initialize the totalJoinPrice variable to 0
let totalJoinPrice := 0x00
/// @dev Load baseMinePrice from storage
let basePrice := sload(baseMinePrice.slot)
/// @dev Load joinMineDiscountRate from storage
let discountRate := sload(joinMineDiscountRate.slot)
/// @dev Loop from 1 to the given level
for { let i := 0x01 } lt(i, add(level, 0x01)) { i := add(i, 0x01) }
{
/// @dev Calculate the price increment and add it to the totalJoinPrice
let increment := mul(basePrice, shl(sub(i, 0x01), 0x01))
totalJoinPrice := add(totalJoinPrice, increment)
}
/// @dev Calculate the discount amount
let discountAmount := div(mul(totalJoinPrice, discountRate), 0x64)
/// @dev Subtract the discount amount from the total join price to get the result
result := sub(totalJoinPrice, discountAmount)
}
}
/// @dev Compute the rewards to claim for a given mine.
/// @param mineValue Value of the mine.
/// @param mineAPR APR of the mine.
/// @param lastClaimedAt Last time the rewards were claimed.
/// @return rewardsToClaim Rewards to claim.
function _computeRewardsToClaim(
uint256 mineValue,
uint256 mineAPR,
uint256 lastClaimedAt
) internal view returns (uint256 rewardsToClaim) {
assembly {
/// @dev Calculate dailyAPR: mineAPR / 0x016D
let dailyAPR := div(mineAPR, 0x016D)
/// @dev Calculate timeElapsed: block.timestamp - lastClaimedAt
let timeElapsed := sub(timestamp(), lastClaimedAt)
/// @dev Calculate dailyRewardsRate: mineValue * dailyAPR / 0x64
let dailyRewardsRate := div(mul(mineValue, dailyAPR), 0x64)
/// @dev Calculate rewardsToClaim: dailyRewardsRate * timeElapsed / ONE_DAY
rewardsToClaim := div(mul(dailyRewardsRate, timeElapsed), ONE_DAY)
}
}
/// @dev Compute the value of a mine.
/// @param level Level of the mine to join.
/// @return result Price of the mine join.
function _computeMineValue(uint8 level)
internal
view
returns (uint256 result)
{
assembly {
/// @dev Initialize totalJoinPrice to 0
let totalJoinPrice := 0x00
/// @dev Load baseMinePrice from storage
let basePrice := sload(baseMinePrice.slot)
/// @dev Loop from 1 (0x01) to the given level, incrementing i by 1 each iteration
for { let i := 0x01 } lt(i, add(level, 0x01)) { i := add(i, 0x01) }
{
/// @dev Calculate the price increment (baseMinePrice * 2^(i - 1)) and add it to the totalJoinPrice
let increment := mul(basePrice, shl(sub(i, 0x01), 0x01))
totalJoinPrice := add(totalJoinPrice, increment)
}
/// @dev Assign the calculated totalJoinPrice to the result variable to be returned
result := totalJoinPrice
}
}