-
Notifications
You must be signed in to change notification settings - Fork 152
QSwap audit fixes #713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
baoLuck
wants to merge
2
commits into
qubic:develop
Choose a base branch
from
baoLuck:feature/2026-01-06-QSwap-audit-fixes
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
QSwap audit fixes #713
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -629,7 +629,7 @@ struct QSWAP : public ContractBase | |
| } | ||
| } | ||
|
|
||
| // no available solt for new pool | ||
| // no available slot for new pool | ||
| if (locals.poolSlot == -1) | ||
| { | ||
| return; | ||
|
|
@@ -687,7 +687,7 @@ struct QSWAP : public ContractBase | |
| } | ||
| } | ||
|
|
||
| // no available solt for new pool | ||
| // no available slot for new pool | ||
| if (locals.poolSlot == -1) | ||
| { | ||
| return; | ||
|
|
@@ -750,7 +750,7 @@ struct QSWAP : public ContractBase | |
| } | ||
| } | ||
|
|
||
| // no available solt for new pool | ||
| // no available slot for new pool | ||
| if (locals.poolSlot == -1) | ||
| { | ||
| return; | ||
|
|
@@ -819,7 +819,7 @@ struct QSWAP : public ContractBase | |
| } | ||
| } | ||
|
|
||
| // no available solt for new pool | ||
| // no available slot for new pool | ||
| if (locals.poolSlot == -1) | ||
| { | ||
| return; | ||
|
|
@@ -983,7 +983,7 @@ struct QSWAP : public ContractBase | |
| } | ||
| } | ||
|
|
||
| // no available solt for new pool | ||
| // no available slot for new pool | ||
| if (locals.poolSlot == -1) | ||
| { | ||
| qpi.transfer(qpi.invocator(), qpi.invocationReward()); | ||
|
|
@@ -1498,6 +1498,8 @@ struct QSWAP : public ContractBase | |
|
|
||
| uint128 feeToQx; | ||
| uint128 feeToBurn; | ||
|
|
||
| sint64 totalFee; | ||
| }; | ||
|
|
||
| // given an input qu amountIn, only execute swap in case (amountOut >= amountOutMin) | ||
|
|
@@ -1612,7 +1614,13 @@ struct QSWAP : public ContractBase | |
| state.investRewardsEarnedFee += locals.feeToInvestRewards.low; | ||
| state.burnEarnedFee += locals.feeToBurn.low; | ||
|
|
||
| locals.poolBasicState.reservedQuAmount += locals.quAmountIn - sint64(locals.feeToShareholders.low) - sint64(locals.feeToQx.low) - sint64(locals.feeToInvestRewards.low) - sint64(locals.feeToBurn.low); | ||
| locals.totalFee = sint64(locals.feeToShareholders.low) + sint64(locals.feeToQx.low) + sint64(locals.feeToInvestRewards.low) + sint64(locals.feeToBurn.low); | ||
| if (locals.quAmountIn < locals.totalFee) | ||
| { | ||
| qpi.transfer(qpi.invocator(), qpi.invocationReward()); | ||
| return; | ||
| } | ||
| locals.poolBasicState.reservedQuAmount += locals.quAmountIn - locals.totalFee; | ||
| locals.poolBasicState.reservedAssetAmount -= locals.assetAmountOut; | ||
| state.mPoolBasicStates.set(locals.poolSlot, locals.poolBasicState); | ||
|
|
||
|
|
@@ -1642,6 +1650,8 @@ struct QSWAP : public ContractBase | |
| uint128 feeToShareholders; | ||
| uint128 feeToQx; | ||
| uint128 feeToBurn; | ||
|
|
||
| sint64 totalFee; | ||
| }; | ||
|
|
||
| // https://docs.uniswap.org/contracts/v2/reference/smart-contracts/router-02#swaptokensforexacttokens | ||
|
|
@@ -1708,21 +1718,7 @@ struct QSWAP : public ContractBase | |
| ); | ||
|
|
||
| // above call overflow | ||
| if (locals.quAmountIn == -1) | ||
| { | ||
| qpi.transfer(qpi.invocator(), qpi.invocationReward()); | ||
| return; | ||
| } | ||
|
|
||
| // not enough qu amountIn | ||
| if (locals.quAmountIn > qpi.invocationReward()) | ||
| { | ||
| qpi.transfer(qpi.invocator(), qpi.invocationReward()); | ||
| return; | ||
| } | ||
|
|
||
| // not meet user's amountIn limit | ||
| if (locals.quAmountIn > qpi.invocationReward()) | ||
| if (locals.quAmountIn == -1 || locals.quAmountIn > qpi.invocationReward()) | ||
| { | ||
| qpi.transfer(qpi.invocator(), qpi.invocationReward()); | ||
| return; | ||
|
|
@@ -1773,7 +1769,13 @@ struct QSWAP : public ContractBase | |
| state.investRewardsEarnedFee += locals.feeToInvestRewards.low; | ||
| state.burnEarnedFee += locals.feeToBurn.low; | ||
|
|
||
| locals.poolBasicState.reservedQuAmount += locals.quAmountIn - sint64(locals.feeToShareholders.low) - sint64(locals.feeToQx.low) - sint64(locals.feeToInvestRewards.low) - sint64(locals.feeToBurn.low); | ||
| locals.totalFee = sint64(locals.feeToShareholders.low) + sint64(locals.feeToQx.low) + sint64(locals.feeToInvestRewards.low) + sint64(locals.feeToBurn.low); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, move this to before transfer |
||
| if (locals.quAmountIn < locals.totalFee) | ||
| { | ||
| qpi.transfer(qpi.invocator(), locals.quAmountIn); | ||
| return; | ||
| } | ||
| locals.poolBasicState.reservedQuAmount += locals.quAmountIn - locals.totalFee; | ||
| locals.poolBasicState.reservedAssetAmount -= input.assetAmountOut; | ||
| state.mPoolBasicStates.set(locals.poolSlot, locals.poolBasicState); | ||
|
|
||
|
|
@@ -1805,6 +1807,8 @@ struct QSWAP : public ContractBase | |
| uint128 feeToShareholders; | ||
| uint128 feeToQx; | ||
| uint128 feeToBurn; | ||
|
|
||
| sint64 totalFee; | ||
| }; | ||
|
|
||
| // given an amount of asset swap in, only execute swaping if quAmountOut >= input.amountOutMin | ||
|
|
@@ -1960,11 +1964,15 @@ struct QSWAP : public ContractBase | |
|
|
||
| // update pool states | ||
| locals.poolBasicState.reservedAssetAmount += input.assetAmountIn; | ||
| locals.poolBasicState.reservedQuAmount -= locals.quAmountOut; | ||
| locals.poolBasicState.reservedQuAmount -= sint64(locals.feeToShareholders.low); | ||
| locals.poolBasicState.reservedQuAmount -= sint64(locals.feeToQx.low); | ||
| locals.poolBasicState.reservedQuAmount -= sint64(locals.feeToInvestRewards.low); | ||
| locals.poolBasicState.reservedQuAmount -= sint64(locals.feeToBurn.low); | ||
| locals.totalFee = locals.quAmountOut + sint64(locals.feeToShareholders.low) + sint64(locals.feeToQx.low) + sint64(locals.feeToInvestRewards.low) + sint64(locals.feeToBurn.low); | ||
| if (locals.poolBasicState.reservedQuAmount < locals.totalFee) | ||
| { | ||
| locals.poolBasicState.reservedQuAmount = 0; | ||
| } | ||
| else | ||
| { | ||
| locals.poolBasicState.reservedQuAmount -= locals.totalFee; | ||
| } | ||
| state.mPoolBasicStates.set(locals.poolSlot, locals.poolBasicState); | ||
|
|
||
| // Log SwapExactAssetForQu procedure | ||
|
|
@@ -1994,6 +2002,8 @@ struct QSWAP : public ContractBase | |
| uint128 feeToShareholders; | ||
| uint128 feeToQx; | ||
| uint128 feeToBurn; | ||
|
|
||
| sint64 totalFee; | ||
| }; | ||
|
|
||
| PUBLIC_PROCEDURE_WITH_LOCALS(SwapAssetForExactQu) | ||
|
|
@@ -2145,11 +2155,15 @@ struct QSWAP : public ContractBase | |
|
|
||
| // update pool states | ||
| locals.poolBasicState.reservedAssetAmount += locals.assetAmountIn; | ||
| locals.poolBasicState.reservedQuAmount -= input.quAmountOut; | ||
| locals.poolBasicState.reservedQuAmount -= sint64(locals.feeToShareholders.low); | ||
| locals.poolBasicState.reservedQuAmount -= sint64(locals.feeToQx.low); | ||
| locals.poolBasicState.reservedQuAmount -= sint64(locals.feeToInvestRewards.low); | ||
| locals.poolBasicState.reservedQuAmount -= sint64(locals.feeToBurn.low); | ||
| locals.totalFee = input.quAmountOut + sint64(locals.feeToShareholders.low) + sint64(locals.feeToQx.low) + sint64(locals.feeToInvestRewards.low) + sint64(locals.feeToBurn.low); | ||
| if (locals.poolBasicState.reservedQuAmount < locals.totalFee) | ||
| { | ||
| locals.poolBasicState.reservedQuAmount = 0; | ||
| } | ||
| else | ||
| { | ||
| locals.poolBasicState.reservedQuAmount -= locals.totalFee; | ||
| } | ||
| state.mPoolBasicStates.set(locals.poolSlot, locals.poolBasicState); | ||
|
|
||
| // Log SwapAssetForExactQu procedure | ||
|
|
@@ -2241,44 +2255,84 @@ struct QSWAP : public ContractBase | |
| output.success = true; | ||
| } | ||
|
|
||
| PUBLIC_PROCEDURE(TransferShareManagementRights) | ||
| struct TransferShareManagementRights_locals | ||
| { | ||
| if (qpi.invocationReward() < QSWAP_FEE_BASE_100) | ||
| { | ||
| return ; | ||
| } | ||
| sint64 result; | ||
| sint64 reward; | ||
| sint64 refundAmount; | ||
| sint64 requiredFee; | ||
| bit success; | ||
| }; | ||
|
|
||
| if (qpi.numberOfPossessedShares(input.asset.assetName, input.asset.issuer,qpi.invocator(), qpi.invocator(), SELF_INDEX, SELF_INDEX) < input.numberOfShares) | ||
| { | ||
| // not enough shares available | ||
| output.transferredNumberOfShares = 0; | ||
| if (qpi.invocationReward() > 0) | ||
| { | ||
| qpi.transfer(qpi.invocator(), qpi.invocationReward()); | ||
| } | ||
| } | ||
| else | ||
| PUBLIC_PROCEDURE_WITH_LOCALS(TransferShareManagementRights) | ||
| { | ||
| locals.reward = qpi.invocationReward(); | ||
| locals.refundAmount = locals.reward; | ||
|
|
||
| output.transferredNumberOfShares = 0; | ||
|
|
||
| locals.success = false; | ||
|
|
||
| if (qpi.numberOfPossessedShares( | ||
| input.asset.assetName, | ||
| input.asset.issuer, | ||
| qpi.invocator(), | ||
| qpi.invocator(), | ||
| SELF_INDEX, | ||
| SELF_INDEX) >= input.numberOfShares) | ||
| { | ||
| if (qpi.releaseShares(input.asset, qpi.invocator(), qpi.invocator(), input.numberOfShares, | ||
| input.newManagingContractIndex, input.newManagingContractIndex, QSWAP_FEE_BASE_100) < 0) | ||
| locals.result = qpi.releaseShares( | ||
| input.asset, | ||
| qpi.invocator(), | ||
| qpi.invocator(), | ||
| input.numberOfShares, | ||
| input.newManagingContractIndex, | ||
| input.newManagingContractIndex, | ||
| 0 | ||
| ); | ||
|
|
||
| if (locals.result != INVALID_AMOUNT) | ||
| { | ||
| // error | ||
| output.transferredNumberOfShares = 0; | ||
| if (qpi.invocationReward() > 0) | ||
| if (locals.result == 0) | ||
| { | ||
| qpi.transfer(qpi.invocator(), qpi.invocationReward()); | ||
| // success, fee = 0 | ||
| locals.success = true; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // success | ||
| output.transferredNumberOfShares = input.numberOfShares; | ||
| if (qpi.invocationReward() > QSWAP_FEE_BASE_100) | ||
| else if (locals.result < 0) | ||
| { | ||
| qpi.transfer(qpi.invocator(), qpi.invocationReward() - QSWAP_FEE_BASE_100); | ||
| locals.requiredFee = -locals.result; | ||
|
|
||
| if (locals.reward >= locals.requiredFee) | ||
| { | ||
| locals.result = qpi.releaseShares( | ||
| input.asset, | ||
| qpi.invocator(), | ||
| qpi.invocator(), | ||
| input.numberOfShares, | ||
| input.newManagingContractIndex, | ||
| input.newManagingContractIndex, | ||
| locals.requiredFee | ||
| ); | ||
|
|
||
| if (locals.result != INVALID_AMOUNT) | ||
| { | ||
| locals.success = true; | ||
| locals.refundAmount = locals.reward - locals.result; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (locals.success) | ||
| { | ||
| output.transferredNumberOfShares = input.numberOfShares; | ||
| } | ||
|
|
||
| if (locals.refundAmount > 0) | ||
| { | ||
| qpi.transfer(qpi.invocator(), locals.refundAmount); | ||
| } | ||
| } | ||
|
|
||
| REGISTER_USER_FUNCTIONS_AND_PROCEDURES() | ||
|
|
@@ -2318,6 +2372,8 @@ struct QSWAP : public ContractBase | |
| state.qxFeeRate = 5; // 5% of swap fees to QX | ||
| state.burnFeeRate = 1; // 1% of swap fees burned | ||
|
|
||
| ASSERT(state.swapFeeRate < QSWAP_SWAP_FEE_BASE); | ||
| ASSERT(state.shareholderFeeRate + state.investRewardsFeeRate + state.qxFeeRate + state.burnFeeRate <= 100); | ||
| // | ||
| state.investRewardsId = ID(_V, _J, _G, _R, _U, _F, _W, _J, _C, _U, _S, _N, _H, _C, _Q, _J, _R, _W, _R, _R, _Y, _X, _A, _U, _E, _J, _F, _C, _V, _H, _Y, _P, _X, _W, _K, _T, _D, _L, _Y, _K, _U, _A, _C, _P, _V, _V, _Y, _B, _G, _O, _L, _V, _C, _J, _S, _F); | ||
| } | ||
|
|
@@ -2385,6 +2441,7 @@ struct QSWAP : public ContractBase | |
| state.burnedAmount += locals.toBurn; | ||
| } | ||
| } | ||
|
|
||
| PRE_ACQUIRE_SHARES() | ||
| { | ||
| output.allowTransfer = true; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should move this check to before the transfer of assets (~line 1592). With the version right now it can happen that the assets are transferred and the caller is reimbursed.