Skip to content

Commit

Permalink
Merge pull request #1464 from bancorprotocol/set-safedecimal-precision
Browse files Browse the repository at this point in the history
Set Decimal precision and use in SafeDecimal and SDK worker
  • Loading branch information
tiagofilipenunes authored Sep 3, 2024
2 parents 0402983 + 896f28f commit 119bec6
Show file tree
Hide file tree
Showing 24 changed files with 27 additions and 9 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified e2e/screenshots/strategy/overlapping/Overlapping/withdraw/form.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion src/libs/safedecimal/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Decimal } from 'decimal.js';

Decimal.set({
precision: 100,
rounding: Decimal.ROUND_HALF_DOWN,
toExpNeg: -30,
toExpPos: 30,
});

/**
* Sanitizes the input value for safe decimal creation.
* If the value is a string, it checks if it's a valid
Expand Down Expand Up @@ -52,4 +59,4 @@ class SafeDecimal extends Decimal {
}
}

export { SafeDecimal };
export { SafeDecimal, Decimal };
5 changes: 4 additions & 1 deletion src/libs/safedecimal/safedecimal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ describe('SafeDecimal', () => {
it('should return a valid output if input is exponential number', () => {
expect(new SafeDecimal('1E-5').toString()).toEqual('0.00001');
expect(new SafeDecimal('10E0').toString()).toEqual('10');
expect(new SafeDecimal('10E20').toString()).toEqual('1e+21');
expect(new SafeDecimal('10E20').toString()).toEqual(
'1000000000000000000000'
);
expect(new SafeDecimal('10E101').toString()).toEqual('1e+102');
});
it('should return a valid output if input is a negative number', () => {
expect(new SafeDecimal('-1E-5').toString()).toEqual('-0.00001');
Expand Down
22 changes: 15 additions & 7 deletions src/utils/fullOutcome.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ describe('fullOutcome', () => {
});

it('should return mean price for range price', () => {
const mean = new SafeDecimal('158.1138830084189666');
const mean = new SafeDecimal(
'158.1138830084189665999446772216359266859777569662608413428752426396297219319619110672124054189650148'
);
expect(geoMean('100', '250')).toEqual(mean);
});
});
Expand Down Expand Up @@ -55,9 +57,11 @@ describe('fullOutcome', () => {
expect(getFullOutcome(noPriceAndMax)).toBeUndefined();
});
describe('Limit rate', () => {
it('[Buy] should return 3.1645569620253164557 with price 1580 & budget 5000', () => {
it('[Buy] should return 3.164556962025316455696202531645569620253164556962025316455696202531645569620253164556962025316455696 with price 1580 & budget 5000', () => {
const input = { ...base, min: '1580', max: '1580', budget: '5000' };
expect(getFullOutcome(input)?.amount).toBe('3.1645569620253164557');
expect(getFullOutcome(input)?.amount).toBe(
'3.164556962025316455696202531645569620253164556962025316455696202531645569620253164556962025316455696'
);
});
it('[Buy] should return 2 with price 1600 & budget 3200', () => {
const input = { ...base, min: '1600', max: '1600', budget: '3200' };
Expand All @@ -79,24 +83,28 @@ describe('fullOutcome', () => {
});
});
describe('Range rate', () => {
it('[Buy] should return 1.8353258709644941273 with min 1600, max 1900 & budget 3200', () => {
it('[Buy] should return 1.835325870964494127257676624782996067005054284308397868164355426593497822401329720276277539306081906 with min 1600, max 1900 & budget 3200', () => {
const input = {
...base,
min: '1600',
max: '1900',
budget: '3200',
};
expect(getFullOutcome(input)?.amount).toBe('1.8353258709644941273');
expect(getFullOutcome(input)?.amount).toBe(
'1.835325870964494127257676624782996067005054284308397868164355426593497822401329720276277539306081906'
);
});
it('[Sell] should return 4623.3105022267323378 with min 1800, max 1900 & budget 2.5', () => {
it('[Sell] should return 4623.31050222673233768764428609068316892671801758343090865592905434379973769774815972504734939917496 with min 1800, max 1900 & budget 2.5', () => {
const input = {
...base,
buy: false,
min: '1800',
max: '1900',
budget: '2.5',
};
expect(getFullOutcome(input)?.amount).toBe('4623.3105022267323378');
expect(getFullOutcome(input)?.amount).toBe(
'4623.31050222673233768764428609068316892671801758343090865592905434379973769774815972504734939917496'
);
});
});
});
Expand Down

0 comments on commit 119bec6

Please sign in to comment.