fixes: fee application to swap quote

commit 4931ef1d70
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Tue Mar 26 16:56:44 2024 +0800

    fix: include fees in public liquidity

commit 78844169c5
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Thu Mar 21 17:00:33 2024 +0800

    update: fix-pool builds

commit ab897bf901
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Thu Mar 21 16:57:06 2024 +0800

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

commit c38fb552e9
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Thu Mar 21 13:09:29 2024 +0800

    feat: make getQuote a public function

    * useful for 3rd part integration. No need to make a 2nd call to the quoter.
This commit is contained in:
2024-04-08 12:13:50 +08:00
parent f812790610
commit 561c4b3a71
5 changed files with 16 additions and 10 deletions

View File

@@ -119,7 +119,7 @@ contract SwapPool {
return fee;
}
function getQuote(address _outToken, address _inToken, uint256 _value) private returns (uint256) {
function getQuote(address _outToken, address _inToken, uint256 _value) public returns (uint256) {
bool r;
bytes memory v;
uint256 quote;
@@ -137,22 +137,26 @@ contract SwapPool {
function withdraw(address _outToken, address _inToken, uint256 _value) public {
bool r;
bytes memory v;
uint256 netValue;
uint256 balance;
uint256 fee;
uint256 outValue;
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");
// 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 +239,4 @@ contract SwapPool {
return false;
}
}