Genesis.sol:onGenesisSuccess() users may lose their unclaimed agentTokens if a second launch occurs before they’ve claimed their rewards from the first
The contract overwrites a user's claimable agent token balance on each successful genesis launch instead of adding to it. If a user does not claim before a second launch, their unclaimed tokens from the first launch are lost.
Fitro Follow with dreamcoder
Originally disclosed on Code4rena
Submitted by Fitro, also found by dreamcoder
Finding description and impact
onGenesisSuccess() is used to refund virtual tokens to users and to account for the agent tokens earned by participating in a successful genesis event.
function onGenesisSuccess(
address[] calldata refundVirtualsTokenUserAddresses,
uint256[] calldata refundVirtualsTokenUserAmounts,
address[] calldata distributeAgentTokenUserAddresses,
uint256[] calldata distributeAgentTokenUserAmounts,
address creator
) external onlyRole(FACTORY_ROLE) nonReentrant whenNotCancelled whenNotFailed whenEnded returns (address) {
//code...
// Check if launch has been called before
bool isFirstLaunch = agentTokenAddress == address(0);
// Calculate required balance based on whether this is first launch
uint256 requiredVirtualsBalance = isFirstLaunch ? totalRefundAmount + reserveAmount :
totalRefundAmount;
// Check if contract has enough virtuals balance
require(
IERC20(virtualTokenAddress).balanceOf(address(this)) >= requiredVirtualsBalance,
"Insufficient Virtual Token balance"
);
// Only do launch related operations if this is first launch
if (isFirstLaunch) {
// grant allowance to agentFactoryAddress for launch
IERC20(virtualTokenAddress).approve(agentFactoryAddress, reserveAmount);
// Call initFromBondingCurve and executeBondingCurveApplication
uint256 id = IAgentFactoryV3(agentFactoryAddress).initFromBondingCurve(
string.concat(genesisName, " by Virtuals"),
genesisTicker,
genesisCores,
tbaSalt,
tbaImplementation,
daoVotingPeriod,
daoThreshold,
reserveAmount,
creator
);
address agentToken = IAgentFactoryV3(agentFactoryAddress).executeBondingCurveApplication(
id,
agentTokenTotalSupply,
agentTokenLpSupply,
address(this) // vault
);
require(agentToken != address(0), "Agent token creation failed");
// Store the created agent token address
agentTokenAddress = agentToken;
}
// Calculate total distribution amount
uint256 totalDistributionAmount = 0;
for (uint256 i = 0; i < distributeAgentTokenUserAmounts.length; i++) {
totalDistributionAmount += distributeAgentTokenUserAmounts[i];
}
// Check if contract has enough agent token balance only after agentTokenAddress be set
require(
IERC20(agentTokenAddress).balanceOf(address(this)) >= totalDistributionAmount,
"Insufficient Agent Token balance"
);
// Directly transfer Virtual Token refunds
for (uint256 i = 0; i < refundVirtualsTokenUserAddresses.length; i++) {
// first decrease the virtuals mapping of the user to prevent reentrancy attacks
mapAddrToVirtuals[refundVirtualsTokenUserAddresses[i]] -= refundVirtualsTokenUserAmounts[i];
// then transfer the virtuals
IERC20(virtualTokenAddress).safeTransfer(
refundVirtualsTokenUserAddresses[i],
refundVirtualsTokenUserAmounts[i]
);
emit RefundClaimed(genesisId, refundVirtualsTokenUserAddresses[i], refundVirtualsTokenUserAmounts[i]);
}
// save the amount of agent tokens to claim
for (uint256 i = 0; i < distributeAgentTokenUserAddresses.length; i++) {
@> claimableAgentTokens[distributeAgentTokenUserAddresses[i]] = distributeAgentTokenUserAmounts[i];
}
emit GenesisSucceeded(genesisId);
return agentTokenAddress;
}
Multiple launches can occur, which is why the isFirstLaunch flag is implemented. However, there’s an issue in how agentTokens are accounted for in the claimableAgentTokens mapping.
Currently, the mapping value is being overwritten instead of incremented. This means if a user doesn’t call claimAgentToken() to redeem their agentTokens before a subsequent launch occurs, the second launch will overwrite the previous value resulting in a loss of agentTokens from the first launch.
Recommended mitigation steps
To resolve the issue, update the logic to add the new agentTokens amount instead of overwriting it.
function onGenesisSuccess(
address[] calldata refundVirtualsTokenUserAddresses,
uint256[] calldata refundVirtualsTokenUserAmounts,
address[] calldata distributeAgentTokenUserAddresses,
uint256[] calldata distributeAgentTokenUserAmounts,
address creator
) external onlyRole(FACTORY_ROLE) nonReentrant whenNotCancelled whenNotFailed whenEnded returns (address) {
require(refundUserCountForFailed == 0, "OnGenesisFailed already called");
require(
refundVirtualsTokenUserAddresses.length == refundVirtualsTokenUserAmounts.length,
"Mismatched refund arrays"
);
require(
distributeAgentTokenUserAddresses.length == distributeAgentTokenUserAmounts.length,
"Mismatched distribution arrays"
);
// Calculate total refund amount
uint256 totalRefundAmount = 0;
for (uint256 i = 0; i < refundVirtualsTokenUserAmounts.length; i++) {
// check if the user has enough virtuals committed
require(
mapAddrToVirtuals[refundVirtualsTokenUserAddresses[i]] >= refundVirtualsTokenUserAmounts[i],
"Insufficient Virtual Token committed"
);
totalRefundAmount += refundVirtualsTokenUserAmounts[i];
}
// Check if launch has been called before
bool isFirstLaunch = agentTokenAddress == address(0);
// Calculate required balance based on whether this is first launch
uint256 requiredVirtualsBalance = isFirstLaunch ? totalRefundAmount + reserveAmount : totalRefundAmount;
// Check if contract has enough virtuals balance
require(
IERC20(virtualTokenAddress).balanceOf(address(this)) >= requiredVirtualsBalance,
"Insufficient Virtual Token balance"
);
// Only do launch related operations if this is first launch
if (isFirstLaunch) {
// grant allowance to agentFactoryAddress for launch
IERC20(virtualTokenAddress).approve(agentFactoryAddress, reserveAmount);
// Call initFromBondingCurve and executeBondingCurveApplication
uint256 id = IAgentFactoryV3(agentFactoryAddress).initFromBondingCurve(
string.concat(genesisName, " by Virtuals"),
genesisTicker,
genesisCores,
tbaSalt,
tbaImplementation,
daoVotingPeriod,
daoThreshold,
reserveAmount,
creator
);
address agentToken = IAgentFactoryV3(agentFactoryAddress).executeBondingCurveApplication(
id,
agentTokenTotalSupply,
agentTokenLpSupply,
address(this) // vault
);
require(agentToken != address(0), "Agent token creation failed");
// Store the created agent token address
agentTokenAddress = agentToken;
}
// Calculate total distribution amount
uint256 totalDistributionAmount = 0;
for (uint256 i = 0; i < distributeAgentTokenUserAmounts.length; i++) {
totalDistributionAmount += distributeAgentTokenUserAmounts[i];
}
// Check if contract has enough agent token balance only after agentTokenAddress be set
require(
IERC20(agentTokenAddress).balanceOf(address(this)) >= totalDistributionAmount,
"Insufficient Agent Token balance"
);
// Directly transfer Virtual Token refunds
for (uint256 i = 0; i < refundVirtualsTokenUserAddresses.length; i++) {
// first decrease the virtuals mapping of the user to prevent reentrancy attacks
mapAddrToVirtuals[refundVirtualsTokenUserAddresses[i]] -= refundVirtualsTokenUserAmounts[i];
// then transfer the virtuals
IERC20(virtualTokenAddress).safeTransfer(
refundVirtualsTokenUserAddresses[i],
refundVirtualsTokenUserAmounts[i]
);
emit RefundClaimed(genesisId, refundVirtualsTokenUserAddresses[i], refundVirtualsTokenUserAmounts[i]);
}
// save the amount of agent tokens to claim
for (uint256 i = 0; i < distributeAgentTokenUserAddresses.length; i++) {
- claimableAgentTokens[distributeAgentTokenUserAddresses[i]] = distributeAgentTokenUserAmounts[i];
+ claimableAgentTokens[distributeAgentTokenUserAddresses[i]] += distributeAgentTokenUserAmounts[i];
}
emit GenesisSucceeded(genesisId);
return agentTokenAddress;
}
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The assignment to claimableAgentTokens uses '=' instead of '+=' so each distribution overwrites the previous claimable amount.
What the attacker needed
Multiple successful genesis launches; user does not claim between launches
The fix
The code should add the new amount to the existing claimable balance instead of replacing it.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A smart contract keeps track of how many reward tokens each user can claim. When a new reward round happens, the contract replaces the old number with the new one instead of adding them together. So if you wait to claim, your earlier reward is erased.
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.