blockworks-foundation / mango-client-v3

Mango Markets V3 TypeScript Client Library

Home Page:https://blockworks-foundation.github.io/mango-client-v3/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inconsistent value in utils.ZERO_BN causes "size too small" error

creatzor opened this issue · comments

Background:
Installed in project via yarn @blockworks-foundation/mango-client
Version: 3.2.16
Problem: Orders placed to BTC SPOT intermittently fail with "size too small"

Diagnosis:
The value of utils.ZERO_BN is randomly inconsistent at

if (maxBaseQuantity.lte(ZERO_BN)) {

Additional Information:
I place a log in that function for utils.ZERO_BN and noticed it's value was not 0 when placing my order. After manually changing the base quantity comparison to a consistent value of BN(0), my orders stopped failing.

if (maxBaseQuantity.lte(new BN(0))) {
  throw new Error('size too small');
}

It's possible that the value is being changed in-place somewhere in the library. The behavior is not consistent, so reproduction is difficult, but it's happening.

I believe ZERO_BN is changed in the line below.

s.iadd(order.sizeLots);

We should be using a clone of ZERO_BN instead of changing it directly via s.iadd. iadd is an inplace method. This could be happening in many places if thats the case

https://github.com/indutny/bn.js/blob/5d5532d9fb7192b5d2545bdb2aa024ca1822a313/test/arithmetic-test.js#L21

    const zero = new BN(0);
    console.log('zero 1', zero.toNumber());
    const a = zero;
    console.log('a 1', a.toNumber());
    a.iadd(new BN(323));
    console.log('zero 2', zero.toNumber());
    console.log('a 2', a.toNumber());

Notice the value of zero is 323 after the iadd instruction.

Hey @creatzor can you try the above PR and see if that one solves he issues you are seeing?

Yep that PR fixes the "size too small" issue @mschneider thanks!