mirror of
https://github.com/grassrootseconomics/erc20-pool.git
synced 2024-11-07 06:26:47 +01:00
fix: apply fee after quote. Exclude fees from public liquidity
This commit is contained in:
parent
c38fb552e9
commit
ab897bf901
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,3 +5,4 @@ build/
|
|||||||
dist/
|
dist/
|
||||||
solidity/*.json
|
solidity/*.json
|
||||||
solidity/*.bin
|
solidity/*.bin
|
||||||
|
.venv
|
@ -137,22 +137,30 @@ contract SwapPool {
|
|||||||
function withdraw(address _outToken, address _inToken, uint256 _value) public {
|
function withdraw(address _outToken, address _inToken, uint256 _value) public {
|
||||||
bool r;
|
bool r;
|
||||||
bytes memory v;
|
bytes memory v;
|
||||||
uint256 netValue;
|
|
||||||
uint256 balance;
|
uint256 balance;
|
||||||
uint256 fee;
|
uint256 fee;
|
||||||
|
uint256 outValue;
|
||||||
|
uint256 trueBalance;
|
||||||
|
|
||||||
fee = getFee(_value);
|
outValue = getQuote(_outToken, _inToken, _value);
|
||||||
netValue = _value - fee;
|
|
||||||
netValue = getQuote(_outToken, _inToken, netValue);
|
|
||||||
|
|
||||||
(r, v) = _outToken.call(abi.encodeWithSignature("balanceOf(address)", this));
|
(r, v) = _outToken.call(abi.encodeWithSignature("balanceOf(address)", this));
|
||||||
require(r, "ERR_TOKEN");
|
require(r, "ERR_TOKEN");
|
||||||
balance = abi.decode(v, (uint256));
|
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);
|
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");
|
require(r, "ERR_TOKEN");
|
||||||
r = abi.decode(v, (bool));
|
r = abi.decode(v, (bool));
|
||||||
require(r, "ERR_TRANSFER");
|
require(r, "ERR_TRANSFER");
|
||||||
@ -235,3 +243,4 @@ contract SwapPool {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user