Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
swap-v2-sdk
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LuckySwap
swap-v2-sdk
Commits
9e8e63b8
Unverified
Commit
9e8e63b8
authored
Feb 14, 2020
by
Noah Zinsmeister
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
don't pad toSignificant with 0s...
parent
18817962
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
7 additions
and
16 deletions
+7
-16
fraction.ts
src/entities/fractions/fraction.ts
+2
-11
tokenAmount.ts
src/entities/fractions/tokenAmount.ts
+1
-1
entities.test.ts
test/entities.test.ts
+4
-4
No files found.
src/entities/fractions/fraction.ts
View file @
9e8e63b8
...
@@ -52,25 +52,16 @@ export class Fraction {
...
@@ -52,25 +52,16 @@ export class Fraction {
public
toSignificant
(
public
toSignificant
(
significantDigits
:
number
,
significantDigits
:
number
,
format
:
object
=
{
groupSeparator
:
''
},
format
:
object
=
{
groupSeparator
:
''
},
rounding
:
Rounding
=
Rounding
.
ROUND_HALF_UP
,
rounding
:
Rounding
=
Rounding
.
ROUND_HALF_UP
maximumDecimalPlaces
:
number
=
Number
.
MAX_SAFE_INTEGER
// should only be used to properly bound token amounts
):
string
{
):
string
{
invariant
(
Number
.
isInteger
(
significantDigits
),
`
${
significantDigits
}
is not a positive integer.`
)
invariant
(
Number
.
isInteger
(
significantDigits
),
`
${
significantDigits
}
is not a positive integer.`
)
invariant
(
significantDigits
>
0
,
`
${
significantDigits
}
is not positive.`
)
invariant
(
significantDigits
>
0
,
`
${
significantDigits
}
is not positive.`
)
invariant
(
Number
.
isInteger
(
maximumDecimalPlaces
),
`
${
maximumDecimalPlaces
}
is not an integer.`
)
invariant
(
maximumDecimalPlaces
>=
0
,
`maximumDecimalPlaces
${
maximumDecimalPlaces
}
is negative.`
)
Decimal
.
set
({
precision
:
significantDigits
+
1
,
rounding
:
toSignificantRounding
[
rounding
]
})
Decimal
.
set
({
precision
:
significantDigits
+
1
,
rounding
:
toSignificantRounding
[
rounding
]
})
const
quotient
=
new
Decimal
(
this
.
numerator
.
toString
())
const
quotient
=
new
Decimal
(
this
.
numerator
.
toString
())
.
div
(
this
.
denominator
.
toString
())
.
div
(
this
.
denominator
.
toString
())
.
toSignificantDigits
(
significantDigits
)
.
toSignificantDigits
(
significantDigits
)
const
decimalPlaces
=
return
quotient
.
toFormat
(
quotient
.
decimalPlaces
(),
format
)
quotient
.
decimalPlaces
()
===
0
?
0
// 0 decimal places for integer quotients
:
quotient
.
precision
(
true
)
>=
significantDigits
?
quotient
.
decimalPlaces
()
// else, the default number of decimal plcaes if there's enough precision already
:
significantDigits
-
(
quotient
.
precision
(
true
)
-
quotient
.
decimalPlaces
())
// else, pad with 0s
return
quotient
.
toFormat
(
Math
.
min
(
decimalPlaces
,
maximumDecimalPlaces
),
format
)
// while respecting max
}
}
public
toFixed
(
public
toFixed
(
...
...
src/entities/fractions/tokenAmount.ts
View file @
9e8e63b8
...
@@ -42,7 +42,7 @@ export class TokenAmount extends Fraction {
...
@@ -42,7 +42,7 @@ export class TokenAmount extends Fraction {
}
}
public
toSignificant
(
significantDigits
:
number
,
format
?:
object
,
rounding
:
Rounding
=
Rounding
.
ROUND_DOWN
):
string
{
public
toSignificant
(
significantDigits
:
number
,
format
?:
object
,
rounding
:
Rounding
=
Rounding
.
ROUND_DOWN
):
string
{
return
super
.
toSignificant
(
significantDigits
,
format
,
rounding
,
this
.
token
.
decimals
)
return
super
.
toSignificant
(
significantDigits
,
format
,
rounding
)
}
}
public
toFixed
(
public
toFixed
(
...
...
test/entities.test.ts
View file @
9e8e63b8
...
@@ -73,7 +73,7 @@ describe('entities', () => {
...
@@ -73,7 +73,7 @@ describe('entities', () => {
expect
(
route
.
midPrice
.
toSignificant
(
5
,
{
groupSeparator
:
'
,
'
})).
toEqual
(
'
1,234
'
)
expect
(
route
.
midPrice
.
toSignificant
(
5
,
{
groupSeparator
:
'
,
'
})).
toEqual
(
'
1,234
'
)
expect
(
route
.
midPrice
.
invert
().
toSignificant
(
1
)).
toEqual
(
'
0.0008
'
)
expect
(
route
.
midPrice
.
invert
().
toSignificant
(
1
)).
toEqual
(
'
0.0008
'
)
expect
(
route
.
midPrice
.
invert
().
toSignificant
(
2
)).
toEqual
(
'
0.00081
'
)
expect
(
route
.
midPrice
.
invert
().
toSignificant
(
2
)).
toEqual
(
'
0.00081
'
)
expect
(
route
.
midPrice
.
invert
().
toSignificant
(
3
)).
toEqual
(
'
0.00081
0
'
)
expect
(
route
.
midPrice
.
invert
().
toSignificant
(
3
)).
toEqual
(
'
0.00081
'
)
expect
(
route
.
midPrice
.
invert
().
toSignificant
(
4
)).
toEqual
(
'
0.0008104
'
)
expect
(
route
.
midPrice
.
invert
().
toSignificant
(
4
)).
toEqual
(
'
0.0008104
'
)
expect
(
route
.
midPrice
.
invert
().
toSignificant
(
4
,
undefined
,
Rounding
.
ROUND_DOWN
)).
toEqual
(
'
0.0008103
'
)
expect
(
route
.
midPrice
.
invert
().
toSignificant
(
4
,
undefined
,
Rounding
.
ROUND_DOWN
)).
toEqual
(
'
0.0008103
'
)
expect
(
route
.
midPrice
.
invert
().
toSignificant
(
5
)).
toEqual
(
'
0.00081037
'
)
expect
(
route
.
midPrice
.
invert
().
toSignificant
(
5
)).
toEqual
(
'
0.00081037
'
)
...
@@ -120,7 +120,7 @@ describe('entities', () => {
...
@@ -120,7 +120,7 @@ describe('entities', () => {
expect
(
trade
.
executionPrice
.
invert
().
quote
(
expectedOutputAmount
)).
toEqual
(
inputAmount
)
expect
(
trade
.
executionPrice
.
invert
().
quote
(
expectedOutputAmount
)).
toEqual
(
inputAmount
)
expect
(
trade
.
nextMidPrice
.
toSignificant
(
18
)).
toEqual
(
'
1.38958368072925352
'
)
expect
(
trade
.
nextMidPrice
.
toSignificant
(
18
)).
toEqual
(
'
1.38958368072925352
'
)
expect
(
trade
.
nextMidPrice
.
invert
().
toSignificant
(
18
)).
toEqual
(
'
0.71964
0000000000000
'
)
expect
(
trade
.
nextMidPrice
.
invert
().
toSignificant
(
18
)).
toEqual
(
'
0.71964
'
)
expect
(
trade
.
slippage
.
toSignificant
(
18
)).
toEqual
(
'
-16.8751042187760547
'
)
expect
(
trade
.
slippage
.
toSignificant
(
18
)).
toEqual
(
'
-16.8751042187760547
'
)
...
@@ -142,7 +142,7 @@ describe('entities', () => {
...
@@ -142,7 +142,7 @@ describe('entities', () => {
expect
(
trade
.
executionPrice
.
invert
().
quote
(
outputAmount
)).
toEqual
(
expectedInputAmount
)
expect
(
trade
.
executionPrice
.
invert
().
quote
(
outputAmount
)).
toEqual
(
expectedInputAmount
)
expect
(
trade
.
nextMidPrice
.
toSignificant
(
18
)).
toEqual
(
'
1.38958368072925352
'
)
expect
(
trade
.
nextMidPrice
.
toSignificant
(
18
)).
toEqual
(
'
1.38958368072925352
'
)
expect
(
trade
.
nextMidPrice
.
invert
().
toSignificant
(
18
)).
toEqual
(
'
0.71964
0000000000000
'
)
expect
(
trade
.
nextMidPrice
.
invert
().
toSignificant
(
18
)).
toEqual
(
'
0.71964
'
)
expect
(
trade
.
slippage
.
toSignificant
(
18
)).
toEqual
(
'
-16.8751042187760547
'
)
expect
(
trade
.
slippage
.
toSignificant
(
18
)).
toEqual
(
'
-16.8751042187760547
'
)
...
@@ -168,7 +168,7 @@ describe('entities', () => {
...
@@ -168,7 +168,7 @@ describe('entities', () => {
const
trade
=
new
Trade
(
route
,
outputAmount
,
TradeType
.
EXACT_INPUT
)
const
trade
=
new
Trade
(
route
,
outputAmount
,
TradeType
.
EXACT_INPUT
)
expect
(
trade
.
slippage
.
toSignificant
(
18
)).
toEqual
(
expect
(
trade
.
slippage
.
toSignificant
(
18
)).
toEqual
(
tokens
[
1
].
decimals
===
9
?
'
-0.300000099400899902
'
:
'
-0.3000000000000001
00
'
tokens
[
1
].
decimals
===
9
?
'
-0.300000099400899902
'
:
'
-0.3000000000000001
'
)
)
}
}
})
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment