-
Notifications
You must be signed in to change notification settings - Fork 41
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
Maker fee #64
Open
ivanzhelyazkov
wants to merge
5
commits into
dev
Choose a base branch
from
maker-fee
base: dev
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
Maker fee #64
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8aca34b
maker fee - contracts
ivanzhelyazkov 4928126
maker fee - tests
ivanzhelyazkov 35f279a
Merge branch 'dev' into maker-fee
ivanzhelyazkov cb64e05
maker fee - small fixes
ivanzhelyazkov 88d7ffa
maker fee - minor fixes
ivanzhelyazkov 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 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
This file contains 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 |
---|---|---|
|
@@ -7,7 +7,7 @@ import { SafeCastUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/m | |
import { Address } from "@openzeppelin/contracts/utils/Address.sol"; | ||
import { MathEx } from "../utility/MathEx.sol"; | ||
import { InvalidIndices } from "../utility/Utils.sol"; | ||
import { Token } from "../token/Token.sol"; | ||
import { Token, NATIVE_TOKEN } from "../token/Token.sol"; | ||
import { Pair } from "./Pairs.sol"; | ||
import { IVoucher } from "../voucher/interfaces/IVoucher.sol"; | ||
import { PPM_RESOLUTION } from "../utility/Constants.sol"; | ||
|
@@ -137,6 +137,8 @@ abstract contract Strategies is Initializable { | |
error InvalidRate(); | ||
error InvalidTradeActionStrategyId(); | ||
error InvalidTradeActionAmount(); | ||
error UnnecessaryNativeTokenReceived(); | ||
error InsufficientNativeTokenReceived(); | ||
error StrategyDoesNotExist(); | ||
error OutDated(); | ||
|
||
|
@@ -156,6 +158,8 @@ abstract contract Strategies is Initializable { | |
|
||
uint256 private constant ONE = 1 << 48; | ||
|
||
uint256 private constant DEFAULT_MAKER_FEE = 5e14; // 0.0005 ETH | ||
|
||
uint32 private constant DEFAULT_TRADING_FEE_PPM = 2000; // 0.2% | ||
|
||
// total number of strategies | ||
|
@@ -164,6 +168,9 @@ abstract contract Strategies is Initializable { | |
// the global trading fee (in units of PPM) | ||
uint32 private _tradingFeePPM; | ||
|
||
// the global maker fee (for strategy owners, in native token units) | ||
uint256 private _makerFee; | ||
|
||
// mapping between a strategy to its packed orders | ||
mapping(uint256 => uint256[3]) private _packedOrdersByStrategyId; | ||
|
||
|
@@ -174,13 +181,18 @@ abstract contract Strategies is Initializable { | |
mapping(Token => uint256) internal _accumulatedFees; | ||
|
||
// upgrade forward-compatibility storage gap | ||
uint256[MAX_GAP - 4] private __gap; | ||
uint256[MAX_GAP - 5] private __gap; | ||
|
||
/** | ||
* @dev triggered when the network fee is updated | ||
*/ | ||
event TradingFeePPMUpdated(uint32 prevFeePPM, uint32 newFeePPM); | ||
|
||
/** | ||
* @dev triggered when the maker fee is updated | ||
*/ | ||
event MakerFeeUpdated(uint256 prevFee, uint256 newFee); | ||
|
||
/** | ||
* @dev triggered when a strategy is created | ||
*/ | ||
|
@@ -248,6 +260,7 @@ abstract contract Strategies is Initializable { | |
*/ | ||
function __Strategies_init_unchained() internal onlyInitializing { | ||
_setTradingFeePPM(DEFAULT_TRADING_FEE_PPM); | ||
_setMakerFee(DEFAULT_MAKER_FEE); | ||
} | ||
|
||
// solhint-enable func-name-mixedcase | ||
|
@@ -263,6 +276,9 @@ abstract contract Strategies is Initializable { | |
address owner, | ||
uint256 value | ||
) internal returns (uint256) { | ||
// account for maker fee | ||
bool revertOnExcess = !tokens[0].isNative() && !tokens[1].isNative(); | ||
value = _deductMakerFee(revertOnExcess, value); | ||
// transfer funds | ||
_validateDepositAndRefundExcessNativeToken(tokens[0], owner, orders[0].y, value); | ||
_validateDepositAndRefundExcessNativeToken(tokens[1], owner, orders[1].y, value); | ||
|
@@ -304,6 +320,9 @@ abstract contract Strategies is Initializable { | |
address owner, | ||
uint256 value | ||
) internal { | ||
// account for maker fee | ||
bool revertOnExcess = !pair.tokens[0].isNative() && !pair.tokens[1].isNative(); | ||
value = _deductMakerFee(revertOnExcess, value); | ||
barakman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// prepare storage variable | ||
uint256[3] storage packedOrders = _packedOrdersByStrategyId[strategyId]; | ||
uint256[3] memory packedOrdersMemory = _packedOrdersByStrategyId[strategyId]; | ||
|
@@ -357,7 +376,9 @@ abstract contract Strategies is Initializable { | |
/** | ||
* @dev deletes a strategy | ||
*/ | ||
function _deleteStrategy(Strategy memory strategy, IVoucher voucher, Pair memory pair) internal { | ||
function _deleteStrategy(Strategy memory strategy, IVoucher voucher, Pair memory pair, uint256 value) internal { | ||
// account for maker fee | ||
_deductMakerFee(true, value); | ||
// burn the voucher nft token | ||
voucher.burn(strategy.id); | ||
|
||
|
@@ -637,6 +658,26 @@ abstract contract Strategies is Initializable { | |
} | ||
} | ||
|
||
/** | ||
* @dev checks if maker fee has been sent with the transaction and returns updated tx value | ||
*/ | ||
function _deductMakerFee(bool revertOnExcess, uint256 txValue) private returns (uint256) { | ||
uint fee = _makerFee; | ||
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. Minor: 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. fixed |
||
// revert if not enough native token is sent | ||
if (txValue < fee) { | ||
revert InsufficientNativeTokenReceived(); | ||
} | ||
// update accumulated fees | ||
if (fee != 0) { | ||
_accumulatedFees[NATIVE_TOKEN] += fee; | ||
} | ||
// revert if excess native token is sent | ||
if (revertOnExcess && txValue > fee) { | ||
revert UnnecessaryNativeTokenReceived(); | ||
} | ||
barakman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return txValue - fee; | ||
} | ||
|
||
function _validateTradeParams(uint128 pairId, uint256 strategyId, uint128 tradeAmount) private pure { | ||
// make sure the strategy id matches the pair id | ||
if (_pairIdByStrategyId(strategyId) != pairId) { | ||
|
@@ -663,13 +704,34 @@ abstract contract Strategies is Initializable { | |
emit TradingFeePPMUpdated({ prevFeePPM: prevTradingFeePPM, newFeePPM: newTradingFeePPM }); | ||
} | ||
|
||
/** | ||
* @dev sets the maker fee (in native token units) | ||
*/ | ||
function _setMakerFee(uint256 newMakerFee) internal { | ||
uint256 prevMakerFee = _makerFee; | ||
if (prevMakerFee == newMakerFee) { | ||
return; | ||
} | ||
|
||
_makerFee = newMakerFee; | ||
|
||
emit MakerFeeUpdated(prevMakerFee, newMakerFee); | ||
} | ||
|
||
/** | ||
* returns the current trading fee | ||
*/ | ||
function _currentTradingFeePPM() internal view returns (uint32) { | ||
return _tradingFeePPM; | ||
} | ||
|
||
/** | ||
* returns the current maker fee | ||
*/ | ||
function _currentMakerFee() internal view returns (uint256) { | ||
return _makerFee; | ||
} | ||
|
||
/** | ||
* returns the current amount of accumulated fees for a specific token | ||
*/ | ||
|
This file contains 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
This file contains 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
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 believe that you can simply use
0.0005 ether
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.
changed to
0.0005 ether
, removed comment - seems a bit clearer