Imprecise token age calculation results in an incorrect nuke factor, causing users to claim the wrong amount
Matin Follow with preslavxyz, JuggerNaut63 and 8 others
Originally disclosed on Code4rena
Submitted by Matin, also found by preslavxyz, JuggerNaut63, samuraii77, the_hunter, stanchev, swapnaliss, inzinko, Udsen, nikhil840096, and Abhan
The function calculateAge() computes the age of a token ID based on the current and creation time difference. This token age is then used to determine the nuke factor to calculate the possible claim amount.
The problem here is that when calculating the age of a token, it first divides the time difference to 1 day in seconds, and then multiplies the result to the other variables.
function calculateAge(uint256 tokenId) public view returns (uint256) {
require(nftContract.ownerOf(tokenId) != address(0), 'Token does not exist');
uint256 daysOld = (block.timestamp -
nftContract.getTokenCreationTimestamp(tokenId)) /
60 /
60 /
24;
uint256 perfomanceFactor = nftContract.getTokenEntropy(tokenId) % 10;
uint256 age = (daysOld *
perfomanceFactor *
MAX_DENOMINATOR *
ageMultiplier) / 365; // add 5 digits for decimals
return age;
}
With having this pattern, users will face significant losses. Especially, in the case of small differences (less than 1 day), the daysOld variable becomes 0, making the age zero respectively.
Proof of Concept
The function below illustrates the significant discrepancies between the results:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.17;
import "forge-std/Test.sol";
contract DifferenceTest is Test {
uint256 public constant MAX_DENOMINATOR = 100000;
error InvalidTokenAmount();
function setUp() public {
}
function calculateAge_precision(
uint256 creationTimestamp,
uint256 startTime,
uint256 ageMultiplier
) public view returns (uint256) {
uint256 perfomanceFactor = 150000;
uint256 age = ((startTime - creationTimestamp) *
perfomanceFactor *
MAX_DENOMINATOR *
ageMultiplier) / (60 * 60 * 24 * 365);
return age;
}
function calculateAge_actual(
uint256 creationTimestamp,
uint256 startTime,
uint256 ageMultiplier
) public view returns (uint256) {
uint256 perfomanceFactor = 150000;
uint256 daysOld = (startTime -
creationTimestamp) /
60 /
60 /
24;
uint256 age = (daysOld *
perfomanceFactor *
MAX_DENOMINATOR *
ageMultiplier) / 365; // add 5 digits for decimals
return age;
}
function test_diffAges(
uint256 creationTimestamp,
uint256 startTime,
uint256 ageMultiplier
) public {
vm.assume(startTime > creationTimestamp);
vm.assume(ageMultiplier < MAX_DENOMINATOR);
uint actualAge = calculateAge_actual(creationTimestamp, startTime, ageMultiplier);
uint accurateAge = calculateAge_precision(creationTimestamp, startTime, ageMultiplier);
console.log("The actual age is: ", actualAge);
console.log("The accurate age is: ", accurateAge);
assertFalse(actualAge == accurateAge);
}
}
For these results we will have:
creationTimestamp = 1722511200,startTime = 1722799000,ageMultiplier = 150000,perfomanceFactor = 15620:
[PASS] test_diffAges() (gas: 8201)
Logs:
The actual age is: 1925753424657
The accurate age is: 2138240106544
Traces:
[8201] DifferenceTest::test_diffAges()
├─ [0] console::log("The actual age is: ", 1925753424657 [1.925e12]) [staticcall]
│ └─ ← [Stop]
├─ [0] console::log("The accurate age is: ", 2138240106544 [2.138e12]) [staticcall]
│ └─ ← [Stop]
├─ [0] VM::assertFalse(false) [staticcall]
│ └─ ← [Return]
└─ ← [Stop]
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 520.80µs (150.20µs CPU time)
- The calculated age is:
1925753424657 - The accurate age is:
2138240106544
This will lead to ~ 11% of errors which is a significant error here. This huge difference will also increase the nuke factor (even more) and affect the results.
Recommended Mitigation Steps
Consider multiplying the numerator variables first and then divide by the denominator:
function calculateAge(uint256 tokenId) public view returns (uint256) {
require(nftContract.ownerOf(tokenId) != address(0), 'Token does not exist');
- uint256 daysOld = (block.timestamp -
- nftContract.getTokenCreationTimestamp(tokenId)) /
- 60 /
- 60 /
- 24;
uint256 perfomanceFactor = nftContract.getTokenEntropy(tokenId) % 10;
- uint256 age = (daysOld *
+ uint256 age = (perfomanceFactor *
MAX_DENOMINATOR *
+ (block.timestamp - nftContract.getTokenCreationTimestamp(tokenId)) *
ageMultiplier) / (60 * 60 * 24 * 365); // add 5 digits for decimals
return age;
}
Assessed type
Math
TForge1 (TraitForge) acknowledged and commented:
Age multiplier is marked as an invalid value, and should not have been used as I marked several times in the public channel (I forgot to remove from codebase which is my fault). The default for age multiplier should be 0.
The difference of nuke factor % between each day is 0.006% which is not much 2.5% / 365. Although this is still a valid finding as it could be more accurate, I personally would mark as low risk.
The issue as demonstrated is valid with a Medium risk. For fairness, since it is a part of the actual code, I will keep it as it is (Medium). If there is something that I may have missed here that may change the risk level, please let me know. Will consider inputs from all parties in post-judging QA, as usual.
@Koolex - I believe this issue is invalid, the sponsor was asked several times about the
ageMultipliervalue in the public chat as this value can’t be found anywhere in the tests or deployments scripts, and was adamant that this value won’t be used.From the code implementation, it is clear that the age multiplier should increase only once whole days are passed not seconds or minutes. I believe this issue is invalid, or QA at best. Given the fact that the sponsor has mentioned that
ageMultiplierwon’t be used multiple times in the public channel it will be extremely unfair to other wardens to validate this issue.
Kalogerone (warden) commented:
I agree with the comment above and want to add some additional info as to why this issue seems like an invalid, at best QA. Quoting from the issue report:
With having this pattern, users will face significant losses. Especially, in the case of small differences (less than 1 day), the
daysOldvariable becomes0, making theagezero respectively.The
daysOldvariable is how many days old an NFT is. I don’t understand why it shouldn’t be0if an NFT is minted less than 24 hours ago. It’s not anhoursOldvariable. What matters to the protocol is how many full days old an NFT is. There is no precision loss here and the variable works as intended.Also, the issue above in the PoC is setting the
ageMultipliervariable to something different that1to make the percentage of the error seem big (ageMultiplier = 150000). With the correctageMultiplier = 1, the calculation of thedaysOldvariable is at most 23 hours and 59 minutes behind. Which is not really behind because, again, the variable is supposed to calculate the full days of the NFT’s existence. The sponsors have mentioned multiple times that theageMultipliershould be set to1for testing.
After further review, I will keep it as it is, since it is a part of the actual code; which I believe it should take priority over the public chat. I understand that this might not be favourable to some of you, but I have to do this, to keep the game fair.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity business logic bug in TraitForge. Code4rena made it public on 13 September 2024. Found in the 2024-07-traitforge audit contest (finding M-10), also found by 10 other wardens.
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.