Incorrect amounts of ETH are transferred to the DAO treasury in ERC20TokenEmitter::buyToken() , causing a value leak in every transaction
The buyToken function sends only the buyer's share of ETH to the DAO treasury, while the portion reserved for creators to buy governance tokens remains stuck in the contract. This causes a value leak from the treasury on every purchase.
osmanozdemir1 Follow with shaka, SovaSlava and 17 others
Originally disclosed on Code4rena
Submitted by osmanozdemir1, also found by shaka, SovaSlava, KupiaSec, MrPotatoMagic, ast3ros, BARW, 0xDING99YA, 0xCiphky, bart1e, ktg, AS, SpicyMeatball, hakymulla, plasmablocks, Abdessamed, 0xlemon, twcctop, 0xluckhu, and n1punp
While users buying governance tokens with ERC20TokenEmitter::buyToken function, some portion of the provided ETH is reserved for creators according to the creatorRateBps.
A part of this creator’s reserved ETH is directly sent to the creators according to entropyRateBps, and the remaining part is used to buy governance tokens for creators.
That remaining part, which is used to buy governance tokens, is never sent to the DAO treasury. It is locked in the ERC20Emitter contract, causing value leaks for treasury in every buyToken function call.
function buyToken(
address[] calldata addresses,
uint[] calldata basisPointSplits,
ProtocolRewardAddresses calldata protocolRewardsRecipients
) public payable nonReentrant whenNotPaused returns (uint256 tokensSoldWad) {
// ...
// Get value left after protocol rewards
uint256 msgValueRemaining = _handleRewardsAndGetValueToSend(
msg.value,
protocolRewardsRecipients.builder,
protocolRewardsRecipients.purchaseReferral,
protocolRewardsRecipients.deployer
);
//Share of purchase amount to send to treasury
uint256 toPayTreasury = (msgValueRemaining * (10_000 - creatorRateBps)) / 10_000;
//Share of purchase amount to reserve for creators
//Ether directly sent to creators
uint256 creatorDirectPayment = ((msgValueRemaining - toPayTreasury) * entropyRateBps) / 10_000;
//Tokens to emit to creators
int totalTokensForCreators = ((msgValueRemaining - toPayTreasury) - creatorDirectPayment) > 0
? getTokenQuoteForEther((msgValueRemaining - toPayTreasury) - creatorDirectPayment)
: int(0);
// Tokens to emit to buyers
int totalTokensForBuyers = toPayTreasury > 0 ? getTokenQuoteForEther(toPayTreasury) : int(0);
//Transfer ETH to treasury and update emitted
emittedTokenWad += totalTokensForBuyers;
if (totalTokensForCreators > 0) emittedTokenWad += totalTokensForCreators;
//Deposit funds to treasury
--> (bool success, ) = treasury.call{ value: toPayTreasury }(new bytes(0)); //@audit-issue Treasury is not paid correctly. Only the buyers share is sent. Creators share to buy governance tokens are not sent to treasury
require(success, "Transfer failed."); //@audit `creators total share` - `creatorDirectPayment` should also be sent to treasury. ==> Which is "((msgValueRemaining - toPayTreasury) - creatorDirectPayment)"
//Transfer ETH to creators
if (creatorDirectPayment > 0) {
(success, ) = creatorsAddress.call{ value: creatorDirectPayment }(new bytes(0));
require(success, "Transfer failed.");
}
// ... rest of the code
}
In the code above:
toPayTreasury is the buyer’s portion of the sent ether.
(msgValueRemaining - toPayTreasury) is the creator’s portion of the sent ether.
((msgValueRemaining - toPayTreasury) - creatorDirectPayment) is the remaining part of the creator’s share after direct payment (which is used to buy the governance token).
As we can see above, the part that is used to buy governance tokens is not sent to the treasury. Only the buyer’s portion is sent.
Impact
- DAO treasury is not properly paid even though the corresponding governance tokens are minted.
- Every
buyTokentransaction will cause a value leak to the DAO treasury. The leaked ETH amounts are stuck in theERC20TokenEmittercontract.
Proof of Concept
Coded PoC
You can use the protocol’s own test suite to run this PoC.
-Copy and paste the snippet below into the ERC20TokenEmitter.t.sol test file.
-Run it with forge test --match-test testBuyToken_ValueLeak -vvv
function testBuyToken_ValueLeak() public {
// Set creator and entropy rates.
// Creator rate will be 10% and entropy rate will be 40%
uint256 creatorRate = 1000;
uint256 entropyRate = 5000;
vm.startPrank(address(dao));
erc20TokenEmitter.setCreatorRateBps(creatorRate);
erc20TokenEmitter.setEntropyRateBps(entropyRate);
// Check dao treasury and erc20TokenEmitter balances. Balance of both of them should be 0.
uint256 treasuryETHBalance_BeforePurchase = address(erc20TokenEmitter.treasury()).balance;
uint256 emitterContractETHBalance_BeforePurchase = address(erc20TokenEmitter).balance;
assertEq(treasuryETHBalance_BeforePurchase, 0);
assertEq(emitterContractETHBalance_BeforePurchase, 0);
// Create token purchase parameters
address[] memory recipients = new address[](1);
recipients[0] = address(1);
uint256[] memory bps = new uint256[](1);
bps[0] = 10_000;
// Give some ETH to user and buy governance token.
vm.startPrank(address(0));
vm.deal(address(0), 100000 ether);
erc20TokenEmitter.buyToken{ value: 100 ether }(
recipients,
bps,
IERC20TokenEmitter.ProtocolRewardAddresses({
builder: address(0),
purchaseReferral: address(0),
deployer: address(0)
})
);
// User bought 100 ether worth of tokens.
// Normally with 2.5% fixed protocol rewards, 10% creator share and 50% entropy share:
// -> 2.5 ether is protocol rewards.
// -> 87.75 ether is buyer share (90% of the 97.5)
// -> 9.75 of the ether is creators share
// - 4.875 ether directly sent to creators
// - 4.875 ether should be used to buy governance token and should be sent to the treasury.
// However, the 4.875 ether is never sent to the treasury even though it is used to buy governance tokens. It is stuck in the Emitter contract.
// Check balances after purchase.
uint256 treasuryETHBalance_AfterPurchase = address(erc20TokenEmitter.treasury()).balance;
uint256 emitterContractETHBalance_AfterPurchase = address(erc20TokenEmitter).balance;
uint256 creatorETHBalance_AfterPurchase = address(erc20TokenEmitter.creatorsAddress()).balance;
// Creator direct payment amount is 4.875 as expected
assertEq(creatorETHBalance_AfterPurchase, 4.875 ether);
// Dao treasury has 87.75 ether instead of 92.625 ether.
// 4.875 ether that is used to buy governance tokens for creators is never sent to treasury and still in the emitter contract.
assertEq(treasuryETHBalance_AfterPurchase, 87.75 ether);
assertEq(emitterContractETHBalance_AfterPurchase, 4.875 ether);
}
Results after running the test:
Running 1 test for test/token-emitter/ERC20TokenEmitter.t.sol:ERC20TokenEmitterTest
[PASS] testBuyToken_ValueLeak() (gas: 459490)
Test result: ok. 1 passed; 0 failed; 0 skipped; finished in 11.25ms
Ran 1 test suites: 1 tests passed, 0 failed, 0 skipped (1 total tests)
Tools Used
Foundry
Recommended Mitigation Steps
I would recommend transferring the remaining ETH used to buy governance tokens to the treasury.
+ uint256 creatorsEthAfterDirectPayment = ((msgValueRemaining - toPayTreasury) - creatorDirectPayment);
//Deposit funds to treasury
- (bool success, ) = treasury.call{ value: toPayTreasury }(new bytes(0));
+ (bool success, ) = treasury.call{ value: toPayTreasury + creatorsEthAfterDirectPayment }(new bytes(0));
require(success, "Transfer failed.");
rocketman-21 (Revolution) confirmed
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 code transfers only toPayTreasury to the treasury, omitting the creator share used to buy governance tokens.
Impact
Every buyToken transaction causes a value leak to the DAO treasury, with leaked ETH stuck in the contract.
What the attacker needed
None
The fix
The remaining ETH used to buy governance tokens should be transferred to the treasury.
How it was found
- Set creator and entropy rates.
- Check initial balances of treasury and emitter contract.
- Create token purchase parameters.
- Give ETH to a user and call buyToken.
- Check balances after purchase to see the discrepancy.
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.
When someone buys tokens, the money they pay is split: some goes to the DAO treasury, some to creators directly, and some is meant to buy tokens for creators. The bug is that the last part is not sent to the treasury, so it gets stuck in the contract and the treasury loses out.
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.