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

fix: fix rounding error on order sizes #240

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions v4-client-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ nvm alias default $(nvm version) # optional
You can run the following commands to ensure that you are running the correct `node` and `npm` versions.

```
node -v # expected: v18.x.x (should match .nvmrc)
npm -v # expected: 8.x.x
node -v # expected: v20.x.x (should match .nvmrc)
npm -v # expected: 10.x.x
```

### 1. Clone or fork the V4 clients repo
Expand Down
10 changes: 8 additions & 2 deletions v4-client-js/__tests__/lib/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ import { PartialTransactionOptions, TransactionOptions } from '../../src';
import { DEFAULT_SEQUENCE } from '../../src/lib/constants';
import { convertPartialTransactionOptionsToFull, stripHexPrefix } from '../../src/lib/helpers';
import { defaultTransactionOptions } from '../helpers/constants';
import { calculateSubticks } from '../../src/clients/helpers/chain-helpers';
import { calculateSubticks, calculateQuantums } from '../../src/clients/helpers/chain-helpers';
import Long from 'long';

describe('helpers', () => {
describe('calculateSubticks', () => {
it('test test', () => {
it('correctly handles decimals', () => {
expect(calculateSubticks(8.45, -7, -9, 1000000)).toEqual(new Long(845_000_000));
});
});

describe('calculateQuantums', () => {
it('correctly handles decimals', () => {
expect(calculateQuantums(0.0003, -10, 1000000)).toEqual(new Long(3_000_000));
});
});

describe('convertPartialTransactionOptionsToFull', () =>
it.each([
Expand Down
6 changes: 3 additions & 3 deletions v4-client-js/src/clients/helpers/chain-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { Order_ConditionType, Order_Side, Order_TimeInForce } from '../modules/p
import { OrderFlags } from '../types';

export function round(input: number, base: number): number {
return Math.floor(input / base) * base;
return BigNumber(input).div(BigNumber(base)).integerValue(BigNumber.ROUND_FLOOR).times(BigNumber(base)).toNumber();
}

export function calculateQuantums(
size: number,
atomicResolution: number,
stepBaseQuantums: number,
): Long {
const rawQuantums = size * 10 ** (-1 * atomicResolution);
const quantums = round(rawQuantums, stepBaseQuantums);
const rawQuantums = BigNumber(size).times(BigNumber(10).pow(BigNumber(atomicResolution).negated()));
const quantums = round(rawQuantums.toNumber(), stepBaseQuantums);
// stepBaseQuantums functions as minimum order size
const result = Math.max(quantums, stepBaseQuantums);
return Long.fromNumber(result);
Expand Down
Loading