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
4755e4f4
Unverified
Commit
4755e4f4
authored
Apr 30, 2020
by
Noah Zinsmeister
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add math methods to Fraction
parent
9da46ab8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
8 deletions
+33
-8
fraction.ts
src/entities/fractions/fraction.ts
+30
-0
trade.ts
src/entities/trade.ts
+3
-8
No files found.
src/entities/fractions/fraction.ts
View file @
4755e4f4
...
@@ -41,6 +41,28 @@ export class Fraction {
...
@@ -41,6 +41,28 @@ export class Fraction {
return
new
Fraction
(
this
.
denominator
,
this
.
numerator
)
return
new
Fraction
(
this
.
denominator
,
this
.
numerator
)
}
}
add
(
other
:
Fraction
|
BigintIsh
):
Fraction
{
const
otherParsed
=
other
instanceof
Fraction
?
other
:
new
Fraction
(
parseBigintIsh
(
other
))
return
new
Fraction
(
JSBI
.
add
(
JSBI
.
multiply
(
this
.
numerator
,
otherParsed
.
denominator
),
JSBI
.
multiply
(
otherParsed
.
numerator
,
this
.
denominator
)
),
JSBI
.
multiply
(
this
.
denominator
,
otherParsed
.
denominator
)
)
}
subtract
(
other
:
Fraction
|
BigintIsh
):
Fraction
{
const
otherParsed
=
other
instanceof
Fraction
?
other
:
new
Fraction
(
parseBigintIsh
(
other
))
return
new
Fraction
(
JSBI
.
subtract
(
JSBI
.
multiply
(
this
.
numerator
,
otherParsed
.
denominator
),
JSBI
.
multiply
(
otherParsed
.
numerator
,
this
.
denominator
)
),
JSBI
.
multiply
(
this
.
denominator
,
otherParsed
.
denominator
)
)
}
multiply
(
other
:
Fraction
|
BigintIsh
):
Fraction
{
multiply
(
other
:
Fraction
|
BigintIsh
):
Fraction
{
const
otherParsed
=
other
instanceof
Fraction
?
other
:
new
Fraction
(
parseBigintIsh
(
other
))
const
otherParsed
=
other
instanceof
Fraction
?
other
:
new
Fraction
(
parseBigintIsh
(
other
))
return
new
Fraction
(
return
new
Fraction
(
...
@@ -49,6 +71,14 @@ export class Fraction {
...
@@ -49,6 +71,14 @@ export class Fraction {
)
)
}
}
divide
(
other
:
Fraction
|
BigintIsh
):
Fraction
{
const
otherParsed
=
other
instanceof
Fraction
?
other
:
new
Fraction
(
parseBigintIsh
(
other
))
return
new
Fraction
(
JSBI
.
multiply
(
this
.
numerator
,
otherParsed
.
denominator
),
JSBI
.
multiply
(
this
.
denominator
,
otherParsed
.
numerator
)
)
}
toSignificant
(
toSignificant
(
significantDigits
:
number
,
significantDigits
:
number
,
format
:
object
=
{
groupSeparator
:
''
},
format
:
object
=
{
groupSeparator
:
''
},
...
...
src/entities/trade.ts
View file @
4755e4f4
import
invariant
from
'
tiny-invariant
'
import
invariant
from
'
tiny-invariant
'
import
JSBI
from
'
jsbi
'
import
{
TradeType
}
from
'
../constants
'
import
{
TradeType
}
from
'
../constants
'
import
{
Pair
}
from
'
./pair
'
import
{
Pair
}
from
'
./pair
'
import
{
Route
}
from
'
./route
'
import
{
Route
}
from
'
./route
'
import
{
Fraction
,
TokenAmount
}
from
'
./fractions
'
import
{
TokenAmount
}
from
'
./fractions
'
import
{
Price
}
from
'
./fractions/price
'
import
{
Price
}
from
'
./fractions/price
'
import
{
Percent
}
from
'
./fractions/percent
'
import
{
Percent
}
from
'
./fractions/percent
'
function
getSlippage
(
midPrice
:
Price
,
inputAmount
:
TokenAmount
,
outputAmount
:
TokenAmount
):
Percent
{
function
getSlippage
(
midPrice
:
Price
,
inputAmount
:
TokenAmount
,
outputAmount
:
TokenAmount
):
Percent
{
const
exactQuote
=
midPrice
.
raw
.
multiply
(
inputAmount
.
raw
)
const
exactQuote
=
midPrice
.
raw
.
multiply
(
inputAmount
.
raw
)
// calculate (exactQuote - outputAmount) / exactQuote
// calculate slippage := (exactQuote - outputAmount) / exactQuote
const
exactDifference
=
new
Fraction
(
const
slippage
=
exactQuote
.
subtract
(
outputAmount
.
raw
).
divide
(
exactQuote
)
JSBI
.
subtract
(
exactQuote
.
numerator
,
JSBI
.
multiply
(
outputAmount
.
raw
,
exactQuote
.
denominator
)),
exactQuote
.
denominator
)
const
slippage
=
exactDifference
.
multiply
(
exactQuote
.
invert
())
return
new
Percent
(
slippage
.
numerator
,
slippage
.
denominator
)
return
new
Percent
(
slippage
.
numerator
,
slippage
.
denominator
)
}
}
...
...
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