fix: apply fee after quote. Exclude fees from public liquidity

This commit is contained in:
Mohamed Sohail 2024-03-21 16:57:06 +08:00
parent c38fb552e9
commit ab897bf901
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
2 changed files with 16 additions and 6 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ build/
dist/
solidity/*.json
solidity/*.bin
.venv

View File

@ -137,22 +137,30 @@ contract SwapPool {
function withdraw(address _outToken, address _inToken, uint256 _value) public {
bool r;
bytes memory v;
uint256 netValue;
uint256 balance;
uint256 fee;
uint256 outValue;
uint256 trueBalance;
fee = getFee(_value);
netValue = _value - fee;
netValue = getQuote(_outToken, _inToken, netValue);
outValue = getQuote(_outToken, _inToken, _value);
(r, v) = _outToken.call(abi.encodeWithSignature("balanceOf(address)", this));
require(r, "ERR_TOKEN");
balance = abi.decode(v, (uint256));
require(balance >= netValue + fee, "ERR_BALANCE");
// unwithdrawn fees should remain untouched
trueBalance = balance - fees[_outToken];
// deduct the fees from the quoted outValue
fee = getFee(outValue);
outValue -= fee;
// pool should have enough balance to cover the final outValue (fees already deducted)
require(balance >= outValue, "ERR_BALANCE");
deposit(_inToken, _value);
(r, v) = _outToken.call(abi.encodeWithSignature('transfer(address,uint256)', msg.sender, netValue));
(r, v) = _outToken.call(abi.encodeWithSignature('transfer(address,uint256)', msg.sender, outValue));
require(r, "ERR_TOKEN");
r = abi.decode(v, (bool));
require(r, "ERR_TRANSFER");
@ -235,3 +243,4 @@ contract SwapPool {
return false;
}
}