Skip to content

Commit

Permalink
fix market orders by accounting for fees
Browse files Browse the repository at this point in the history
  • Loading branch information
tomjohn1028 committed Jul 9, 2021
1 parent f4f1380 commit 69f969c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 48 deletions.
14 changes: 13 additions & 1 deletion packages/serum/src/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ export class Market {
maxBaseQuantity: this.baseSizeNumberToLots(size),
maxQuoteQuantity: new BN(this._decoded.quoteLotSize.toNumber()).mul(
this.baseSizeNumberToLots(size).mul(
this.priceNumberToLots(price * (1 + feeRate)),
this.priceNumberToLotsRoundUp(price * (1 + feeRate)),
),
),
orderType,
Expand Down Expand Up @@ -1106,6 +1106,18 @@ export class Market {
);
}

priceNumberToLotsRoundUp(price: number): BN {
return new BN(
Math.ceil(
(price *
Math.pow(10, this._quoteSplTokenDecimals) *
this._decoded.baseLotSize.toNumber()) /
(Math.pow(10, this._baseSplTokenDecimals) *
this._decoded.quoteLotSize.toNumber()),
),
);
}

baseSplSizeToNumber(size: BN) {
return divideBnToNumber(size, this._baseSplTokenMultiplier);
}
Expand Down
48 changes: 1 addition & 47 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3550,25 +3550,6 @@
tweetnacl "^1.0.0"
ws "^7.0.0"

"@solana/web3.js@^1.1.0", "@solana/web3.js@^1.2.6":
version "1.2.6"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.2.6.tgz#ee57c28d2639d95cc5de1179babe695870041c94"
integrity sha512-TAAC5+zq7BHn3+mz6jpJlKFvcTqy0Fj/a+Fq3T5MVvsidlVTmIh18qiGPpe3Ve4eMZquF7JcytS8JanEyvgO6g==
dependencies:
"@babel/runtime" "^7.12.5"
bn.js "^5.0.0"
bs58 "^4.0.1"
buffer "6.0.1"
buffer-layout "^1.2.0"
crypto-hash "^1.2.2"
jayson "^3.4.4"
js-sha3 "^0.8.0"
node-fetch "^2.6.1"
rpc-websockets "^7.4.2"
secp256k1 "^4.0.2"
superstruct "^0.14.2"
tweetnacl "^1.0.0"

"@surma/rollup-plugin-off-main-thread@^1.1.1":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@surma/rollup-plugin-off-main-thread/-/rollup-plugin-off-main-thread-1.4.2.tgz#e6786b6af5799f82f7ab3a82e53f6182d2b91a58"
Expand Down Expand Up @@ -9255,7 +9236,7 @@ [email protected]:
dependencies:
harmony-reflect "^1.4.6"

ieee754@^1.1.13, ieee754@^1.1.4, ieee754@^1.2.1:
ieee754@^1.1.4, ieee754@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352"
integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==
Expand Down Expand Up @@ -9965,23 +9946,6 @@ jayson@^3.0.1:
lodash "^4.17.20"
uuid "^3.4.0"

jayson@^3.4.4:
version "3.4.4"
resolved "https://registry.yarnpkg.com/jayson/-/jayson-3.4.4.tgz#dcedffba0c02785c4aa22dbff8c28966cae59773"
integrity sha512-fgQflh+Qnhdv9fjxTnpTsa2WUG/dgyeKQzIh5MJ77Qv2sqFyyAZn7mTUYgPjJMFjsKfb4HNsSBh6ktJeeQiAGQ==
dependencies:
"@types/connect" "^3.4.33"
"@types/express-serve-static-core" "^4.17.9"
"@types/lodash" "^4.14.159"
"@types/node" "^12.12.54"
JSONStream "^1.3.5"
commander "^2.20.3"
es6-promisify "^5.0.0"
eyes "^0.1.8"
json-stringify-safe "^5.0.1"
lodash "^4.17.20"
uuid "^3.4.0"

jest-changed-files@^26.6.2:
version "26.6.2"
resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-26.6.2.tgz#f6198479e1cc66f22f9ae1e22acaa0b429c042d0"
Expand Down Expand Up @@ -10480,11 +10444,6 @@ js-sha256@^0.9.0:
resolved "https://registry.yarnpkg.com/js-sha256/-/js-sha256-0.9.0.tgz#0b89ac166583e91ef9123644bd3c5334ce9d0966"
integrity sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA==

js-sha3@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.8.0.tgz#b9b7a5da73afad7dedd0f8c463954cbde6818840"
integrity sha512-gF1cRrHhIzNfToc802P800N8PpXS+evLLXfsVpowqmAFR9uwbi89WvXg2QspOmXL8QL86J4T1EpFu+yUkwJY3Q==

"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down Expand Up @@ -15751,11 +15710,6 @@ [email protected]:
kind-of "^6.0.2"
tiny-invariant "^1.0.6"

superstruct@^0.14.2:
version "0.14.2"
resolved "https://registry.yarnpkg.com/superstruct/-/superstruct-0.14.2.tgz#0dbcdf3d83676588828f1cf5ed35cda02f59025b"
integrity sha512-nPewA6m9mR3d6k7WkZ8N8zpTWfenFH3q9pA2PkuiZxINr9DKB2+40wEQf0ixn8VaGuJ78AB6iWOtStI+/4FKZQ==

superstruct@^0.8.3:
version "0.8.4"
resolved "https://registry.yarnpkg.com/superstruct/-/superstruct-0.8.4.tgz#478a19649f6b02c6319c02044db6a1f5863c391f"
Expand Down

0 comments on commit 69f969c

Please sign in to comment.