Skip to content
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

feat(broker): trading limits #65

Merged
merged 12 commits into from
Nov 23, 2022
Merged

feat(broker): trading limits #65

merged 12 commits into from
Nov 23, 2022

Conversation

bowd
Copy link
Contributor

@bowd bowd commented Nov 15, 2022

Description

This PR implements trading limits for the broker via the TradingLimits library.
This library provides two structs:

  • TradingLimits.State which holds the counters associated with a trading limit setup
  • TradingLimits.Config which holds the configuration for a trading limit setup.

A trading limit is applied to an asset in a pair. For example, say we have the cUSD/CELO pair which has an exchangeId, if we want to apply a trading limit for cUSD in that pair we configure the trading limits on limitId = exchangeId ^ cUSDTokenAddress. Inside of a TradingLimits.Config we can enable 3 trading limits:

  • L0 which is a time-window based limit, allowing a netflow of limit0 tokens every timespan0 seconds.
  • L1 which is the same as L0 but with a different limit and timespan.
  • LG which is a global limit without a timespan

Other changes

  • Create a MockExchangeProvider to simplify testing in the Broker
  • Compile warning fixes

Tested

  • Library has test
  • Integration with broker tested

Related issues

Backwards compatibility

N/A

Documentation

N/A

Copy link
Contributor

@philbow61 philbow61 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool stuff!!!

  • enjoyed the usage of the bit mask for the different trading limit variants
  • liked the Balance snapshot and the overall refactoring of the broker test

int48 limit0;
int48 limit1;
int48 limitGlobal;
uint8 flags;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😎


/* ==================== State#update ==================== */

function test_update_withNoLimit_updatesOnlyGlobal() public {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about the name of the test. When we provide an empty config the config.flag == 0 therefore all the if clauses in update should be false and the netflowGlobal isn't updated which is also checked in the third assert.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! At some point I was toying around with the idea of always updating netflowGlobal in order to actually be "global", so also when the limit isn't active, but in the end, I decided to keep it simple and only update netflow* when the limit is active.


function setUp() public {
TradingLimits.State memory _state;
state = _state;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this setup needed? Surely you get the same result from just declaring it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can't reconstruct how I ended up in this situation 😂. Removed it.

Copy link
Member

@bayological bayological left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on this. LGTM

@bowd bowd merged commit f659f9c into feature/mcmint Nov 23, 2022
@ninabarbakadze ninabarbakadze deleted the feature/trading-limits branch November 23, 2022 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants