Skip to content

Commit

Permalink
fix (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
moo-onthelawn authored Sep 17, 2024
1 parent 20e524e commit b6a24a2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
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

0 comments on commit b6a24a2

Please sign in to comment.