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
53c7c345
Unverified
Commit
53c7c345
authored
Jul 06, 2020
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more docs
parent
280d3be3
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
10 deletions
+42
-10
token.ts
src/entities/token.ts
+11
-0
trade.ts
src/entities/trade.ts
+31
-10
No files found.
src/entities/token.ts
View file @
53c7c345
...
@@ -16,6 +16,11 @@ export class Token extends Currency {
...
@@ -16,6 +16,11 @@ export class Token extends Currency {
this
.
address
=
validateAndParseAddress
(
address
)
this
.
address
=
validateAndParseAddress
(
address
)
}
}
/**
* Returns true if the two tokens are equivalent.
* @param other other token to compare
* @throws if the tokens share the address and chain ID but have different metadata
*/
public
equals
(
other
:
Token
):
boolean
{
public
equals
(
other
:
Token
):
boolean
{
// short circuit on reference equality
// short circuit on reference equality
if
(
this
===
other
)
{
if
(
this
===
other
)
{
...
@@ -31,6 +36,12 @@ export class Token extends Currency {
...
@@ -31,6 +36,12 @@ export class Token extends Currency {
return
equivalent
return
equivalent
}
}
/**
* Returns true if the address of this token sorts before the address of the other token
* @param other other token to compare
* @throws if the tokens have the same address
* @throws if the tokens are on different chains
*/
public
sortsBefore
(
other
:
Token
):
boolean
{
public
sortsBefore
(
other
:
Token
):
boolean
{
invariant
(
this
.
chainId
===
other
.
chainId
,
'
CHAIN_IDS
'
)
invariant
(
this
.
chainId
===
other
.
chainId
,
'
CHAIN_IDS
'
)
invariant
(
this
.
address
!==
other
.
address
,
'
ADDRESSES
'
)
invariant
(
this
.
address
!==
other
.
address
,
'
ADDRESSES
'
)
...
...
src/entities/trade.ts
View file @
53c7c345
...
@@ -12,8 +12,12 @@ import { Pair } from './pair'
...
@@ -12,8 +12,12 @@ import { Pair } from './pair'
import
{
Route
}
from
'
./route
'
import
{
Route
}
from
'
./route
'
import
{
currencyEquals
,
Token
,
WETH
}
from
'
./token
'
import
{
currencyEquals
,
Token
,
WETH
}
from
'
./token
'
// returns the percent difference between the mid price and the execution price
/**
// we call this price impact in the UI
* Returns the percent difference between the mid price and the execution price, i.e. price impact.
* @param midPrice mid price before the trade
* @param inputAmount the input amount of the trade
* @param outputAmount the output amount of the trade
*/
function
computePriceImpact
(
midPrice
:
Price
,
inputAmount
:
CurrencyAmount
,
outputAmount
:
CurrencyAmount
):
Percent
{
function
computePriceImpact
(
midPrice
:
Price
,
inputAmount
:
CurrencyAmount
,
outputAmount
:
CurrencyAmount
):
Percent
{
const
exactQuote
=
midPrice
.
raw
.
multiply
(
inputAmount
.
raw
)
const
exactQuote
=
midPrice
.
raw
.
multiply
(
inputAmount
.
raw
)
// calculate slippage := (exactQuote - outputAmount) / exactQuote
// calculate slippage := (exactQuote - outputAmount) / exactQuote
...
@@ -95,23 +99,40 @@ function wrappedCurrency(currency: Currency, chainId: ChainId): Token {
...
@@ -95,23 +99,40 @@ function wrappedCurrency(currency: Currency, chainId: ChainId): Token {
invariant
(
false
,
'
CURRENCY
'
)
invariant
(
false
,
'
CURRENCY
'
)
}
}
/**
* Represents a trade executed against a list of pairs.
* Does not account for slippage, i.e. trades that front run this trade and move the price.
*/
export
class
Trade
{
export
class
Trade
{
/**
* The route of the trade, i.e. which pairs the trade goes through.
*/
public
readonly
route
:
Route
public
readonly
route
:
Route
/**
* The type of the trade, either exact in or exact out.
*/
public
readonly
tradeType
:
TradeType
public
readonly
tradeType
:
TradeType
/**
* The input amount for the trade assuming no slippage.
*/
public
readonly
inputAmount
:
CurrencyAmount
public
readonly
inputAmount
:
CurrencyAmount
/**
* The output amount for the trade assuming no slippage.
*/
public
readonly
outputAmount
:
CurrencyAmount
public
readonly
outputAmount
:
CurrencyAmount
// the price expressed in terms of output/input
/**
* The price expressed in terms of output amount/input amount.
*/
public
readonly
executionPrice
:
Price
public
readonly
executionPrice
:
Price
// the mid price after the trade executes assuming zero slippage
/**
* The mid price after the trade executes assuming no slippage.
*/
public
readonly
nextMidPrice
:
Price
public
readonly
nextMidPrice
:
Price
// the percent difference between the mid price before the trade and the price after the trade
/**
* The percent difference between the mid price before the trade and the trade execution price.
*/
public
readonly
priceImpact
:
Percent
public
readonly
priceImpact
:
Percent
// this is a misnomer for price impact, but kept for compatibility
public
get
slippage
():
Percent
{
return
this
.
priceImpact
}
/**
/**
* Constructs an exact in trade with the given amount in and route
* Constructs an exact in trade with the given amount in and route
* @param route route of the exact in trade
* @param route route of the exact in trade
...
...
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