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
4d0f43f8
Unverified
Commit
4d0f43f8
authored
Jan 21, 2020
by
Noah Zinsmeister
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
export format functions
parent
169271b3
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
28 additions
and
13 deletions
+28
-13
index.ts
src/index.ts
+1
-0
formatOutputs.ts
src/utils/formatOutputs.ts
+24
-12
index.ts
src/utils/index.ts
+1
-0
computation.test.ts
test/computation.test.ts
+2
-1
No files found.
src/index.ts
View file @
4d0f43f8
...
...
@@ -2,3 +2,4 @@ export * from './types'
export
{
ChainId
,
WETH
,
TradeType
}
from
'
./constants
'
export
*
from
'
./entities
'
export
*
from
'
./utils
'
src/utils/formatOutputs.ts
View file @
4d0f43f8
import
invariant
from
'
tiny-invariant
'
import
JSBI
from
'
jsbi
'
import
_Decimal
from
'
decimal.js-light
'
import
_Big
,
{
RoundingMode
}
from
'
big.js
'
import
toFormat
from
'
toformat
'
import
{
BigintIsh
}
from
'
../types
'
import
{
parseBigintIsh
}
from
'
./parseInputs
'
const
Decimal
=
toFormat
(
_Decimal
)
const
Big
=
toFormat
(
_Big
)
export
function
formatSignificant
(
numerator
:
JSBI
,
denominator
:
JSBI
,
numerator
:
BigintIsh
,
denominator
:
BigintIsh
,
significantDigits
:
number
,
format
:
object
=
{
groupSeparator
:
''
},
roundingMode
?:
number
roundingMode
:
number
=
Decimal
.
ROUND_HALF_UP
,
minimumDecimalPlaces
:
number
=
0
):
string
{
invariant
(
Number
.
isInteger
(
significantDigits
),
`
${
significantDigits
}
is not an integer.`
)
invariant
(
significantDigits
>
0
,
`
${
significantDigits
}
isn't positive.`
)
const
numeratorParsed
=
parseBigintIsh
(
numerator
)
const
denominatorParsed
=
parseBigintIsh
(
denominator
)
Decimal
.
set
({
precision
:
significantDigits
+
1
,
rounding
:
roundingMode
??
Decimal
.
ROUND_HALF_UP
})
const
quotient
=
new
Decimal
(
numerator
.
toString
()).
div
(
denominator
.
toString
()).
toSignificantDigits
(
significantDigits
)
return
quotient
.
toFormat
(
quotient
.
decimalPlaces
(),
format
)
Decimal
.
set
({
precision
:
significantDigits
+
1
,
rounding
:
roundingMode
})
const
quotient
=
new
Decimal
(
numeratorParsed
.
toString
())
.
div
(
denominatorParsed
.
toString
())
.
toSignificantDigits
(
significantDigits
)
return
quotient
.
toFormat
(
minimumDecimalPlaces
>
quotient
.
decimalPlaces
()
?
minimumDecimalPlaces
:
quotient
.
decimalPlaces
(),
format
)
}
export
function
formatFixed
(
numerator
:
JSBI
,
denominator
:
JSBI
,
numerator
:
BigintIsh
,
denominator
:
BigintIsh
,
decimalPlaces
:
number
,
format
:
object
=
{
groupSeparator
:
''
},
roundingMode
?:
RoundingMode
roundingMode
:
RoundingMode
=
RoundingMode
.
RoundHalfUp
):
string
{
invariant
(
Number
.
isInteger
(
decimalPlaces
),
`
${
decimalPlaces
}
is not an integer.`
)
invariant
(
decimalPlaces
>=
0
,
`
${
decimalPlaces
}
is negative.`
)
const
numeratorParsed
=
parseBigintIsh
(
numerator
)
const
denominatorParsed
=
parseBigintIsh
(
denominator
)
Big
.
DP
=
decimalPlaces
Big
.
RM
=
roundingMode
??
RoundingMode
.
RoundHalfUp
return
new
Big
(
numerator
.
toString
()).
div
(
denominator
.
toString
()).
toFormat
(
decimalPlaces
,
format
)
Big
.
RM
=
roundingMode
return
new
Big
(
numerator
Parsed
.
toString
()).
div
(
denominatorParsed
.
toString
()).
toFormat
(
decimalPlaces
,
format
)
}
src/utils/index.ts
0 → 100644
View file @
4d0f43f8
export
{
formatSignificant
,
formatFixed
}
from
'
./formatOutputs
'
test/computation.test.ts
View file @
4d0f43f8
...
...
@@ -62,7 +62,7 @@ describe('entities', () => {
expect
(
route
.
output
).
toEqual
(
WETH
)
})
it
(
'
Rat
e via Route.marketRate
'
,
()
=>
{
it
(
'
Pric
e via Route.marketRate
'
,
()
=>
{
expect
(
route
.
midPrice
.
quote
(
decimalize
(
1
,
route
.
input
.
decimals
)).
toString
()).
toEqual
(
decimalize
(
1234
,
route
.
output
.
decimals
).
toString
()
)
...
...
@@ -79,6 +79,7 @@ describe('entities', () => {
expect
(
route
.
midPrice
.
formatSignificant
(
4
)).
toEqual
(
'
1234
'
)
expect
(
route
.
midPrice
.
formatSignificant
(
5
)).
toEqual
(
'
1234
'
)
expect
(
route
.
midPrice
.
formatSignificant
(
4
,
{
groupSeparator
:
'
,
'
})).
toEqual
(
'
1,234
'
)
expect
(
route
.
midPrice
.
formatSignificant
(
4
,
undefined
,
undefined
,
2
)).
toEqual
(
'
1234.00
'
)
expect
(
route
.
midPrice
.
invert
().
formatSignificant
(
1
)).
toEqual
(
'
0.0008
'
)
expect
(
route
.
midPrice
.
invert
().
formatSignificant
(
2
)).
toEqual
(
'
0.00081
'
)
expect
(
route
.
midPrice
.
invert
().
formatSignificant
(
3
)).
toEqual
(
'
0.00081
'
)
...
...
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