project-serum / serum-ts

Project Serum TypeScript monorepo
https://projectserum.com
Apache License 2.0
273 stars 246 forks source link

maxQuoteQuantity is being set incorrectly for bids #84

Open sconybeare opened 3 years ago

sconybeare commented 3 years ago

maxQuoteQuantity is being calculated as price*quantity, which fails to take fees into account when the order is a bid that matches against a resting offer. Instead, maxQuoteQuantity should be set to price*quantity*(1+fee_rate), unless the order is post-only.

yamen commented 3 years ago

Background on this is here: https://docs.google.com/document/d/17-FlYTIcLiciWdjXaQEdUnEJFbvIeIEYW3fdcEQu8ds/edit

Currently a brute force approach of using 1% is done when creating the wrapped SOL account in cases where quote token == SOL: https://github.com/project-serum/serum-ts/blob/5f7ceafb1e99e542a74e4377cde1ffcf5a999e1c/packages/serum/src/market.ts#L672

Suggesting the same could be done here for consistency: https://github.com/project-serum/serum-ts/blob/5f7ceafb1e99e542a74e4377cde1ffcf5a999e1c/packages/serum/src/market.ts#L817-L818

Ideal version of course takes into account the actual fees expected, but given there are scenarios where it's not obvious if order will be a taker or maker, this is not foolproof.

Alternatively the current PR (https://github.com/project-serum/serum-ts/pull/86) fixes the issue properly by assuming taker fees as long as its not a postOnly order.