base boilerplate
This commit is contained in:
commit
e4889e3bfa
|
@ -0,0 +1 @@
|
|||
node_modules
|
|
@ -0,0 +1,4 @@
|
|||
declare module '*.svg' {
|
||||
const content: any
|
||||
export default content
|
||||
}
|
|
@ -0,0 +1,142 @@
|
|||
import { AccountInfo, PublicKey } from '@solana/web3.js'
|
||||
import { Market, OpenOrders } from '@project-serum/serum'
|
||||
import { Event } from '@project-serum/serum/lib/queue'
|
||||
import { I80F48 } from '@blockworks-foundation/mango-client'
|
||||
|
||||
export interface Token {
|
||||
chainId: number // 101,
|
||||
address: string // 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
|
||||
symbol: string // 'USDC',
|
||||
name: string // 'Wrapped USDC',
|
||||
decimals: number // 6,
|
||||
logoURI: string // 'https://raw.githubusercontent.com/solana-labs/token-list/main/assets/mainnet/BXXkv6z8ykpG1yuvUDPgh732wzVHB69RnB9YgSYh3itW/logo.png',
|
||||
tags: string[] // [ 'stablecoin' ]
|
||||
}
|
||||
export interface MarketInfo {
|
||||
address: PublicKey
|
||||
name: string
|
||||
programId: PublicKey
|
||||
deprecated: boolean
|
||||
quoteLabel?: string
|
||||
baseLabel?: string
|
||||
}
|
||||
|
||||
export interface TokenAccount {
|
||||
pubkey: PublicKey
|
||||
account: AccountInfo<Buffer> | null
|
||||
effectiveMint: PublicKey
|
||||
}
|
||||
|
||||
export interface Trade extends Event {
|
||||
side: string
|
||||
price: number
|
||||
feeCost: number
|
||||
size: number
|
||||
}
|
||||
|
||||
interface BalancesBase {
|
||||
key: string
|
||||
symbol: string
|
||||
wallet?: number | null | undefined
|
||||
orders?: number | null | undefined
|
||||
openOrders?: OpenOrders | null | undefined
|
||||
unsettled?: number | null | undefined
|
||||
}
|
||||
|
||||
export interface Balances extends BalancesBase {
|
||||
market?: Market | null | undefined
|
||||
deposits?: I80F48 | null | undefined
|
||||
borrows?: I80F48 | null | undefined
|
||||
net?: I80F48 | null | undefined
|
||||
value?: I80F48 | null | undefined
|
||||
depositRate?: I80F48 | null | undefined
|
||||
borrowRate?: I80F48 | null | undefined
|
||||
decimals?: number | null | undefined
|
||||
}
|
||||
|
||||
export interface OpenOrdersBalances extends BalancesBase {
|
||||
market?: string | null | undefined
|
||||
baseCurrencyAccount:
|
||||
| { pubkey: PublicKey; account: AccountInfo<Buffer> }
|
||||
| null
|
||||
| undefined
|
||||
quoteCurrencyAccount:
|
||||
| { pubkey: PublicKey; account: AccountInfo<Buffer> }
|
||||
| null
|
||||
| undefined
|
||||
}
|
||||
|
||||
export interface EndpointInfo {
|
||||
name: string
|
||||
url: string
|
||||
websocket: string
|
||||
custom: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* {tokenMint: preferred token account's base58 encoded public key}
|
||||
*/
|
||||
export interface SelectedTokenAccounts {
|
||||
[tokenMint: string]: string
|
||||
}
|
||||
|
||||
export interface ChartTradeType {
|
||||
market: string
|
||||
size: number
|
||||
price: number
|
||||
orderId: string
|
||||
time: number
|
||||
side: string
|
||||
feeCost: number
|
||||
marketAddress: string
|
||||
}
|
||||
|
||||
export interface FeeRates {
|
||||
taker: number
|
||||
maker: number
|
||||
}
|
||||
|
||||
// Type declaration for the margin accounts for the mango group
|
||||
export type mangoTokenAccounts = {
|
||||
mango_group: string
|
||||
accounts: TokenAccount[]
|
||||
}
|
||||
|
||||
// Token infos
|
||||
export interface KnownToken {
|
||||
tokenSymbol: string
|
||||
tokenName: string
|
||||
icon?: string
|
||||
mintAddress: string
|
||||
}
|
||||
|
||||
export const DEFAULT_PUBLIC_KEY = new PublicKey(
|
||||
'11111111111111111111111111111111'
|
||||
)
|
||||
|
||||
export interface PerpTriggerOrder {
|
||||
orderId: number
|
||||
marketIndex: number
|
||||
orderType: 'limit' | 'ioc' | 'postOnly' | 'market'
|
||||
side: 'buy' | 'sell'
|
||||
price: number
|
||||
size: number
|
||||
triggerCondition: 'above' | 'below'
|
||||
triggerPrice: number
|
||||
}
|
||||
|
||||
export type StringPublicKey = string
|
||||
|
||||
export interface PromiseFulfilledResult<T> {
|
||||
status: 'fulfilled'
|
||||
value: T
|
||||
}
|
||||
|
||||
export interface PromiseRejectedResult {
|
||||
status: 'rejected'
|
||||
reason: any
|
||||
}
|
||||
|
||||
export type PromiseSettledResult<T> =
|
||||
| PromiseFulfilledResult<T>
|
||||
| PromiseRejectedResult
|
|
@ -0,0 +1,661 @@
|
|||
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
Version 3, 19 November 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU Affero General Public License is a free, copyleft license for
|
||||
software and other kinds of works, specifically designed to ensure
|
||||
cooperation with the community in the case of network server software.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
our General Public Licenses are intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
Developers that use our General Public Licenses protect your rights
|
||||
with two steps: (1) assert copyright on the software, and (2) offer
|
||||
you this License which gives you legal permission to copy, distribute
|
||||
and/or modify the software.
|
||||
|
||||
A secondary benefit of defending all users' freedom is that
|
||||
improvements made in alternate versions of the program, if they
|
||||
receive widespread use, become available for other developers to
|
||||
incorporate. Many developers of free software are heartened and
|
||||
encouraged by the resulting cooperation. However, in the case of
|
||||
software used on network servers, this result may fail to come about.
|
||||
The GNU General Public License permits making a modified version and
|
||||
letting the public access it on a server without ever releasing its
|
||||
source code to the public.
|
||||
|
||||
The GNU Affero General Public License is designed specifically to
|
||||
ensure that, in such cases, the modified source code becomes available
|
||||
to the community. It requires the operator of a network server to
|
||||
provide the source code of the modified version running there to the
|
||||
users of that server. Therefore, public use of a modified version, on
|
||||
a publicly accessible server, gives the public access to the source
|
||||
code of the modified version.
|
||||
|
||||
An older license, called the Affero General Public License and
|
||||
published by Affero, was designed to accomplish similar goals. This is
|
||||
a different license, not a version of the Affero GPL, but Affero has
|
||||
released a new version of the Affero GPL which permits relicensing under
|
||||
this license.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, if you modify the
|
||||
Program, your modified version must prominently offer all users
|
||||
interacting with it remotely through a computer network (if your version
|
||||
supports such interaction) an opportunity to receive the Corresponding
|
||||
Source of your version by providing access to the Corresponding Source
|
||||
from a network server at no charge, through some standard or customary
|
||||
means of facilitating copying of software. This Corresponding Source
|
||||
shall include the Corresponding Source for any work covered by version 3
|
||||
of the GNU General Public License that is incorporated pursuant to the
|
||||
following paragraph.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the work with which it is combined will remain governed by version
|
||||
3 of the GNU General Public License.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU Affero General Public License from time to time. Such new versions
|
||||
will be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU Affero General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU Affero General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU Affero General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If your software can interact with users remotely through a computer
|
||||
network, you should also make sure that it provides a way for users to
|
||||
get its source. For example, if your program is a web application, its
|
||||
interface could display a "Source" link that leads users to an archive
|
||||
of the code. There are many ways you could offer source, and different
|
||||
solutions will be better for different programs; see section 13 for the
|
||||
specific requirements.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU AGPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
|
@ -0,0 +1,24 @@
|
|||
# Mango Reimbursement UI V3
|
||||
|
||||
Uses:
|
||||
|
||||
- [Typescript](https://www.typescriptlang.org/)
|
||||
- Linting with [ESLint](https://eslint.org/)
|
||||
- Formatting with [Prettier](https://prettier.io/)
|
||||
- Linting, typechecking and formatting on by default using [`husky`](https://github.com/typicode/husky) for commit hooks
|
||||
- Testing with [Jest](https://jestjs.io/) and [`react-testing-library`](https://testing-library.com/docs/react-testing-library/intro)
|
||||
|
||||
## Running the UI locally
|
||||
|
||||
1. Install Node.js and npm (https://nodejs.org/en/download/), and Git (https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
|
||||
2. Open a new terminal window (if running Windows use Git Bash) and run `npm install -g yarn`
|
||||
3. Run `git clone https://github.com/blockworks-foundation/mango-v3-reimbursement-ui.git && cd mango-v3-reimbursement-ui` to get the UI source code
|
||||
4. Run `yarn install` to install dependencies
|
||||
5. Run `yarn dev` to start the webserver
|
||||
6. Navigate to `http://localhost:3000` in your browser
|
||||
|
||||
<!-- ## Branches
|
||||
|
||||
- `production` is deployed to trade.mango.markets and app.mango.markets
|
||||
- `main` is deployed to alpha.mango.markets
|
||||
- `devnet` is deployed to devnet.mango.markets -->
|
|
@ -0,0 +1,395 @@
|
|||
import {
|
||||
I80F48,
|
||||
nativeI80F48ToUi,
|
||||
nativeToUi,
|
||||
QUOTE_INDEX,
|
||||
ZERO_BN,
|
||||
ZERO_I80F48,
|
||||
} from '@blockworks-foundation/mango-client'
|
||||
import { useCallback, useState } from 'react'
|
||||
import {
|
||||
BellIcon,
|
||||
ExclamationIcon,
|
||||
ExternalLinkIcon,
|
||||
} from '@heroicons/react/solid'
|
||||
import useMangoStore, { MNGO_INDEX } from '../stores/useMangoStore'
|
||||
import { abbreviateAddress, formatUsdValue, usdFormatter } from '../utils'
|
||||
import { notify } from '../utils/notifications'
|
||||
import { IconButton, LinkButton } from './Button'
|
||||
import { ElementTitle } from './styles'
|
||||
import Tooltip from './Tooltip'
|
||||
import DepositModal from './DepositModal'
|
||||
import WithdrawModal from './WithdrawModal'
|
||||
import Button from './Button'
|
||||
import { DataLoader } from './MarketPosition'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from './TradePageGrid'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import useMangoAccount from '../hooks/useMangoAccount'
|
||||
import Loading from './Loading'
|
||||
import CreateAlertModal from './CreateAlertModal'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
import { useRouter } from 'next/router'
|
||||
import HealthHeart from './HealthHeart'
|
||||
|
||||
const I80F48_100 = I80F48.fromString('100')
|
||||
|
||||
export default function AccountInfo() {
|
||||
const { t } = useTranslation('common')
|
||||
const { publicKey, wallet, connected } = useWallet()
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
|
||||
const { mangoAccount, initialLoad } = useMangoAccount()
|
||||
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
const { width } = useViewport()
|
||||
const isMobile = width ? width < breakpoints.sm : false
|
||||
const [redeeming, setRedeeming] = useState(false)
|
||||
|
||||
const [showDepositModal, setShowDepositModal] = useState(false)
|
||||
const [showWithdrawModal, setShowWithdrawModal] = useState(false)
|
||||
const [showAlertsModal, setShowAlertsModal] = useState(false)
|
||||
const router = useRouter()
|
||||
const { pubkey } = router.query
|
||||
|
||||
const canWithdraw =
|
||||
mangoAccount?.owner && publicKey
|
||||
? mangoAccount?.owner?.equals(publicKey)
|
||||
: false
|
||||
|
||||
const handleCloseDeposit = useCallback(() => {
|
||||
setShowDepositModal(false)
|
||||
}, [])
|
||||
|
||||
const handleCloseWithdraw = useCallback(() => {
|
||||
setShowWithdrawModal(false)
|
||||
}, [])
|
||||
|
||||
const handleCloseAlerts = useCallback(() => {
|
||||
setShowAlertsModal(false)
|
||||
}, [])
|
||||
|
||||
const equity =
|
||||
mangoAccount && mangoGroup && mangoCache
|
||||
? mangoAccount.computeValue(mangoGroup, mangoCache)
|
||||
: ZERO_I80F48
|
||||
|
||||
const mngoAccrued = mangoAccount
|
||||
? mangoAccount.perpAccounts.reduce((acc, perpAcct) => {
|
||||
return perpAcct.mngoAccrued.add(acc)
|
||||
}, ZERO_BN)
|
||||
: ZERO_BN
|
||||
|
||||
const handleRedeemMngo = async () => {
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
const mngoNodeBank =
|
||||
mangoGroup?.rootBankAccounts?.[MNGO_INDEX]?.nodeBankAccounts[0]
|
||||
|
||||
if (!mngoNodeBank || !mangoAccount || !wallet) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
setRedeeming(true)
|
||||
const txids = await mangoClient.redeemAllMngo(
|
||||
mangoGroup,
|
||||
mangoAccount,
|
||||
wallet.adapter,
|
||||
mangoGroup.tokens[MNGO_INDEX].rootBank,
|
||||
mngoNodeBank.publicKey,
|
||||
mngoNodeBank.vault
|
||||
)
|
||||
if (txids) {
|
||||
for (const txid of txids) {
|
||||
notify({
|
||||
title: t('redeem-success'),
|
||||
description: '',
|
||||
txid,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
notify({
|
||||
title: t('redeem-failure'),
|
||||
description: t('transaction-failed'),
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
notify({
|
||||
title: t('redeem-failure'),
|
||||
description: e.message,
|
||||
txid: e.txid,
|
||||
type: 'error',
|
||||
})
|
||||
} finally {
|
||||
actions.reloadMangoAccount()
|
||||
setRedeeming(false)
|
||||
}
|
||||
}
|
||||
|
||||
const maintHealthRatio =
|
||||
mangoAccount && mangoGroup && mangoCache
|
||||
? mangoAccount.getHealthRatio(mangoGroup, mangoCache, 'Maint')
|
||||
: I80F48_100
|
||||
|
||||
const maintHealth =
|
||||
mangoAccount && mangoGroup && mangoCache
|
||||
? mangoAccount.getHealth(mangoGroup, mangoCache, 'Maint')
|
||||
: I80F48_100
|
||||
const initHealth =
|
||||
mangoAccount && mangoGroup && mangoCache
|
||||
? mangoAccount.getHealth(mangoGroup, mangoCache, 'Init')
|
||||
: I80F48_100
|
||||
|
||||
const liquidationPrice =
|
||||
mangoGroup && mangoAccount && marketConfig && mangoGroup && mangoCache
|
||||
? mangoAccount.getLiquidationPrice(
|
||||
mangoGroup,
|
||||
mangoCache,
|
||||
marketConfig.marketIndex
|
||||
)
|
||||
: undefined
|
||||
|
||||
return (
|
||||
<>
|
||||
<div
|
||||
className={
|
||||
!connected && !isMobile && !pubkey ? 'blur-sm filter' : undefined
|
||||
}
|
||||
id="account-details-tip"
|
||||
>
|
||||
{!isMobile ? (
|
||||
mangoAccount ? (
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="h-8 w-8" />
|
||||
<ElementTitle>
|
||||
<Tooltip
|
||||
content={
|
||||
mangoAccount ? (
|
||||
<div>
|
||||
{t('init-health')}: {initHealth.toFixed(4)}
|
||||
<br />
|
||||
{t('maint-health')}: {maintHealth.toFixed(4)}
|
||||
</div>
|
||||
) : (
|
||||
''
|
||||
)
|
||||
}
|
||||
>
|
||||
{t('account')}
|
||||
</Tooltip>
|
||||
</ElementTitle>
|
||||
<IconButton onClick={() => setShowAlertsModal(true)}>
|
||||
<BellIcon className={`h-4 w-4`} />
|
||||
</IconButton>
|
||||
</div>
|
||||
) : (
|
||||
<ElementTitle>{t('account')}</ElementTitle>
|
||||
)
|
||||
) : null}
|
||||
<div>
|
||||
{mangoAccount ? (
|
||||
<div className="-mt-2 mb-2 flex justify-center text-xs">
|
||||
<a
|
||||
className="flex items-center text-th-fgd-4 hover:text-th-primary"
|
||||
href={`https://explorer.solana.com/address/${mangoAccount?.publicKey}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
{abbreviateAddress(mangoAccount.publicKey, 6)}
|
||||
<ExternalLinkIcon className={`ml-1.5 h-4 w-4`} />
|
||||
</a>
|
||||
</div>
|
||||
) : null}
|
||||
<div>
|
||||
<div className="flex justify-between pb-2">
|
||||
<div className="font-normal leading-4 text-th-fgd-3">
|
||||
{t('value')}
|
||||
</div>
|
||||
<div className="text-th-fgd-1">
|
||||
{initialLoad ? <DataLoader /> : formatUsdValue(+equity)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex justify-between pb-2">
|
||||
<Tooltip
|
||||
content={
|
||||
<div>
|
||||
{t('tooltip-account-liquidated')}{' '}
|
||||
<a
|
||||
href="https://docs.mango.markets/mango/health-overview"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
{t('learn-more')}
|
||||
</a>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<div className="default-transition cursor-help border-b border-dashed border-th-fgd-3 border-opacity-20 font-normal leading-4 text-th-fgd-3 hover:border-th-bkg-2">
|
||||
{t('health')}
|
||||
</div>
|
||||
</Tooltip>
|
||||
<div className="flex items-center space-x-2">
|
||||
<HealthHeart size={24} health={Number(maintHealthRatio)} />
|
||||
<div className="text-th-fgd-1">
|
||||
{maintHealthRatio.gt(I80F48_100)
|
||||
? '>100'
|
||||
: maintHealthRatio.toFixed(2)}
|
||||
%
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex justify-between pb-2">
|
||||
<div className="font-normal leading-4 text-th-fgd-3">
|
||||
{t('leverage')}
|
||||
</div>
|
||||
<div className="text-th-fgd-1">
|
||||
{initialLoad ? (
|
||||
<DataLoader />
|
||||
) : mangoAccount && mangoGroup && mangoCache ? (
|
||||
`${mangoAccount
|
||||
.getLeverage(mangoGroup, mangoCache)
|
||||
.toFixed(2)}x`
|
||||
) : (
|
||||
'0.00x'
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className={`flex justify-between pb-2`}>
|
||||
<div className="font-normal leading-4 text-th-fgd-3">
|
||||
{t('collateral-available')}
|
||||
</div>
|
||||
<div className={`text-th-fgd-1`}>
|
||||
{initialLoad ? (
|
||||
<DataLoader />
|
||||
) : mangoAccount && mangoGroup ? (
|
||||
usdFormatter(
|
||||
nativeI80F48ToUi(
|
||||
initHealth,
|
||||
mangoGroup.tokens[QUOTE_INDEX].decimals
|
||||
).toFixed()
|
||||
)
|
||||
) : (
|
||||
'--'
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className={`flex justify-between pb-2`}>
|
||||
<div className="font-normal leading-4 text-th-fgd-3">
|
||||
{marketConfig.name} {t('margin-available')}
|
||||
</div>
|
||||
<div className={`text-th-fgd-1`}>
|
||||
{mangoAccount && mangoGroup && mangoCache
|
||||
? usdFormatter(
|
||||
nativeI80F48ToUi(
|
||||
mangoAccount.getMarketMarginAvailable(
|
||||
mangoGroup,
|
||||
mangoCache,
|
||||
marketConfig.marketIndex,
|
||||
marketConfig.kind
|
||||
),
|
||||
mangoGroup.tokens[QUOTE_INDEX].decimals
|
||||
).toFixed()
|
||||
)
|
||||
: '0.00'}
|
||||
</div>
|
||||
</div>
|
||||
<div className={`flex justify-between pb-2`}>
|
||||
<div className="font-normal leading-4 text-th-fgd-3">
|
||||
{marketConfig.name} {t('estimated-liq-price')}
|
||||
</div>
|
||||
<div className={`text-th-fgd-1`}>
|
||||
{liquidationPrice && liquidationPrice.gt(ZERO_I80F48)
|
||||
? usdFormatter(liquidationPrice)
|
||||
: 'N/A'}
|
||||
</div>
|
||||
</div>
|
||||
<div className={`flex justify-between pb-2`}>
|
||||
<Tooltip
|
||||
content={
|
||||
<div>
|
||||
{t('tooltip-earn-mngo')}{' '}
|
||||
<a
|
||||
href="https://docs.mango.markets/mango-v3/liquidity-incentives"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
{t('learn-more')}
|
||||
</a>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<div className="default-transition cursor-help border-b border-dashed border-th-fgd-3 border-opacity-20 font-normal leading-4 text-th-fgd-3 hover:border-th-bkg-2">
|
||||
{t('mngo-rewards')}
|
||||
</div>
|
||||
</Tooltip>
|
||||
<div className={`flex items-center text-th-fgd-1`}>
|
||||
{initialLoad ? (
|
||||
<DataLoader />
|
||||
) : mangoGroup ? (
|
||||
nativeToUi(
|
||||
mngoAccrued.toNumber(),
|
||||
mangoGroup.tokens[MNGO_INDEX].decimals
|
||||
).toLocaleString(undefined, {
|
||||
minimumSignificantDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
})
|
||||
) : (
|
||||
0
|
||||
)}
|
||||
{redeeming ? (
|
||||
<Loading className="ml-2" />
|
||||
) : (
|
||||
<LinkButton
|
||||
onClick={handleRedeemMngo}
|
||||
className="ml-2 text-xs text-th-primary disabled:cursor-not-allowed disabled:opacity-60 disabled:hover:underline"
|
||||
disabled={mngoAccrued.eq(ZERO_BN)}
|
||||
>
|
||||
{t('claim')}
|
||||
</LinkButton>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{mangoAccount && mangoAccount.beingLiquidated ? (
|
||||
<div className="flex items-center justify-center pt-0.5 text-xs">
|
||||
<ExclamationIcon className="mr-1.5 h-5 w-5 flex-shrink-0 text-th-red" />
|
||||
<span className="text-th-red">{t('being-liquidated')}</span>
|
||||
</div>
|
||||
) : null}
|
||||
<div className={`grid grid-cols-2 grid-rows-1 gap-4 pt-2 sm:pt-2`}>
|
||||
<Button
|
||||
onClick={() => setShowDepositModal(true)}
|
||||
className="w-full"
|
||||
disabled={!connected}
|
||||
>
|
||||
<span>{t('deposit')}</span>
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => setShowWithdrawModal(true)}
|
||||
className="w-full"
|
||||
disabled={!connected || !mangoAccount || !canWithdraw}
|
||||
>
|
||||
<span>{t('withdraw')}</span>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{showDepositModal && (
|
||||
<DepositModal isOpen={showDepositModal} onClose={handleCloseDeposit} />
|
||||
)}
|
||||
{showWithdrawModal && (
|
||||
<WithdrawModal
|
||||
isOpen={showWithdrawModal}
|
||||
onClose={handleCloseWithdraw}
|
||||
/>
|
||||
)}
|
||||
|
||||
{showAlertsModal && (
|
||||
<CreateAlertModal
|
||||
isOpen={showAlertsModal}
|
||||
onClose={handleCloseAlerts}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
import React, { useCallback, useEffect, useState } from 'react'
|
||||
import IntroTips, { SHOW_TOUR_KEY } from '../components/IntroTips'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from '../components/TradePageGrid'
|
||||
import { mangoAccountSelector } from '../stores/selectors'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
import AccountsModal from 'components/AccountsModal'
|
||||
import useMangoStore from 'stores/useMangoStore'
|
||||
import useLocalStorageState from '../hooks/useLocalStorageState'
|
||||
|
||||
const DISMISS_CREATE_ACCOUNT_KEY = 'show-create-account'
|
||||
|
||||
const AccountIntro = () => {
|
||||
const mangoAccount = useMangoStore(mangoAccountSelector)
|
||||
const [showTour] = useLocalStorageState(SHOW_TOUR_KEY, false)
|
||||
const [showCreateAccount, setShowCreateAccount] = useState(false)
|
||||
const { width } = useViewport()
|
||||
const [dismissCreateAccount, setDismissCreateAccount] = useLocalStorageState(
|
||||
DISMISS_CREATE_ACCOUNT_KEY,
|
||||
false
|
||||
)
|
||||
const { connected } = useWallet()
|
||||
|
||||
const hideTips = width ? width < breakpoints.md : false
|
||||
|
||||
useEffect(() => {
|
||||
if (connected && !mangoAccount && !dismissCreateAccount) {
|
||||
setShowCreateAccount(true)
|
||||
}
|
||||
}, [connected, mangoAccount])
|
||||
|
||||
const handleCloseCreateAccount = useCallback(() => {
|
||||
setShowCreateAccount(false)
|
||||
setDismissCreateAccount(true)
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<>
|
||||
{showTour && !hideTips ? (
|
||||
<IntroTips connected={connected} mangoAccount={mangoAccount} />
|
||||
) : null}
|
||||
{showCreateAccount ? (
|
||||
<AccountsModal
|
||||
isOpen={showCreateAccount}
|
||||
onClose={() => handleCloseCreateAccount()}
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default AccountIntro
|
|
@ -0,0 +1,116 @@
|
|||
import { FunctionComponent, useState } from 'react'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import {
|
||||
ExclamationCircleIcon,
|
||||
InformationCircleIcon,
|
||||
} from '@heroicons/react/solid'
|
||||
import Input, { Label } from './Input'
|
||||
import Button from './Button'
|
||||
import Modal from './Modal'
|
||||
import { ElementTitle } from './styles'
|
||||
import Tooltip from './Tooltip'
|
||||
import { notify } from '../utils/notifications'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
|
||||
interface AccountNameModalProps {
|
||||
accountName?: string
|
||||
isOpen: boolean
|
||||
onClose?: (x?) => void
|
||||
}
|
||||
|
||||
const AccountNameModal: FunctionComponent<AccountNameModalProps> = ({
|
||||
accountName,
|
||||
isOpen,
|
||||
onClose,
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const { wallet } = useWallet()
|
||||
const [name, setName] = useState(accountName || '')
|
||||
const [invalidNameMessage, setInvalidNameMessage] = useState('')
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
|
||||
const submitName = async () => {
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
if (!wallet || !mangoAccount || !mangoGroup) return
|
||||
try {
|
||||
const txid = await mangoClient.addMangoAccountInfo(
|
||||
mangoGroup,
|
||||
mangoAccount,
|
||||
wallet?.adapter,
|
||||
name
|
||||
)
|
||||
actions.fetchAllMangoAccounts(wallet)
|
||||
await actions.reloadMangoAccount()
|
||||
onClose?.()
|
||||
notify({
|
||||
title: t('name-updated'),
|
||||
txid,
|
||||
})
|
||||
} catch (err) {
|
||||
console.warn('Error setting account name:', err)
|
||||
notify({
|
||||
title: t('name-error'),
|
||||
description: `${err}`,
|
||||
txid: err.txid,
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const validateNameInput = () => {
|
||||
if (name.length >= 33) {
|
||||
setInvalidNameMessage(t('character-limit'))
|
||||
}
|
||||
if (name.length === 0) {
|
||||
setInvalidNameMessage(t('enter-name'))
|
||||
}
|
||||
}
|
||||
|
||||
const onChangeNameInput = (name) => {
|
||||
setName(name)
|
||||
if (invalidNameMessage) {
|
||||
setInvalidNameMessage('')
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal onClose={onClose} isOpen={isOpen}>
|
||||
<Modal.Header>
|
||||
<ElementTitle noMarginBottom>{t('name-your-account')}</ElementTitle>
|
||||
<p className="flex items-center justify-center">
|
||||
{t('edit-nickname')}
|
||||
<Tooltip content={t('tooltip-name-onchain')}>
|
||||
<InformationCircleIcon className="ml-2 h-5 w-5 text-th-fgd-4" />
|
||||
</Tooltip>
|
||||
</p>
|
||||
</Modal.Header>
|
||||
<Label>{t('account-name')}</Label>
|
||||
<Input
|
||||
type="text"
|
||||
error={!!invalidNameMessage}
|
||||
placeholder="e.g. Calypso"
|
||||
value={name}
|
||||
onBlur={validateNameInput}
|
||||
onChange={(e) => onChangeNameInput(e.target.value)}
|
||||
/>
|
||||
{invalidNameMessage ? (
|
||||
<div className="flex items-center pt-1.5 text-th-red">
|
||||
<ExclamationCircleIcon className="mr-1.5 h-4 w-4" />
|
||||
{invalidNameMessage}
|
||||
</div>
|
||||
) : null}
|
||||
<Button
|
||||
onClick={() => submitName()}
|
||||
disabled={name.length >= 33}
|
||||
className="mt-6 w-full"
|
||||
>
|
||||
{t('save-name')}
|
||||
</Button>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default AccountNameModal
|
|
@ -0,0 +1,121 @@
|
|||
import { useTranslation } from 'next-i18next'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import useMangoAccount from '../hooks/useMangoAccount'
|
||||
import {
|
||||
I80F48,
|
||||
nativeI80F48ToUi,
|
||||
QUOTE_INDEX,
|
||||
ZERO_I80F48,
|
||||
} from '@blockworks-foundation/mango-client'
|
||||
import { abbreviateAddress, formatUsdValue, usdFormatter } from 'utils'
|
||||
import { DataLoader } from './MarketPosition'
|
||||
|
||||
const AccountOverviewPopover = ({
|
||||
collapsed,
|
||||
health,
|
||||
}: {
|
||||
collapsed: boolean
|
||||
health: I80F48
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
|
||||
const { mangoAccount, initialLoad } = useMangoAccount()
|
||||
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
|
||||
|
||||
const I80F48_100 = I80F48.fromString('100')
|
||||
|
||||
const initHealth =
|
||||
mangoAccount && mangoGroup && mangoCache
|
||||
? mangoAccount.getHealth(mangoGroup, mangoCache, 'Init')
|
||||
: I80F48_100
|
||||
|
||||
const equity =
|
||||
mangoAccount && mangoGroup && mangoCache
|
||||
? mangoAccount.computeValue(mangoGroup, mangoCache)
|
||||
: ZERO_I80F48
|
||||
|
||||
return (
|
||||
<>
|
||||
{mangoAccount ? (
|
||||
<div className={`w-full ${!collapsed ? 'px-2' : ''}`}>
|
||||
{collapsed ? (
|
||||
<div className="pb-2">
|
||||
<p className="mb-0 text-xs text-th-fgd-3">{t('account')}</p>
|
||||
<p className="mb-0 font-bold text-th-fgd-1">
|
||||
{abbreviateAddress(mangoAccount.publicKey)}
|
||||
</p>
|
||||
</div>
|
||||
) : null}
|
||||
<div className="pb-2">
|
||||
<p className="mb-0 text-xs leading-4">{t('value')}</p>
|
||||
<p className="mb-0 font-bold text-th-fgd-1">
|
||||
{initialLoad ? <DataLoader /> : formatUsdValue(+equity)}
|
||||
</p>
|
||||
</div>
|
||||
<div className="pb-2">
|
||||
<p className="mb-0 text-xs leading-4">{t('health')}</p>
|
||||
<p className="mb-0 font-bold text-th-fgd-1">
|
||||
{health.gt(I80F48_100) ? '>100' : health.toFixed(2)}%
|
||||
</p>
|
||||
</div>
|
||||
<div className="pb-2">
|
||||
<p className="mb-0 text-xs leading-4">{t('leverage')}</p>
|
||||
<p className="mb-0 font-bold text-th-fgd-1">
|
||||
{initialLoad ? (
|
||||
<DataLoader />
|
||||
) : mangoAccount && mangoGroup && mangoCache ? (
|
||||
`${mangoAccount
|
||||
.getLeverage(mangoGroup, mangoCache)
|
||||
.toFixed(2)}x`
|
||||
) : (
|
||||
'0.00x'
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
<div className="pb-2">
|
||||
<p className="mb-0 text-xs leading-4">
|
||||
{t('collateral-available')}
|
||||
</p>
|
||||
<p className="mb-0 font-bold text-th-fgd-1">
|
||||
{initialLoad ? (
|
||||
<DataLoader />
|
||||
) : mangoAccount && mangoGroup ? (
|
||||
usdFormatter(
|
||||
nativeI80F48ToUi(
|
||||
initHealth,
|
||||
mangoGroup.tokens[QUOTE_INDEX].decimals
|
||||
).toFixed()
|
||||
)
|
||||
) : (
|
||||
'--'
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<p className="mb-0 text-xs leading-4">
|
||||
{marketConfig.name} {t('margin-available')}
|
||||
</p>
|
||||
<p className="mb-0 font-bold text-th-fgd-1">
|
||||
{mangoAccount && mangoGroup && mangoCache
|
||||
? usdFormatter(
|
||||
nativeI80F48ToUi(
|
||||
mangoAccount.getMarketMarginAvailable(
|
||||
mangoGroup,
|
||||
mangoCache,
|
||||
marketConfig.marketIndex,
|
||||
marketConfig.kind
|
||||
),
|
||||
mangoGroup.tokens[QUOTE_INDEX].decimals
|
||||
).toFixed()
|
||||
)
|
||||
: '0.00'}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default AccountOverviewPopover
|
|
@ -0,0 +1,193 @@
|
|||
import { useMemo, useState } from 'react'
|
||||
import { Listbox } from '@headlessui/react'
|
||||
import { ChevronDownIcon } from '@heroicons/react/solid'
|
||||
import { abbreviateAddress } from '../utils'
|
||||
import useMangoStore, { WalletToken } from '../stores/useMangoStore'
|
||||
import { RefreshClockwiseIcon } from './icons'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { LinkButton } from './Button'
|
||||
import { Label } from './Input'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
|
||||
type AccountSelectProps = {
|
||||
accounts: WalletToken[]
|
||||
selectedAccount: WalletToken
|
||||
onSelectAccount: (WalletToken) => any
|
||||
hideAddress?: boolean
|
||||
}
|
||||
|
||||
const AccountSelect = ({
|
||||
accounts,
|
||||
selectedAccount,
|
||||
onSelectAccount,
|
||||
hideAddress = false,
|
||||
}: AccountSelectProps) => {
|
||||
const { wallet } = useWallet()
|
||||
const { t } = useTranslation('common')
|
||||
const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
|
||||
const tokenSymbols = useMemo(
|
||||
() => groupConfig?.tokens.map((t) => t.symbol),
|
||||
[groupConfig]
|
||||
)
|
||||
const missingTokenSymbols = useMemo(() => {
|
||||
const symbolsForAccounts = accounts.map((a) => a.config.symbol)
|
||||
return tokenSymbols?.filter((sym) => !symbolsForAccounts.includes(sym))
|
||||
}, [accounts, tokenSymbols])
|
||||
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const handleChange = (value: string) => {
|
||||
const newAccount = accounts.find(
|
||||
(a) => a.account.publicKey.toBase58() === value
|
||||
)
|
||||
onSelectAccount(newAccount)
|
||||
}
|
||||
|
||||
const handleRefreshBalances = async () => {
|
||||
if (!wallet) return
|
||||
setLoading(true)
|
||||
await actions.fetchWalletTokens(wallet)
|
||||
setLoading(false)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={`relative inline-block w-full`}>
|
||||
<div className="flex justify-between">
|
||||
<Label>{t('asset')}</Label>
|
||||
{missingTokenSymbols && missingTokenSymbols.length > 0 ? (
|
||||
<LinkButton className="mb-1.5 ml-2" onClick={handleRefreshBalances}>
|
||||
<div className="flex items-center">
|
||||
<RefreshClockwiseIcon
|
||||
className={`mr-1 h-4 w-4 ${loading ? 'animate-spin' : ''}`}
|
||||
/>
|
||||
{t('refresh')}
|
||||
</div>
|
||||
</LinkButton>
|
||||
) : null}
|
||||
</div>
|
||||
<Listbox
|
||||
value={selectedAccount?.account.publicKey.toBase58()}
|
||||
onChange={handleChange}
|
||||
>
|
||||
{({ open }) => (
|
||||
<div className="relative">
|
||||
<div className="flex items-center">
|
||||
<Listbox.Button
|
||||
className={`default-transition w-full rounded-md border border-th-bkg-4 bg-th-bkg-1 p-2 font-normal hover:border-th-fgd-4 focus:border-th-fgd-4 focus:outline-none`}
|
||||
>
|
||||
<div
|
||||
className={`flex items-center justify-between text-th-fgd-1`}
|
||||
>
|
||||
{selectedAccount ? (
|
||||
<div className={`flex flex-grow items-center`}>
|
||||
<img
|
||||
alt=""
|
||||
width="20"
|
||||
height="20"
|
||||
src={`/assets/icons/${selectedAccount.config.symbol.toLowerCase()}.svg`}
|
||||
className={`mr-2`}
|
||||
/>
|
||||
<div className="text-left">
|
||||
{selectedAccount.config.symbol}
|
||||
{!hideAddress ? (
|
||||
<div className="text-xs text-th-fgd-4">
|
||||
{abbreviateAddress(
|
||||
selectedAccount.account.publicKey
|
||||
)}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
<div className={`ml-4 flex-grow text-right`}>
|
||||
{selectedAccount.uiBalance}
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
t('select-asset')
|
||||
)}
|
||||
<ChevronDownIcon
|
||||
className={`default-transition ml-2 h-5 w-5 text-th-fgd-1 ${
|
||||
open ? 'rotate-180 transform' : 'rotate-360 transform'
|
||||
}`}
|
||||
/>
|
||||
</div>
|
||||
</Listbox.Button>
|
||||
</div>
|
||||
<Listbox.Options
|
||||
className={`thin-scroll absolute right-0 top-14 z-20 max-h-60 w-full overflow-auto rounded-md bg-th-bkg-2 p-1`}
|
||||
>
|
||||
{accounts.map((account) => {
|
||||
const symbolForAccount = account.config.symbol
|
||||
|
||||
return (
|
||||
<Listbox.Option
|
||||
className="mb-0"
|
||||
disabled={account.uiBalance === 0}
|
||||
key={account?.account.publicKey.toBase58()}
|
||||
value={account?.account.publicKey.toBase58()}
|
||||
>
|
||||
{({ disabled, selected }) => (
|
||||
<div
|
||||
className={`default-transition rounded p-2 text-th-fgd-1 ${
|
||||
selected && `text-th-primary`
|
||||
} ${
|
||||
disabled
|
||||
? 'text-th-fgd-1 opacity-50 hover:cursor-not-allowed hover:text-th-fgd-1'
|
||||
: 'hover:cursor-pointer hover:bg-th-bkg-3 hover:text-th-primary'
|
||||
}`}
|
||||
>
|
||||
<div className={`flex items-center`}>
|
||||
<img
|
||||
alt=""
|
||||
width="16"
|
||||
height="16"
|
||||
src={`/assets/icons/${symbolForAccount.toLowerCase()}.svg`}
|
||||
className="mr-2"
|
||||
/>
|
||||
<div className={`flex-grow text-left`}>
|
||||
{symbolForAccount}
|
||||
{!hideAddress ? (
|
||||
<div className="text-xs text-th-fgd-4">
|
||||
{abbreviateAddress(account.account.publicKey)}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
{!hideAddress ? (
|
||||
<div className={`text-sm`}>
|
||||
{account.uiBalance} {symbolForAccount}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</Listbox.Option>
|
||||
)
|
||||
})}
|
||||
{missingTokenSymbols?.map((token) => (
|
||||
<Listbox.Option disabled key={token} value={token}>
|
||||
<div
|
||||
className={`px-2 py-1 opacity-50 hover:cursor-not-allowed`}
|
||||
>
|
||||
<div className={`flex items-center text-th-fgd-1`}>
|
||||
<img
|
||||
alt=""
|
||||
width="16"
|
||||
height="16"
|
||||
src={`/assets/icons/${token.toLowerCase()}.svg`}
|
||||
className="mr-2"
|
||||
/>
|
||||
<div className={`flex-grow text-left`}>{token}</div>
|
||||
<div className={`text-xs`}>{t('no-wallet')}</div>
|
||||
</div>
|
||||
</div>
|
||||
</Listbox.Option>
|
||||
))}
|
||||
</Listbox.Options>
|
||||
</div>
|
||||
)}
|
||||
</Listbox>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default AccountSelect
|
|
@ -0,0 +1,215 @@
|
|||
import React, { FunctionComponent, useEffect, useState } from "react";
|
||||
import { RadioGroup } from "@headlessui/react";
|
||||
import {
|
||||
CheckCircleIcon,
|
||||
HeartIcon,
|
||||
PlusCircleIcon,
|
||||
UsersIcon,
|
||||
} from "@heroicons/react/solid";
|
||||
import useMangoStore from "../stores/useMangoStore";
|
||||
import { MangoAccount, MangoGroup } from "@blockworks-foundation/mango-client";
|
||||
import { abbreviateAddress, formatUsdValue } from "../utils";
|
||||
import useLocalStorageState from "../hooks/useLocalStorageState";
|
||||
import Modal from "./Modal";
|
||||
import { ElementTitle } from "./styles";
|
||||
import Button, { LinkButton } from "./Button";
|
||||
import NewAccount from "./NewAccount";
|
||||
import { useTranslation } from "next-i18next";
|
||||
import Tooltip from "./Tooltip";
|
||||
import { useWallet } from "@solana/wallet-adapter-react";
|
||||
|
||||
export const LAST_ACCOUNT_KEY = "lastAccountViewed-3.0";
|
||||
|
||||
interface AccountsModalProps {
|
||||
onClose: () => void;
|
||||
isOpen: boolean;
|
||||
}
|
||||
|
||||
const AccountsModal: FunctionComponent<AccountsModalProps> = ({
|
||||
isOpen,
|
||||
onClose,
|
||||
}) => {
|
||||
const { t } = useTranslation(["common", "delegate"]);
|
||||
const { publicKey } = useWallet();
|
||||
const [showNewAccountForm, setShowNewAccountForm] = useState(false);
|
||||
const [newAccPublicKey, setNewAccPublicKey] = useState(null);
|
||||
const mangoAccounts = useMangoStore((s) => s.mangoAccounts);
|
||||
const selectedMangoAccount = useMangoStore(
|
||||
(s) => s.selectedMangoAccount.current
|
||||
);
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current);
|
||||
const setMangoStore = useMangoStore((s) => s.set);
|
||||
const actions = useMangoStore((s) => s.actions);
|
||||
const [, setLastAccountViewed] = useLocalStorageState(LAST_ACCOUNT_KEY);
|
||||
|
||||
const handleMangoAccountChange = (mangoAccount: MangoAccount) => {
|
||||
setLastAccountViewed(mangoAccount.publicKey.toString());
|
||||
setMangoStore((state) => {
|
||||
state.selectedMangoAccount.current = mangoAccount;
|
||||
});
|
||||
|
||||
actions.fetchTradeHistory();
|
||||
onClose();
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (newAccPublicKey) {
|
||||
setMangoStore((state) => {
|
||||
state.selectedMangoAccount.current =
|
||||
mangoAccounts.find(
|
||||
(ma) => ma.publicKey.toString() === newAccPublicKey
|
||||
) ?? null;
|
||||
});
|
||||
}
|
||||
}, [mangoAccounts, newAccPublicKey]);
|
||||
|
||||
const handleNewAccountCreation = (newAccPublicKey) => {
|
||||
if (newAccPublicKey) {
|
||||
setNewAccPublicKey(newAccPublicKey);
|
||||
}
|
||||
setShowNewAccountForm(false);
|
||||
};
|
||||
|
||||
const handleShowNewAccountForm = () => {
|
||||
setNewAccPublicKey(null);
|
||||
setShowNewAccountForm(true);
|
||||
};
|
||||
|
||||
return (
|
||||
<Modal isOpen={isOpen} onClose={onClose}>
|
||||
{mangoAccounts.length > 0 ? (
|
||||
!showNewAccountForm ? (
|
||||
<>
|
||||
<Modal.Header>
|
||||
<ElementTitle noMarginBottom>{t("mango-accounts")}</ElementTitle>
|
||||
</Modal.Header>
|
||||
<div className="flex items-center justify-between pb-3 text-th-fgd-1">
|
||||
<p className="mb-0">
|
||||
{mangoAccounts.length > 1
|
||||
? t("select-account")
|
||||
: t("your-account")}
|
||||
</p>
|
||||
</div>
|
||||
<RadioGroup
|
||||
value={selectedMangoAccount}
|
||||
onChange={(acc) => {
|
||||
if (acc) {
|
||||
handleMangoAccountChange(acc);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<RadioGroup.Label className="sr-only">
|
||||
{t("select-account")}
|
||||
</RadioGroup.Label>
|
||||
<div className="space-y-2">
|
||||
{mangoAccounts.map((account) => (
|
||||
<RadioGroup.Option
|
||||
key={account.publicKey.toString()}
|
||||
value={account}
|
||||
className={({ checked }) =>
|
||||
`border ${
|
||||
checked ? "border-th-primary" : "border-th-fgd-4"
|
||||
} default-transition mb-2 flex cursor-pointer items-center rounded-md p-3 text-th-fgd-1 hover:border-th-primary`
|
||||
}
|
||||
>
|
||||
{({ checked }) => (
|
||||
<>
|
||||
<div className="flex w-full items-center justify-between">
|
||||
<div className="flex items-center">
|
||||
<CheckCircleIcon
|
||||
className={`mr-2 h-5 w-5 ${
|
||||
checked ? "text-th-primary" : "text-th-fgd-4"
|
||||
}`}
|
||||
/>
|
||||
<div className="text-sm">
|
||||
<RadioGroup.Label className="flex cursor-pointer items-center text-th-fgd-1">
|
||||
<div>
|
||||
<div className="flex items-center pb-0.5">
|
||||
{account?.name ||
|
||||
abbreviateAddress(account.publicKey)}
|
||||
{publicKey &&
|
||||
!account?.owner.equals(publicKey) ? (
|
||||
<Tooltip
|
||||
content={t(
|
||||
"delegate:delegated-account"
|
||||
)}
|
||||
>
|
||||
<UsersIcon className="ml-1.5 h-3 w-3" />
|
||||
</Tooltip>
|
||||
) : (
|
||||
""
|
||||
)}
|
||||
</div>
|
||||
{mangoGroup && (
|
||||
<div className="mt-0.5 text-xs text-th-fgd-3">
|
||||
<AccountInfo
|
||||
mangoGroup={mangoGroup}
|
||||
mangoAccount={account}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</RadioGroup.Label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</RadioGroup.Option>
|
||||
))}
|
||||
</div>
|
||||
</RadioGroup>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<NewAccount onAccountCreation={handleNewAccountCreation} />
|
||||
<LinkButton
|
||||
className="mt-4 flex w-full justify-center"
|
||||
onClick={() => setShowNewAccountForm(false)}
|
||||
>
|
||||
{t("cancel")}
|
||||
</LinkButton>
|
||||
</>
|
||||
)
|
||||
) : (
|
||||
<NewAccount onAccountCreation={handleNewAccountCreation} />
|
||||
)}
|
||||
</Modal>
|
||||
);
|
||||
};
|
||||
|
||||
const AccountInfo = ({
|
||||
mangoGroup,
|
||||
mangoAccount,
|
||||
}: {
|
||||
mangoGroup: MangoGroup;
|
||||
mangoAccount: MangoAccount;
|
||||
}) => {
|
||||
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache);
|
||||
if (!mangoCache) {
|
||||
return null;
|
||||
}
|
||||
const accountEquity = mangoAccount.computeValue(mangoGroup, mangoCache);
|
||||
const health = mangoAccount.getHealthRatio(mangoGroup, mangoCache, "Maint");
|
||||
|
||||
return (
|
||||
<div className="flex items-center text-xs text-th-fgd-3">
|
||||
{formatUsdValue(accountEquity.toNumber())}
|
||||
<span className="px-1.5 text-th-fgd-4">|</span>
|
||||
<span
|
||||
className={`flex items-center ${
|
||||
Number(health) < 15
|
||||
? "text-th-red"
|
||||
: Number(health) < 30
|
||||
? "text-th-orange"
|
||||
: "text-th-green"
|
||||
}`}
|
||||
>
|
||||
<HeartIcon className="mr-0.5 h-4 w-4" />
|
||||
{Number(health) > 100 ? ">100" : health.toFixed(2)}%
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default React.memo(AccountsModal);
|
|
@ -0,0 +1,156 @@
|
|||
import React, { useState } from 'react'
|
||||
import { CheckCircleIcon } from '@heroicons/react/solid'
|
||||
import Modal from './Modal'
|
||||
import Button from './Button'
|
||||
import useLocalStorageState from '../hooks/useLocalStorageState'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import Checkbox from './Checkbox'
|
||||
// import { SHOW_TOUR_KEY } from './IntroTips'
|
||||
// import { useViewport } from '../hooks/useViewport'
|
||||
// import { breakpoints } from './TradePageGrid'
|
||||
import { useRouter } from 'next/router'
|
||||
import { LANGS } from './SettingsModal'
|
||||
import { RadioGroup } from '@headlessui/react'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
export const ALPHA_MODAL_KEY = 'mangoAlphaAccepted-3.06'
|
||||
|
||||
const AlphaModal = ({
|
||||
isOpen,
|
||||
onClose,
|
||||
}: {
|
||||
isOpen: boolean
|
||||
onClose?: (x) => void
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const [acceptRisks, setAcceptRisks] = useState(false)
|
||||
const [, setAlphaAccepted] = useLocalStorageState(ALPHA_MODAL_KEY, false)
|
||||
// const [, setShowTips] = useLocalStorageState(SHOW_TOUR_KEY, false)
|
||||
const [savedLanguage, setSavedLanguage] = useLocalStorageState('language', '')
|
||||
const [language, setLanguage] = useState('en')
|
||||
const router = useRouter()
|
||||
const { pathname, asPath, query } = router
|
||||
// const { width } = useViewport()
|
||||
// const hideTips = width ? width < breakpoints.md : false
|
||||
|
||||
const handleLanguageSelect = () => {
|
||||
setSavedLanguage(language)
|
||||
document.cookie = `NEXT_LOCALE=${language}; max-age=31536000; path=/`
|
||||
router.push({ pathname, query }, asPath, { locale: language })
|
||||
dayjs.locale(savedLanguage == 'zh_tw' ? 'zh-tw' : savedLanguage)
|
||||
}
|
||||
|
||||
const handleGetStarted = () => {
|
||||
setAlphaAccepted(true)
|
||||
}
|
||||
|
||||
// const handleTakeTour = () => {
|
||||
// setAlphaAccepted(true)
|
||||
// setShowTips(true)
|
||||
// }
|
||||
|
||||
return (
|
||||
<Modal isOpen={isOpen} onClose={onClose} hideClose>
|
||||
<Modal.Header>
|
||||
<div className="flex flex-col items-center">
|
||||
<div className="flex items-center justify-center space-x-8">
|
||||
<img
|
||||
className={`h-12 w-auto`}
|
||||
src="/assets/icons/logo.svg"
|
||||
alt="next"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</Modal.Header>
|
||||
<h1 className="relative m-auto mb-4 w-max">
|
||||
{t('v3-welcome')}
|
||||
<span className="absolute -right-8 -top-1 w-max rounded-full bg-th-primary px-1.5 py-0.5 text-xs font-bold text-black">
|
||||
V3
|
||||
</span>
|
||||
</h1>
|
||||
{savedLanguage ? (
|
||||
<>
|
||||
<div className="space-y-3 rounded-md bg-th-bkg-3 p-4">
|
||||
<div className="flex items-center text-th-fgd-1">
|
||||
<CheckCircleIcon className="mr-2 h-5 w-5 flex-shrink-0 text-th-green" />
|
||||
{t('intro-feature-1')}
|
||||
</div>
|
||||
<div className="flex items-center text-th-fgd-1">
|
||||
<CheckCircleIcon className="mr-2 h-5 w-5 flex-shrink-0 text-th-green" />
|
||||
{t('intro-feature-2')}
|
||||
</div>
|
||||
<div className="flex items-center text-th-fgd-1">
|
||||
<CheckCircleIcon className="mr-2 h-5 w-5 flex-shrink-0 text-th-green" />
|
||||
{t('intro-feature-3')}
|
||||
</div>
|
||||
<div className="flex items-center text-th-fgd-1">
|
||||
<CheckCircleIcon className="mr-2 h-5 w-5 flex-shrink-0 text-th-green" />
|
||||
{t('intro-feature-4')}
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-4 px-6 text-center text-th-fgd-3">
|
||||
{t('v3-unaudited')}
|
||||
</div>
|
||||
<div className="mt-4 rounded-md border border-th-fgd-4 p-3">
|
||||
<Checkbox
|
||||
checked={acceptRisks}
|
||||
onChange={(e) => setAcceptRisks(e.target.checked)}
|
||||
>
|
||||
{t('accept-terms')}
|
||||
</Checkbox>
|
||||
</div>
|
||||
<div className={`mt-6 flex justify-center space-x-4`}>
|
||||
<Button
|
||||
className="w-40"
|
||||
disabled={!acceptRisks}
|
||||
onClick={handleGetStarted}
|
||||
>
|
||||
{t('get-started')}
|
||||
</Button>
|
||||
{/* {!hideTips ? (
|
||||
<Button
|
||||
className="w-40"
|
||||
disabled={!acceptRisks}
|
||||
onClick={handleTakeTour}
|
||||
>
|
||||
{t('show-tips')}
|
||||
</Button>
|
||||
) : null} */}
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<div className="flex flex-col items-center pt-2">
|
||||
<RadioGroup
|
||||
className="w-full"
|
||||
value={language}
|
||||
onChange={setLanguage}
|
||||
>
|
||||
{LANGS.map((l) => (
|
||||
<RadioGroup.Option key={l.locale} value={l.locale}>
|
||||
{({ checked }) => (
|
||||
<div
|
||||
className={`border ${
|
||||
checked ? 'border-th-primary' : 'border-th-fgd-4'
|
||||
} default-transition mb-2 flex cursor-pointer items-center rounded-md p-3 text-th-fgd-1 hover:border-th-primary`}
|
||||
>
|
||||
<CheckCircleIcon
|
||||
className={`mr-2 h-5 w-5 ${
|
||||
checked ? 'text-th-primary' : 'text-th-fgd-4'
|
||||
}`}
|
||||
/>
|
||||
<span>{t(l.name.toLowerCase())}</span>
|
||||
</div>
|
||||
)}
|
||||
</RadioGroup.Option>
|
||||
))}
|
||||
</RadioGroup>
|
||||
<Button className="mt-4" onClick={() => handleLanguageSelect()}>
|
||||
Save
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(AlphaModal)
|
|
@ -0,0 +1,654 @@
|
|||
import { useCallback, useMemo, useState } from 'react'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import Button from '../components/Button'
|
||||
import { notify } from '../utils/notifications'
|
||||
import {
|
||||
ExclamationIcon,
|
||||
// InformationCircleIcon
|
||||
} from '@heroicons/react/solid'
|
||||
import { Market } from '@project-serum/serum'
|
||||
import {
|
||||
// getMarketIndexBySymbol,
|
||||
getTokenBySymbol,
|
||||
// ZERO_I80F48,
|
||||
} from '@blockworks-foundation/mango-client'
|
||||
import Loading from './Loading'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from './TradePageGrid'
|
||||
import {
|
||||
floorToDecimal,
|
||||
formatUsdValue,
|
||||
getPrecisionDigits,
|
||||
// usdFormatter,
|
||||
} from '../utils'
|
||||
import { ExpandableRow, Table, Td, Th, TrBody, TrHead } from './TableElements'
|
||||
import DepositModal from './DepositModal'
|
||||
import WithdrawModal from './WithdrawModal'
|
||||
import MobileTableHeader from './mobile/MobileTableHeader'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { TransactionSignature } from '@solana/web3.js'
|
||||
import Link from 'next/link'
|
||||
import { useRouter } from 'next/router'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
// import Tooltip from './Tooltip'
|
||||
|
||||
const BalancesTable = ({
|
||||
showZeroBalances = false,
|
||||
showDepositWithdraw = false,
|
||||
clickToPopulateTradeForm = false,
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const [showDepositModal, setShowDepositModal] = useState(false)
|
||||
const [showWithdrawModal, setShowWithdrawModal] = useState(false)
|
||||
const [actionSymbol, setActionSymbol] = useState('')
|
||||
const spotBalances = useMangoStore((s) => s.selectedMangoAccount.spotBalances)
|
||||
|
||||
const balances = useMemo(() => {
|
||||
return spotBalances?.length > 0
|
||||
? spotBalances
|
||||
.filter((bal) => {
|
||||
return (
|
||||
showZeroBalances ||
|
||||
(bal.deposits && +bal.deposits > 0) ||
|
||||
(bal.borrows && +bal.borrows > 0) ||
|
||||
(bal.orders && bal.orders > 0) ||
|
||||
(bal.unsettled && bal.unsettled > 0)
|
||||
)
|
||||
})
|
||||
.sort((a, b) => {
|
||||
const aV = a.value ? Math.abs(+a.value) : 0
|
||||
const bV = b.value ? Math.abs(+b.value) : 0
|
||||
return bV - aV
|
||||
})
|
||||
: []
|
||||
}, [spotBalances])
|
||||
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
|
||||
// const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
|
||||
const selectedMarket = useMangoStore((s) => s.selectedMarket.current)
|
||||
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
|
||||
const setMangoStore = useMangoStore((s) => s.set)
|
||||
const price = useMangoStore((s) => s.tradeForm.price)
|
||||
const mangoGroupCache = useMangoStore((s) => s.selectedMangoGroup.cache)
|
||||
const { width } = useViewport()
|
||||
const [submitting, setSubmitting] = useState(false)
|
||||
const isMobile = width ? width < breakpoints.md : false
|
||||
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
|
||||
const { wallet, publicKey } = useWallet()
|
||||
const canWithdraw = publicKey ? mangoAccount?.owner.equals(publicKey) : true
|
||||
const { asPath } = useRouter()
|
||||
|
||||
const handleSizeClick = (size, symbol) => {
|
||||
const minOrderSize = selectedMarket?.minOrderSize
|
||||
const sizePrecisionDigits = minOrderSize
|
||||
? getPrecisionDigits(minOrderSize)
|
||||
: null
|
||||
const marketIndex = marketConfig.marketIndex
|
||||
|
||||
const priceOrDefault = price
|
||||
? price
|
||||
: mangoGroup && mangoGroupCache
|
||||
? mangoGroup.getPriceUi(marketIndex, mangoGroupCache)
|
||||
: null
|
||||
|
||||
if (!priceOrDefault || !sizePrecisionDigits || !minOrderSize) {
|
||||
return
|
||||
}
|
||||
|
||||
let roundedSize, side
|
||||
if (symbol === 'USDC') {
|
||||
roundedSize = parseFloat(
|
||||
(
|
||||
Math.abs(size) / priceOrDefault +
|
||||
(size < 0 ? minOrderSize / 2 : -minOrderSize / 2)
|
||||
) // round up so neg USDC gets cleared
|
||||
.toFixed(sizePrecisionDigits)
|
||||
)
|
||||
side = size > 0 ? 'buy' : 'sell'
|
||||
} else {
|
||||
roundedSize = parseFloat(
|
||||
(
|
||||
Math.abs(size) + (size < 0 ? minOrderSize / 2 : -minOrderSize / 2)
|
||||
).toFixed(sizePrecisionDigits)
|
||||
)
|
||||
side = size > 0 ? 'sell' : 'buy'
|
||||
}
|
||||
const quoteSize = parseFloat((roundedSize * priceOrDefault).toFixed(2))
|
||||
setMangoStore((state) => {
|
||||
state.tradeForm.baseSize = roundedSize
|
||||
state.tradeForm.quoteSize = quoteSize
|
||||
state.tradeForm.side = side
|
||||
})
|
||||
}
|
||||
const handleOpenDepositModal = useCallback((symbol) => {
|
||||
setActionSymbol(symbol)
|
||||
setShowDepositModal(true)
|
||||
}, [])
|
||||
|
||||
const handleOpenWithdrawModal = useCallback((symbol) => {
|
||||
setActionSymbol(symbol)
|
||||
setShowWithdrawModal(true)
|
||||
}, [])
|
||||
|
||||
async function handleSettleAll() {
|
||||
const markets = useMangoStore.getState().selectedMangoGroup.markets
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
|
||||
try {
|
||||
setSubmitting(true)
|
||||
const spotMarkets = Object.values(markets).filter(
|
||||
(mkt) => mkt instanceof Market
|
||||
) as Market[]
|
||||
|
||||
if (!mangoGroup || !mangoAccount || !wallet) {
|
||||
return
|
||||
}
|
||||
|
||||
const txids: TransactionSignature[] | undefined =
|
||||
await mangoClient.settleAll(
|
||||
mangoGroup,
|
||||
mangoAccount,
|
||||
spotMarkets,
|
||||
wallet?.adapter
|
||||
)
|
||||
if (txids) {
|
||||
for (const txid of txids) {
|
||||
notify({ title: t('settle-success'), txid })
|
||||
}
|
||||
} else {
|
||||
notify({
|
||||
title: t('settle-error'),
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Error settling all:', e)
|
||||
if (e.message === 'No unsettled funds') {
|
||||
notify({
|
||||
title: t('no-unsettled'),
|
||||
type: 'error',
|
||||
})
|
||||
} else {
|
||||
notify({
|
||||
title: t('settle-error'),
|
||||
description: e.message,
|
||||
txid: e.txid,
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
} finally {
|
||||
actions.reloadOrders()
|
||||
// actions.reloadMangoAccount()
|
||||
setSubmitting(false)
|
||||
}
|
||||
}
|
||||
|
||||
const unsettledBalances = spotBalances.filter(
|
||||
(bal) => bal.unsettled && bal.unsettled > 0
|
||||
)
|
||||
|
||||
const trimDecimals = useCallback((num: string) => {
|
||||
if (parseFloat(num) === 0) {
|
||||
return '0'
|
||||
}
|
||||
// Trim the decimals depending on the length of the whole number
|
||||
const splitNum = num.split('.')
|
||||
if (splitNum.length > 1) {
|
||||
const wholeNum = splitNum[0]
|
||||
const decimals = splitNum[1]
|
||||
if (wholeNum.length > 8) {
|
||||
return `${wholeNum}.${decimals.substring(0, 2)}`
|
||||
} else if (wholeNum.length > 3) {
|
||||
return `${wholeNum}.${decimals.substring(0, 3)}`
|
||||
}
|
||||
}
|
||||
return num
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<div className={`flex flex-col pb-2 sm:pb-4`}>
|
||||
{unsettledBalances.length > 0 ? (
|
||||
<div className="mb-6 rounded-lg border border-th-bkg-4 p-4 sm:p-6">
|
||||
<div className="flex items-center justify-between pb-4">
|
||||
<div className="flex items-center">
|
||||
<ExclamationIcon className="mr-1.5 mt-0.5 h-5 w-5 flex-shrink-0 text-th-primary" />
|
||||
<h3>{t('unsettled-balances')}</h3>
|
||||
</div>
|
||||
<Button
|
||||
className="h-8 whitespace-nowrap pt-0 pb-0 pl-3 pr-3 text-xs"
|
||||
onClick={handleSettleAll}
|
||||
>
|
||||
{submitting ? <Loading /> : t('settle-all')}
|
||||
</Button>
|
||||
</div>
|
||||
<div className="grid grid-flow-row grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
|
||||
{unsettledBalances.map((bal) => {
|
||||
const tokenConfig = getTokenBySymbol(mangoGroupConfig, bal.symbol)
|
||||
return (
|
||||
<div
|
||||
className="col-span-1 flex items-center justify-between rounded-full bg-th-bkg-2 px-5 py-3"
|
||||
key={bal.symbol}
|
||||
>
|
||||
<div className="flex space-x-2">
|
||||
<div className="flex items-center">
|
||||
<img
|
||||
alt=""
|
||||
width="20"
|
||||
height="20"
|
||||
src={`/assets/icons/${bal.symbol.toLowerCase()}.svg`}
|
||||
className={`mr-3`}
|
||||
/>
|
||||
<div>
|
||||
<p className="mb-0 text-xs text-th-fgd-1">
|
||||
{bal.symbol}
|
||||
</p>
|
||||
{bal.unsettled ? (
|
||||
<div className="font-bold text-th-green">
|
||||
{floorToDecimal(
|
||||
bal.unsettled,
|
||||
tokenConfig.decimals
|
||||
)}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
<div className={`md:overflow-x-auto`}>
|
||||
<div className={`inline-block min-w-full align-middle`}>
|
||||
{balances.length > 0 ? (
|
||||
!isMobile ? (
|
||||
<Table>
|
||||
<thead>
|
||||
<TrHead>
|
||||
<Th>{''}</Th>
|
||||
<Th>{t('deposits')}</Th>
|
||||
<Th>{t('borrows')}</Th>
|
||||
<Th>{t('in-orders')}</Th>
|
||||
<Th>{t('unsettled')}</Th>
|
||||
<Th>{t('net-balance')}</Th>
|
||||
<Th>{t('value')}</Th>
|
||||
{/* <Th>
|
||||
<Tooltip content={t('tooltip-estimated-liq-price')}>
|
||||
<span className="flex items-center">
|
||||
{t('estimated-liq-price')}
|
||||
<InformationCircleIcon className="ml-1 h-4 w-4 flex-shrink-0 text-th-fgd-4" />
|
||||
</span>
|
||||
</Tooltip>
|
||||
</Th> */}
|
||||
<Th>
|
||||
{t('deposit')}
|
||||
<span className="mx-1 text-th-fgd-4">|</span>
|
||||
{t('borrow')} (APR)
|
||||
</Th>
|
||||
</TrHead>
|
||||
</thead>
|
||||
<tbody>
|
||||
{balances.map((balance, index) => {
|
||||
if (
|
||||
!balance ||
|
||||
typeof balance.decimals !== 'number' ||
|
||||
!balance.deposits ||
|
||||
!balance.borrows ||
|
||||
!balance.net ||
|
||||
!balance.value ||
|
||||
!balance.borrowRate ||
|
||||
!balance.depositRate
|
||||
) {
|
||||
return null
|
||||
}
|
||||
|
||||
// const marketIndex = getMarketIndexBySymbol(
|
||||
// mangoGroupConfig,
|
||||
// balance.symbol
|
||||
// )
|
||||
|
||||
// const liquidationPrice =
|
||||
// mangoGroup &&
|
||||
// mangoAccount &&
|
||||
// marketIndex &&
|
||||
// mangoGroup &&
|
||||
// mangoCache
|
||||
// ? mangoAccount.getLiquidationPrice(
|
||||
// mangoGroup,
|
||||
// mangoCache,
|
||||
// marketIndex
|
||||
// )
|
||||
// : undefined
|
||||
|
||||
return (
|
||||
<TrBody key={`${balance.symbol}${index}`}>
|
||||
<Td>
|
||||
<div className="flex items-center">
|
||||
<img
|
||||
alt=""
|
||||
width="20"
|
||||
height="20"
|
||||
src={`/assets/icons/${balance.symbol.toLowerCase()}.svg`}
|
||||
className={`mr-2.5`}
|
||||
/>
|
||||
|
||||
{balance.symbol === 'USDC' ||
|
||||
decodeURIComponent(asPath).includes(
|
||||
`${balance.symbol}/USDC`
|
||||
) ? (
|
||||
<span>{balance.symbol}</span>
|
||||
) : (
|
||||
<Link
|
||||
href={{
|
||||
pathname: '/',
|
||||
query: { name: `${balance.symbol}/USDC` },
|
||||
}}
|
||||
shallow={true}
|
||||
>
|
||||
<a className="text-th-fgd-1 underline hover:text-th-fgd-1 hover:no-underline">
|
||||
{balance.symbol}
|
||||
</a>
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
</Td>
|
||||
<Td>
|
||||
{trimDecimals(
|
||||
balance.deposits.toFormat(balance.decimals)
|
||||
)}
|
||||
</Td>
|
||||
<Td>
|
||||
{trimDecimals(
|
||||
balance.borrows.toFormat(balance.decimals)
|
||||
)}
|
||||
</Td>
|
||||
<Td>
|
||||
{balance.orders?.toLocaleString(undefined, {
|
||||
maximumFractionDigits: balance.decimals,
|
||||
})}
|
||||
</Td>
|
||||
<Td>
|
||||
{balance.unsettled?.toLocaleString(undefined, {
|
||||
maximumFractionDigits: balance.decimals,
|
||||
})}
|
||||
</Td>
|
||||
<Td>
|
||||
{marketConfig.kind === 'spot' &&
|
||||
marketConfig.name.includes(balance.symbol) &&
|
||||
selectedMarket &&
|
||||
clickToPopulateTradeForm ? (
|
||||
<span
|
||||
className={
|
||||
balance.net.toNumber() != 0
|
||||
? 'cursor-pointer underline hover:no-underline'
|
||||
: ''
|
||||
}
|
||||
onClick={() =>
|
||||
handleSizeClick(balance.net, balance.symbol)
|
||||
}
|
||||
>
|
||||
{trimDecimals(
|
||||
balance.net.toFormat(balance.decimals)
|
||||
)}
|
||||
</span>
|
||||
) : (
|
||||
trimDecimals(balance.net.toFormat(balance.decimals))
|
||||
)}
|
||||
</Td>
|
||||
<Td>{formatUsdValue(balance.value.toNumber())}</Td>
|
||||
{/* <Td>
|
||||
{liquidationPrice && liquidationPrice.gt(ZERO_I80F48)
|
||||
? usdFormatter(liquidationPrice)
|
||||
: '–'}
|
||||
</Td> */}
|
||||
<Td>
|
||||
<span className="text-th-green">
|
||||
{balance.depositRate.toFixed(2)}%
|
||||
</span>
|
||||
<span className="mx-1 text-th-fgd-4">|</span>
|
||||
<span className="text-th-red">
|
||||
{balance.borrowRate.toFixed(2)}%
|
||||
</span>
|
||||
</Td>
|
||||
{showDepositWithdraw ? (
|
||||
<Td>
|
||||
<div className="flex justify-end space-x-2">
|
||||
<Button
|
||||
className="h-8 w-[86px] pt-0 pb-0 pl-3 pr-3 text-xs"
|
||||
onClick={() =>
|
||||
handleOpenDepositModal(balance.symbol)
|
||||
}
|
||||
>
|
||||
{balance.borrows.toNumber() > 0
|
||||
? t('repay')
|
||||
: t('deposit')}
|
||||
</Button>
|
||||
<Button
|
||||
className="h-8 w-[86px] pt-0 pb-0 pl-3 pr-3 text-xs"
|
||||
onClick={() =>
|
||||
handleOpenWithdrawModal(balance.symbol)
|
||||
}
|
||||
disabled={!canWithdraw}
|
||||
primary={false}
|
||||
>
|
||||
{t('withdraw')}
|
||||
</Button>
|
||||
</div>
|
||||
</Td>
|
||||
) : null}
|
||||
</TrBody>
|
||||
)
|
||||
})}
|
||||
</tbody>
|
||||
{showDepositModal && (
|
||||
<DepositModal
|
||||
isOpen={showDepositModal}
|
||||
onClose={() => setShowDepositModal(false)}
|
||||
tokenSymbol={actionSymbol}
|
||||
// repayAmount={
|
||||
// balance.borrows.toNumber() > 0
|
||||
// ? balance.borrows.toFixed()
|
||||
// : ''
|
||||
// }
|
||||
/>
|
||||
)}
|
||||
{showWithdrawModal && (
|
||||
<WithdrawModal
|
||||
isOpen={showWithdrawModal}
|
||||
onClose={() => setShowWithdrawModal(false)}
|
||||
tokenSymbol={actionSymbol}
|
||||
/>
|
||||
)}
|
||||
</Table>
|
||||
) : (
|
||||
<div className="border-b border-th-bkg-3">
|
||||
<MobileTableHeader
|
||||
colOneHeader={t('asset')}
|
||||
colTwoHeader={t('net-balance')}
|
||||
/>
|
||||
{balances.map((balance, index) => {
|
||||
if (
|
||||
!balance ||
|
||||
typeof balance.decimals !== 'number' ||
|
||||
typeof balance.orders !== 'number' ||
|
||||
typeof balance.unsettled !== 'number' ||
|
||||
!balance.deposits ||
|
||||
!balance.borrows ||
|
||||
!balance.net ||
|
||||
!balance.value ||
|
||||
!balance.borrowRate ||
|
||||
!balance.depositRate
|
||||
) {
|
||||
return null
|
||||
}
|
||||
// const marketIndex = getMarketIndexBySymbol(
|
||||
// mangoGroupConfig,
|
||||
// balance.symbol
|
||||
// )
|
||||
|
||||
// const liquidationPrice =
|
||||
// mangoGroup &&
|
||||
// mangoAccount &&
|
||||
// marketIndex &&
|
||||
// mangoGroup &&
|
||||
// mangoCache
|
||||
// ? mangoAccount.getLiquidationPrice(
|
||||
// mangoGroup,
|
||||
// mangoCache,
|
||||
// marketIndex
|
||||
// )
|
||||
// : undefined
|
||||
return (
|
||||
<ExpandableRow
|
||||
buttonTemplate={
|
||||
<div className="flex w-full items-center justify-between text-th-fgd-1">
|
||||
<div className="flex items-center text-th-fgd-1">
|
||||
<img
|
||||
alt=""
|
||||
width="20"
|
||||
height="20"
|
||||
src={`/assets/icons/${balance.symbol.toLowerCase()}.svg`}
|
||||
className={`mr-2.5`}
|
||||
/>
|
||||
|
||||
{balance.symbol}
|
||||
</div>
|
||||
<div className="text-right text-th-fgd-1">
|
||||
{balance.net.toFormat(balance.decimals)}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
key={`${balance.symbol}${index}`}
|
||||
panelTemplate={
|
||||
<>
|
||||
<div className="grid grid-flow-row grid-cols-2 gap-4 pb-4">
|
||||
<div className="text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('deposits')}
|
||||
</div>
|
||||
{balance.deposits.toFormat(balance.decimals)}
|
||||
</div>
|
||||
<div className="text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('borrows')}
|
||||
</div>
|
||||
{balance.borrows.toFormat(balance.decimals)}
|
||||
</div>
|
||||
<div className="text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('in-orders')}
|
||||
</div>
|
||||
{balance.orders.toLocaleString(undefined, {
|
||||
maximumFractionDigits: balance.decimals,
|
||||
})}
|
||||
</div>
|
||||
<div className="text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('unsettled')}
|
||||
</div>
|
||||
{balance.unsettled.toLocaleString(undefined, {
|
||||
maximumFractionDigits: balance.decimals,
|
||||
})}
|
||||
</div>
|
||||
<div className="text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('value')}
|
||||
</div>
|
||||
{formatUsdValue(balance.value.toNumber())}
|
||||
</div>
|
||||
{/* <div className="text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
<Tooltip
|
||||
content={t('tooltip-estimated-liq-price')}
|
||||
>
|
||||
<span className="flex items-center">
|
||||
{t('estimated-liq-price')}
|
||||
<InformationCircleIcon className="ml-1 h-4 w-4 flex-shrink-0 text-th-fgd-4" />
|
||||
</span>
|
||||
</Tooltip>
|
||||
</div>
|
||||
{liquidationPrice &&
|
||||
liquidationPrice.gt(ZERO_I80F48)
|
||||
? usdFormatter(liquidationPrice)
|
||||
: '–'}
|
||||
</div> */}
|
||||
<div className="text-left text-th-fgd-4">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
<span>{t('deposit')}</span>
|
||||
<span className="mx-1 text-th-fgd-4">|</span>
|
||||
<span>{`${t('borrow')} (APR)`}</span>
|
||||
</div>
|
||||
<span className="text-th-green">
|
||||
{balance.depositRate.toFixed(2)}%
|
||||
</span>
|
||||
<span className="mx-1 text-th-fgd-4">|</span>
|
||||
<span className="text-th-red">
|
||||
{balance.borrowRate.toFixed(2)}%
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex space-x-4">
|
||||
<Button
|
||||
className="h-8 w-1/2 pt-0 pb-0 pl-3 pr-3 text-xs"
|
||||
onClick={() =>
|
||||
handleOpenDepositModal(balance.symbol)
|
||||
}
|
||||
>
|
||||
{balance.borrows.toNumber() > 0
|
||||
? t('repay')
|
||||
: t('deposit')}
|
||||
</Button>
|
||||
<Button
|
||||
className="h-8 w-1/2 border border-th-fgd-4 bg-transparent pt-0 pb-0 pl-3 pr-3 text-xs"
|
||||
onClick={() =>
|
||||
handleOpenWithdrawModal(balance.symbol)
|
||||
}
|
||||
primary={false}
|
||||
>
|
||||
{t('withdraw')}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{showDepositModal && (
|
||||
<DepositModal
|
||||
isOpen={showDepositModal}
|
||||
onClose={() => setShowDepositModal(false)}
|
||||
tokenSymbol={actionSymbol}
|
||||
repayAmount={
|
||||
balance.borrows.toNumber() > 0
|
||||
? balance.borrows.toFormat(balance.decimals)
|
||||
: ''
|
||||
}
|
||||
/>
|
||||
)}
|
||||
{showWithdrawModal && (
|
||||
<WithdrawModal
|
||||
isOpen={showWithdrawModal}
|
||||
onClose={() => setShowWithdrawModal(false)}
|
||||
tokenSymbol={actionSymbol}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
) : (
|
||||
<div
|
||||
className={`w-full rounded-md border border-th-bkg-3 py-6 text-center text-th-fgd-3`}
|
||||
>
|
||||
{t('no-balances')}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default BalancesTable
|
|
@ -0,0 +1,74 @@
|
|||
import { FunctionComponent } from 'react'
|
||||
|
||||
interface ButtonProps {
|
||||
onClick?: (x?) => void
|
||||
disabled?: boolean
|
||||
className?: string
|
||||
primary?: boolean
|
||||
}
|
||||
|
||||
const Button: FunctionComponent<ButtonProps> = ({
|
||||
children,
|
||||
onClick,
|
||||
disabled = false,
|
||||
className,
|
||||
primary = true,
|
||||
...props
|
||||
}) => {
|
||||
return (
|
||||
<button
|
||||
onClick={onClick}
|
||||
disabled={disabled}
|
||||
className={`whitespace-nowrap rounded-full ${
|
||||
primary ? 'bg-th-bkg-button' : 'border border-th-fgd-4'
|
||||
} px-6 py-2 font-bold text-th-fgd-1 focus:outline-none disabled:cursor-not-allowed disabled:bg-th-bkg-4 disabled:text-th-fgd-4 md:hover:brightness-[1.1] md:disabled:hover:brightness-100 ${className}`}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
export default Button
|
||||
|
||||
export const LinkButton: FunctionComponent<ButtonProps> = ({
|
||||
children,
|
||||
onClick,
|
||||
disabled = false,
|
||||
className,
|
||||
primary,
|
||||
...props
|
||||
}) => {
|
||||
return (
|
||||
<button
|
||||
onClick={onClick}
|
||||
disabled={disabled}
|
||||
className={`border-0 font-bold ${
|
||||
primary ? 'text-th-primary' : 'text-th-fgd-2'
|
||||
} underline focus:outline-none disabled:cursor-not-allowed disabled:underline disabled:opacity-60 md:hover:no-underline md:hover:opacity-60 ${className}`}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
export const IconButton: FunctionComponent<ButtonProps> = ({
|
||||
children,
|
||||
onClick,
|
||||
disabled = false,
|
||||
className,
|
||||
...props
|
||||
}) => {
|
||||
return (
|
||||
<button
|
||||
onClick={onClick}
|
||||
disabled={disabled}
|
||||
className={`${className} flex h-7 w-7 items-center justify-center rounded-full bg-th-bkg-4 text-th-fgd-1 focus:outline-none disabled:cursor-not-allowed disabled:bg-th-bkg-4
|
||||
disabled:text-th-fgd-4 md:hover:text-th-primary md:disabled:hover:text-th-fgd-4`}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</button>
|
||||
)
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
import { FunctionComponent } from 'react'
|
||||
|
||||
interface ButtonGroupProps {
|
||||
activeValue: string
|
||||
className?: string
|
||||
onChange: (x) => void
|
||||
unit?: string
|
||||
values: Array<any>
|
||||
names?: Array<string>
|
||||
}
|
||||
|
||||
const ButtonGroup: FunctionComponent<ButtonGroupProps> = ({
|
||||
activeValue,
|
||||
className,
|
||||
unit,
|
||||
values,
|
||||
onChange,
|
||||
names,
|
||||
}) => {
|
||||
return (
|
||||
<div className="rounded-md bg-th-bkg-3">
|
||||
<div className="relative flex">
|
||||
{activeValue && values.includes(activeValue) ? (
|
||||
<div
|
||||
className={`default-transition absolute left-0 top-0 h-full transform rounded-md bg-th-bkg-4`}
|
||||
style={{
|
||||
transform: `translateX(${
|
||||
values.findIndex((v) => v === activeValue) * 100
|
||||
}%)`,
|
||||
width: `${100 / values.length}%`,
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
{values.map((v, i) => (
|
||||
<button
|
||||
className={`${className} default-transition relative w-1/2 cursor-pointer rounded-md px-2 py-1.5 text-center text-xs font-normal
|
||||
${
|
||||
v === activeValue
|
||||
? `text-th-primary`
|
||||
: `text-th-fgd-2 md:hover:text-th-primary`
|
||||
}
|
||||
`}
|
||||
key={`${v}${i}`}
|
||||
onClick={() => onChange(v)}
|
||||
style={{
|
||||
width: `${100 / values.length}%`,
|
||||
}}
|
||||
>
|
||||
{names ? (unit ? names[i] + unit : names[i]) : unit ? v + unit : v}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ButtonGroup
|
|
@ -0,0 +1,378 @@
|
|||
import { FunctionComponent, useState, ReactNode } from 'react'
|
||||
import { useTheme } from 'next-themes'
|
||||
import dayjs from 'dayjs'
|
||||
import {
|
||||
AreaChart,
|
||||
Area,
|
||||
Cell,
|
||||
XAxis,
|
||||
YAxis,
|
||||
Tooltip,
|
||||
BarChart,
|
||||
Bar,
|
||||
ReferenceLine,
|
||||
} from 'recharts'
|
||||
import useDimensions from 'react-cool-dimensions'
|
||||
import { numberCompactFormatter } from '../utils'
|
||||
|
||||
interface ChartProps {
|
||||
data: any
|
||||
daysRange?: number
|
||||
hideRangeFilters?: boolean
|
||||
title?: string
|
||||
xAxis: string
|
||||
yAxis: string
|
||||
yAxisWidth?: number
|
||||
type: string
|
||||
labelFormat: (x) => ReactNode
|
||||
tickFormat?: (x) => any
|
||||
showAll?: boolean
|
||||
titleValue?: number
|
||||
useMulticoloredBars?: boolean
|
||||
zeroLine?: boolean
|
||||
loading?: boolean
|
||||
}
|
||||
|
||||
const Chart: FunctionComponent<ChartProps> = ({
|
||||
title,
|
||||
xAxis,
|
||||
yAxis,
|
||||
data,
|
||||
daysRange,
|
||||
labelFormat,
|
||||
tickFormat,
|
||||
type,
|
||||
hideRangeFilters,
|
||||
yAxisWidth,
|
||||
showAll = false,
|
||||
titleValue,
|
||||
useMulticoloredBars,
|
||||
zeroLine,
|
||||
loading,
|
||||
}) => {
|
||||
const [mouseData, setMouseData] = useState<string | null>(null)
|
||||
const [daysToShow, setDaysToShow] = useState(daysRange || 30)
|
||||
const { observe, width, height } = useDimensions()
|
||||
const { theme } = useTheme()
|
||||
|
||||
const handleMouseMove = (coords) => {
|
||||
if (coords.activePayload) {
|
||||
setMouseData(coords.activePayload[0].payload)
|
||||
}
|
||||
}
|
||||
|
||||
const handleMouseLeave = () => {
|
||||
setMouseData(null)
|
||||
}
|
||||
|
||||
const handleDaysToShow = (time) => {
|
||||
const startFrom = time
|
||||
? new Date(Date.now() - time * 24 * 60 * 60 * 1000).getTime()
|
||||
: null
|
||||
|
||||
return startFrom
|
||||
? data.filter((d) => new Date(d.time).getTime() > startFrom)
|
||||
: data
|
||||
}
|
||||
|
||||
const formatDateAxis = (date) => {
|
||||
if (daysToShow === 1) {
|
||||
return dayjs(date).format('h:mma')
|
||||
} else {
|
||||
return dayjs(date).format('D MMM')
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="h-52 w-full" ref={observe}>
|
||||
{data.length > 0 ? (
|
||||
<>
|
||||
<div className="flex w-full items-start justify-between pb-6">
|
||||
<div className="pl-2">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">{title}</div>
|
||||
{mouseData ? (
|
||||
<>
|
||||
<div className="pb-1 text-xl font-bold text-th-fgd-1">
|
||||
{labelFormat(mouseData[yAxis])}
|
||||
</div>
|
||||
<div className="text-xs font-normal text-th-fgd-4">
|
||||
{dayjs(mouseData[xAxis]).format('ddd MMM D YYYY, h:mma')}
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<div className="pb-1 text-xl font-bold text-th-fgd-1">
|
||||
{titleValue
|
||||
? labelFormat(titleValue)
|
||||
: labelFormat(data[data.length - 1][yAxis])}
|
||||
</div>
|
||||
<div className="h-4 text-xs font-normal text-th-fgd-4">
|
||||
{titleValue
|
||||
? ''
|
||||
: dayjs(data[data.length - 1][xAxis]).format(
|
||||
'ddd MMM D YYYY, h:mma'
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
{!hideRangeFilters ? (
|
||||
<div className="flex h-5">
|
||||
<button
|
||||
className={`default-transition mx-3 text-xs font-bold text-th-fgd-1 focus:outline-none md:hover:text-th-primary ${
|
||||
daysToShow === 1 && 'text-th-primary'
|
||||
}`}
|
||||
onClick={() => setDaysToShow(1)}
|
||||
>
|
||||
24H
|
||||
</button>
|
||||
<button
|
||||
className={`default-transition mx-3 text-xs font-bold text-th-fgd-1 focus:outline-none md:hover:text-th-primary ${
|
||||
daysToShow === 7 && 'text-th-primary'
|
||||
}`}
|
||||
onClick={() => setDaysToShow(7)}
|
||||
>
|
||||
7D
|
||||
</button>
|
||||
<button
|
||||
className={`default-transition ml-3 text-xs font-bold text-th-fgd-1 focus:outline-none md:hover:text-th-primary ${
|
||||
daysToShow === 30 && 'text-th-primary'
|
||||
}`}
|
||||
onClick={() => setDaysToShow(30)}
|
||||
>
|
||||
30D
|
||||
</button>
|
||||
{showAll ? (
|
||||
<button
|
||||
className={`default-transition ml-3 text-xs font-bold text-th-fgd-1 focus:outline-none md:hover:text-th-primary ${
|
||||
daysToShow === 1000 && 'text-th-primary'
|
||||
}`}
|
||||
onClick={() => setDaysToShow(1000)}
|
||||
>
|
||||
All
|
||||
</button>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
{width > 0 && type === 'area' ? (
|
||||
<AreaChart
|
||||
width={width}
|
||||
height={height}
|
||||
data={data ? handleDaysToShow(daysToShow) : null}
|
||||
onMouseMove={handleMouseMove}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
>
|
||||
<Tooltip
|
||||
cursor={{
|
||||
strokeOpacity: 0,
|
||||
}}
|
||||
content={<></>}
|
||||
/>
|
||||
<defs>
|
||||
<linearGradient id="gradientArea" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0%" stopColor="#ffba24" stopOpacity={1} />
|
||||
<stop offset="100%" stopColor="#ffba24" stopOpacity={0} />
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<Area
|
||||
isAnimationActive={false}
|
||||
type="monotone"
|
||||
dataKey={yAxis}
|
||||
stroke="#ffba24"
|
||||
fill="url(#gradientArea)"
|
||||
/>
|
||||
<XAxis
|
||||
dataKey={xAxis}
|
||||
axisLine={false}
|
||||
hide={data.length > 0 ? false : true}
|
||||
dy={10}
|
||||
minTickGap={20}
|
||||
tick={{
|
||||
fill:
|
||||
theme === 'Light'
|
||||
? 'rgba(0,0,0,0.4)'
|
||||
: 'rgba(255,255,255,0.35)',
|
||||
fontSize: 10,
|
||||
}}
|
||||
tickLine={false}
|
||||
tickFormatter={(v) => formatDateAxis(v)}
|
||||
/>
|
||||
<YAxis
|
||||
dataKey={yAxis}
|
||||
axisLine={false}
|
||||
hide={data.length > 0 ? false : true}
|
||||
dx={-10}
|
||||
domain={['dataMin', 'dataMax']}
|
||||
tick={{
|
||||
fill:
|
||||
theme === 'Light'
|
||||
? 'rgba(0,0,0,0.4)'
|
||||
: 'rgba(255,255,255,0.35)',
|
||||
fontSize: 10,
|
||||
}}
|
||||
tickLine={false}
|
||||
tickFormatter={
|
||||
tickFormat
|
||||
? (v) => tickFormat(v)
|
||||
: (v) => numberCompactFormatter.format(v)
|
||||
}
|
||||
type="number"
|
||||
width={yAxisWidth || 50}
|
||||
/>
|
||||
{zeroLine ? (
|
||||
<ReferenceLine
|
||||
y={0}
|
||||
stroke={
|
||||
theme === 'Light'
|
||||
? 'rgba(0,0,0,0.4)'
|
||||
: 'rgba(255,255,255,0.35)'
|
||||
}
|
||||
strokeDasharray="3 3"
|
||||
/>
|
||||
) : null}
|
||||
</AreaChart>
|
||||
) : null}
|
||||
{width > 0 && type === 'bar' ? (
|
||||
<BarChart
|
||||
width={width}
|
||||
height={height}
|
||||
data={
|
||||
data
|
||||
? hideRangeFilters
|
||||
? data
|
||||
: handleDaysToShow(daysToShow)
|
||||
: null
|
||||
}
|
||||
onMouseMove={handleMouseMove}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
>
|
||||
<Tooltip
|
||||
cursor={{
|
||||
fill: '#fff',
|
||||
opacity: 0.2,
|
||||
}}
|
||||
content={<></>}
|
||||
/>
|
||||
<defs>
|
||||
<linearGradient
|
||||
id="defaultGradientBar"
|
||||
x1="0"
|
||||
y1="0"
|
||||
x2="0"
|
||||
y2="1"
|
||||
>
|
||||
<stop offset="0%" stopColor="#ffba24" stopOpacity={1} />
|
||||
<stop offset="100%" stopColor="#ffba24" stopOpacity={0.5} />
|
||||
</linearGradient>
|
||||
<linearGradient
|
||||
id="greenGradientBar"
|
||||
x1="0"
|
||||
y1="0"
|
||||
x2="0"
|
||||
y2="1"
|
||||
>
|
||||
<stop
|
||||
offset="0%"
|
||||
stopColor={theme === 'Mango' ? '#AFD803' : '#5EBF4D'}
|
||||
stopOpacity={1}
|
||||
/>
|
||||
<stop
|
||||
offset="100%"
|
||||
stopColor={theme === 'Mango' ? '#91B503' : '#4BA53B'}
|
||||
stopOpacity={1}
|
||||
/>
|
||||
</linearGradient>
|
||||
<linearGradient id="redGradientBar" x1="0" y1="1" x2="0" y2="0">
|
||||
<stop
|
||||
offset="0%"
|
||||
stopColor={theme === 'Mango' ? '#F84638' : '#CC2929'}
|
||||
stopOpacity={1}
|
||||
/>
|
||||
<stop
|
||||
offset="100%"
|
||||
stopColor={theme === 'Mango' ? '#EC1809' : '#BB2525'}
|
||||
stopOpacity={1}
|
||||
/>
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<Bar dataKey={yAxis}>
|
||||
{data.map((entry, index) => (
|
||||
<Cell
|
||||
key={`cell-${index}`}
|
||||
fill={
|
||||
useMulticoloredBars
|
||||
? entry[yAxis] > 0
|
||||
? 'url(#greenGradientBar)'
|
||||
: 'url(#redGradientBar)'
|
||||
: 'url(#defaultGradientBar)'
|
||||
}
|
||||
/>
|
||||
))}
|
||||
</Bar>
|
||||
<XAxis
|
||||
dataKey={xAxis}
|
||||
axisLine={false}
|
||||
hide={data.length > 0 ? false : true}
|
||||
dy={10}
|
||||
minTickGap={20}
|
||||
tick={{
|
||||
fill:
|
||||
theme === 'Light'
|
||||
? 'rgba(0,0,0,0.4)'
|
||||
: 'rgba(255,255,255,0.35)',
|
||||
fontSize: 10,
|
||||
}}
|
||||
tickLine={false}
|
||||
tickFormatter={(v) => formatDateAxis(v)}
|
||||
/>
|
||||
<YAxis
|
||||
dataKey={yAxis}
|
||||
interval="preserveStartEnd"
|
||||
axisLine={false}
|
||||
hide={data.length > 0 ? false : true}
|
||||
dx={-10}
|
||||
tick={{
|
||||
fill:
|
||||
theme === 'Light'
|
||||
? 'rgba(0,0,0,0.4)'
|
||||
: 'rgba(255,255,255,0.35)',
|
||||
fontSize: 10,
|
||||
}}
|
||||
tickLine={false}
|
||||
tickFormatter={
|
||||
tickFormat
|
||||
? (v) => tickFormat(v)
|
||||
: (v) => numberCompactFormatter.format(v)
|
||||
}
|
||||
type="number"
|
||||
width={yAxisWidth || 50}
|
||||
/>
|
||||
{zeroLine ? (
|
||||
<ReferenceLine
|
||||
y={0}
|
||||
stroke={
|
||||
theme === 'Light'
|
||||
? 'rgba(0,0,0,0.4)'
|
||||
: 'rgba(255,255,255,0.2)'
|
||||
}
|
||||
/>
|
||||
) : null}
|
||||
</BarChart>
|
||||
) : null}
|
||||
</>
|
||||
) : loading ? (
|
||||
<>
|
||||
<div className="mt-1 h-8 w-48 animate-pulse rounded bg-th-bkg-3" />
|
||||
<div className="mt-1 h-4 w-24 animate-pulse rounded bg-th-bkg-3" />
|
||||
</>
|
||||
) : (
|
||||
<div className="flex h-full w-full items-center justify-center">
|
||||
<p className="mb-0">Chart not available</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Chart
|
|
@ -0,0 +1,57 @@
|
|||
import React from 'react'
|
||||
import { CheckIcon } from '@heroicons/react/solid'
|
||||
|
||||
const Checkbox = ({
|
||||
checked,
|
||||
children,
|
||||
disabled = false,
|
||||
halfState = false,
|
||||
...props
|
||||
}) => (
|
||||
<label className="default-transition flex cursor-pointer items-center text-th-fgd-3 hover:text-th-fgd-2">
|
||||
<input
|
||||
checked={checked}
|
||||
{...props}
|
||||
disabled={disabled}
|
||||
type="checkbox"
|
||||
style={{
|
||||
border: '0',
|
||||
clip: 'rect(0 0 0 0)',
|
||||
clipPath: 'inset(50%)',
|
||||
height: '1px',
|
||||
margin: '-1px',
|
||||
overflow: 'hidden',
|
||||
padding: '0',
|
||||
position: 'absolute',
|
||||
whiteSpace: 'nowrap',
|
||||
width: '1px',
|
||||
}}
|
||||
/>
|
||||
<div
|
||||
className={`${
|
||||
checked && !disabled && !halfState
|
||||
? 'border-th-primary'
|
||||
: 'border-th-fgd-4'
|
||||
} default-transition flex h-4 w-4 flex-shrink-0 cursor-pointer items-center justify-center rounded border`}
|
||||
>
|
||||
{halfState ? (
|
||||
<div className="mb-0.5 font-bold">–</div>
|
||||
) : (
|
||||
<CheckIcon
|
||||
className={`${checked ? 'block' : 'hidden'} h-4 w-4 ${
|
||||
disabled ? 'text-th-fgd-4' : 'text-th-primary'
|
||||
}`}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
<span
|
||||
className={`ml-2 whitespace-nowrap text-xs ${
|
||||
checked && !disabled ? 'text-th-fgd-2' : ''
|
||||
}`}
|
||||
>
|
||||
{children}
|
||||
</span>
|
||||
</label>
|
||||
)
|
||||
|
||||
export default Checkbox
|
|
@ -0,0 +1,277 @@
|
|||
import {
|
||||
FunctionComponent,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
} from 'react'
|
||||
import useMangoStore, { MNGO_INDEX } from '../stores/useMangoStore'
|
||||
import { CheckCircleIcon, ExclamationCircleIcon } from '@heroicons/react/solid'
|
||||
import Button from './Button'
|
||||
import Modal from './Modal'
|
||||
import { ElementTitle } from './styles'
|
||||
import { notify } from '../utils/notifications'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import {
|
||||
getMultipleAccounts,
|
||||
nativeToUi,
|
||||
zeroKey,
|
||||
ZERO_BN,
|
||||
ZERO_I80F48,
|
||||
} from '@blockworks-foundation/mango-client'
|
||||
import { formatUsdValue } from '../utils'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
|
||||
interface CloseAccountModalProps {
|
||||
lamports?: number
|
||||
isOpen: boolean
|
||||
onClose?: (x?) => void
|
||||
}
|
||||
|
||||
const CloseAccountModal: FunctionComponent<CloseAccountModalProps> = ({
|
||||
isOpen,
|
||||
onClose,
|
||||
}) => {
|
||||
const { t } = useTranslation(['common', 'close-account'])
|
||||
const { wallet } = useWallet()
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
|
||||
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
|
||||
const openPositions = useMangoStore(
|
||||
(s) => s.selectedMangoAccount.openPerpPositions
|
||||
)
|
||||
const unsettledPositions = useMangoStore(
|
||||
(s) => s.selectedMangoAccount.unsettledPerpPositions
|
||||
)
|
||||
const [hasBorrows, setHasBorrows] = useState(false)
|
||||
const [hasOpenPositions, setHasOpenPositions] = useState(false)
|
||||
const [totalAccountSOL, setTotalAccountSOL] = useState(0)
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
const connection = useMangoStore((s) => s.connection.current)
|
||||
const openOrders = useMangoStore((s) => s.selectedMangoAccount.openOrders)
|
||||
const setMangoStore = useMangoStore((s) => s.set)
|
||||
const activeAlerts = useMangoStore((s) => s.alerts.activeAlerts)
|
||||
const spotBalances = useMangoStore((s) => s.selectedMangoAccount.spotBalances)
|
||||
|
||||
const unsettledBalances = spotBalances.filter(
|
||||
(bal) => bal.unsettled && bal.unsettled > 0
|
||||
)
|
||||
|
||||
const fetchTotalAccountSOL = useCallback(async () => {
|
||||
if (!mangoAccount) {
|
||||
return
|
||||
}
|
||||
const accountKeys = [
|
||||
mangoAccount.publicKey,
|
||||
...mangoAccount.spotOpenOrders.filter((oo) => !oo.equals(zeroKey)),
|
||||
...(!mangoAccount.advancedOrdersKey.equals(zeroKey)
|
||||
? [mangoAccount.advancedOrdersKey]
|
||||
: []),
|
||||
]
|
||||
const accounts = await getMultipleAccounts(connection, accountKeys)
|
||||
const lamports =
|
||||
accounts.reduce((total, account) => {
|
||||
return total + account.accountInfo.lamports
|
||||
}, 0) * 0.000000001
|
||||
|
||||
setTotalAccountSOL(lamports)
|
||||
}, [mangoAccount])
|
||||
|
||||
useEffect(() => {
|
||||
if (mangoAccount) {
|
||||
if (mangoAccount.borrows.some((b) => b.gt(ZERO_I80F48))) {
|
||||
setHasBorrows(true)
|
||||
}
|
||||
if (openPositions.length || unsettledPositions.length) {
|
||||
setHasOpenPositions(true)
|
||||
}
|
||||
}
|
||||
|
||||
fetchTotalAccountSOL()
|
||||
}, [mangoAccount])
|
||||
|
||||
const mngoAccrued = useMemo(() => {
|
||||
return mangoAccount
|
||||
? mangoAccount.perpAccounts.reduce((acc, perpAcct) => {
|
||||
return perpAcct.mngoAccrued.add(acc)
|
||||
}, ZERO_BN)
|
||||
: ZERO_BN
|
||||
}, [mangoAccount])
|
||||
|
||||
const deleteAlerts = () => {
|
||||
try {
|
||||
for (const alert of activeAlerts) {
|
||||
actions.deleteAlert(alert._id)
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('Error deleting active alerts:', err)
|
||||
}
|
||||
}
|
||||
|
||||
const closeAccount = async () => {
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
|
||||
if (!mangoGroup || !mangoAccount || !mangoCache || !wallet) {
|
||||
return
|
||||
}
|
||||
|
||||
if (activeAlerts.length > 0) {
|
||||
deleteAlerts()
|
||||
}
|
||||
|
||||
try {
|
||||
// Disable prioritization for this function as it gets too large
|
||||
const oldPrioritizationFee = mangoClient.prioritizationFee
|
||||
mangoClient.prioritizationFee = 0
|
||||
const txids = await mangoClient.emptyAndCloseMangoAccount(
|
||||
mangoGroup,
|
||||
mangoAccount,
|
||||
mangoCache,
|
||||
MNGO_INDEX,
|
||||
wallet.adapter
|
||||
)
|
||||
mangoClient.prioritizationFee = oldPrioritizationFee
|
||||
|
||||
await actions.fetchAllMangoAccounts(wallet)
|
||||
const mangoAccounts = useMangoStore.getState().mangoAccounts
|
||||
|
||||
setMangoStore((state) => {
|
||||
state.selectedMangoAccount.current = mangoAccounts.length
|
||||
? mangoAccounts[0]
|
||||
: null
|
||||
})
|
||||
|
||||
onClose?.()
|
||||
if (txids) {
|
||||
for (const txid of txids) {
|
||||
notify({
|
||||
title: t('close-account:transaction-confirmed'),
|
||||
txid,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
notify({
|
||||
title: t('close-account:error-deleting-account'),
|
||||
description: t('transaction-failed'),
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('Error deleting account:', err)
|
||||
notify({
|
||||
title: t('close-account:error-deleting-account'),
|
||||
description: `${err.message}`,
|
||||
txid: err.txid,
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const isDisabled =
|
||||
(openOrders && openOrders.length > 0) ||
|
||||
hasBorrows ||
|
||||
hasOpenPositions ||
|
||||
!!unsettledBalances.length
|
||||
|
||||
return (
|
||||
<Modal onClose={onClose} isOpen={isOpen && mangoAccount !== undefined}>
|
||||
<Modal.Header>
|
||||
<ElementTitle noMarginBottom>
|
||||
{t('close-account:are-you-sure')}
|
||||
</ElementTitle>
|
||||
<p className="mt-1 text-center">
|
||||
{t('close-account:closing-account-will')}
|
||||
</p>
|
||||
</Modal.Header>
|
||||
<div className="overflow-wrap space-y-2 rounded-md bg-th-bkg-4 p-2 sm:p-4">
|
||||
<div className="flex items-center text-th-fgd-2">
|
||||
<CheckCircleIcon className="mr-1.5 h-4 w-4 text-th-green" />
|
||||
{t('close-account:delete-your-account')}
|
||||
</div>
|
||||
{mangoAccount &&
|
||||
mangoGroup &&
|
||||
mangoCache &&
|
||||
mangoAccount.getAssetsVal(mangoGroup, mangoCache).gt(ZERO_I80F48) ? (
|
||||
<div className="flex items-center text-th-fgd-2">
|
||||
<CheckCircleIcon className="mr-1.5 h-4 w-4 text-th-green" />
|
||||
{t('close-account:withdraw-assets-worth', {
|
||||
value: formatUsdValue(
|
||||
+mangoAccount.computeValue(mangoGroup, mangoCache)
|
||||
),
|
||||
})}
|
||||
</div>
|
||||
) : (
|
||||
''
|
||||
)}
|
||||
<div className="flex items-center text-th-fgd-2">
|
||||
<CheckCircleIcon className="mr-1.5 h-4 w-4 text-th-green" />
|
||||
{t('close-account:recover-x-sol', {
|
||||
amount: totalAccountSOL.toFixed(3),
|
||||
})}
|
||||
</div>
|
||||
{!mngoAccrued.isZero() ? (
|
||||
<div className="flex items-center text-th-fgd-2">
|
||||
<CheckCircleIcon className="mr-1.5 h-4 w-4 text-th-green" />
|
||||
{t('close-account:claim-x-mngo-rewards', {
|
||||
amount: mangoGroup
|
||||
? nativeToUi(
|
||||
mngoAccrued.toNumber(),
|
||||
mangoGroup.tokens[MNGO_INDEX].decimals
|
||||
).toFixed(3)
|
||||
: 0,
|
||||
})}
|
||||
</div>
|
||||
) : (
|
||||
''
|
||||
)}
|
||||
</div>
|
||||
{isDisabled ? (
|
||||
<>
|
||||
<h3 className="my-3 text-center">
|
||||
{t('close-account:before-you-continue')}
|
||||
</h3>
|
||||
<div className="overflow-none space-y-2 rounded-md bg-th-bkg-4 p-2 sm:p-4">
|
||||
{hasBorrows ? (
|
||||
<div className="flex items-center text-th-fgd-2">
|
||||
<ExclamationCircleIcon className="mr-1.5 h-4 w-4 text-th-red" />
|
||||
{t('close-account:close-all-borrows')}
|
||||
</div>
|
||||
) : null}
|
||||
{hasOpenPositions ? (
|
||||
<div className="flex items-center text-th-fgd-2">
|
||||
<ExclamationCircleIcon className="mr-1.5 h-4 w-4 text-th-red" />
|
||||
{t('close-account:close-perp-positions')}
|
||||
</div>
|
||||
) : null}
|
||||
{openOrders && openOrders.length ? (
|
||||
<div className="flex items-center text-th-fgd-2">
|
||||
<ExclamationCircleIcon className="mr-1.5 h-4 w-4 text-th-red" />
|
||||
{t('close-account:close-open-orders')}
|
||||
</div>
|
||||
) : null}
|
||||
{unsettledBalances.length ? (
|
||||
<div className="flex items-center text-th-fgd-2">
|
||||
<ExclamationCircleIcon className="mr-1.5 h-4 w-4 text-th-red" />
|
||||
{t('close-account:settle-balances')}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</>
|
||||
) : null}
|
||||
{!isDisabled ? (
|
||||
<div className="mt-4 text-center text-th-fgd-2">
|
||||
{t('close-account:goodbye')}
|
||||
</div>
|
||||
) : null}
|
||||
<Button
|
||||
onClick={() => closeAccount()}
|
||||
disabled={isDisabled}
|
||||
className="mt-6 w-full"
|
||||
>
|
||||
{t('close-account:close-account')}
|
||||
</Button>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default CloseAccountModal
|
|
@ -0,0 +1,228 @@
|
|||
import React, {
|
||||
Fragment,
|
||||
useCallback,
|
||||
useState,
|
||||
useMemo,
|
||||
useEffect,
|
||||
} from "react";
|
||||
import { Menu, Transition } from "@headlessui/react";
|
||||
import { useWallet, Wallet } from "@solana/wallet-adapter-react";
|
||||
import { WalletReadyState } from "@solana/wallet-adapter-base";
|
||||
import {
|
||||
CurrencyDollarIcon,
|
||||
LogoutIcon,
|
||||
UserCircleIcon,
|
||||
} from "@heroicons/react/outline";
|
||||
import { notify } from "utils/notifications";
|
||||
import { abbreviateAddress } from "utils";
|
||||
import useMangoStore from "stores/useMangoStore";
|
||||
import { WalletIcon } from "./icons";
|
||||
import { useTranslation } from "next-i18next";
|
||||
import { WalletSelect } from "components/WalletSelect";
|
||||
import AccountsModal from "./AccountsModal";
|
||||
import uniqBy from "lodash/uniqBy";
|
||||
import ProfileImage from "./ProfileImage";
|
||||
import { useRouter } from "next/router";
|
||||
import { PublicKey } from "@solana/web3.js";
|
||||
import { breakpoints } from "../components/TradePageGrid";
|
||||
import { useViewport } from "hooks/useViewport";
|
||||
import Loading from "./Loading";
|
||||
|
||||
export const handleWalletConnect = (wallet: Wallet) => {
|
||||
if (!wallet) {
|
||||
return;
|
||||
}
|
||||
|
||||
return wallet?.adapter?.connect().catch((e) => {
|
||||
if (e.name.includes("WalletLoadError")) {
|
||||
notify({
|
||||
title: `${wallet.adapter.name} Error`,
|
||||
type: "error",
|
||||
description: `Please install ${wallet.adapter.name} and then reload this page.`,
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const ConnectWalletButton: React.FC = () => {
|
||||
const { connected, publicKey, wallet, wallets, select } = useWallet();
|
||||
const { t } = useTranslation(["common", "profile"]);
|
||||
const router = useRouter();
|
||||
const set = useMangoStore((s) => s.set);
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current);
|
||||
const [showAccountsModal, setShowAccountsModal] = useState(false);
|
||||
const [connecting, setConnecting] = useState(false);
|
||||
const actions = useMangoStore((s) => s.actions);
|
||||
const profileDetails = useMangoStore((s) => s.profile.details);
|
||||
const loadProfileDetails = useMangoStore((s) => s.profile.loadDetails);
|
||||
const { width } = useViewport();
|
||||
const isMobile = width ? width < breakpoints.md : false;
|
||||
|
||||
useEffect(() => {
|
||||
if (publicKey) {
|
||||
actions.fetchProfileDetails(publicKey.toString());
|
||||
}
|
||||
}, [publicKey]);
|
||||
|
||||
const installedWallets = useMemo(() => {
|
||||
const installed: Wallet[] = [];
|
||||
|
||||
for (const wallet of wallets) {
|
||||
if (wallet.readyState === WalletReadyState.Installed) {
|
||||
installed.push(wallet);
|
||||
}
|
||||
}
|
||||
|
||||
return installed?.length ? installed : wallets;
|
||||
}, [wallets]);
|
||||
|
||||
const displayedWallets = useMemo(() => {
|
||||
return uniqBy([...installedWallets, ...wallets], (w) => {
|
||||
return w.adapter.name;
|
||||
});
|
||||
}, [wallets, installedWallets]);
|
||||
|
||||
const handleConnect = useCallback(() => {
|
||||
if (wallet) {
|
||||
setConnecting(true);
|
||||
handleWalletConnect(wallet)?.then(() => setConnecting(false));
|
||||
}
|
||||
}, [wallet]);
|
||||
|
||||
const handleCloseAccounts = useCallback(() => {
|
||||
setShowAccountsModal(false);
|
||||
}, []);
|
||||
|
||||
const handleDisconnect = useCallback(() => {
|
||||
wallet?.adapter?.disconnect();
|
||||
set((state) => {
|
||||
state.mangoAccounts = [];
|
||||
state.selectedMangoAccount.current = null;
|
||||
state.tradeHistory = {
|
||||
spot: [],
|
||||
perp: [],
|
||||
parsed: [],
|
||||
initialLoad: false,
|
||||
};
|
||||
});
|
||||
notify({
|
||||
type: "info",
|
||||
title: t("wallet-disconnected"),
|
||||
});
|
||||
}, [wallet, set, t]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!wallet && displayedWallets?.length) {
|
||||
select(displayedWallets[0].adapter.name);
|
||||
}
|
||||
}, [wallet, displayedWallets, select]);
|
||||
|
||||
return (
|
||||
<>
|
||||
{connected && publicKey ? (
|
||||
<Menu>
|
||||
{({ open }) => (
|
||||
<div className="relative" id="profile-menu-tip">
|
||||
<Menu.Button
|
||||
className={`flex h-14 ${
|
||||
!isMobile ? "w-48 border-x border-th-bkg-3 px-3" : ""
|
||||
} items-center rounded-none rounded-full hover:bg-th-bkg-2 focus:outline-none`}
|
||||
>
|
||||
<ProfileImage
|
||||
imageSize="40"
|
||||
placeholderSize="24"
|
||||
isOwnerProfile
|
||||
/>
|
||||
{!loadProfileDetails && !isMobile ? (
|
||||
<div className="ml-2 w-32 text-left">
|
||||
<p className="mb-0.5 truncate text-xs font-bold capitalize text-th-fgd-1">
|
||||
{profileDetails.profile_name}
|
||||
</p>
|
||||
<p className="mb-0 text-xs text-th-fgd-4">
|
||||
{profileDetails.wallet_pk
|
||||
? abbreviateAddress(
|
||||
new PublicKey(profileDetails.wallet_pk)
|
||||
)
|
||||
: ""}
|
||||
</p>
|
||||
</div>
|
||||
) : null}
|
||||
</Menu.Button>
|
||||
<Transition
|
||||
appear={true}
|
||||
show={open}
|
||||
as={Fragment}
|
||||
enter="transition-all ease-in duration-200"
|
||||
enterFrom="opacity-0 transform scale-75"
|
||||
enterTo="opacity-100 transform scale-100"
|
||||
leave="transition ease-out duration-200"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<Menu.Items className="absolute right-0 z-20 mt-1 w-48 space-y-1.5 rounded-md bg-th-bkg-2 px-4 py-2.5">
|
||||
<Menu.Item>
|
||||
<button
|
||||
className="flex w-full flex-row items-center rounded-none py-0.5 font-normal hover:cursor-pointer hover:text-th-primary focus:outline-none"
|
||||
onClick={() => setShowAccountsModal(true)}
|
||||
>
|
||||
<CurrencyDollarIcon className="h-4 w-4" />
|
||||
<div className="pl-2 text-left">{t("accounts")}</div>
|
||||
</button>
|
||||
</Menu.Item>
|
||||
<Menu.Item>
|
||||
<button
|
||||
className="flex w-full flex-row items-center rounded-none py-0.5 font-normal hover:cursor-pointer focus:outline-none md:hover:text-th-primary"
|
||||
onClick={handleDisconnect}
|
||||
>
|
||||
<LogoutIcon className="h-4 w-4" />
|
||||
<div className="pl-2 text-left">
|
||||
<div className="pb-0.5">{t("disconnect")}</div>
|
||||
<div className="text-xs text-th-fgd-4">
|
||||
{abbreviateAddress(publicKey)}
|
||||
</div>
|
||||
</div>
|
||||
</button>
|
||||
</Menu.Item>
|
||||
</Menu.Items>
|
||||
</Transition>
|
||||
</div>
|
||||
)}
|
||||
</Menu>
|
||||
) : (
|
||||
<div
|
||||
className="flex h-14 divide-x divide-th-bkg-3"
|
||||
id="connect-wallet-tip"
|
||||
>
|
||||
<button
|
||||
onClick={handleConnect}
|
||||
disabled={!mangoGroup}
|
||||
className="rounded-none bg-th-primary-dark text-th-bkg-1 focus:outline-none disabled:cursor-wait disabled:text-th-bkg-2"
|
||||
>
|
||||
<div className="default-transition flex h-full flex-row items-center justify-center px-3">
|
||||
<WalletIcon className="mr-2 h-4 w-4 fill-current" />
|
||||
<div className="text-left">
|
||||
<div className="mb-1 flex justify-center whitespace-nowrap font-bold leading-none">
|
||||
{connecting ? <Loading className="h-4 w-4" /> : t("connect")}
|
||||
</div>
|
||||
{wallet?.adapter?.name && (
|
||||
<div className="text-xxs font-normal leading-3 tracking-wider text-th-bkg-2">
|
||||
{wallet.adapter.name}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</button>
|
||||
<div className="relative">
|
||||
<WalletSelect wallets={displayedWallets} />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{showAccountsModal && (
|
||||
<AccountsModal
|
||||
onClose={handleCloseAccounts}
|
||||
isOpen={showAccountsModal}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
|
@ -0,0 +1,252 @@
|
|||
import React, { FunctionComponent, useEffect, useState } from 'react'
|
||||
import { PlusCircleIcon, TrashIcon } from '@heroicons/react/solid'
|
||||
import Modal from './Modal'
|
||||
import Input, { Label } from './Input'
|
||||
import { ElementTitle } from './styles'
|
||||
import useMangoStore, { AlertRequest } from '../stores/useMangoStore'
|
||||
import Button, { LinkButton } from './Button'
|
||||
import { notify } from '../utils/notifications'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import ButtonGroup from './ButtonGroup'
|
||||
import InlineNotification from './InlineNotification'
|
||||
|
||||
interface CreateAlertModalProps {
|
||||
onClose: () => void
|
||||
isOpen: boolean
|
||||
repayAmount?: string
|
||||
tokenSymbol?: string
|
||||
}
|
||||
|
||||
const CreateAlertModal: FunctionComponent<CreateAlertModalProps> = ({
|
||||
isOpen,
|
||||
onClose,
|
||||
}) => {
|
||||
const { t } = useTranslation(['common', 'alerts'])
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
|
||||
const activeAlerts = useMangoStore((s) => s.alerts.activeAlerts)
|
||||
const loading = useMangoStore((s) => s.alerts.loading)
|
||||
const submitting = useMangoStore((s) => s.alerts.submitting)
|
||||
const error = useMangoStore((s) => s.alerts.error)
|
||||
const [email, setEmail] = useState<string>('')
|
||||
const [invalidAmountMessage, setInvalidAmountMessage] = useState('')
|
||||
const [health, setHealth] = useState('')
|
||||
const [showCustomHealthForm, setShowCustomHealthForm] = useState(false)
|
||||
const [showAlertForm, setShowAlertForm] = useState(false)
|
||||
|
||||
const healthPresets = ['5', '10', '15', '25', '30']
|
||||
|
||||
const validateEmailInput = (amount) => {
|
||||
if (Number(amount) <= 0) {
|
||||
setInvalidAmountMessage(t('enter-amount'))
|
||||
}
|
||||
}
|
||||
|
||||
const onChangeEmailInput = (amount) => {
|
||||
setEmail(amount)
|
||||
setInvalidAmountMessage('')
|
||||
}
|
||||
|
||||
async function onCreateAlert() {
|
||||
if (!mangoGroup || !mangoAccount) return
|
||||
const parsedHealth = parseFloat(health)
|
||||
if (!email) {
|
||||
notify({
|
||||
title: t('alerts:email-address-required'),
|
||||
type: 'error',
|
||||
})
|
||||
return
|
||||
} else if (typeof parsedHealth !== 'number') {
|
||||
notify({
|
||||
title: t('alerts:alert-health-required'),
|
||||
type: 'error',
|
||||
})
|
||||
return
|
||||
}
|
||||
const body: AlertRequest = {
|
||||
mangoGroupPk: mangoGroup.publicKey.toString(),
|
||||
mangoAccountPk: mangoAccount.publicKey.toString(),
|
||||
health: parsedHealth,
|
||||
alertProvider: 'mail',
|
||||
email,
|
||||
}
|
||||
const success: any = await actions.createAlert(body)
|
||||
if (success) {
|
||||
setShowAlertForm(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleCancelCreateAlert = () => {
|
||||
if (activeAlerts.length > 0) {
|
||||
setShowAlertForm(false)
|
||||
} else {
|
||||
onClose()
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (mangoAccount) {
|
||||
actions.loadAlerts(mangoAccount?.publicKey)
|
||||
}
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<Modal isOpen={isOpen} onClose={onClose}>
|
||||
{!loading && !submitting ? (
|
||||
<>
|
||||
{activeAlerts.length > 0 && !showAlertForm ? (
|
||||
<>
|
||||
<Modal.Header>
|
||||
<div className="flex w-full items-center justify-between">
|
||||
<div className="w-20" />
|
||||
<ElementTitle noMarginBottom>
|
||||
{t('alerts:active-alerts')}
|
||||
</ElementTitle>
|
||||
<Button
|
||||
className="min-w-20 flex h-8 items-center justify-center pt-0 pb-0 text-xs"
|
||||
disabled={activeAlerts.length >= 5}
|
||||
onClick={() => setShowAlertForm(true)}
|
||||
>
|
||||
<div className="flex items-center">
|
||||
<PlusCircleIcon className="mr-1.5 h-4 w-4" />
|
||||
{t('alerts:new-alert')}
|
||||
</div>
|
||||
</Button>
|
||||
</div>
|
||||
</Modal.Header>
|
||||
<div className="mt-2 border-b border-th-fgd-4">
|
||||
{activeAlerts.map((alert, index) => (
|
||||
<div
|
||||
className="flex items-center justify-between border-t border-th-fgd-4 p-4"
|
||||
key={`${alert._id}${index}`}
|
||||
>
|
||||
<div className="text-th-fgd-1">
|
||||
{t('alerts:alert-info', { health: alert.health })}
|
||||
</div>
|
||||
<TrashIcon
|
||||
className="default-transition h-5 w-5 cursor-pointer text-th-fgd-3 hover:text-th-primary"
|
||||
onClick={() => actions.deleteAlert(alert._id)}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
{activeAlerts.length >= 3 ? (
|
||||
<div className="mt-1 text-center text-xxs text-th-fgd-3">
|
||||
{t('alerts:alerts-max')}
|
||||
</div>
|
||||
) : null}
|
||||
</>
|
||||
) : showAlertForm ? (
|
||||
<>
|
||||
<Modal.Header>
|
||||
<ElementTitle noMarginBottom>
|
||||
{t('alerts:create-alert')}
|
||||
</ElementTitle>
|
||||
<p className="mt-1 text-center">
|
||||
{t('alerts:alerts-disclaimer')}
|
||||
</p>
|
||||
</Modal.Header>
|
||||
{error ? (
|
||||
<div className="my-4">
|
||||
<InlineNotification title={error} type="error" />
|
||||
</div>
|
||||
) : null}
|
||||
<Label>{t('email-address')}</Label>
|
||||
<Input
|
||||
type="email"
|
||||
error={!!invalidAmountMessage}
|
||||
onBlur={(e) => validateEmailInput(e.target.value)}
|
||||
value={email || ''}
|
||||
onChange={(e) => onChangeEmailInput(e.target.value)}
|
||||
/>
|
||||
<div className="mt-4 flex items-end">
|
||||
<div className="w-full">
|
||||
<div className="flex justify-between">
|
||||
<Label>{t('alerts:alert-health')}</Label>
|
||||
|
||||
<LinkButton
|
||||
className="mb-1.5"
|
||||
onClick={() =>
|
||||
setShowCustomHealthForm(!showCustomHealthForm)
|
||||
}
|
||||
>
|
||||
{showCustomHealthForm ? t('presets') : t('custom')}
|
||||
</LinkButton>
|
||||
</div>
|
||||
{showCustomHealthForm ? (
|
||||
<Input
|
||||
type="number"
|
||||
min="0"
|
||||
max="100"
|
||||
onChange={(e) => setHealth(e.target.value)}
|
||||
suffix={
|
||||
<div className="text-base font-bold text-th-fgd-3">
|
||||
%
|
||||
</div>
|
||||
}
|
||||
value={health}
|
||||
/>
|
||||
) : (
|
||||
<ButtonGroup
|
||||
activeValue={health.toString()}
|
||||
onChange={(p) => setHealth(p)}
|
||||
unit="%"
|
||||
values={healthPresets}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<Button className="mt-6 w-full" onClick={() => onCreateAlert()}>
|
||||
{t('alerts:create-alert')}
|
||||
</Button>
|
||||
<LinkButton
|
||||
className="mt-4 w-full text-center"
|
||||
onClick={handleCancelCreateAlert}
|
||||
>
|
||||
{t('cancel')}
|
||||
</LinkButton>
|
||||
</>
|
||||
) : error ? (
|
||||
<div>
|
||||
<InlineNotification title={error} type="error" />
|
||||
<Button
|
||||
className="mx-auto mt-6 flex justify-center"
|
||||
onClick={() =>
|
||||
mangoAccount
|
||||
? actions.loadAlerts(mangoAccount.publicKey)
|
||||
: null
|
||||
}
|
||||
>
|
||||
{t('try-again')}
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<div>
|
||||
<Modal.Header>
|
||||
<ElementTitle noMarginBottom>
|
||||
{t('alerts:no-alerts')}
|
||||
</ElementTitle>
|
||||
<p className="mt-1 text-center">{t('alerts:no-alerts-desc')}</p>
|
||||
</Modal.Header>
|
||||
<Button
|
||||
className="m-auto flex justify-center"
|
||||
onClick={() => setShowAlertForm(true)}
|
||||
>
|
||||
{t('alerts:new-alert')}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
<div className="h-12 w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-12 w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-12 w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
</div>
|
||||
)}
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(CreateAlertModal)
|
|
@ -0,0 +1,54 @@
|
|||
import { ChevronRightIcon } from '@heroicons/react/solid'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { enAU } from 'date-fns/locale'
|
||||
import { DateRangePicker } from 'react-nice-dates'
|
||||
import { Label } from './Input'
|
||||
|
||||
const MangoDateRangePicker = ({
|
||||
startDate,
|
||||
setStartDate,
|
||||
endDate,
|
||||
setEndDate,
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
|
||||
return (
|
||||
<DateRangePicker
|
||||
startDate={startDate}
|
||||
endDate={endDate}
|
||||
onStartDateChange={setStartDate}
|
||||
onEndDateChange={setEndDate}
|
||||
minimumDate={new Date('January 01, 2020 00:00:00')}
|
||||
maximumDate={new Date()}
|
||||
minimumLength={1}
|
||||
format="dd MMM yyyy"
|
||||
locale={enAU}
|
||||
>
|
||||
{({ startDateInputProps, endDateInputProps }) => (
|
||||
<div className="date-range flex items-end">
|
||||
<div className="w-full">
|
||||
<Label>{t('from')}</Label>
|
||||
<input
|
||||
className="default-transition h-10 w-full rounded-md border border-th-bkg-4 bg-th-bkg-1 px-2 text-th-fgd-1 hover:border-th-fgd-4 focus:border-th-fgd-4 focus:outline-none"
|
||||
{...startDateInputProps}
|
||||
placeholder="Start Date"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex h-10 items-center justify-center">
|
||||
<ChevronRightIcon className="mx-1 h-5 w-5 flex-shrink-0 text-th-fgd-3" />
|
||||
</div>
|
||||
<div className="w-full">
|
||||
<Label>{t('to')}</Label>
|
||||
<input
|
||||
className="default-transition h-10 w-full rounded-md border border-th-bkg-4 bg-th-bkg-1 px-2 text-th-fgd-1 hover:border-th-fgd-4 focus:border-th-fgd-4 focus:outline-none"
|
||||
{...endDateInputProps}
|
||||
placeholder="End Date"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</DateRangePicker>
|
||||
)
|
||||
}
|
||||
|
||||
export default MangoDateRangePicker
|
|
@ -0,0 +1,46 @@
|
|||
import useOraclePrice from '../hooks/useOraclePrice'
|
||||
import { formatUsdValue } from '../utils'
|
||||
import { MarketDataLoader } from './MarketDetails'
|
||||
|
||||
interface DayHighLowProps {
|
||||
high: number
|
||||
low: number
|
||||
isTableView?: boolean
|
||||
}
|
||||
|
||||
const DayHighLow = ({ high, low, isTableView }: DayHighLowProps) => {
|
||||
const price = useOraclePrice()
|
||||
let rangePercent = 0
|
||||
let latestPrice = 0
|
||||
|
||||
if (price) {
|
||||
latestPrice = price?.toNumber()
|
||||
}
|
||||
|
||||
if (high) {
|
||||
rangePercent = ((latestPrice - low) * 100) / (high - low)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex items-center justify-between md:block">
|
||||
<div className="flex items-center">
|
||||
<div className={`pr-2 text-th-fgd-2 ${!isTableView && 'md:text-xs'}`}>
|
||||
{low ? formatUsdValue(low) : <MarketDataLoader />}
|
||||
</div>
|
||||
<div className="flex h-1.5 w-12 rounded bg-th-bkg-3 sm:w-16">
|
||||
<div
|
||||
style={{
|
||||
width: `${rangePercent}%`,
|
||||
}}
|
||||
className="flex rounded bg-th-primary"
|
||||
></div>
|
||||
</div>
|
||||
<div className={`pl-2 text-th-fgd-2 ${!isTableView && 'md:text-xs'}`}>
|
||||
{high ? formatUsdValue(high) : <MarketDataLoader />}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default DayHighLow
|
|
@ -0,0 +1,167 @@
|
|||
import { FunctionComponent, useState } from 'react'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import {
|
||||
ExclamationCircleIcon,
|
||||
XIcon,
|
||||
InformationCircleIcon,
|
||||
} from '@heroicons/react/solid'
|
||||
import Input, { Label } from './Input'
|
||||
import Tooltip from './Tooltip'
|
||||
import Button, { IconButton } from './Button'
|
||||
import Modal from './Modal'
|
||||
import { ElementTitle } from './styles'
|
||||
import { notify } from '../utils/notifications'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { PublicKey } from '@solana/web3.js'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
|
||||
interface DelegateModalProps {
|
||||
delegate?: PublicKey
|
||||
isOpen: boolean
|
||||
onClose?: (x?) => void
|
||||
}
|
||||
|
||||
const DelegateModal: FunctionComponent<DelegateModalProps> = ({
|
||||
delegate,
|
||||
isOpen,
|
||||
onClose,
|
||||
}) => {
|
||||
const { t } = useTranslation(['common', 'delegate'])
|
||||
const { wallet } = useWallet()
|
||||
|
||||
const [keyBase58, setKeyBase58] = useState(
|
||||
delegate && delegate.equals(PublicKey.default)
|
||||
? ''
|
||||
: delegate
|
||||
? delegate.toBase58()
|
||||
: ''
|
||||
)
|
||||
const [invalidKeyMessage, setInvalidKeyMessage] = useState('')
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
|
||||
const setDelegate = async () => {
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
|
||||
if (!mangoGroup || !mangoAccount || !wallet) return
|
||||
|
||||
try {
|
||||
const key = keyBase58.length
|
||||
? new PublicKey(keyBase58)
|
||||
: PublicKey.default
|
||||
const txid = await mangoClient.setDelegate(
|
||||
mangoGroup,
|
||||
mangoAccount,
|
||||
wallet.adapter,
|
||||
key
|
||||
)
|
||||
actions.reloadMangoAccount()
|
||||
onClose?.()
|
||||
notify({
|
||||
title: t('delegate:delegate-updated'),
|
||||
txid,
|
||||
})
|
||||
} catch (err) {
|
||||
console.warn('Error setting delegate key:', err)
|
||||
notify({
|
||||
title: t('delegate:set-error'),
|
||||
description: `${err}`,
|
||||
txid: err.txid,
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const validateKeyInput = () => {
|
||||
if (isKeyValid()) {
|
||||
setInvalidKeyMessage('')
|
||||
} else {
|
||||
setInvalidKeyMessage(t('invalid-address'))
|
||||
}
|
||||
}
|
||||
|
||||
const isKeyValid = () => {
|
||||
try {
|
||||
if (keyBase58.length == 0) {
|
||||
return true
|
||||
}
|
||||
|
||||
// will throw if key is wrong length
|
||||
new PublicKey(keyBase58)
|
||||
return true
|
||||
} catch (e) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const onChangeKeyInput = (name) => {
|
||||
setKeyBase58(name)
|
||||
validateKeyInput()
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal onClose={onClose} isOpen={isOpen}>
|
||||
<Modal.Header>
|
||||
<div className="flex items-center">
|
||||
<ElementTitle noMarginBottom>
|
||||
{t('delegate:delegate-your-account')}
|
||||
<Tooltip
|
||||
content={
|
||||
<div>
|
||||
<a
|
||||
href="https://docs.mango.markets/mango/account-delegation"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
{t('learn-more')}
|
||||
</a>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<InformationCircleIcon className="ml-2 h-5 w-5 text-th-fgd-4" />
|
||||
</Tooltip>
|
||||
</ElementTitle>
|
||||
</div>
|
||||
</Modal.Header>
|
||||
<div className="flex items-center justify-center pb-4 text-th-fgd-3">
|
||||
<p className="text-center">{t('delegate:info')}</p>
|
||||
</div>
|
||||
<Label>{t('delegate:public-key')}</Label>
|
||||
<Input
|
||||
type="text"
|
||||
error={!!invalidKeyMessage}
|
||||
value={keyBase58}
|
||||
onChange={(e) => {
|
||||
validateKeyInput()
|
||||
onChangeKeyInput(e.target.value)
|
||||
}}
|
||||
suffix={
|
||||
<IconButton
|
||||
disabled={!keyBase58.length}
|
||||
onClick={() => {
|
||||
onChangeKeyInput('')
|
||||
}}
|
||||
>
|
||||
<XIcon className="h-4 w-4" />
|
||||
</IconButton>
|
||||
}
|
||||
/>
|
||||
{invalidKeyMessage ? (
|
||||
<div className="flex items-center pt-1.5 text-th-red">
|
||||
<ExclamationCircleIcon className="mr-1.5 h-4 w-4" />
|
||||
{invalidKeyMessage}
|
||||
</div>
|
||||
) : null}
|
||||
<Button
|
||||
onClick={() => setDelegate()}
|
||||
disabled={!isKeyValid()}
|
||||
className="mt-6 w-full"
|
||||
>
|
||||
{t('delegate:set-delegate')}
|
||||
</Button>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default DelegateModal
|
|
@ -0,0 +1,301 @@
|
|||
import React, { FunctionComponent } from 'react'
|
||||
// import React, { FunctionComponent, useEffect, useState } from 'react'
|
||||
// import { ExclamationCircleIcon } from '@heroicons/react/solid'
|
||||
// import Modal from './Modal'
|
||||
// import Input, { Label } from './Input'
|
||||
// import AccountSelect from './AccountSelect'
|
||||
// import { ElementTitle } from './styles'
|
||||
// import useMangoStore from '../stores/useMangoStore'
|
||||
// import Loading from './Loading'
|
||||
// import Button from './Button'
|
||||
// import InlineNotification from './InlineNotification'
|
||||
// import { deposit } from '../utils/mango'
|
||||
// import { notify } from '../utils/notifications'
|
||||
// import { sleep, trimDecimals } from '../utils'
|
||||
// import { useTranslation } from 'next-i18next'
|
||||
// import ButtonGroup from './ButtonGroup'
|
||||
// import { useWallet } from '@solana/wallet-adapter-react'
|
||||
// import MangoAccountSelect from './MangoAccountSelect'
|
||||
// import { MangoAccount } from '@blockworks-foundation/mango-client'
|
||||
// import { connectionSelector } from 'stores/selectors'
|
||||
// import { INVESTIN_PROGRAM_ID } from 'utils/tokens'
|
||||
|
||||
interface DepositModalProps {
|
||||
onClose: () => void
|
||||
isOpen: boolean
|
||||
repayAmount?: string
|
||||
tokenSymbol?: string
|
||||
}
|
||||
|
||||
const DepositModal: FunctionComponent<DepositModalProps> = () => {
|
||||
return null
|
||||
}
|
||||
|
||||
// const DepositModal: FunctionComponent<DepositModalProps> = ({
|
||||
// isOpen,
|
||||
// onClose,
|
||||
// repayAmount,
|
||||
// tokenSymbol = '',
|
||||
// }) => {
|
||||
// const { t } = useTranslation('common')
|
||||
// const { wallet } = useWallet()
|
||||
// const [inputAmount, setInputAmount] = useState<string>(repayAmount || '')
|
||||
// const [submitting, setSubmitting] = useState(false)
|
||||
// const [invalidAmountMessage, setInvalidAmountMessage] = useState('')
|
||||
// const [depositPercentage, setDepositPercentage] = useState('')
|
||||
// const walletTokens = useMangoStore((s) => s.wallet.tokens)
|
||||
// const actions = useMangoStore((s) => s.actions)
|
||||
// const [selectedAccount, setSelectedAccount] = useState(walletTokens[0])
|
||||
// const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
|
||||
// const mangoAccounts = useMangoStore((s) => s.mangoAccounts)
|
||||
// const [depositMangoAccount, setDepositMangoAccount] =
|
||||
// useState<MangoAccount | null>(mangoAccount)
|
||||
// const connection = useMangoStore(connectionSelector)
|
||||
// const [isInvestinDelegate, setIsInvestinDelegate] = useState(false)
|
||||
|
||||
// useEffect(() => {
|
||||
// const checkForInvestinDelegate = async () => {
|
||||
// if (mangoAccount && mangoAccount.owner) {
|
||||
// const ai = await connection.getAccountInfo(mangoAccount.owner)
|
||||
// if (ai?.owner.toBase58() === INVESTIN_PROGRAM_ID.toBase58()) {
|
||||
// setIsInvestinDelegate(true)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// checkForInvestinDelegate()
|
||||
// }, [mangoAccount])
|
||||
|
||||
// useEffect(() => {
|
||||
// if (tokenSymbol) {
|
||||
// const symbolAccount = walletTokens.find(
|
||||
// (a) => a.config.symbol === tokenSymbol
|
||||
// )
|
||||
// if (symbolAccount) {
|
||||
// setSelectedAccount(symbolAccount)
|
||||
// } else {
|
||||
// setSelectedAccount(null)
|
||||
// }
|
||||
// }
|
||||
// }, [tokenSymbol, walletTokens])
|
||||
|
||||
// const handleAccountSelect = (account) => {
|
||||
// setInputAmount('')
|
||||
// setDepositPercentage('')
|
||||
// setInvalidAmountMessage('')
|
||||
// setSelectedAccount(account)
|
||||
// }
|
||||
|
||||
// const handleDeposit = () => {
|
||||
// if (!wallet || !depositMangoAccount) return
|
||||
|
||||
// setSubmitting(true)
|
||||
// deposit({
|
||||
// amount: parseFloat(inputAmount),
|
||||
// fromTokenAcc: selectedAccount.account,
|
||||
// mangoAccount: depositMangoAccount,
|
||||
// wallet,
|
||||
// })
|
||||
// .then((response) => {
|
||||
// notify({
|
||||
// title: t('deposit-successful'),
|
||||
// type: 'success',
|
||||
// txid: response instanceof Array ? response[1] : response,
|
||||
// })
|
||||
// setSubmitting(false)
|
||||
// onClose()
|
||||
// sleep(500).then(() => {
|
||||
// mangoAccount
|
||||
// ? actions.reloadMangoAccount()
|
||||
// : actions.fetchAllMangoAccounts(wallet)
|
||||
// actions.fetchWalletTokens(wallet)
|
||||
// })
|
||||
// })
|
||||
// .catch((err) => {
|
||||
// notify({
|
||||
// title: t('deposit-failed'),
|
||||
// description: err.message,
|
||||
// txid: err?.txid,
|
||||
// type: 'error',
|
||||
// })
|
||||
// onClose()
|
||||
// })
|
||||
// }
|
||||
|
||||
// const validateAmountInput = (amount) => {
|
||||
// if (Number(amount) <= 0) {
|
||||
// setInvalidAmountMessage(t('enter-amount'))
|
||||
// }
|
||||
// if (selectedAccount && Number(amount) > selectedAccount.uiBalance) {
|
||||
// setInvalidAmountMessage(t('insufficient-balance-deposit'))
|
||||
// }
|
||||
// }
|
||||
|
||||
// const onChangeAmountInput = (amount) => {
|
||||
// setInputAmount(amount)
|
||||
|
||||
// if (!selectedAccount) {
|
||||
// setInvalidAmountMessage(t('supported-assets'))
|
||||
// return
|
||||
// }
|
||||
|
||||
// setDepositPercentage('')
|
||||
// setInvalidAmountMessage('')
|
||||
// }
|
||||
|
||||
// const onChangeAmountButtons = async (percentage) => {
|
||||
// setDepositPercentage(percentage)
|
||||
|
||||
// if (!selectedAccount) {
|
||||
// setInvalidAmountMessage(t('supported-assets'))
|
||||
// return
|
||||
// }
|
||||
|
||||
// const max = selectedAccount.uiBalance
|
||||
// const amount = ((parseInt(percentage) / 100) * max).toString()
|
||||
// if (percentage === '100') {
|
||||
// setInputAmount(amount)
|
||||
// } else {
|
||||
// setInputAmount(trimDecimals(amount, 6).toString())
|
||||
// }
|
||||
// setInvalidAmountMessage('')
|
||||
// validateAmountInput(amount)
|
||||
// }
|
||||
|
||||
// // const percentage = repayAmount
|
||||
// // ? (parseFloat(inputAmount) / parseFloat(repayAmount)) * 100
|
||||
// // : null
|
||||
// // const net = repayAmount
|
||||
// // ? parseFloat(inputAmount) - parseFloat(repayAmount)
|
||||
// // : null
|
||||
// // const repayMessage =
|
||||
// // percentage === 100
|
||||
// // ? t('repay-full')
|
||||
// // : typeof percentage === 'number' && percentage > 100
|
||||
// // ? t('repay-and-deposit', {
|
||||
// // amount: trimDecimals(net, 6).toString(),
|
||||
// // symbol: selectedAccount.config.symbol,
|
||||
// // })
|
||||
// // : typeof percentage === 'number'
|
||||
// // ? t('repay-partial', {
|
||||
// // percentage: percentage.toFixed(2),
|
||||
// // })
|
||||
// // : ''
|
||||
|
||||
// // const inputDisabled =
|
||||
// // selectedAccount &&
|
||||
// // selectedAccount.config.symbol === 'SOL' &&
|
||||
// // selectedAccount.uiBalance.toString() === inputAmount
|
||||
|
||||
// return <div></div>
|
||||
|
||||
// // return (
|
||||
// // <Modal isOpen={isOpen} onClose={onClose}>
|
||||
// // <Modal.Header>
|
||||
// // <ElementTitle noMarginBottom>{t('deposit-funds')}</ElementTitle>
|
||||
// // </Modal.Header>
|
||||
// // {!mangoAccount ? (
|
||||
// // <div className="mb-4 mt-2 text-center text-xs text-th-fgd-3">
|
||||
// // {t('first-deposit-desc')}
|
||||
// // </div>
|
||||
// // ) : null}
|
||||
// // {tokenSymbol && !selectedAccount ? (
|
||||
// // <div className="mb-4">
|
||||
// // <InlineNotification
|
||||
// // desc={t('deposit-help', { tokenSymbol: tokenSymbol })}
|
||||
// // title={t('no-address', { tokenSymbol: tokenSymbol })}
|
||||
// // type="error"
|
||||
// // />
|
||||
// // </div>
|
||||
// // ) : null}
|
||||
// // {repayAmount && selectedAccount?.uiBalance < parseFloat(repayAmount) ? (
|
||||
// // <div className="mb-4">
|
||||
// // <InlineNotification
|
||||
// // desc={t('deposit-before', {
|
||||
// // tokenSymbol: tokenSymbol,
|
||||
// // })}
|
||||
// // title={t('not-enough-balance')}
|
||||
// // type="warning"
|
||||
// // />
|
||||
// // </div>
|
||||
// // ) : null}
|
||||
// // {mangoAccounts.length > 1 ? (
|
||||
// // <div className="mb-4">
|
||||
// // <Label>{t('to-account')}</Label>
|
||||
// // <MangoAccountSelect
|
||||
// // onChange={(v) => setDepositMangoAccount(v)}
|
||||
// // value={depositMangoAccount}
|
||||
// // />
|
||||
// // </div>
|
||||
// // ) : null}
|
||||
|
||||
// // {isInvestinDelegate ? (
|
||||
// // <div className="mb-4">
|
||||
// // <InlineNotification
|
||||
// // title={t('deposit-investin-delegate')}
|
||||
// // type="error"
|
||||
// // />
|
||||
// // </div>
|
||||
// // ) : null}
|
||||
// // <AccountSelect
|
||||
// // accounts={walletTokens}
|
||||
// // selectedAccount={selectedAccount}
|
||||
// // onSelectAccount={handleAccountSelect}
|
||||
// // />
|
||||
// // <Label className={`mt-4`}>{t('amount')}</Label>
|
||||
// // <Input
|
||||
// // type="number"
|
||||
// // min="0"
|
||||
// // placeholder="0.00"
|
||||
// // error={!!invalidAmountMessage}
|
||||
// // onBlur={(e) => validateAmountInput(e.target.value)}
|
||||
// // value={inputAmount || ''}
|
||||
// // onChange={(e) => onChangeAmountInput(e.target.value)}
|
||||
// // suffix={selectedAccount?.config.symbol}
|
||||
// // />
|
||||
// // {invalidAmountMessage ? (
|
||||
// // <div className="flex items-center pt-1.5 text-th-red">
|
||||
// // <ExclamationCircleIcon className="mr-1.5 h-4 w-4" />
|
||||
// // {invalidAmountMessage}
|
||||
// // </div>
|
||||
// // ) : null}
|
||||
// // <div className="pt-1">
|
||||
// // <ButtonGroup
|
||||
// // activeValue={depositPercentage}
|
||||
// // onChange={(v) => onChangeAmountButtons(v)}
|
||||
// // unit="%"
|
||||
// // values={['25', '50', '75', '100']}
|
||||
// // />
|
||||
// // </div>
|
||||
// // {selectedAccount?.config.symbol === 'SOL' &&
|
||||
// // parseFloat(inputAmount) > selectedAccount?.uiBalance - 0.01 ? (
|
||||
// // <div className="-mb-4 mt-1 text-center text-xs text-th-red">
|
||||
// // {t('you-must-leave-enough-sol')}
|
||||
// // </div>
|
||||
// // ) : null}
|
||||
// // {repayAmount ? (
|
||||
// // <div className="pt-3">
|
||||
// // <InlineNotification desc={repayMessage} type="info" />
|
||||
// // </div>
|
||||
// // ) : null}
|
||||
// // <div className={`flex justify-center pt-6`}>
|
||||
// // <Button
|
||||
// // onClick={handleDeposit}
|
||||
// // className="w-full"
|
||||
// // disabled={submitting || inputDisabled || isInvestinDelegate}
|
||||
// // >
|
||||
// // <div className={`flex items-center justify-center`}>
|
||||
// // {submitting ? <Loading className="-ml-1 mr-3" /> : null}
|
||||
// // {t('deposit')}
|
||||
// // </div>
|
||||
// // </Button>
|
||||
// // </div>
|
||||
// // {!repayAmount ? (
|
||||
// // <div className="pt-3">
|
||||
// // <InlineNotification desc={t('interest-info')} type="info" />
|
||||
// // </div>
|
||||
// // ) : null}
|
||||
// // </Modal>
|
||||
// // )
|
||||
// }
|
||||
|
||||
export default React.memo(DepositModal)
|
|
@ -0,0 +1,77 @@
|
|||
import { useState } from 'react'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import Button, { LinkButton } from './Button'
|
||||
import { notify } from '../utils/notifications'
|
||||
import Loading from './Loading'
|
||||
import Modal from './Modal'
|
||||
import { msrmMints } from '@blockworks-foundation/mango-client'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
|
||||
const DepositMsrmModal = ({ onClose, isOpen }) => {
|
||||
const { t } = useTranslation('common')
|
||||
const [submitting, setSubmitting] = useState(false)
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const { wallet } = useWallet()
|
||||
const walletTokens = useMangoStore((s) => s.wallet.tokens)
|
||||
const cluster = useMangoStore.getState().connection.cluster
|
||||
|
||||
const handleMsrmDeposit = async () => {
|
||||
if (!mangoGroup || !mangoAccount || !wallet) {
|
||||
return
|
||||
}
|
||||
setSubmitting(true)
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
const ownerMsrmAccount = walletTokens.find((t) =>
|
||||
t.account.mint.equals(msrmMints[cluster])
|
||||
)
|
||||
try {
|
||||
const txid = await mangoClient.depositMsrm(
|
||||
mangoGroup,
|
||||
mangoAccount,
|
||||
wallet?.adapter,
|
||||
ownerMsrmAccount?.account?.publicKey,
|
||||
1
|
||||
)
|
||||
notify({
|
||||
title: t('msrm-deposited'),
|
||||
txid,
|
||||
})
|
||||
} catch (e) {
|
||||
console.log('error:', e)
|
||||
notify({
|
||||
type: 'error',
|
||||
title: t('msrm-deposit-error'),
|
||||
description: e.message,
|
||||
})
|
||||
} finally {
|
||||
setSubmitting(false)
|
||||
actions.fetchMangoGroup()
|
||||
actions.reloadMangoAccount()
|
||||
onClose()
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal onClose={onClose} isOpen={isOpen}>
|
||||
<div className="flex justify-center text-lg text-th-fgd-1">
|
||||
{t('deposit')}
|
||||
</div>
|
||||
<div className="mt-4 border border-th-bkg-3 bg-th-bkg-1 p-6 text-center text-lg text-th-fgd-1">
|
||||
1 MSRM
|
||||
</div>
|
||||
<div className="mt-6 flex items-center justify-center">
|
||||
<Button onClick={handleMsrmDeposit}>
|
||||
{submitting ? <Loading /> : <span>{t('confirm')}</span>}
|
||||
</Button>
|
||||
<LinkButton className="ml-4 text-th-fgd-1" onClick={onClose}>
|
||||
{t('cancel')}
|
||||
</LinkButton>
|
||||
</div>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default DepositMsrmModal
|
|
@ -0,0 +1,84 @@
|
|||
import { Fragment, FunctionComponent, ReactNode } from 'react'
|
||||
import { Listbox, Transition } from '@headlessui/react'
|
||||
import Tooltip from './Tooltip'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
|
||||
type DropMenuProps = {
|
||||
button: ReactNode
|
||||
buttonClassName?: string
|
||||
disabled?: boolean
|
||||
onChange: (...args: any[]) => any
|
||||
options: Array<any>
|
||||
toolTipContent?: string
|
||||
value?: any
|
||||
}
|
||||
|
||||
const DropMenu: FunctionComponent<DropMenuProps> = ({
|
||||
button,
|
||||
buttonClassName,
|
||||
disabled,
|
||||
value,
|
||||
onChange,
|
||||
options,
|
||||
toolTipContent,
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
|
||||
return (
|
||||
<div className={`relative`}>
|
||||
<Listbox value={value} onChange={onChange}>
|
||||
{({ open }) => (
|
||||
<>
|
||||
<Listbox.Button className={buttonClassName} disabled={disabled}>
|
||||
{toolTipContent && !open ? (
|
||||
<Tooltip content={toolTipContent} className="py-1 text-xs">
|
||||
{button}
|
||||
</Tooltip>
|
||||
) : (
|
||||
button
|
||||
)}
|
||||
</Listbox.Button>
|
||||
<Transition
|
||||
appear={true}
|
||||
show={open}
|
||||
as={Fragment}
|
||||
enter="transition-all ease-in duration-200"
|
||||
enterFrom="opacity-0 transform scale-75"
|
||||
enterTo="opacity-100 transform scale-100"
|
||||
leave="transition ease-out duration-200"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<Listbox.Options
|
||||
className={`absolute left-1/2 z-10 mt-2 -translate-x-1/2 transform rounded-md bg-th-bkg-3 px-4 py-2.5`}
|
||||
static
|
||||
>
|
||||
{options.map((option) => (
|
||||
<Listbox.Option
|
||||
key={option.name}
|
||||
value={option.locale || option.name}
|
||||
>
|
||||
{({ selected }) => (
|
||||
<div
|
||||
className={`default-transition whitespace-nowrap tracking-wider text-th-fgd-1 hover:cursor-pointer hover:text-th-primary ${
|
||||
selected && `text-th-primary`
|
||||
} ${option.icon && `flex items-center`}`}
|
||||
>
|
||||
{option.icon ? (
|
||||
<div className="mr-2">{option.icon}</div>
|
||||
) : null}
|
||||
{t(option.name.toLowerCase())}
|
||||
</div>
|
||||
)}
|
||||
</Listbox.Option>
|
||||
))}
|
||||
</Listbox.Options>
|
||||
</Transition>
|
||||
</>
|
||||
)}
|
||||
</Listbox>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default DropMenu
|
|
@ -0,0 +1,55 @@
|
|||
import Modal from './Modal'
|
||||
import Button from './Button'
|
||||
import useLocalStorageState from '../hooks/useLocalStorageState'
|
||||
import { ElementTitle } from './styles'
|
||||
import Switch from './Switch'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
|
||||
const EditTableColumnsModal = ({
|
||||
columns,
|
||||
isOpen,
|
||||
onClose,
|
||||
storageKey,
|
||||
}: {
|
||||
columns: { [key: string]: boolean }
|
||||
isOpen: boolean
|
||||
onClose?: (x) => void
|
||||
storageKey: string
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const [tableColumns, setTableColumns] = useLocalStorageState(
|
||||
storageKey,
|
||||
columns
|
||||
)
|
||||
|
||||
const handleToggleColumn = (column) => {
|
||||
const newColumns = { ...tableColumns, [column[0]]: !column[1] }
|
||||
setTableColumns(newColumns)
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal isOpen={isOpen} onClose={onClose}>
|
||||
<Modal.Header>
|
||||
<ElementTitle noMarginBottom>{t('edit-table-columns')}</ElementTitle>
|
||||
</Modal.Header>
|
||||
<div className="space-y-2">
|
||||
{Object.entries(tableColumns).map((entry: any) => (
|
||||
<div className="flex items-center justify-between" key={entry[0]}>
|
||||
<p className="mb-0">{t(entry[0])}</p>
|
||||
<Switch
|
||||
checked={entry[1]}
|
||||
onChange={() => handleToggleColumn(entry)}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<div className="flex justify-center">
|
||||
<Button className="mt-6" onClick={onClose}>
|
||||
{t('save')}
|
||||
</Button>
|
||||
</div>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default EditTableColumnsModal
|
|
@ -0,0 +1,43 @@
|
|||
import { FunctionComponent, ReactNode } from 'react'
|
||||
import Button from './Button'
|
||||
|
||||
interface EmptyStateProps {
|
||||
buttonText?: string
|
||||
icon: ReactNode
|
||||
onClickButton?: () => void
|
||||
desc?: string
|
||||
title: string
|
||||
disabled?: boolean
|
||||
}
|
||||
|
||||
const EmptyState: FunctionComponent<EmptyStateProps> = ({
|
||||
buttonText,
|
||||
icon,
|
||||
onClickButton,
|
||||
desc,
|
||||
title,
|
||||
disabled = false,
|
||||
}) => {
|
||||
return (
|
||||
<div className="flex flex-col items-center rounded-lg px-4 pb-2 text-th-fgd-1">
|
||||
<div className="mb-1 h-6 w-6 text-th-primary">{icon}</div>
|
||||
<h2 className="mb-1 text-base">{title}</h2>
|
||||
{desc ? (
|
||||
<p
|
||||
className={`text-center ${
|
||||
buttonText && onClickButton ? 'mb-1' : 'mb-0'
|
||||
}`}
|
||||
>
|
||||
{desc}
|
||||
</p>
|
||||
) : null}
|
||||
{buttonText && onClickButton ? (
|
||||
<Button className="mt-2" onClick={onClickButton} disabled={disabled}>
|
||||
{buttonText}
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default EmptyState
|
|
@ -0,0 +1,45 @@
|
|||
import React, { useCallback } from 'react'
|
||||
import { FiveOhFive } from './FiveOhFive'
|
||||
import * as Sentry from '@sentry/react'
|
||||
import { useRouter } from 'next/router'
|
||||
|
||||
const ErrorBoundary: React.FC<any> = (props) => {
|
||||
const { asPath } = useRouter()
|
||||
|
||||
const postError = useCallback(
|
||||
(error, componentStack) => {
|
||||
if (process.env.NEXT_PUBLIC_ERROR_WEBHOOK_URL) {
|
||||
try {
|
||||
fetch(process.env.NEXT_PUBLIC_ERROR_WEBHOOK_URL, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
content:
|
||||
`UI ERROR: (${asPath}) ${error} : ${componentStack}`.slice(
|
||||
0,
|
||||
1999
|
||||
),
|
||||
}),
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('Error posting to notify webhook:', err)
|
||||
}
|
||||
}
|
||||
},
|
||||
[asPath]
|
||||
)
|
||||
|
||||
return (
|
||||
<Sentry.ErrorBoundary
|
||||
fallback={({ error, componentStack }) => {
|
||||
postError(error, componentStack)
|
||||
|
||||
return <FiveOhFive error={error} />
|
||||
}}
|
||||
>
|
||||
{props.children}
|
||||
</Sentry.ErrorBoundary>
|
||||
)
|
||||
}
|
||||
|
||||
export default ErrorBoundary
|
|
@ -0,0 +1,78 @@
|
|||
import useLocalStorageState from '../hooks/useLocalStorageState'
|
||||
import { FAVORITE_MARKETS_KEY } from './TradeNavMenu'
|
||||
import { StarIcon, QuestionMarkCircleIcon } from '@heroicons/react/solid'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from './TradePageGrid'
|
||||
import Link from 'next/link'
|
||||
import { useRouter } from 'next/router'
|
||||
import { initialMarket } from './SettingsModal'
|
||||
import * as MonoIcons from './icons'
|
||||
import { Transition } from '@headlessui/react'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
|
||||
const FavoritesShortcutBar = () => {
|
||||
const [favoriteMarkets] = useLocalStorageState(FAVORITE_MARKETS_KEY, [])
|
||||
const { width } = useViewport()
|
||||
const isMobile = width ? width < breakpoints.sm : false
|
||||
const { asPath } = useRouter()
|
||||
const marketsInfo = useMangoStore((s) => s.marketsInfo)
|
||||
|
||||
const renderIcon = (mktName) => {
|
||||
const symbol = mktName.slice(0, -5)
|
||||
const iconName = `${symbol.slice(0, 1)}${symbol
|
||||
.slice(1, 4)
|
||||
.toLowerCase()}MonoIcon`
|
||||
|
||||
const SymbolIcon = MonoIcons[iconName] || QuestionMarkCircleIcon
|
||||
return <SymbolIcon className={`mr-1.5 h-3.5 w-auto`} />
|
||||
}
|
||||
|
||||
return !isMobile ? (
|
||||
<Transition
|
||||
appear={true}
|
||||
className="flex items-center space-x-4 border-b border-th-bkg-3 py-1 px-6"
|
||||
show={favoriteMarkets.length > 0}
|
||||
enter="transition-all ease-in duration-200"
|
||||
enterFrom="opacity-0"
|
||||
enterTo="opacity-100"
|
||||
leave="transition ease-out duration-200"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<StarIcon className="h-5 w-5 text-th-fgd-4" />
|
||||
{favoriteMarkets.map((mkt) => {
|
||||
const change24h = marketsInfo?.find((m) => m.name === mkt)?.change24h
|
||||
return (
|
||||
<Link href={`/?name=${mkt}`} key={mkt} shallow={true}>
|
||||
<a
|
||||
className={`default-transition flex items-center whitespace-nowrap py-1 text-xs hover:text-th-primary ${
|
||||
asPath.includes(mkt) ||
|
||||
(asPath === '/' && initialMarket.name === mkt)
|
||||
? 'text-th-primary'
|
||||
: 'text-th-fgd-3'
|
||||
}`}
|
||||
>
|
||||
{renderIcon(mkt)}
|
||||
<span className="mb-0 mr-1.5 text-xs">{mkt}</span>
|
||||
{change24h ? (
|
||||
<div
|
||||
className={`text-xs ${
|
||||
change24h
|
||||
? change24h >= 0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
: 'text-th-fgd-4'
|
||||
}`}
|
||||
>
|
||||
{`${(change24h * 100).toFixed(1)}%`}
|
||||
</div>
|
||||
) : null}
|
||||
</a>
|
||||
</Link>
|
||||
)
|
||||
})}
|
||||
</Transition>
|
||||
) : null
|
||||
}
|
||||
|
||||
export default FavoritesShortcutBar
|
|
@ -0,0 +1,130 @@
|
|||
import { useState } from 'react'
|
||||
import { ChevronRightIcon, ChevronDownIcon } from '@heroicons/react/solid'
|
||||
import GradientText from './GradientText'
|
||||
import cls from 'classnames'
|
||||
|
||||
const social = [
|
||||
{
|
||||
name: 'Twitter',
|
||||
href: 'https://twitter.com/mangomarkets',
|
||||
icon: (props) => (
|
||||
<svg fill="currentColor" viewBox="0 0 24 24" {...props}>
|
||||
<path d="M8.29 20.251c7.547 0 11.675-6.253 11.675-11.675 0-.178 0-.355-.012-.53A8.348 8.348 0 0022 5.92a8.19 8.19 0 01-2.357.646 4.118 4.118 0 001.804-2.27 8.224 8.224 0 01-2.605.996 4.107 4.107 0 00-6.993 3.743 11.65 11.65 0 01-8.457-4.287 4.106 4.106 0 001.27 5.477A4.072 4.072 0 012.8 9.713v.052a4.105 4.105 0 003.292 4.022 4.095 4.095 0 01-1.853.07 4.108 4.108 0 003.834 2.85A8.233 8.233 0 012 18.407a11.616 11.616 0 006.29 1.84" />
|
||||
</svg>
|
||||
),
|
||||
},
|
||||
{
|
||||
name: 'GitHub',
|
||||
href: 'https://github.com/blockworks-foundation',
|
||||
icon: (props) => (
|
||||
<svg fill="currentColor" viewBox="0 0 24 24" {...props}>
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M12 2C6.477 2 2 6.484 2 12.017c0 4.425 2.865 8.18 6.839 9.504.5.092.682-.217.682-.483 0-.237-.008-.868-.013-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.029-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0112 6.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.202 2.398.1 2.651.64.7 1.028 1.595 1.028 2.688 0 3.848-2.339 4.695-4.566 4.943.359.309.678.92.678 1.855 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.019 10.019 0 0022 12.017C22 6.484 17.522 2 12 2z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
),
|
||||
},
|
||||
]
|
||||
|
||||
export const FiveOhFive = ({ error }) => {
|
||||
const stack = error.stack.split('\n').slice(0, 5).join('\n')
|
||||
const [showDetails, toggleDetails] = useState(false)
|
||||
|
||||
const Icon = showDetails ? ChevronDownIcon : ChevronRightIcon
|
||||
|
||||
return (
|
||||
<div className="flex h-screen flex-col bg-th-bkg-1">
|
||||
<div className="absolute top-0 h-2 w-full bg-gradient-to-r from-mango-theme-green via-mango-theme-yellow-dark to-mango-theme-red-dark"></div>
|
||||
<main className="mx-auto w-full max-w-7xl flex-grow px-4 sm:px-6 lg:px-8">
|
||||
<div className="flex-shrink-0 pt-16">
|
||||
<img
|
||||
className="mx-auto h-12 w-auto"
|
||||
src="/assets/icons/mngo.svg"
|
||||
alt="Logo"
|
||||
/>
|
||||
</div>
|
||||
<div className="mx-auto max-w-xl py-16 sm:py-24">
|
||||
<div className="text-center">
|
||||
<p className="text-sm font-semibold uppercase tracking-wide">
|
||||
<GradientText>500 error</GradientText>
|
||||
</p>
|
||||
<h1 className="mt-2 text-3xl font-extrabold tracking-tight text-white sm:text-5xl">
|
||||
Something went wrong
|
||||
</h1>
|
||||
<p className="mt-2 text-base text-th-fgd-3">
|
||||
The page you are looking for could not be loaded.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="my-10 text-center">
|
||||
<div className="font-mono mt-8 rounded-lg bg-th-bkg-2 p-8 text-left text-th-fgd-1">
|
||||
<div className="flex">
|
||||
<div className="text-mango-theme-fgd-2">{error.message}</div>
|
||||
<div className="flex-grow"></div>
|
||||
<div className="flex-shrink-0 self-center">
|
||||
<Icon
|
||||
className="text-mango-yellow h-5 w-5"
|
||||
aria-hidden="true"
|
||||
onClick={() => toggleDetails(!showDetails)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
style={{
|
||||
maxHeight: showDetails ? '500px' : 0,
|
||||
opacity: showDetails ? 1 : 0,
|
||||
}}
|
||||
className={cls('overflow-hidden transition-all')}
|
||||
>
|
||||
<div className="mt-6">{stack}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-col items-center">
|
||||
<div className="mt-10 flex flex-row">
|
||||
<button
|
||||
className="mx-2 whitespace-nowrap rounded-full bg-th-bkg-button px-6 py-2 font-bold text-th-fgd-1 focus:outline-none disabled:cursor-not-allowed disabled:bg-th-bkg-4 disabled:text-th-fgd-4"
|
||||
onClick={() => location.reload()}
|
||||
>
|
||||
Refresh and try again
|
||||
</button>
|
||||
|
||||
<a
|
||||
className="whitespace-nowrap rounded-full bg-mango-theme-bkg-3 px-6 py-2 font-bold text-th-fgd-1 hover:brightness-[1.1] focus:outline-none disabled:cursor-not-allowed disabled:bg-th-bkg-4 disabled:text-th-fgd-4 disabled:hover:brightness-100"
|
||||
href="https://discord.gg/mangomarkets"
|
||||
>
|
||||
<div className="flex">
|
||||
<img
|
||||
className="mr-2 h-[20px] w-[20px]"
|
||||
src="/assets/icons/discord.svg"
|
||||
/>
|
||||
Join Discord
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
<footer className="mx-auto w-full max-w-7xl px-4 sm:px-6 lg:px-8">
|
||||
<div className="border-t border-th-bkg-4 py-10 text-center md:flex md:justify-between">
|
||||
<div className="mt-6 flex justify-center space-x-8 md:mt-0">
|
||||
{social.map((item, itemIdx) => (
|
||||
<a
|
||||
key={itemIdx}
|
||||
href={item.href}
|
||||
className="inline-flex text-gray-400 hover:text-gray-500"
|
||||
>
|
||||
<span className="sr-only">{item.name}</span>
|
||||
<item.icon className="h-6 w-6" aria-hidden="true" />
|
||||
</a>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
<div className="absolute bottom-0 h-2 w-full bg-gradient-to-r from-mango-theme-green via-mango-theme-yellow-dark to-mango-theme-red-dark"></div>
|
||||
</div>
|
||||
)
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
export const FlipCard = ({ children, ...props }) => {
|
||||
return (
|
||||
<div {...props} className="flipcard">
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export const FlipCardInner = (props) => {
|
||||
return (
|
||||
<div
|
||||
className="relative h-full w-full text-center"
|
||||
style={{
|
||||
transition: 'transform 0.8s ease-out',
|
||||
transformStyle: 'preserve-3d',
|
||||
transform: `${props.flip ? 'rotateY(0deg)' : 'rotateY(180deg)'}`,
|
||||
}}
|
||||
>
|
||||
{props.children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export const FlipCardFront = (props) => {
|
||||
return <div className="flipcard-front h-full w-full">{props.children}</div>
|
||||
}
|
||||
|
||||
export const FlipCardBack = ({ children }) => {
|
||||
return (
|
||||
<div
|
||||
className="absolute h-full w-full"
|
||||
style={{ transform: 'rotateY(180deg)' }}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
import React, { FunctionComponent, useCallback } from 'react'
|
||||
import { LinkIcon } from '@heroicons/react/solid'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import { MoveIcon } from './icons'
|
||||
import EmptyState from './EmptyState'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { handleWalletConnect } from 'components/ConnectWalletButton'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
import { useRouter } from 'next/router'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from './TradePageGrid'
|
||||
|
||||
interface FloatingElementProps {
|
||||
className?: string
|
||||
showConnect?: boolean
|
||||
}
|
||||
|
||||
const FloatingElement: FunctionComponent<FloatingElementProps> = ({
|
||||
className,
|
||||
children,
|
||||
showConnect,
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const { wallet, connected } = useWallet()
|
||||
const { uiLocked } = useMangoStore((s) => s.settings)
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const router = useRouter()
|
||||
const { pubkey } = router.query
|
||||
const { width } = useViewport()
|
||||
const isMobile = width ? width < breakpoints.sm : false
|
||||
|
||||
const handleConnect = useCallback(() => {
|
||||
if (wallet) {
|
||||
handleWalletConnect(wallet)
|
||||
}
|
||||
}, [wallet])
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`thin-scroll relative overflow-auto overflow-x-hidden rounded-md ${
|
||||
!isMobile ? 'border border-th-bkg-3' : ''
|
||||
} bg-th-bkg-1 p-2.5 md:p-4 ${className}`}
|
||||
>
|
||||
{!connected && showConnect && !pubkey ? (
|
||||
<div className="absolute top-0 left-0 z-10 h-full w-full">
|
||||
<div className="relative z-10 flex h-full flex-col items-center justify-center">
|
||||
<EmptyState
|
||||
disabled={!wallet || !mangoGroup}
|
||||
buttonText={t('connect')}
|
||||
icon={<LinkIcon />}
|
||||
onClickButton={handleConnect}
|
||||
title={t('connect-wallet')}
|
||||
/>
|
||||
</div>
|
||||
<div className="absolute top-0 left-0 h-full w-full rounded-lg bg-th-bkg-1 opacity-50" />
|
||||
</div>
|
||||
) : null}
|
||||
{!uiLocked ? (
|
||||
<div className="absolute top-0 left-0 z-50 h-full w-full cursor-move opacity-80">
|
||||
<div className="relative z-50 flex h-full flex-col items-center justify-center text-th-fgd-1">
|
||||
<MoveIcon className="h-8 w-8" />
|
||||
<div className="mt-2">{t('reposition')}</div>
|
||||
</div>
|
||||
<div className="absolute top-0 left-0 h-full w-full rounded-lg bg-th-bkg-3" />
|
||||
</div>
|
||||
) : null}
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default FloatingElement
|
|
@ -0,0 +1,82 @@
|
|||
import { useEffect, useState } from 'react'
|
||||
import sumBy from 'lodash/sumBy'
|
||||
import useInterval from '../hooks/useInterval'
|
||||
import { CLUSTER, SECONDS } from '../stores/useMangoStore'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { ExclamationIcon } from '@heroicons/react/solid'
|
||||
import { Connection } from '@solana/web3.js'
|
||||
|
||||
const tpsAlertThreshold = 1000
|
||||
const tpsWarningThreshold = 1300
|
||||
|
||||
const connection = new Connection('https://mango.genesysgo.net')
|
||||
|
||||
const getRecentPerformance = async (setShow, setTps) => {
|
||||
try {
|
||||
const samples = 2
|
||||
const response = await connection.getRecentPerformanceSamples(samples)
|
||||
const totalSecs = sumBy(response, 'samplePeriodSecs')
|
||||
const totalTransactions = sumBy(response, 'numTransactions')
|
||||
const tps = totalTransactions / totalSecs
|
||||
|
||||
if (tps < tpsWarningThreshold) {
|
||||
setShow(true)
|
||||
setTps(tps)
|
||||
} else {
|
||||
setShow(false)
|
||||
}
|
||||
} catch {
|
||||
console.log('Unable to fetch TPS')
|
||||
}
|
||||
}
|
||||
|
||||
const GlobalNotification = () => {
|
||||
const [show, setShow] = useState(false)
|
||||
const [tps, setTps] = useState(0)
|
||||
const { t } = useTranslation('common')
|
||||
|
||||
useEffect(() => {
|
||||
getRecentPerformance(setShow, setTps)
|
||||
}, [])
|
||||
|
||||
useInterval(() => {
|
||||
getRecentPerformance(setShow, setTps)
|
||||
}, 45 * SECONDS)
|
||||
|
||||
if (show && CLUSTER == 'mainnet') {
|
||||
return (
|
||||
<div className="flex items-center bg-th-bkg-4 text-th-fgd-2">
|
||||
<div className="flex w-full items-center justify-center p-1">
|
||||
<ExclamationIcon
|
||||
className={`mr-1.5 mt-0.5 h-5 w-5 flex-shrink-0 ${
|
||||
tps < tpsAlertThreshold ? 'text-th-red' : 'text-th-orange'
|
||||
}`}
|
||||
/>
|
||||
{tps < 50 ? (
|
||||
<span>{t('solana-down')}</span>
|
||||
) : (
|
||||
<span>{t('degraded-performance')}</span>
|
||||
)}
|
||||
<div
|
||||
className={`ml-2 whitespace-nowrap rounded-full px-1.5 py-0.5 text-xs ${
|
||||
tps < tpsAlertThreshold
|
||||
? 'bg-th-red text-white'
|
||||
: 'bg-th-orange text-th-bkg-1'
|
||||
}`}
|
||||
>
|
||||
TPS:{' '}
|
||||
<span className="font-bold">
|
||||
{tps?.toLocaleString(undefined, {
|
||||
maximumFractionDigits: 0,
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export default GlobalNotification
|
|
@ -0,0 +1,7 @@
|
|||
const GradientText = (props) => (
|
||||
<span className="bg-gradient-to-bl from-mango-theme-green via-mango-theme-yellow-dark to-mango-theme-red-dark bg-clip-text text-transparent">
|
||||
{props.children}
|
||||
</span>
|
||||
)
|
||||
|
||||
export default GradientText
|
|
@ -0,0 +1,67 @@
|
|||
import React, { useMemo } from 'react'
|
||||
import { Listbox } from '@headlessui/react'
|
||||
import { ChevronDownIcon } from '@heroicons/react/solid'
|
||||
import { isEqual } from '../utils'
|
||||
|
||||
const GroupSize = ({ tickSize, value, onChange, className = '' }) => {
|
||||
const sizes = useMemo(
|
||||
() => [
|
||||
tickSize,
|
||||
tickSize * 5,
|
||||
tickSize * 10,
|
||||
tickSize * 50,
|
||||
tickSize * 100,
|
||||
],
|
||||
[tickSize]
|
||||
)
|
||||
|
||||
return (
|
||||
<div className={`relative ${className}`}>
|
||||
<Listbox value={value} onChange={onChange}>
|
||||
{({ open }) => (
|
||||
<>
|
||||
<Listbox.Button
|
||||
className={`rounded border border-th-bkg-4 bg-th-bkg-1 py-0.5 font-normal hover:border-th-fgd-4 focus:border-th-fgd-4 focus:outline-none`}
|
||||
>
|
||||
<div
|
||||
className={`flex items-center justify-between space-x-1 pr-1 pl-2 text-xs`}
|
||||
>
|
||||
<span>{value}</span>
|
||||
|
||||
<ChevronDownIcon
|
||||
className={`default-transition h-4 w-4 text-th-fgd-1 ${
|
||||
open ? 'rotate-180 transform' : 'rotate-360 transform'
|
||||
}`}
|
||||
/>
|
||||
</div>
|
||||
</Listbox.Button>
|
||||
{open ? (
|
||||
<Listbox.Options
|
||||
static
|
||||
className={`thin-scroll absolute left-0 top-5 z-20 mt-1 max-h-60 w-full overflow-auto rounded-md bg-th-bkg-3 p-2 text-th-fgd-1 outline-none`}
|
||||
>
|
||||
{sizes.map((size) => (
|
||||
<Listbox.Option key={size} value={size}>
|
||||
{({ selected }) => (
|
||||
<div
|
||||
className={`default-transition text-right text-th-fgd-1 hover:cursor-pointer hover:bg-th-bkg-3 hover:text-th-primary ${
|
||||
selected && `text-th-primary`
|
||||
}`}
|
||||
>
|
||||
{size}
|
||||
</div>
|
||||
)}
|
||||
</Listbox.Option>
|
||||
))}
|
||||
</Listbox.Options>
|
||||
) : null}
|
||||
</>
|
||||
)}
|
||||
</Listbox>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(GroupSize, (prevProps, nextProps) =>
|
||||
isEqual(prevProps, nextProps, ['tickSize', 'value'])
|
||||
)
|
|
@ -0,0 +1,50 @@
|
|||
const HealthHeart = ({ health, size }: { health: number; size: number }) => {
|
||||
const styles = {
|
||||
height: `${size}px`,
|
||||
width: `${size}px`,
|
||||
}
|
||||
|
||||
return (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
className={
|
||||
health > 15 && health < 50
|
||||
? 'text-th-orange'
|
||||
: health >= 50
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}
|
||||
style={styles}
|
||||
viewBox="0 0 20 20"
|
||||
fill="currentColor"
|
||||
>
|
||||
<g transform-origin="center">
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M3.172 5.172a4 4 0 015.656 0L10 6.343l1.172-1.171a4 4 0 115.656 5.656L10 17.657l-6.828-6.829a4 4 0 010-5.656z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
<animateTransform
|
||||
attributeName="transform"
|
||||
type="scale"
|
||||
keyTimes="0;0.5;1"
|
||||
values="1;1.1;1"
|
||||
dur={
|
||||
health > 15 && health < 50 ? '1s' : health >= 50 ? '2s' : '0.33s'
|
||||
}
|
||||
repeatCount="indefinite"
|
||||
/>
|
||||
<animate
|
||||
attributeName="opacity"
|
||||
values="0.8;1;0.8"
|
||||
dur={
|
||||
health > 15 && health < 50 ? '1s' : health >= 50 ? '2s' : '0.33s'
|
||||
}
|
||||
repeatCount="indefinite"
|
||||
/>
|
||||
</g>
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
export default HealthHeart
|
|
@ -0,0 +1,19 @@
|
|||
import React, { useState } from 'react'
|
||||
import Image from 'next/image'
|
||||
|
||||
const ImageWithFallback = (props) => {
|
||||
const { src, fallbackSrc, ...rest } = props
|
||||
const [imgSrc, setImgSrc] = useState(src)
|
||||
|
||||
return (
|
||||
<Image
|
||||
{...rest}
|
||||
src={imgSrc}
|
||||
onError={() => {
|
||||
setImgSrc(fallbackSrc)
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export default ImageWithFallback
|
|
@ -0,0 +1,56 @@
|
|||
import { FunctionComponent } from 'react'
|
||||
import {
|
||||
CheckCircleIcon,
|
||||
ExclamationCircleIcon,
|
||||
ExclamationIcon,
|
||||
InformationCircleIcon,
|
||||
} from '@heroicons/react/solid'
|
||||
|
||||
interface InlineNotificationProps {
|
||||
desc?: string | (() => string)
|
||||
title?: string
|
||||
type: string
|
||||
}
|
||||
|
||||
const InlineNotification: FunctionComponent<InlineNotificationProps> = ({
|
||||
desc,
|
||||
title,
|
||||
type,
|
||||
}) => (
|
||||
<div
|
||||
className={`border ${
|
||||
type === 'error'
|
||||
? 'border-th-red'
|
||||
: type === 'success'
|
||||
? 'border-th-green'
|
||||
: type === 'info'
|
||||
? 'border-th-bkg-4'
|
||||
: 'border-th-orange'
|
||||
} flex items-center rounded-md p-2`}
|
||||
>
|
||||
{type === 'error' ? (
|
||||
<ExclamationCircleIcon className="mr-2 h-5 w-5 flex-shrink-0 text-th-red" />
|
||||
) : null}
|
||||
{type === 'success' ? (
|
||||
<CheckCircleIcon className="mr-2 h-5 w-5 flex-shrink-0 text-th-green" />
|
||||
) : null}
|
||||
{type === 'warning' ? (
|
||||
<ExclamationIcon className="mr-2 h-5 w-5 flex-shrink-0 text-th-orange" />
|
||||
) : null}
|
||||
{type === 'info' ? (
|
||||
<InformationCircleIcon className="mr-2 h-5 w-5 flex-shrink-0 text-th-fgd-4" />
|
||||
) : null}
|
||||
<div>
|
||||
<div className="text-th-fgd-3">{title}</div>
|
||||
<div
|
||||
className={`${
|
||||
title && desc && 'pt-1'
|
||||
} text-left text-xs font-normal text-th-fgd-3`}
|
||||
>
|
||||
{desc}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
export default InlineNotification
|
|
@ -0,0 +1,76 @@
|
|||
import { forwardRef, ReactNode } from 'react'
|
||||
|
||||
interface InputProps {
|
||||
type: string
|
||||
value: any
|
||||
onChange: (e) => void
|
||||
className?: string
|
||||
disabled?: boolean
|
||||
prefixClassname?: string
|
||||
error?: boolean
|
||||
[x: string]: any
|
||||
}
|
||||
|
||||
const Input = forwardRef<HTMLInputElement, InputProps>((props, ref) => {
|
||||
const {
|
||||
type,
|
||||
value,
|
||||
onChange,
|
||||
className,
|
||||
error,
|
||||
wrapperClassName = 'w-full',
|
||||
disabled,
|
||||
prefix,
|
||||
prefixClassName,
|
||||
suffix,
|
||||
} = props
|
||||
return (
|
||||
<div className={`relative flex ${wrapperClassName}`}>
|
||||
{prefix ? (
|
||||
<div
|
||||
className={`absolute left-2 top-1/2 -translate-y-1/2 transform ${prefixClassName}`}
|
||||
>
|
||||
{prefix}
|
||||
</div>
|
||||
) : null}
|
||||
<input
|
||||
className={`${className} h-10 w-full flex-1 rounded-md border bg-th-bkg-1 px-2 pb-px
|
||||
text-th-fgd-1 ${
|
||||
error ? 'border-th-red' : 'border-th-bkg-4'
|
||||
} default-transition hover:border-th-fgd-4
|
||||
focus:border-th-fgd-4 focus:outline-none
|
||||
${
|
||||
disabled
|
||||
? 'cursor-not-allowed bg-th-bkg-3 text-th-fgd-3 hover:border-th-fgd-4'
|
||||
: ''
|
||||
}
|
||||
${prefix ? 'pl-7' : ''}
|
||||
${suffix ? 'pr-11' : ''}`}
|
||||
disabled={disabled}
|
||||
ref={ref}
|
||||
{...props}
|
||||
type={type}
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
/>
|
||||
{suffix ? (
|
||||
<span className="absolute right-0 flex h-full items-center bg-transparent pr-2 text-xs text-th-fgd-4">
|
||||
{suffix}
|
||||
</span>
|
||||
) : null}
|
||||
</div>
|
||||
)
|
||||
})
|
||||
|
||||
export default Input
|
||||
|
||||
interface LabelProps {
|
||||
children: ReactNode
|
||||
className?: string
|
||||
}
|
||||
|
||||
export const Label = ({ children, className }: LabelProps) => (
|
||||
<label className={`mb-1.5 block text-th-fgd-2 ${className}`}>
|
||||
{children}
|
||||
</label>
|
||||
)
|
|
@ -0,0 +1,227 @@
|
|||
import React, { Component } from 'react'
|
||||
import { Steps } from 'intro.js-react'
|
||||
import { withTranslation } from 'react-i18next'
|
||||
import { MangoAccount } from '@blockworks-foundation/mango-client'
|
||||
import AccountsModal from './AccountsModal'
|
||||
|
||||
export const SHOW_TOUR_KEY = 'showTour'
|
||||
|
||||
interface Props {
|
||||
connected: boolean
|
||||
mangoAccount: MangoAccount
|
||||
t: any
|
||||
}
|
||||
|
||||
interface State {
|
||||
steps: any
|
||||
stepsEnabled: boolean
|
||||
initialStep: number
|
||||
showCreateAccount: boolean
|
||||
}
|
||||
|
||||
class IntroTips extends Component<Props, State> {
|
||||
steps: any
|
||||
constructor(props) {
|
||||
super(props)
|
||||
this.state = {
|
||||
showCreateAccount: false,
|
||||
stepsEnabled: true,
|
||||
initialStep: 0,
|
||||
steps: [
|
||||
{
|
||||
element: '#connect-wallet-tip',
|
||||
intro: (
|
||||
<div>
|
||||
<h4>{this.props.t('connect-wallet-tip-title')}</h4>
|
||||
<p>{this.props.t('connect-wallet-tip-desc')}</p>
|
||||
</div>
|
||||
),
|
||||
position: 'left',
|
||||
tooltipClass: 'intro-tooltip',
|
||||
highlightClass: 'intro-highlight',
|
||||
},
|
||||
{
|
||||
element: '#profile-menu-tip',
|
||||
intro: (
|
||||
<div>
|
||||
<h4>{this.props.t('profile-menu-tip-title')}</h4>
|
||||
<p>{this.props.t('profile-menu-tip-desc')}</p>
|
||||
</div>
|
||||
),
|
||||
tooltipClass: 'intro-tooltip',
|
||||
highlightClass: 'intro-highlight',
|
||||
disableInteraction: true,
|
||||
},
|
||||
{
|
||||
element: '#data-refresh-tip',
|
||||
intro: (
|
||||
<div>
|
||||
<h4>{this.props.t('data-refresh-tip-title')}</h4>
|
||||
<p>{this.props.t('data-refresh-tip-desc')}</p>
|
||||
</div>
|
||||
),
|
||||
tooltipClass: 'intro-tooltip',
|
||||
highlightClass: 'intro-highlight',
|
||||
disableInteraction: true,
|
||||
},
|
||||
{
|
||||
element: '#layout-tip',
|
||||
intro: (
|
||||
<div>
|
||||
<h4>{this.props.t('layout-tip-title')}</h4>
|
||||
<p>{this.props.t('layout-tip-desc')}</p>
|
||||
</div>
|
||||
),
|
||||
tooltipClass: 'intro-tooltip',
|
||||
highlightClass: 'intro-highlight',
|
||||
disableInteraction: true,
|
||||
},
|
||||
{
|
||||
element: '#perp-positions-tip',
|
||||
intro: (
|
||||
<div>
|
||||
<h4>{this.props.t('perp-positions-tip-title')}</h4>
|
||||
<p>
|
||||
{this.props.t('perp-positions-tip-desc')}{' '}
|
||||
<a
|
||||
className="underline"
|
||||
href="https://docs.mango.markets/mango-v3/perp-faq#what-is-my-unsettled-pnl"
|
||||
rel="noopener noreferrer"
|
||||
target="_blank"
|
||||
>
|
||||
{this.props.t('read-more')}
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
),
|
||||
position: 'left',
|
||||
tooltipClass: 'intro-tooltip',
|
||||
highlightClass: 'intro-highlight',
|
||||
disableInteraction: true,
|
||||
},
|
||||
{
|
||||
element: '#account-details-tip',
|
||||
intro: (
|
||||
<div>
|
||||
<h4>{this.props.t('account-details-tip-title')}</h4>
|
||||
<p>{this.props.t('account-details-tip-desc')}</p>
|
||||
</div>
|
||||
),
|
||||
position: 'left',
|
||||
tooltipClass: 'intro-tooltip',
|
||||
highlightClass: 'intro-highlight',
|
||||
disableInteraction: true,
|
||||
},
|
||||
{
|
||||
element: '#account-details-tip',
|
||||
intro: (
|
||||
<div>
|
||||
<h4>{this.props.t('collateral-available-tip-title')}</h4>
|
||||
<p>
|
||||
{this.props.t('collateral-available-tip-desc')}{' '}
|
||||
<a
|
||||
className="underline"
|
||||
href="https://docs.mango.markets/mango-v3/token-specs"
|
||||
rel="noopener noreferrer"
|
||||
target="_blank"
|
||||
>
|
||||
{this.props.t('read-more')}
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
),
|
||||
position: 'left',
|
||||
tooltipClass: 'intro-tooltip',
|
||||
highlightClass: 'intro-highlight',
|
||||
disableInteraction: true,
|
||||
},
|
||||
{
|
||||
element: '#account-details-tip',
|
||||
intro: (
|
||||
<div>
|
||||
<h4>{this.props.t('account-health-tip-title')}</h4>
|
||||
<p>
|
||||
{this.props.t('account-health-tip-desc')}{' '}
|
||||
<a
|
||||
className="underline"
|
||||
href="https://docs.mango.markets/mango-v3/overview#health"
|
||||
rel="noopener noreferrer"
|
||||
target="_blank"
|
||||
>
|
||||
{this.props.t('read-more')}
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
),
|
||||
position: 'left',
|
||||
tooltipClass: 'intro-tooltip',
|
||||
highlightClass: 'intro-highlight',
|
||||
disableInteraction: true,
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
closeCreateAccountModal = () => {
|
||||
this.setState({ showCreateAccount: false })
|
||||
}
|
||||
|
||||
handleEndTour = () => {
|
||||
localStorage.setItem('showTour', 'false')
|
||||
this.setState({ stepsEnabled: false })
|
||||
if (!this.props.mangoAccount) {
|
||||
this.setState({ showCreateAccount: true })
|
||||
}
|
||||
}
|
||||
|
||||
onBeforeChange = (nextStepIndex) => {
|
||||
if (nextStepIndex === 1) {
|
||||
this.steps.updateStepElement(nextStepIndex)
|
||||
const el = document.querySelector<HTMLElement>('.introjs-nextbutton')
|
||||
if (el) {
|
||||
el.style.pointerEvents = 'auto'
|
||||
el.style.opacity = '100%'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps) {
|
||||
if (this.props.connected !== prevProps.connected) {
|
||||
this.steps.introJs.nextStep()
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const { initialStep, showCreateAccount, stepsEnabled, steps } = this.state
|
||||
|
||||
return (
|
||||
<>
|
||||
<Steps
|
||||
enabled={stepsEnabled}
|
||||
steps={steps}
|
||||
initialStep={initialStep}
|
||||
onBeforeChange={this.onBeforeChange}
|
||||
onExit={() => this.handleEndTour()}
|
||||
options={{
|
||||
doneLabel: this.props.t('get-started'),
|
||||
exitOnOverlayClick: false,
|
||||
nextLabel: this.props.t('next'),
|
||||
overlayOpacity: 0.6,
|
||||
scrollToElement: true,
|
||||
showBullets: false,
|
||||
showProgress: true,
|
||||
skipLabel: this.props.t('close'),
|
||||
}}
|
||||
ref={(steps) => (this.steps = steps)}
|
||||
/>
|
||||
{showCreateAccount ? (
|
||||
<AccountsModal
|
||||
isOpen={showCreateAccount}
|
||||
onClose={this.closeCreateAccountModal}
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
)
|
||||
}
|
||||
}
|
||||
export default withTranslation()(IntroTips)
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,93 @@
|
|||
import { ConnectWalletButton } from "./ConnectWalletButton";
|
||||
import GlobalNotification from "./GlobalNotification";
|
||||
import { useCallback, useState } from "react";
|
||||
import AccountsModal from "./AccountsModal";
|
||||
import { useRouter } from "next/router";
|
||||
import FavoritesShortcutBar from "./FavoritesShortcutBar";
|
||||
import SettingsModal from "./SettingsModal";
|
||||
import { useTranslation } from "next-i18next";
|
||||
import { useWallet } from "@solana/wallet-adapter-react";
|
||||
import useMangoStore from "stores/useMangoStore";
|
||||
import { Transition } from "@headlessui/react";
|
||||
|
||||
const Layout = ({ children }) => {
|
||||
const router = useRouter();
|
||||
const { pathname } = router;
|
||||
|
||||
return (
|
||||
<div className={`flex-grow bg-th-bkg-1 text-th-fgd-1 transition-all`}>
|
||||
<div className="flex">
|
||||
<div className="w-full overflow-hidden">
|
||||
<GlobalNotification />
|
||||
<TopBar />
|
||||
{pathname === "/" ? <FavoritesShortcutBar /> : null}
|
||||
<div className="h-full">{children}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const TopBar = () => {
|
||||
const { t } = useTranslation(["common", "delegate"]);
|
||||
const { publicKey } = useWallet();
|
||||
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current);
|
||||
const [showAccountsModal, setShowAccountsModal] = useState(false);
|
||||
const [showSettingsModal, setShowSettingsModal] = useState(false);
|
||||
|
||||
const handleCloseAccounts = useCallback(() => {
|
||||
setShowAccountsModal(false);
|
||||
}, []);
|
||||
|
||||
const canWithdraw =
|
||||
mangoAccount?.owner && publicKey
|
||||
? mangoAccount?.owner?.equals(publicKey)
|
||||
: false;
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="flex h-14 items-center justify-between border-b border-th-bkg-3 bg-th-bkg-1 px-6">
|
||||
<div className="flex items-center justify-center">
|
||||
<div className={`flex flex-shrink-0 cursor-pointer items-center`}>
|
||||
<img
|
||||
className={`h-8 w-auto`}
|
||||
src="/assets/icons/logo.svg"
|
||||
alt="next"
|
||||
/>
|
||||
<Transition
|
||||
show={true}
|
||||
appear={true}
|
||||
enter="transition-all ease-in duration-300"
|
||||
enterFrom="opacity-50"
|
||||
enterTo="opacity-100"
|
||||
leave="transition ease-out duration-300"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<span className="ml-2 text-lg font-bold text-th-fgd-1">
|
||||
Mango
|
||||
</span>
|
||||
</Transition>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center space-x-4">
|
||||
<ConnectWalletButton />
|
||||
</div>
|
||||
</div>
|
||||
{showAccountsModal ? (
|
||||
<AccountsModal
|
||||
onClose={handleCloseAccounts}
|
||||
isOpen={showAccountsModal}
|
||||
/>
|
||||
) : null}
|
||||
{showSettingsModal ? (
|
||||
<SettingsModal
|
||||
onClose={() => setShowSettingsModal(false)}
|
||||
isOpen={showSettingsModal}
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default Layout;
|
|
@ -0,0 +1,342 @@
|
|||
import { ReactNode, useCallback, useEffect, useMemo, useState } from 'react'
|
||||
import dayjs from 'dayjs'
|
||||
import { MedalIcon } from './icons'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { abbreviateAddress, usdFormatter } from '../utils'
|
||||
import { ChevronRightIcon } from '@heroicons/react/solid'
|
||||
import ProfileImage from './ProfileImage'
|
||||
import { useRouter } from 'next/router'
|
||||
import { PublicKey } from '@solana/web3.js'
|
||||
import { notify } from 'utils/notifications'
|
||||
|
||||
const utc = require('dayjs/plugin/utc')
|
||||
dayjs.extend(utc)
|
||||
|
||||
const formatLeaderboardData = async (leaderboard) => {
|
||||
const walletPks = leaderboard.map((u) => u.wallet_pk)
|
||||
const profileDetailsResponse = await fetch(
|
||||
`https://mango-transaction-log.herokuapp.com/v3/user-data/multiple-profile-details?wallet-pks=${walletPks.toString()}`
|
||||
)
|
||||
const parsedProfileDetailsResponse = await profileDetailsResponse.json()
|
||||
|
||||
const leaderboardData = [] as any[]
|
||||
for (const item of leaderboard) {
|
||||
const profileDetails = parsedProfileDetailsResponse[item.wallet_pk]
|
||||
leaderboardData.push({
|
||||
...item,
|
||||
profile: profileDetails ? profileDetails : null,
|
||||
})
|
||||
}
|
||||
|
||||
return leaderboardData
|
||||
}
|
||||
|
||||
const LeaderboardTable = ({ range = '29' }) => {
|
||||
const { t } = useTranslation('common')
|
||||
const [pnlLeaderboardData, setPnlLeaderboardData] = useState<any[]>([])
|
||||
const [perpPnlLeaderboardData, setPerpPnlLeaderboardData] = useState<any[]>(
|
||||
[]
|
||||
)
|
||||
const [spotPnlLeaderboardData, setSpotPnlLeaderboardData] = useState<any[]>(
|
||||
[]
|
||||
)
|
||||
const [leaderboardType, setLeaderboardType] = useState<string>('total-pnl')
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const fetchPnlLeaderboard = async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://mango-transaction-log.herokuapp.com/v3/stats/pnl-leaderboard?start-date=${dayjs()
|
||||
.utc()
|
||||
.hour(0)
|
||||
.minute(0)
|
||||
.subtract(parseInt(range), 'day')
|
||||
.add(1, 'hour')
|
||||
.format('YYYY-MM-DDThh:00:00')}`
|
||||
)
|
||||
const parsedResponse = await response.json()
|
||||
const leaderboardData = await formatLeaderboardData(parsedResponse)
|
||||
setPnlLeaderboardData(leaderboardData)
|
||||
setLoading(false)
|
||||
} catch {
|
||||
notify({ type: 'error', title: t('fetch-leaderboard-fail') })
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const fetchPerpPnlLeaderboard = useCallback(async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://mango-transaction-log.herokuapp.com/v3/stats/perp-pnl-leaderboard?start-date=${dayjs()
|
||||
.utc()
|
||||
.hour(0)
|
||||
.minute(0)
|
||||
.subtract(parseInt(range), 'day')
|
||||
.add(1, 'hour')
|
||||
.format('YYYY-MM-DDThh:00:00')}`
|
||||
)
|
||||
const parsedResponse = await response.json()
|
||||
const leaderboardData = await formatLeaderboardData(parsedResponse)
|
||||
setPerpPnlLeaderboardData(leaderboardData)
|
||||
setLoading(false)
|
||||
} catch {
|
||||
notify({ type: 'error', title: t('fetch-leaderboard-fail') })
|
||||
setLoading(false)
|
||||
}
|
||||
}, [range, t])
|
||||
|
||||
const fetchSpotPnlLeaderboard = useCallback(async () => {
|
||||
setLoading(true)
|
||||
const response = await fetch(
|
||||
`https://mango-transaction-log.herokuapp.com/v3/stats/spot-pnl-leaderboard?start-date=${dayjs()
|
||||
.hour(0)
|
||||
.minute(0)
|
||||
.utc()
|
||||
.subtract(parseInt(range), 'day')
|
||||
.format('YYYY-MM-DDThh:00:00')}`
|
||||
)
|
||||
const parsedResponse = await response.json()
|
||||
const leaderboardData = await formatLeaderboardData(parsedResponse)
|
||||
setSpotPnlLeaderboardData(leaderboardData)
|
||||
|
||||
setLoading(false)
|
||||
}, [range])
|
||||
|
||||
useEffect(() => {
|
||||
if (leaderboardType === 'total-pnl') {
|
||||
fetchPnlLeaderboard()
|
||||
} else if (leaderboardType === 'futures-only') {
|
||||
fetchPerpPnlLeaderboard()
|
||||
} else {
|
||||
fetchSpotPnlLeaderboard()
|
||||
}
|
||||
}, [range, leaderboardType])
|
||||
|
||||
useEffect(() => {
|
||||
fetchPerpPnlLeaderboard()
|
||||
fetchSpotPnlLeaderboard()
|
||||
}, [])
|
||||
|
||||
const leaderboardData = useMemo(
|
||||
() =>
|
||||
leaderboardType === 'total-pnl'
|
||||
? pnlLeaderboardData
|
||||
: leaderboardType === 'futures-only'
|
||||
? perpPnlLeaderboardData
|
||||
: spotPnlLeaderboardData,
|
||||
[
|
||||
leaderboardType,
|
||||
pnlLeaderboardData,
|
||||
perpPnlLeaderboardData,
|
||||
spotPnlLeaderboardData,
|
||||
]
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="grid grid-cols-12 gap-6">
|
||||
<div className="col-span-12 flex space-x-3 lg:col-span-4 lg:flex-col lg:space-y-2 lg:space-x-0">
|
||||
<LeaderboardTypeButton
|
||||
leaderboardType={leaderboardType}
|
||||
setLeaderboardType={setLeaderboardType}
|
||||
range={range}
|
||||
label="total-pnl"
|
||||
/>
|
||||
<LeaderboardTypeButton
|
||||
leaderboardType={leaderboardType}
|
||||
setLeaderboardType={setLeaderboardType}
|
||||
range={range}
|
||||
label="futures-only"
|
||||
/>
|
||||
<LeaderboardTypeButton
|
||||
leaderboardType={leaderboardType}
|
||||
setLeaderboardType={setLeaderboardType}
|
||||
range={range}
|
||||
label="spot-only"
|
||||
/>
|
||||
</div>
|
||||
<div className="col-span-12 lg:col-span-8">
|
||||
{!loading ? (
|
||||
<div className="space-y-2">
|
||||
{leaderboardData.map((acc, i) => (
|
||||
<AccountCard
|
||||
rank={i + 1}
|
||||
acc={acc.mango_account}
|
||||
key={acc.mango_account}
|
||||
rawPnl={
|
||||
leaderboardType === 'total-pnl'
|
||||
? acc.pnl
|
||||
: leaderboardType === 'futures-only'
|
||||
? acc.perp_pnl
|
||||
: acc.spot_pnl
|
||||
}
|
||||
pnl={
|
||||
leaderboardType === 'total-pnl'
|
||||
? acc.pnl.toLocaleString('en-US', {
|
||||
style: 'currency',
|
||||
currency: 'USD',
|
||||
maximumFractionDigits: 0,
|
||||
})
|
||||
: leaderboardType === 'futures-only'
|
||||
? usdFormatter(acc.perp_pnl)
|
||||
: usdFormatter(acc.spot_pnl)
|
||||
}
|
||||
profile={acc.profile}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-2">
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-[84px] w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default LeaderboardTable
|
||||
|
||||
const AccountCard = ({ rank, acc, rawPnl, profile, pnl }) => {
|
||||
const router = useRouter()
|
||||
const medalColors =
|
||||
rank === 1
|
||||
? {
|
||||
darkest: '#E4AF11',
|
||||
dark: '#F2C94C',
|
||||
light: '#FFCF40',
|
||||
lightest: '#FDE877',
|
||||
}
|
||||
: rank === 2
|
||||
? {
|
||||
darkest: '#B8B8B8',
|
||||
dark: '#C0C0C0',
|
||||
light: '#C7C7C7',
|
||||
lightest: '#D8D6D6',
|
||||
}
|
||||
: {
|
||||
darkest: '#CD7F32',
|
||||
dark: '#E5994E',
|
||||
light: '#DBA36B',
|
||||
lightest: '#EFBF8D',
|
||||
}
|
||||
return (
|
||||
<div className="relative">
|
||||
{profile ? (
|
||||
<button
|
||||
className="absolute left-[118px] bottom-4 flex items-center space-x-2 rounded-full border border-th-fgd-4 px-2 py-1 hover:border-th-fgd-2 hover:filter"
|
||||
onClick={() =>
|
||||
router.push(
|
||||
`/profile?name=${profile?.profile_name.replace(/\s/g, '-')}`,
|
||||
undefined,
|
||||
{
|
||||
shallow: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
>
|
||||
<p className="mb-0 text-xs capitalize text-th-fgd-3">
|
||||
{profile?.profile_name}
|
||||
</p>
|
||||
</button>
|
||||
) : null}
|
||||
<a
|
||||
className="default-transition block flex h-[112px] w-full rounded-md border border-th-bkg-4 p-4 hover:border-th-fgd-4 sm:h-[84px] sm:justify-between sm:pb-4"
|
||||
href={`/account?pubkey=${acc}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<p className="my-auto mr-4 flex w-5 justify-center font-bold">{rank}</p>
|
||||
<div className="relative my-auto">
|
||||
{rank < 4 ? (
|
||||
<MedalIcon
|
||||
className="absolute -top-1 -left-1 z-10 h-5 w-auto drop-shadow-lg"
|
||||
colors={medalColors}
|
||||
/>
|
||||
) : null}
|
||||
<ProfileImage
|
||||
imageSize="56"
|
||||
placeholderSize="32"
|
||||
imageUrl={profile?.profile_image_url}
|
||||
/>
|
||||
</div>
|
||||
<div className="ml-3 flex flex-col sm:flex-grow sm:flex-row sm:justify-between">
|
||||
<p className="mb-0 font-bold text-th-fgd-2">
|
||||
{abbreviateAddress(new PublicKey(acc))}
|
||||
</p>
|
||||
|
||||
<span
|
||||
className={`flex items-center text-lg font-bold ${
|
||||
rawPnl > 0 ? 'text-th-green' : 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{pnl}
|
||||
</span>
|
||||
</div>
|
||||
<div className="my-auto ml-auto">
|
||||
<ChevronRightIcon className="ml-2 mt-0.5 h-5 w-5 text-th-fgd-4" />
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const LeaderboardTypeButton = ({
|
||||
leaderboardType,
|
||||
setLeaderboardType,
|
||||
range,
|
||||
icon,
|
||||
label,
|
||||
}: {
|
||||
leaderboardType: string
|
||||
setLeaderboardType: (x) => void
|
||||
range: string
|
||||
icon?: ReactNode
|
||||
label: string
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
return (
|
||||
<button
|
||||
className={`relative flex w-full items-center justify-center rounded-md p-4 text-center lg:h-20 lg:justify-start lg:px-6 lg:text-left ${
|
||||
leaderboardType === label
|
||||
? 'bg-th-bkg-3 text-th-fgd-1 after:absolute after:top-[100%] after:left-1/2 after:-translate-x-1/2 after:transform after:border-l-[12px] after:border-r-[12px] after:border-t-[12px] after:border-l-transparent after:border-t-th-bkg-3 after:border-r-transparent lg:after:left-[100%] lg:after:top-1/2 lg:after:-translate-x-0 lg:after:-translate-y-1/2 lg:after:border-r-0 lg:after:border-b-[12px] lg:after:border-t-transparent lg:after:border-b-transparent lg:after:border-l-th-bkg-3'
|
||||
: 'bg-th-bkg-2 text-th-fgd-3 md:hover:bg-th-bkg-3'
|
||||
}`}
|
||||
onClick={() => setLeaderboardType(label)}
|
||||
>
|
||||
{icon}
|
||||
<div>
|
||||
<div className="font-bold sm:text-lg">{t(label)}</div>
|
||||
<span className="text-sm text-th-fgd-4">
|
||||
{range === '9999'
|
||||
? 'All-time'
|
||||
: range === '29'
|
||||
? 'Last 30 days'
|
||||
: range === '1'
|
||||
? 'Last 24 hours'
|
||||
: `Last ${range} days`}
|
||||
</span>
|
||||
</div>
|
||||
</button>
|
||||
)
|
||||
}
|
|
@ -0,0 +1,195 @@
|
|||
export {}
|
||||
|
||||
// import React, { useEffect, useMemo, useState } from 'react'
|
||||
// import Slider from 'rc-slider'
|
||||
// import 'rc-slider/assets/index.css'
|
||||
// import useMangoStore from '../stores/useMangoStore'
|
||||
// import {
|
||||
// getMarketIndexBySymbol,
|
||||
// getWeights,
|
||||
// I80F48,
|
||||
// PerpMarket,
|
||||
// } from '@blockworks-foundation/mango-client'
|
||||
// import tw from 'twin.macro'
|
||||
// import styled from '@emotion/styled'
|
||||
// import 'rc-slider/assets/index.css'
|
||||
// import { useTranslation } from 'next-i18next'
|
||||
|
||||
// type StyledSliderProps = {
|
||||
// enableTransition?: boolean
|
||||
// disabled?: boolean
|
||||
// }
|
||||
|
||||
// const StyledSlider = styled(Slider)<StyledSliderProps>`
|
||||
// .rc-slider-rail {
|
||||
// ${tw`bg-th-primary h-2 rounded-full`}
|
||||
// opacity: 0.6;
|
||||
// }
|
||||
// .rc-slider-track {
|
||||
// ${tw`bg-th-primary h-2 rounded-full ring-1 ring-th-primary ring-inset`}
|
||||
// ${({ enableTransition }) =>
|
||||
// enableTransition && tw`transition-all duration-500`}
|
||||
// }
|
||||
// .rc-slider-step {
|
||||
// ${tw`hidden`}
|
||||
// }
|
||||
// .rc-slider-handle {
|
||||
// ${tw`border-4 border-th-primary h-4 w-4 ring-white light:ring-gray-400 hover:ring-4 hover:ring-opacity-50 active:ring-8 active:ring-opacity-50`}
|
||||
// background: #fff;
|
||||
// margin-top: -4px;
|
||||
// ${({ enableTransition }) =>
|
||||
// enableTransition && tw`transition-all duration-500`}
|
||||
// ${({ disabled }) => disabled && tw`bg-th-fgd-3 border-th-fgd-4`}
|
||||
// }
|
||||
// ${({ disabled }) => disabled && 'background-color: transparent'}
|
||||
// `
|
||||
|
||||
// type SliderProps = {
|
||||
// onChange: (x) => void
|
||||
// onAfterChange?: (x) => void
|
||||
// step: number
|
||||
// value: number
|
||||
// side: 'buy' | 'sell'
|
||||
// price: number
|
||||
// disabled?: boolean
|
||||
// max?: number
|
||||
// maxButtonTransition?: boolean
|
||||
// decimalCount: number
|
||||
// }
|
||||
|
||||
// const percentToClose = (size, total) => {
|
||||
// return (size / total) * 100
|
||||
// }
|
||||
|
||||
// export default function LeverageSlider({
|
||||
// onChange,
|
||||
// onAfterChange,
|
||||
// step,
|
||||
// value,
|
||||
// disabled,
|
||||
// maxButtonTransition,
|
||||
// side,
|
||||
// price,
|
||||
// decimalCount,
|
||||
// }: SliderProps) {
|
||||
// const { t } = useTranslation('common')
|
||||
// const [enableTransition, setEnableTransition] = useState(false)
|
||||
// const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
|
||||
// const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
// const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
|
||||
// const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
|
||||
// const marketConfig = useMangoStore((s) => s.selectedMarket.config)
|
||||
// const market = useMangoStore((s) => s.selectedMarket.current)
|
||||
// const marketIndex = getMarketIndexBySymbol(
|
||||
// mangoGroupConfig,
|
||||
// marketConfig.baseSymbol
|
||||
// )
|
||||
|
||||
// const initLeverage = useMemo(() => {
|
||||
// if (!mangoGroup || !marketConfig) return 1
|
||||
|
||||
// const ws = getWeights(mangoGroup, marketConfig.marketIndex, 'Init')
|
||||
// const w =
|
||||
// marketConfig.kind === 'perp' ? ws.perpAssetWeight : ws.spotAssetWeight
|
||||
// return Math.round((100 * -1) / (w.toNumber() - 1)) / 100
|
||||
// }, [mangoGroup, marketConfig])
|
||||
|
||||
// const { max, deposits, borrows } = useMemo(() => {
|
||||
// if (!mangoAccount) return { max: 0 }
|
||||
// const priceOrDefault = price
|
||||
// ? I80F48.fromNumber(price)
|
||||
// : mangoGroup.getPrice(marketIndex, mangoCache)
|
||||
|
||||
// const {
|
||||
// max: maxQuote,
|
||||
// deposits,
|
||||
// borrows,
|
||||
// } = mangoAccount.getMaxLeverageForMarket(
|
||||
// mangoGroup,
|
||||
// mangoCache,
|
||||
// marketIndex,
|
||||
// market,
|
||||
// side,
|
||||
// priceOrDefault
|
||||
// )
|
||||
|
||||
// if (maxQuote.toNumber() <= 0) return { max: 0 }
|
||||
// // multiply the maxQuote by a scaler value to account for
|
||||
// // srm fees or rounding issues in getMaxLeverageForMarket
|
||||
// const maxScaler = market instanceof PerpMarket ? 0.99 : 0.95
|
||||
// const scaledMax =
|
||||
// (maxQuote.toNumber() * maxScaler) /
|
||||
// mangoGroup.getPrice(marketIndex, mangoCache).toNumber()
|
||||
|
||||
// return { max: scaledMax, deposits, borrows }
|
||||
// }, [mangoAccount, mangoGroup, mangoCache, marketIndex, market, side, price])
|
||||
|
||||
// useEffect(() => {
|
||||
// if (maxButtonTransition) {
|
||||
// setEnableTransition(true)
|
||||
// }
|
||||
// }, [maxButtonTransition])
|
||||
|
||||
// useEffect(() => {
|
||||
// if (enableTransition) {
|
||||
// const transitionTimer = setTimeout(() => {
|
||||
// setEnableTransition(false)
|
||||
// }, 500)
|
||||
// return () => clearTimeout(transitionTimer)
|
||||
// }
|
||||
// }, [enableTransition])
|
||||
|
||||
// // if (!mangoAccount) return null
|
||||
|
||||
// const roundedDeposits = parseFloat(deposits?.toFixed(decimalCount))
|
||||
// const roundedBorrows = parseFloat(borrows?.toFixed(decimalCount))
|
||||
|
||||
// const closeDepositString =
|
||||
// percentToClose(value, roundedDeposits) > 100
|
||||
// ? `100% ${t('close-and-short')}`
|
||||
// : `${percentToClose(value, roundedDeposits).toFixed(1)}% ${t(
|
||||
// 'close-position'
|
||||
// )}`
|
||||
|
||||
// const closeBorrowString =
|
||||
// percentToClose(value, roundedBorrows) > 100
|
||||
// ? `100% ${t('close-and-long')}`
|
||||
// : `${percentToClose(value, roundedBorrows).toFixed(1)}% ${t(
|
||||
// 'close-position'
|
||||
// )}`
|
||||
|
||||
// const setMaxLeverage = function () {
|
||||
// onChange(Math.round(max / step) * step)
|
||||
// }
|
||||
|
||||
// return (
|
||||
// <>
|
||||
// <div className="flex mt-2 items-center pl-1 pr-1">
|
||||
// <StyledSlider
|
||||
// min={0}
|
||||
// max={max}
|
||||
// value={value || 0}
|
||||
// onChange={onChange}
|
||||
// onAfterChange={onAfterChange}
|
||||
// step={step}
|
||||
// disabled={disabled}
|
||||
// />
|
||||
// <button
|
||||
// className="bg-th-bkg-4 hover:brightness-[1.15] font-normal rounded text-th-fgd-1 text-xs p-2 ml-2"
|
||||
// onClick={setMaxLeverage}
|
||||
// >
|
||||
// {initLeverage}x
|
||||
// </button>
|
||||
// </div>
|
||||
// {side === 'sell' ? (
|
||||
// <div className="text-th-fgd-4 text-xs tracking-normal mt-1">
|
||||
// <span>{roundedDeposits > 0 ? closeDepositString : null}</span>
|
||||
// </div>
|
||||
// ) : (
|
||||
// <div className="text-th-fgd-4 text-xs tracking-normal mt-1">
|
||||
// <span>{roundedBorrows > 0 ? closeBorrowString : null}</span>
|
||||
// </div>
|
||||
// )}
|
||||
// </>
|
||||
// )
|
||||
// }
|
|
@ -0,0 +1,26 @@
|
|||
const Loading = ({ className = '' }) => {
|
||||
return (
|
||||
<svg
|
||||
className={`${className} h-5 w-5 animate-spin-fast`}
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<circle
|
||||
className={`opacity-25`}
|
||||
cx="12"
|
||||
cy="12"
|
||||
r="10"
|
||||
stroke="currentColor"
|
||||
strokeWidth="4"
|
||||
></circle>
|
||||
<path
|
||||
className={`opacity-75`}
|
||||
fill="currentColor"
|
||||
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
|
||||
></path>
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
export default Loading
|
|
@ -0,0 +1,120 @@
|
|||
import { MangoAccount, MangoGroup } from '@blockworks-foundation/mango-client'
|
||||
import {
|
||||
ArrowSmDownIcon,
|
||||
ArrowSmUpIcon,
|
||||
HeartIcon,
|
||||
UsersIcon,
|
||||
} from '@heroicons/react/outline'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import useMangoStore from 'stores/useMangoStore'
|
||||
import { abbreviateAddress } from 'utils'
|
||||
import Tooltip from './Tooltip'
|
||||
|
||||
export const numberCurrencyCompacter = Intl.NumberFormat('en-us', {
|
||||
notation: 'compact',
|
||||
style: 'currency',
|
||||
currency: 'USD',
|
||||
maximumFractionDigits: 2,
|
||||
})
|
||||
|
||||
const MangoAccountCard = ({
|
||||
mangoAccount,
|
||||
pnl,
|
||||
}: {
|
||||
mangoAccount: MangoAccount
|
||||
pnl?: number
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const { publicKey } = useWallet()
|
||||
return (
|
||||
<div>
|
||||
<p className="mb-1 flex items-center font-bold text-th-fgd-1">
|
||||
{pnl ? (
|
||||
<a
|
||||
className="default-transition text-th-fgd-1 hover:text-th-fgd-3"
|
||||
href={`https://trade.mango.markets/account?pubkey=${mangoAccount.publicKey.toString()}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
{mangoAccount?.name || abbreviateAddress(mangoAccount.publicKey)}
|
||||
</a>
|
||||
) : (
|
||||
<span>
|
||||
{mangoAccount?.name || abbreviateAddress(mangoAccount.publicKey)}
|
||||
</span>
|
||||
)}
|
||||
{publicKey && !mangoAccount?.owner.equals(publicKey) ? (
|
||||
<Tooltip content={t('delegate:delegated-account')}>
|
||||
<UsersIcon className="ml-1.5 h-3 w-3 text-th-fgd-3" />
|
||||
</Tooltip>
|
||||
) : (
|
||||
''
|
||||
)}
|
||||
</p>
|
||||
{mangoGroup && (
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
<AccountInfo
|
||||
mangoGroup={mangoGroup}
|
||||
mangoAccount={mangoAccount}
|
||||
pnl={pnl}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default MangoAccountCard
|
||||
|
||||
const AccountInfo = ({
|
||||
mangoGroup,
|
||||
mangoAccount,
|
||||
pnl,
|
||||
}: {
|
||||
mangoGroup: MangoGroup
|
||||
mangoAccount: MangoAccount
|
||||
pnl?: number
|
||||
}) => {
|
||||
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
|
||||
if (!mangoCache) {
|
||||
return null
|
||||
}
|
||||
const accountEquity = mangoAccount.computeValue(mangoGroup, mangoCache)
|
||||
const health = mangoAccount.getHealthRatio(mangoGroup, mangoCache, 'Maint')
|
||||
|
||||
return (
|
||||
<div className="flex items-center text-xs text-th-fgd-3">
|
||||
{numberCurrencyCompacter.format(accountEquity.toNumber())}
|
||||
<span className="pl-2 pr-1 text-th-fgd-4">|</span>
|
||||
{pnl ? (
|
||||
<span
|
||||
className={`flex items-center ${
|
||||
pnl < 0 ? 'text-th-red' : 'text-th-green'
|
||||
}`}
|
||||
>
|
||||
{pnl < 0 ? (
|
||||
<ArrowSmDownIcon className="mr-0.5 h-4 w-4" />
|
||||
) : (
|
||||
<ArrowSmUpIcon className="mr-0.5 h-4 w-4" />
|
||||
)}
|
||||
{numberCurrencyCompacter.format(pnl)}
|
||||
</span>
|
||||
) : (
|
||||
<span
|
||||
className={`flex items-center ${
|
||||
Number(health) < 15
|
||||
? 'text-th-red'
|
||||
: Number(health) < 30
|
||||
? 'text-th-orange'
|
||||
: 'text-th-green'
|
||||
}`}
|
||||
>
|
||||
<HeartIcon className="mr-0.5 h-4 w-4" />
|
||||
{Number(health) > 100 ? '>100' : health.toFixed(0)}%
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
import { MangoAccount } from '@blockworks-foundation/mango-client'
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import Select from './Select'
|
||||
import { abbreviateAddress } from '../utils'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
|
||||
type MangoAccountSelectProps = {
|
||||
className?: string
|
||||
onChange?: (x) => void
|
||||
value?: MangoAccount | null
|
||||
disabled?: boolean
|
||||
}
|
||||
|
||||
const MangoAccountSelect = ({
|
||||
onChange,
|
||||
value,
|
||||
disabled = false,
|
||||
className = '',
|
||||
}: MangoAccountSelectProps) => {
|
||||
const { publicKey } = useWallet()
|
||||
const { t } = useTranslation('common')
|
||||
const mangoAccounts = useMangoStore((s) => s.mangoAccounts)
|
||||
const mangoAccountsWithoutDelegates = useMemo(() => {
|
||||
return mangoAccounts.filter((ma) => {
|
||||
return publicKey && ma?.owner?.equals(publicKey) ? true : false
|
||||
})
|
||||
}, [mangoAccounts, publicKey])
|
||||
const [selectedMangoAccount, setSelectedMangoAccount] = useState(
|
||||
value || mangoAccountsWithoutDelegates[0]
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (value) {
|
||||
setSelectedMangoAccount(value)
|
||||
}
|
||||
}, [value])
|
||||
|
||||
const handleSelectMangoAccount = (value) => {
|
||||
const mangoAccount = mangoAccounts.find(
|
||||
(ma) => ma.publicKey.toString() === value
|
||||
)
|
||||
if (!mangoAccount) {
|
||||
return
|
||||
}
|
||||
setSelectedMangoAccount(mangoAccount)
|
||||
if (onChange) {
|
||||
onChange(mangoAccount)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Select
|
||||
disabled={disabled}
|
||||
value={
|
||||
<div className="text-left">
|
||||
<p className="mb-0 font-bold text-th-fgd-2">
|
||||
{selectedMangoAccount?.name
|
||||
? selectedMangoAccount.name
|
||||
: t('account')}
|
||||
</p>
|
||||
<p className="mb-0 text-xs">
|
||||
{abbreviateAddress(selectedMangoAccount?.publicKey)}
|
||||
</p>
|
||||
</div>
|
||||
}
|
||||
onChange={handleSelectMangoAccount}
|
||||
placeholder={t('select-margin')}
|
||||
className={className}
|
||||
>
|
||||
{mangoAccounts.length ? (
|
||||
mangoAccountsWithoutDelegates.map((ma, index) => (
|
||||
<Select.Option key={index} value={ma.publicKey.toString()}>
|
||||
<div className="text-left">
|
||||
<span
|
||||
className={`mb-0 font-bold ${
|
||||
value?.publicKey.toString() === ma.publicKey.toString()
|
||||
? 'text-th-primary'
|
||||
: ''
|
||||
}`}
|
||||
>
|
||||
{ma?.name ? ma.name : t('account')}
|
||||
</span>
|
||||
<p className="mb-0 text-xs">{abbreviateAddress(ma?.publicKey)}</p>
|
||||
</div>
|
||||
</Select.Option>
|
||||
))
|
||||
) : (
|
||||
<Select.Option value className="text-th-fgd-4">
|
||||
{t('no-margin')}
|
||||
</Select.Option>
|
||||
)}
|
||||
</Select>
|
||||
)
|
||||
}
|
||||
|
||||
export default MangoAccountSelect
|
|
@ -0,0 +1,29 @@
|
|||
const MangoIcon = ({ className }) => {
|
||||
return (
|
||||
<svg
|
||||
className={className}
|
||||
width="14"
|
||||
height="20"
|
||||
viewBox="0 0 22 29"
|
||||
fill="none"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
clipRule="evenodd"
|
||||
d="M19.3612 4.7088L19.3624 4.70878L19.3954 4.68752C15.1331 -2.5831 9.55993 3.14112 9.55993 3.14112L9.5679 3.15513L9.56692 3.15538C12.7718 8.77426 18.8625 5.02924 19.3612 4.7088Z"
|
||||
strokeWidth="2.5"
|
||||
strokeLinejoin="round"
|
||||
/>
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
clipRule="evenodd"
|
||||
d="M10.4268 8.60034C11.4416 9.12103 12.5521 9.29944 13.6147 9.29636C14.2321 9.86347 14.7458 10.5296 15.2285 11.2136C15.4286 11.4993 15.6091 11.7982 15.7688 12.1084C16.1378 12.8191 16.4093 13.5711 16.6839 14.332C16.7644 14.555 16.8454 14.7792 16.929 15.003C16.9552 15.0988 16.9826 15.1946 17.0111 15.2905L17.0126 15.2902C17.4368 16.7451 18.0562 18.2994 18.8862 19.5826C19.1607 20.0042 19.4674 20.4037 19.8051 20.7767C19.8715 20.8486 19.9395 20.9198 20.0076 20.9911C20.3255 21.3241 20.6457 21.6596 20.8272 22.0792C21.0531 22.6022 21.0358 23.2037 20.8999 23.7571C20.2586 26.3656 17.7466 27.0509 15.3459 27.1141L15.3471 27.1111C14.447 27.1291 13.5531 27.062 12.7604 26.9808C12.7604 26.9808 8.93745 26.5851 5.69698 24.216L5.59239 24.1382C5.21415 23.8564 4.85332 23.5518 4.51186 23.2263C3.6036 22.3594 2.79544 21.3762 2.17743 20.2968C2.1801 20.2941 2.18277 20.2915 2.18543 20.2888L2.18982 20.2844C2.11668 20.1527 2.04656 20.0198 1.97946 19.8855C1.38159 18.6889 1.01722 17.3863 1.00072 16.0018C0.9726 13.6787 1.76736 11.2996 3.27803 9.57468L3.27642 9.57041C4.09436 8.67445 5.11672 7.97059 6.327 7.56987C7.08575 7.31635 7.8823 7.19462 8.68213 7.20997C9.16883 7.78148 9.76147 8.25316 10.4268 8.60034Z"
|
||||
strokeWidth="2.5"
|
||||
strokeLinejoin="round"
|
||||
/>
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
export default MangoIcon
|
|
@ -0,0 +1,48 @@
|
|||
import { useEffect, useState } from 'react'
|
||||
import { RefreshClockwiseIcon } from './icons'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import Tooltip from './Tooltip'
|
||||
import { IconButton } from './Button'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
|
||||
const ManualRefresh = ({ className = '' }) => {
|
||||
const { t } = useTranslation('common')
|
||||
const [spin, setSpin] = useState(false)
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
|
||||
|
||||
const handleRefreshData = async () => {
|
||||
setSpin(true)
|
||||
await actions.fetchMangoGroup()
|
||||
if (mangoAccount) {
|
||||
await actions.reloadMangoAccount()
|
||||
actions.reloadOrders()
|
||||
actions.fetchTradeHistory()
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
let timer
|
||||
if (spin) {
|
||||
timer = setTimeout(() => setSpin(false), 5000)
|
||||
}
|
||||
|
||||
return () => {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
}, [spin])
|
||||
|
||||
return (
|
||||
<div className={`relative inline-flex ${className}`}>
|
||||
<Tooltip content={t('refresh-data')} className="py-1 text-xs">
|
||||
<IconButton onClick={handleRefreshData} disabled={spin}>
|
||||
<RefreshClockwiseIcon
|
||||
className={`h-4 w-4 ${spin ? 'animate-spin' : null}`}
|
||||
/>
|
||||
</IconButton>
|
||||
</Tooltip>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ManualRefresh
|
|
@ -0,0 +1,194 @@
|
|||
import { ElementTitle } from './styles'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import { getPrecisionDigits, i80f48ToPercent } from '../utils'
|
||||
import Tooltip from './Tooltip'
|
||||
import { nativeI80F48ToUi } from '@blockworks-foundation/mango-client'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from './TradePageGrid'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
import { useRouter } from 'next/router'
|
||||
|
||||
export default function MarketBalances() {
|
||||
const { t } = useTranslation('common')
|
||||
const { connected } = useWallet()
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
|
||||
const mangoGroupCache = useMangoStore((s) => s.selectedMangoGroup.cache)
|
||||
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
|
||||
const selectedMarket = useMangoStore((s) => s.selectedMarket.current)
|
||||
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
|
||||
const setMangoStore = useMangoStore((s) => s.set)
|
||||
const price = useMangoStore((s) => s.tradeForm.price)
|
||||
const isLoading = useMangoStore((s) => s.selectedMangoAccount.initialLoad)
|
||||
const baseSymbol = marketConfig.baseSymbol
|
||||
const { width } = useViewport()
|
||||
const isMobile = width ? width < breakpoints.sm : false
|
||||
const router = useRouter()
|
||||
const { pubkey } = router.query
|
||||
|
||||
const handleSizeClick = (size, symbol) => {
|
||||
if (!selectedMarket || !mangoGroup || !mangoGroupCache) return
|
||||
const minOrderSize = selectedMarket.minOrderSize
|
||||
const sizePrecisionDigits = getPrecisionDigits(minOrderSize)
|
||||
const marketIndex = marketConfig.marketIndex
|
||||
|
||||
const priceOrDefault = price
|
||||
? price
|
||||
: mangoGroup.getPriceUi(marketIndex, mangoGroupCache)
|
||||
|
||||
let roundedSize, side
|
||||
if (symbol === 'USDC') {
|
||||
roundedSize = parseFloat(
|
||||
(
|
||||
Math.abs(size) / priceOrDefault +
|
||||
(size < 0 ? minOrderSize / 2 : -minOrderSize / 2)
|
||||
) // round up so neg USDC gets cleared
|
||||
.toFixed(sizePrecisionDigits)
|
||||
)
|
||||
side = size > 0 ? 'buy' : 'sell'
|
||||
} else {
|
||||
roundedSize = parseFloat(
|
||||
(
|
||||
Math.abs(size) + (size < 0 ? minOrderSize / 2 : -minOrderSize / 2)
|
||||
).toFixed(sizePrecisionDigits)
|
||||
)
|
||||
side = size > 0 ? 'sell' : 'buy'
|
||||
}
|
||||
const quoteSize = parseFloat((roundedSize * priceOrDefault).toFixed(2))
|
||||
setMangoStore((state) => {
|
||||
state.tradeForm.baseSize = roundedSize
|
||||
state.tradeForm.quoteSize = quoteSize
|
||||
state.tradeForm.side = side
|
||||
})
|
||||
}
|
||||
|
||||
if (!mangoGroup || !selectedMarket || !mangoGroupCache) return null
|
||||
|
||||
return (
|
||||
<div className={!connected && !pubkey ? 'blur filter' : ''}>
|
||||
{!isMobile ? (
|
||||
<ElementTitle className="hidden 2xl:flex">{t('balances')}</ElementTitle>
|
||||
) : null}
|
||||
{mangoGroup ? (
|
||||
<div className="grid grid-cols-2 grid-rows-1 gap-4 md:pt-2">
|
||||
{mangoGroupConfig.tokens
|
||||
.filter((t) => t.symbol === baseSymbol || t.symbol === 'USDC')
|
||||
.reverse()
|
||||
.map(({ decimals, symbol, mintKey }) => {
|
||||
const tokenIndex = mangoGroup.getTokenIndex(mintKey)
|
||||
const balance = mangoAccount
|
||||
? nativeI80F48ToUi(
|
||||
mangoAccount.getNet(
|
||||
mangoGroupCache.rootBankCache[tokenIndex],
|
||||
tokenIndex
|
||||
),
|
||||
decimals
|
||||
)
|
||||
: 0
|
||||
const availableBalance = mangoAccount
|
||||
? nativeI80F48ToUi(
|
||||
mangoAccount.getAvailableBalance(
|
||||
mangoGroup,
|
||||
mangoGroupCache,
|
||||
tokenIndex
|
||||
),
|
||||
decimals
|
||||
)
|
||||
: 0
|
||||
|
||||
return (
|
||||
<div
|
||||
className="rounded-md border border-th-bkg-4 p-4"
|
||||
key={mintKey.toString()}
|
||||
>
|
||||
<div className="mb-4 flex items-center justify-between">
|
||||
<div className="flex items-center">
|
||||
<img
|
||||
alt=""
|
||||
src={`/assets/icons/${symbol.toLowerCase()}.svg`}
|
||||
className={`mr-2.5 h-5 w-auto`}
|
||||
/>
|
||||
<span className="text-th-fgd-2">{symbol}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="pb-3">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('balance')}
|
||||
</div>
|
||||
<div
|
||||
className={`text-th-fgd-1 ${
|
||||
balance != 0
|
||||
? 'cursor-pointer underline hover:no-underline'
|
||||
: ''
|
||||
}`}
|
||||
onClick={() => handleSizeClick(balance, symbol)}
|
||||
>
|
||||
{isLoading ? (
|
||||
<DataLoader />
|
||||
) : (
|
||||
balance.toLocaleString(undefined, {
|
||||
maximumFractionDigits: decimals,
|
||||
})
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="pb-3">
|
||||
<Tooltip content={t('tooltip-available-after')}>
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('available-balance')}
|
||||
</div>
|
||||
</Tooltip>
|
||||
<div
|
||||
className={`text-th-fgd-1 ${
|
||||
availableBalance > selectedMarket.minOrderSize
|
||||
? 'cursor-pointer underline hover:no-underline'
|
||||
: ''
|
||||
}`}
|
||||
onClick={() => handleSizeClick(availableBalance, symbol)}
|
||||
>
|
||||
{isLoading ? (
|
||||
<DataLoader />
|
||||
) : (
|
||||
availableBalance.toLocaleString(undefined, {
|
||||
maximumFractionDigits: decimals,
|
||||
})
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<Tooltip content={t('tooltip-apy-apr')}>
|
||||
<div
|
||||
className={`default-transition cursor-help pb-0.5 text-xs font-normal text-th-fgd-3 hover:border-th-bkg-2 hover:text-th-fgd-3`}
|
||||
>
|
||||
{t('rates')}
|
||||
</div>
|
||||
<div className={`text-th-fgd-1`}>
|
||||
<span className={`text-th-green`}>
|
||||
{i80f48ToPercent(
|
||||
mangoGroup.getDepositRate(tokenIndex)
|
||||
).toFixed(2)}
|
||||
%
|
||||
</span>
|
||||
<span className={`text-th-fgd-4`}>{' / '}</span>
|
||||
<span className={`text-th-red`}>
|
||||
{i80f48ToPercent(
|
||||
mangoGroup.getBorrowRate(tokenIndex)
|
||||
).toFixed(2)}
|
||||
%
|
||||
</span>
|
||||
</div>
|
||||
</Tooltip>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export const DataLoader = () => (
|
||||
<div className="h-5 w-10 animate-pulse rounded-sm bg-th-bkg-3" />
|
||||
)
|
|
@ -0,0 +1,116 @@
|
|||
import { FunctionComponent, useState } from 'react'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import {
|
||||
MarketConfig,
|
||||
PerpMarket,
|
||||
ZERO_BN,
|
||||
} from '@blockworks-foundation/mango-client'
|
||||
import Button, { LinkButton } from './Button'
|
||||
import { notify } from '../utils/notifications'
|
||||
import Loading from './Loading'
|
||||
import { sleep } from '../utils'
|
||||
import Modal from './Modal'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
|
||||
interface MarketCloseModalProps {
|
||||
onClose: () => void
|
||||
isOpen: boolean
|
||||
position: {
|
||||
marketConfig: MarketConfig
|
||||
perpMarket: PerpMarket
|
||||
}
|
||||
}
|
||||
|
||||
const MarketCloseModal: FunctionComponent<MarketCloseModalProps> = ({
|
||||
onClose,
|
||||
isOpen,
|
||||
position,
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const [submitting, setSubmitting] = useState(false)
|
||||
const { wallet } = useWallet()
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
const { marketConfig, perpMarket } = position
|
||||
|
||||
async function handleMarketClose() {
|
||||
const mangoAccount = useMangoStore.getState().selectedMangoAccount.current
|
||||
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
const askInfo =
|
||||
useMangoStore.getState().accountInfos[marketConfig.asksKey.toString()]
|
||||
const bidInfo =
|
||||
useMangoStore.getState().accountInfos[marketConfig.bidsKey.toString()]
|
||||
|
||||
const orderbook = useMangoStore.getState().selectedMarket.orderBook
|
||||
const markPrice = useMangoStore.getState().selectedMarket.markPrice
|
||||
const referrerPk = useMangoStore.getState().referrerPk
|
||||
|
||||
// The reference price is the book mid if book is double sided; else mark price
|
||||
const bb = orderbook?.bids?.length > 0 && Number(orderbook.bids[0][0])
|
||||
const ba = orderbook?.asks?.length > 0 && Number(orderbook.asks[0][0])
|
||||
const referencePrice = bb && ba ? (bb + ba) / 2 : markPrice
|
||||
|
||||
if (!wallet || !mangoGroup || !mangoAccount) return
|
||||
setSubmitting(true)
|
||||
|
||||
try {
|
||||
const perpAccount = mangoAccount.perpAccounts[marketConfig.marketIndex]
|
||||
const side = perpAccount.basePosition.gt(ZERO_BN) ? 'sell' : 'buy'
|
||||
// send a large size to ensure we are reducing the entire position
|
||||
const size =
|
||||
Math.abs(perpMarket.baseLotsToNumber(perpAccount.basePosition)) * 2
|
||||
|
||||
// hard coded for now; market orders are very dangerous and fault prone
|
||||
const maxSlippage: number | undefined = 0.025
|
||||
|
||||
const txid = await mangoClient.placePerpOrder2(
|
||||
mangoGroup,
|
||||
mangoAccount,
|
||||
perpMarket,
|
||||
wallet?.adapter,
|
||||
side,
|
||||
referencePrice * (1 + (side === 'buy' ? 1 : -1) * maxSlippage),
|
||||
size,
|
||||
{
|
||||
orderType: 'ioc',
|
||||
bookSideInfo: side === 'buy' ? askInfo : bidInfo,
|
||||
reduceOnly: true,
|
||||
referrerMangoAccountPk: referrerPk ? referrerPk : undefined,
|
||||
}
|
||||
)
|
||||
await sleep(500)
|
||||
await actions.reloadMangoAccount()
|
||||
notify({ title: t('transaction-sent'), txid })
|
||||
} catch (e) {
|
||||
notify({
|
||||
title: t('order-error'),
|
||||
description: e.message,
|
||||
txid: e.txid,
|
||||
type: 'error',
|
||||
})
|
||||
} finally {
|
||||
setSubmitting(false)
|
||||
onClose()
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal onClose={onClose} isOpen={isOpen}>
|
||||
<h2 className="mb-2">
|
||||
{t('close-confirm', { config_name: marketConfig.name })}
|
||||
</h2>
|
||||
<div className="pb-6 text-th-fgd-3">{t('price-expect')}</div>
|
||||
<div className="flex items-center">
|
||||
<Button onClick={handleMarketClose}>
|
||||
{submitting ? <Loading /> : <span>{t('close-position')}</span>}
|
||||
</Button>
|
||||
<LinkButton className="ml-4 text-th-fgd-1" onClick={onClose}>
|
||||
{t('cancel')}
|
||||
</LinkButton>
|
||||
</div>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default MarketCloseModal
|
|
@ -0,0 +1,191 @@
|
|||
import React, { useMemo } from 'react'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import UiLock from './UiLock'
|
||||
import ManualRefresh from './ManualRefresh'
|
||||
import useOraclePrice from '../hooks/useOraclePrice'
|
||||
import DayHighLow from './DayHighLow'
|
||||
import {
|
||||
getPrecisionDigits,
|
||||
perpContractPrecision,
|
||||
usdFormatter,
|
||||
} from '../utils'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from './TradePageGrid'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import SwitchMarketDropdown from './SwitchMarketDropdown'
|
||||
import Tooltip from './Tooltip'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
import { InformationCircleIcon } from '@heroicons/react/solid'
|
||||
|
||||
const OraclePrice = () => {
|
||||
const oraclePrice = useOraclePrice()
|
||||
const selectedMarket = useMangoStore((s) => s.selectedMarket.current)
|
||||
|
||||
const decimals = useMemo(
|
||||
() =>
|
||||
selectedMarket?.tickSize !== undefined
|
||||
? getPrecisionDigits(selectedMarket?.tickSize)
|
||||
: null,
|
||||
[selectedMarket]
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="text-th-fgd-1 md:text-xs">
|
||||
{decimals && oraclePrice && selectedMarket
|
||||
? oraclePrice.toNumber().toLocaleString(undefined, {
|
||||
minimumFractionDigits: decimals,
|
||||
maximumFractionDigits: decimals,
|
||||
})
|
||||
: '--'}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const MarketDetails = () => {
|
||||
const { t } = useTranslation('common')
|
||||
const { connected } = useWallet()
|
||||
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
|
||||
const baseSymbol = marketConfig.baseSymbol
|
||||
const selectedMarketName = marketConfig.name
|
||||
const isPerpMarket = marketConfig.kind === 'perp'
|
||||
|
||||
const { width } = useViewport()
|
||||
const isMobile = width ? width < breakpoints.sm : false
|
||||
|
||||
const marketsInfo = useMangoStore((s) => s.marketsInfo)
|
||||
|
||||
const market = useMemo(
|
||||
() => marketsInfo.find((market) => market.name === selectedMarketName),
|
||||
[marketsInfo, selectedMarketName]
|
||||
)
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`relative flex flex-col md:px-3 md:pt-3 md:pb-2 lg:flex-row lg:items-end lg:justify-between`}
|
||||
>
|
||||
<div className="flex flex-col lg:flex-row lg:flex-wrap">
|
||||
<div className="hidden md:block md:pr-6 lg:pb-0">
|
||||
<div className="flex items-center">
|
||||
<SwitchMarketDropdown />
|
||||
</div>
|
||||
</div>
|
||||
<div className="grid grid-flow-row grid-cols-1 gap-2 md:mt-2.5 md:grid-cols-3 md:pr-20 lg:grid-flow-col lg:grid-cols-none lg:grid-rows-1 lg:gap-6">
|
||||
<div className="flex items-center justify-between md:block">
|
||||
<div className="text-th-fgd-3 md:pb-0.5 md:text-[0.65rem]">
|
||||
{t('oracle-price')}
|
||||
</div>
|
||||
<OraclePrice />
|
||||
</div>
|
||||
{market ? (
|
||||
<>
|
||||
<div className="flex items-center justify-between md:block">
|
||||
<div className="text-th-fgd-3 md:pb-0.5 md:text-[0.65rem]">
|
||||
{t('rolling-change')}
|
||||
</div>
|
||||
<div
|
||||
className={`md:text-xs ${
|
||||
market.change24h > 0
|
||||
? `text-th-green`
|
||||
: market.change24h < 0
|
||||
? `text-th-red`
|
||||
: `text-th-fgd-1`
|
||||
}`}
|
||||
>
|
||||
{(market.change24h * 100).toFixed(2) + '%'}
|
||||
</div>
|
||||
</div>
|
||||
{isPerpMarket ? (
|
||||
<>
|
||||
<div className="flex items-center justify-between md:block">
|
||||
<div className="text-th-fgd-3 md:pb-0.5 md:text-[0.65rem]">
|
||||
{t('daily-volume')}
|
||||
</div>
|
||||
<div className="text-th-fgd-1 md:text-xs">
|
||||
{usdFormatter(market?.volumeUsd24h, 0)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center justify-between md:block">
|
||||
<div className="flex items-center text-th-fgd-3 md:pb-0.5 md:text-[0.65rem]">
|
||||
{t('average-funding')}
|
||||
<Tooltip
|
||||
content={t('tooltip-funding')}
|
||||
placement={'bottom'}
|
||||
>
|
||||
<InformationCircleIcon className="ml-1.5 h-4 w-4 text-th-fgd-4 hover:cursor-help" />
|
||||
</Tooltip>
|
||||
</div>
|
||||
<div className="text-th-fgd-1 md:text-xs">
|
||||
{`${market?.funding1h.toFixed(4)}% (${(
|
||||
market?.funding1h *
|
||||
24 *
|
||||
365
|
||||
).toFixed(2)}% APR)`}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center justify-between md:block">
|
||||
<div className="text-th-fgd-3 md:pb-0.5 md:text-[0.65rem]">
|
||||
{t('open-interest')}
|
||||
</div>
|
||||
<div className="flex items-center text-th-fgd-1 md:text-xs">
|
||||
{usdFormatter(market?.openInterestUsd, 0)}
|
||||
<Tooltip
|
||||
content={`${market?.openInterest.toLocaleString(
|
||||
undefined,
|
||||
{
|
||||
maximumFractionDigits:
|
||||
perpContractPrecision[baseSymbol],
|
||||
}
|
||||
)} ${baseSymbol}`}
|
||||
placement={'bottom'}
|
||||
>
|
||||
<InformationCircleIcon className="ml-1.5 h-4 w-4 text-th-fgd-4 hover:cursor-help" />
|
||||
</Tooltip>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
) : null}
|
||||
<div className="flex items-center justify-between md:block">
|
||||
<div className="text-left text-th-fgd-3 md:pb-0.5 md:text-[0.65rem] xl:text-center">
|
||||
{t('daily-range')}
|
||||
</div>
|
||||
<DayHighLow high={market?.high24h} low={market?.low24h} />
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<MarketDataLoader />
|
||||
<MarketDataLoader />
|
||||
{isPerpMarket ? (
|
||||
<>
|
||||
<MarketDataLoader />
|
||||
<MarketDataLoader />
|
||||
<MarketDataLoader />
|
||||
</>
|
||||
) : null}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="absolute right-0 bottom-0 flex items-center justify-end space-x-2 sm:bottom-auto lg:right-3">
|
||||
{!isMobile ? (
|
||||
<div id="layout-tip">
|
||||
<UiLock />
|
||||
</div>
|
||||
) : null}
|
||||
<div id="data-refresh-tip">
|
||||
{!isMobile && connected ? <ManualRefresh /> : null}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default MarketDetails
|
||||
|
||||
export const MarketDataLoader = ({ width }: { width?: string }) => (
|
||||
<div
|
||||
className={`mt-0.5 h-8 ${
|
||||
width ? width : 'w-24'
|
||||
} animate-pulse rounded bg-th-bkg-3`}
|
||||
/>
|
||||
)
|
|
@ -0,0 +1,22 @@
|
|||
import useFees from '../hooks/useFees'
|
||||
import { percentFormat } from '../utils'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
|
||||
export default function MarketFee() {
|
||||
const { t } = useTranslation('common')
|
||||
const { takerFee, makerFee } = useFees()
|
||||
|
||||
return (
|
||||
<div className="mt-2.5 flex px-6 text-xs text-th-fgd-4">
|
||||
<div className="mx-auto block text-center sm:flex">
|
||||
<div>
|
||||
{t('maker-fee')}: {percentFormat.format(makerFee)}
|
||||
</div>
|
||||
<div className="hidden px-2 sm:block">|</div>
|
||||
<div>
|
||||
{t('taker-fee')}: {percentFormat.format(takerFee)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
import { useState } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
import { QuestionMarkCircleIcon } from '@heroicons/react/solid'
|
||||
import Link from 'next/link'
|
||||
import * as MonoIcons from './icons'
|
||||
import { initialMarket } from './SettingsModal'
|
||||
|
||||
const MarketMenuItem: React.FC<{ menuTitle: string; linksArray: any[] }> = ({
|
||||
menuTitle = '',
|
||||
linksArray = [],
|
||||
}) => {
|
||||
const { asPath } = useRouter()
|
||||
const [openState, setOpenState] = useState(false)
|
||||
|
||||
const iconName = `${menuTitle.slice(0, 1)}${menuTitle
|
||||
.slice(1, 4)
|
||||
.toLowerCase()}MonoIcon`
|
||||
|
||||
const SymbolIcon = MonoIcons[iconName] || QuestionMarkCircleIcon
|
||||
|
||||
const onHover = (open, action) => {
|
||||
if (
|
||||
(!open && action === 'onMouseEnter') ||
|
||||
(open && action === 'onMouseLeave')
|
||||
) {
|
||||
setOpenState((openState) => !openState)
|
||||
}
|
||||
}
|
||||
|
||||
const isSelected =
|
||||
asPath.includes(`=${menuTitle}`) ||
|
||||
(asPath === '/' && initialMarket.name.includes(menuTitle))
|
||||
|
||||
return (
|
||||
<div className="relative">
|
||||
<div
|
||||
onMouseEnter={() => onHover(openState, 'onMouseEnter')}
|
||||
onMouseLeave={() => onHover(openState, 'onMouseLeave')}
|
||||
className="flex h-10 cursor-pointer flex-col px-3"
|
||||
>
|
||||
<div
|
||||
className={`flex h-10 items-center text-th-fgd-3 hover:text-th-primary focus:outline-none ${
|
||||
isSelected ? 'text-th-primary' : ''
|
||||
}`}
|
||||
>
|
||||
<SymbolIcon className={`mr-1.5 hidden h-3.5 w-auto lg:block `} />
|
||||
<span className={`text-xs font-normal`}>{menuTitle}</span>
|
||||
</div>
|
||||
{openState ? (
|
||||
<div className="absolute top-10 z-10">
|
||||
<div className="relative divide-y divide-th-fgd-4 rounded rounded-t-none bg-th-bkg-3 px-3">
|
||||
{linksArray.map((m) => (
|
||||
<Link
|
||||
href={{
|
||||
pathname: '/',
|
||||
query: { name: m.name },
|
||||
}}
|
||||
key={m.name}
|
||||
shallow={true}
|
||||
>
|
||||
<a
|
||||
className={`block whitespace-nowrap py-2 text-xs hover:text-th-primary ${
|
||||
asPath.includes(m.name) ||
|
||||
(asPath === '/' && initialMarket.name === m.name)
|
||||
? 'text-th-primary'
|
||||
: 'text-th-fgd-1'
|
||||
}`}
|
||||
>
|
||||
{m.name}
|
||||
</a>
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default MarketMenuItem
|
|
@ -0,0 +1,102 @@
|
|||
import { FunctionComponent, RefObject } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
import { initialMarket } from './SettingsModal'
|
||||
import { FavoriteMarketButton } from './TradeNavMenu'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import {
|
||||
getMarketIndexBySymbol,
|
||||
getWeights,
|
||||
} from '@blockworks-foundation/mango-client'
|
||||
|
||||
interface MarketNavItemProps {
|
||||
market: any
|
||||
onClick?: () => void
|
||||
buttonRef?: RefObject<HTMLElement>
|
||||
}
|
||||
|
||||
const MarketNavItem: FunctionComponent<MarketNavItemProps> = ({
|
||||
market,
|
||||
onClick,
|
||||
buttonRef,
|
||||
}) => {
|
||||
const { asPath } = useRouter()
|
||||
const router = useRouter()
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
|
||||
|
||||
const selectMarket = (market) => {
|
||||
buttonRef?.current?.click()
|
||||
router.push(`/?name=${market.name}`, undefined, { shallow: true })
|
||||
if (onClick) {
|
||||
onClick()
|
||||
}
|
||||
}
|
||||
|
||||
const getMarketLeverage = (mangoGroup, mangoGroupConfig, market) => {
|
||||
if (!mangoGroup) return 1
|
||||
const marketIndex = getMarketIndexBySymbol(
|
||||
mangoGroupConfig,
|
||||
market.baseSymbol
|
||||
)
|
||||
|
||||
// The following if statement is for markets not on devnet
|
||||
if (!mangoGroup.spotMarkets[marketIndex]) {
|
||||
return 1
|
||||
}
|
||||
|
||||
const ws = getWeights(mangoGroup, marketIndex, 'Init')
|
||||
const w = market.name.includes('PERP')
|
||||
? ws.perpAssetWeight
|
||||
: ws.spotAssetWeight
|
||||
return Math.round((100 * -1) / (w.toNumber() - 1)) / 100
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="text-th-fgd-3">
|
||||
<div className="flex items-center">
|
||||
<button
|
||||
className={`flex w-full items-center justify-between px-2 py-2 font-normal md:hover:bg-th-bkg-4 md:hover:text-th-primary ${
|
||||
asPath.includes(market.name) ||
|
||||
(asPath === '/' && initialMarket.name === market.name)
|
||||
? 'text-th-primary'
|
||||
: 'text-th-fgd-1'
|
||||
}`}
|
||||
onClick={() => selectMarket(market)}
|
||||
>
|
||||
<div className={`flex w-full items-center whitespace-nowrap text-xs`}>
|
||||
<div className="flex items-center">
|
||||
<img
|
||||
alt=""
|
||||
width="16"
|
||||
height="16"
|
||||
src={`/assets/icons/${market.baseSymbol.toLowerCase()}.svg`}
|
||||
/>
|
||||
<span className="ml-2">{market.name}</span>
|
||||
</div>
|
||||
<span className="ml-1.5 text-xs text-th-fgd-4">
|
||||
{getMarketLeverage(mangoGroup, mangoGroupConfig, market)}x
|
||||
</span>
|
||||
</div>
|
||||
{market?.change24h ? (
|
||||
<div
|
||||
className={`text-xs ${
|
||||
market?.change24h
|
||||
? market.change24h >= 0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
: 'text-th-fgd-4'
|
||||
}`}
|
||||
>
|
||||
{`${(market.change24h * 100).toFixed(1)}%`}
|
||||
</div>
|
||||
) : null}
|
||||
</button>
|
||||
<div className="ml-1 hidden sm:block">
|
||||
<FavoriteMarketButton market={market} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default MarketNavItem
|
|
@ -0,0 +1,412 @@
|
|||
import { useCallback, useMemo, useState } from 'react'
|
||||
import { ElementTitle } from './styles'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import { formatUsdValue, getPrecisionDigits, roundPerpSize } from '../utils'
|
||||
import Button, { LinkButton } from './Button'
|
||||
import Tooltip from './Tooltip'
|
||||
import PerpSideBadge from './PerpSideBadge'
|
||||
import {
|
||||
getMarketIndexBySymbol,
|
||||
MangoAccount,
|
||||
PerpAccount,
|
||||
PerpMarket,
|
||||
QUOTE_INDEX,
|
||||
} from '@blockworks-foundation/mango-client'
|
||||
import { notify } from '../utils/notifications'
|
||||
import MarketCloseModal from './MarketCloseModal'
|
||||
import PnlText from './PnlText'
|
||||
import Loading from './Loading'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from './TradePageGrid'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import useMangoAccount from '../hooks/useMangoAccount'
|
||||
import { useWallet, Wallet } from '@solana/wallet-adapter-react'
|
||||
import { useRouter } from 'next/router'
|
||||
|
||||
export const settlePosPnl = async (
|
||||
perpMarkets: PerpMarket[],
|
||||
t,
|
||||
mangoAccounts: MangoAccount[] | undefined,
|
||||
wallet: Wallet
|
||||
) => {
|
||||
const mangoAccount = useMangoStore.getState().selectedMangoAccount.current
|
||||
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
|
||||
const mangoCache = useMangoStore.getState().selectedMangoGroup.cache
|
||||
const actions = useMangoStore.getState().actions
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
|
||||
const rootBankAccount = mangoGroup?.rootBankAccounts[QUOTE_INDEX]
|
||||
|
||||
if (!mangoGroup || !mangoCache || !mangoAccount || !rootBankAccount) return
|
||||
|
||||
try {
|
||||
const txids = await mangoClient.settlePosPnl(
|
||||
mangoGroup,
|
||||
mangoCache,
|
||||
mangoAccount,
|
||||
perpMarkets,
|
||||
rootBankAccount,
|
||||
wallet?.adapter,
|
||||
mangoAccounts
|
||||
)
|
||||
actions.reloadMangoAccount()
|
||||
// FIXME: Remove filter when settlePosPnl return type is undefined or string[]
|
||||
const filteredTxids = txids?.filter(
|
||||
(x) => typeof x === 'string'
|
||||
) as string[]
|
||||
if (filteredTxids) {
|
||||
for (const txid of filteredTxids) {
|
||||
notify({
|
||||
title: t('pnl-success'),
|
||||
description: '',
|
||||
txid,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
notify({
|
||||
title: t('pnl-error'),
|
||||
description: t('transaction-failed'),
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('Error settling PNL: ', `${e}`)
|
||||
notify({
|
||||
title: t('pnl-error'),
|
||||
description: e.message,
|
||||
txid: e.txid,
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export async function settleAllPnl(
|
||||
perpMarkets: PerpMarket[],
|
||||
t,
|
||||
mangoAccounts: MangoAccount[] | undefined,
|
||||
wallet: Wallet
|
||||
) {
|
||||
const mangoAccount = useMangoStore.getState().selectedMangoAccount.current
|
||||
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
|
||||
const mangoCache = useMangoStore.getState().selectedMangoGroup.cache
|
||||
const actions = useMangoStore.getState().actions
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
|
||||
const rootBankAccount = mangoGroup?.rootBankAccounts[QUOTE_INDEX]
|
||||
|
||||
if (!mangoGroup || !mangoCache || !mangoAccount || !rootBankAccount) return
|
||||
|
||||
try {
|
||||
const txids = await mangoClient.settleAllPerpPnl(
|
||||
mangoGroup,
|
||||
mangoCache,
|
||||
mangoAccount,
|
||||
perpMarkets,
|
||||
rootBankAccount,
|
||||
wallet?.adapter,
|
||||
mangoAccounts
|
||||
)
|
||||
actions.reloadMangoAccount()
|
||||
const filteredTxids = txids?.filter((x) => x !== null) as string[]
|
||||
if (filteredTxids) {
|
||||
for (const txid of filteredTxids) {
|
||||
notify({
|
||||
title: t('pnl-success'),
|
||||
description: '',
|
||||
txid,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
notify({
|
||||
title: t('pnl-error'),
|
||||
description: t('transaction-failed'),
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('Error settling PNL: ', `${e}`)
|
||||
notify({
|
||||
title: t('pnl-error'),
|
||||
description: e.message,
|
||||
txid: e.txid,
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const settlePnl = async (
|
||||
perpMarket: PerpMarket,
|
||||
perpAccount: PerpAccount,
|
||||
t,
|
||||
mangoAccounts: MangoAccount[] | undefined,
|
||||
wallet: Wallet
|
||||
) => {
|
||||
const mangoAccount = useMangoStore.getState().selectedMangoAccount.current
|
||||
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
|
||||
const mangoCache = useMangoStore.getState().selectedMangoGroup.cache
|
||||
const actions = useMangoStore.getState().actions
|
||||
const marketIndex = mangoGroup?.getPerpMarketIndex(perpMarket.publicKey)
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
const rootBank = mangoGroup?.rootBankAccounts[QUOTE_INDEX]
|
||||
|
||||
if (
|
||||
!rootBank ||
|
||||
!mangoGroup ||
|
||||
!mangoCache ||
|
||||
!mangoAccount ||
|
||||
typeof marketIndex !== 'number'
|
||||
)
|
||||
return
|
||||
|
||||
try {
|
||||
const txid = await mangoClient.settlePnl(
|
||||
mangoGroup,
|
||||
mangoCache,
|
||||
mangoAccount,
|
||||
perpMarket,
|
||||
rootBank,
|
||||
mangoCache.priceCache[marketIndex].price,
|
||||
wallet?.adapter,
|
||||
mangoAccounts
|
||||
)
|
||||
actions.reloadMangoAccount()
|
||||
if (txid) {
|
||||
notify({
|
||||
title: t('pnl-success'),
|
||||
description: '',
|
||||
txid,
|
||||
})
|
||||
} else {
|
||||
notify({
|
||||
title: t('pnl-error'),
|
||||
description: t('transaction-failed'),
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('Error settling PNL: ', `${e}`, `${perpAccount}`)
|
||||
notify({
|
||||
title: t('pnl-error'),
|
||||
description: e.message,
|
||||
txid: e.txid,
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default function MarketPosition() {
|
||||
const { t } = useTranslation('common')
|
||||
const { wallet, connected } = useWallet()
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
|
||||
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
|
||||
const { mangoAccount, initialLoad } = useMangoAccount()
|
||||
const selectedMarket = useMangoStore((s) => s.selectedMarket.current)
|
||||
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
|
||||
const setMangoStore = useMangoStore((s) => s.set)
|
||||
const price = useMangoStore((s) => s.tradeForm.price)
|
||||
const perpPositions = useMangoStore(
|
||||
(s) => s.selectedMangoAccount.perpPositions
|
||||
)
|
||||
const baseSymbol = marketConfig.baseSymbol
|
||||
const marketName = marketConfig.name
|
||||
const router = useRouter()
|
||||
const { pubkey } = router.query
|
||||
|
||||
const [showMarketCloseModal, setShowMarketCloseModal] = useState(false)
|
||||
const [settling, setSettling] = useState(false)
|
||||
const { width } = useViewport()
|
||||
const isMobile = width ? width < breakpoints.sm : false
|
||||
|
||||
const marketIndex = useMemo(() => {
|
||||
return getMarketIndexBySymbol(mangoGroupConfig, baseSymbol)
|
||||
}, [mangoGroupConfig, baseSymbol])
|
||||
|
||||
let perpAccount
|
||||
if (marketName.includes('PERP') && mangoAccount) {
|
||||
perpAccount = mangoAccount.perpAccounts[marketIndex]
|
||||
}
|
||||
|
||||
const handleSizeClick = (size) => {
|
||||
if (!mangoGroup || !mangoCache || !selectedMarket) return
|
||||
const sizePrecisionDigits = getPrecisionDigits(selectedMarket.minOrderSize)
|
||||
const priceOrDefault = price
|
||||
? price
|
||||
: mangoGroup.getPriceUi(marketIndex, mangoCache)
|
||||
const roundedSize = parseFloat(Math.abs(size).toFixed(sizePrecisionDigits))
|
||||
const quoteSize = parseFloat((roundedSize * priceOrDefault).toFixed(2))
|
||||
setMangoStore((state) => {
|
||||
state.tradeForm.baseSize = roundedSize
|
||||
state.tradeForm.quoteSize = quoteSize
|
||||
state.tradeForm.side = size > 0 ? 'sell' : 'buy'
|
||||
})
|
||||
}
|
||||
|
||||
const handleCloseWarning = useCallback(() => {
|
||||
setShowMarketCloseModal(false)
|
||||
}, [])
|
||||
|
||||
const handleSettlePnl = (perpMarket, perpAccount) => {
|
||||
if (wallet) {
|
||||
setSettling(true)
|
||||
settlePnl(perpMarket, perpAccount, t, undefined, wallet).then(() => {
|
||||
setSettling(false)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (!mangoGroup || !selectedMarket || !(selectedMarket instanceof PerpMarket))
|
||||
return null
|
||||
|
||||
const {
|
||||
basePosition = 0,
|
||||
avgEntryPrice = 0,
|
||||
breakEvenPrice = 0,
|
||||
notionalSize = 0,
|
||||
unsettledPnl = 0,
|
||||
} = perpPositions.length
|
||||
? perpPositions.find((p) =>
|
||||
p?.perpMarket.publicKey.equals(selectedMarket.publicKey)
|
||||
) ?? {}
|
||||
: {}
|
||||
|
||||
function SettlePnlTooltip() {
|
||||
return (
|
||||
<div>
|
||||
{t('pnl-help')}{' '}
|
||||
<a
|
||||
href="https://docs.mango.markets/mango/settle-pnl"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
{t('learn-more')}
|
||||
</a>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<div
|
||||
className={!connected && !isMobile && !pubkey ? 'blur-sm filter' : ''}
|
||||
id="perp-positions-tip"
|
||||
>
|
||||
{!isMobile ? (
|
||||
<ElementTitle>
|
||||
{marketConfig.name} {t('position')}
|
||||
</ElementTitle>
|
||||
) : null}
|
||||
<div className="flex items-center justify-between pb-2">
|
||||
<div className="font-normal leading-4 text-th-fgd-3">{t('side')}</div>
|
||||
{initialLoad ? (
|
||||
<DataLoader />
|
||||
) : (
|
||||
<PerpSideBadge basePosition={basePosition}></PerpSideBadge>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex justify-between pb-2">
|
||||
<div className="font-normal leading-4 text-th-fgd-3">
|
||||
{t('position-size')}
|
||||
</div>
|
||||
<div className="text-th-fgd-1">
|
||||
{initialLoad ? (
|
||||
<DataLoader />
|
||||
) : basePosition ? (
|
||||
<span
|
||||
className="cursor-pointer underline hover:no-underline"
|
||||
onClick={() => handleSizeClick(basePosition)}
|
||||
>
|
||||
{`${roundPerpSize(basePosition, baseSymbol)} ${baseSymbol}`}
|
||||
</span>
|
||||
) : (
|
||||
`0 ${baseSymbol}`
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex justify-between pb-2">
|
||||
<div className="font-normal leading-4 text-th-fgd-3">
|
||||
{t('notional-size')}
|
||||
</div>
|
||||
<div className="text-th-fgd-1">
|
||||
{initialLoad ? (
|
||||
<DataLoader />
|
||||
) : notionalSize ? (
|
||||
formatUsdValue(Math.abs(notionalSize))
|
||||
) : (
|
||||
'$0'
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex justify-between pb-2">
|
||||
<div className="font-normal leading-4 text-th-fgd-3">
|
||||
{t('average-entry')}
|
||||
</div>
|
||||
<div className="text-th-fgd-1">
|
||||
{initialLoad ? (
|
||||
<DataLoader />
|
||||
) : avgEntryPrice ? (
|
||||
formatUsdValue(avgEntryPrice)
|
||||
) : (
|
||||
'$0'
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex justify-between pb-2">
|
||||
<div className="font-normal leading-4 text-th-fgd-3">
|
||||
{t('break-even')}
|
||||
</div>
|
||||
<div className="text-th-fgd-1">
|
||||
{initialLoad ? (
|
||||
<DataLoader />
|
||||
) : breakEvenPrice ? (
|
||||
formatUsdValue(breakEvenPrice)
|
||||
) : (
|
||||
'$0'
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex justify-between pb-2">
|
||||
<Tooltip content={<SettlePnlTooltip />}>
|
||||
<Tooltip.Content className="font-normal leading-4 text-th-fgd-3">
|
||||
{t('unsettled-balance')}
|
||||
</Tooltip.Content>
|
||||
</Tooltip>
|
||||
<div className="flex items-center">
|
||||
{initialLoad ? <DataLoader /> : <PnlText pnl={unsettledPnl} />}
|
||||
{settling ? (
|
||||
<Loading className="ml-2" />
|
||||
) : (
|
||||
<LinkButton
|
||||
onClick={() => handleSettlePnl(selectedMarket, perpAccount)}
|
||||
className="ml-2 text-xs text-th-primary disabled:cursor-not-allowed disabled:opacity-60 disabled:hover:underline"
|
||||
disabled={unsettledPnl === 0}
|
||||
>
|
||||
{t('redeem-pnl')}
|
||||
</LinkButton>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{basePosition ? (
|
||||
<Button
|
||||
onClick={() => setShowMarketCloseModal(true)}
|
||||
className="mt-2.5 w-full"
|
||||
primary={false}
|
||||
>
|
||||
<span>{t('market-close')}</span>
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
{showMarketCloseModal ? (
|
||||
<MarketCloseModal
|
||||
isOpen={showMarketCloseModal}
|
||||
onClose={handleCloseWarning}
|
||||
position={{ marketConfig: marketConfig, perpMarket: selectedMarket }}
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export const DataLoader = () => (
|
||||
<div className="h-5 w-10 animate-pulse rounded-sm bg-th-bkg-3" />
|
||||
)
|
|
@ -0,0 +1,126 @@
|
|||
import React from 'react'
|
||||
import Link from 'next/link'
|
||||
import { ChevronRightIcon, EyeIcon, EyeOffIcon } from '@heroicons/react/solid'
|
||||
import Modal from './Modal'
|
||||
import useLocalStorageState from '../hooks/useLocalStorageState'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import { formatUsdValue } from '../utils'
|
||||
import { LinkButton } from './Button'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
|
||||
const MarketsModal = ({
|
||||
isOpen,
|
||||
markets,
|
||||
onClose,
|
||||
}: {
|
||||
isOpen: boolean
|
||||
markets: Array<any>
|
||||
onClose?: (x?) => void
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
|
||||
const [hiddenMarkets, setHiddenMarkets] = useLocalStorageState(
|
||||
'hiddenMarkets',
|
||||
[]
|
||||
)
|
||||
|
||||
const handleHideShowMarket = (asset) => {
|
||||
if (hiddenMarkets.includes(asset)) {
|
||||
setHiddenMarkets(hiddenMarkets.filter((m) => m !== asset))
|
||||
} else {
|
||||
setHiddenMarkets(hiddenMarkets.concat(asset))
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal isOpen={isOpen} onClose={onClose}>
|
||||
<div className="flex items-end justify-between pb-3 pt-2">
|
||||
<div className="text-lg font-bold text-th-fgd-1">{t('markets')}</div>
|
||||
{hiddenMarkets.length === 0 ? (
|
||||
<LinkButton
|
||||
className="mb-0.5 hidden text-xs font-normal text-th-fgd-3 disabled:cursor-not-allowed disabled:text-th-fgd-4 disabled:no-underline md:block"
|
||||
// disabled={hiddenMarkets.length === 0}
|
||||
onClick={() =>
|
||||
setHiddenMarkets(markets.map((mkt) => mkt.baseAsset))
|
||||
}
|
||||
>
|
||||
{t('hide-all')}
|
||||
</LinkButton>
|
||||
) : (
|
||||
<LinkButton
|
||||
className="mb-0.5 hidden text-xs font-normal text-th-fgd-3 disabled:cursor-not-allowed disabled:text-th-fgd-4 disabled:no-underline md:block"
|
||||
// disabled={hiddenMarkets.length === 0}
|
||||
onClick={() => setHiddenMarkets([])}
|
||||
>
|
||||
{t('show-all')}
|
||||
</LinkButton>
|
||||
)}
|
||||
</div>
|
||||
{markets.map((mkt) => (
|
||||
<div key={mkt.baseAsset}>
|
||||
<div className="flex items-center justify-between bg-th-bkg-3 px-2.5 py-2">
|
||||
<div className="flex items-center">
|
||||
<img
|
||||
alt=""
|
||||
src={`/assets/icons/${mkt.baseAsset.toLowerCase()}.svg`}
|
||||
className={`mr-2.5 h-5 w-auto`}
|
||||
/>
|
||||
<span className="text-th-fgd-2">{mkt.baseAsset}</span>
|
||||
</div>
|
||||
<div className="hidden md:flex">
|
||||
{hiddenMarkets.includes(mkt.baseAsset) ? (
|
||||
<EyeOffIcon
|
||||
className="default-transition h-4 w-4 cursor-pointer text-th-fgd-4 hover:text-th-fgd-3"
|
||||
onClick={() => handleHideShowMarket(mkt.baseAsset)}
|
||||
/>
|
||||
) : (
|
||||
<EyeIcon
|
||||
className="default-transition h-4 w-4 cursor-pointer text-th-primary hover:text-th-primary-dark"
|
||||
onClick={() => handleHideShowMarket(mkt.baseAsset)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="divide-y divide-th-bkg-4">
|
||||
{mkt.markets.map((m) => (
|
||||
<div
|
||||
className={`flex items-center justify-between px-2.5 text-xs`}
|
||||
key={m.name}
|
||||
>
|
||||
<Link href={`/?name=${m.name}`} key={m.name}>
|
||||
<a
|
||||
className="default-transition flex h-12 w-full cursor-pointer items-center justify-between text-th-fgd-2 hover:text-th-primary"
|
||||
onClick={onClose}
|
||||
>
|
||||
{m.name}
|
||||
<div className="flex items-center">
|
||||
<span className="w-20 text-right">
|
||||
{mangoGroup && mangoCache
|
||||
? formatUsdValue(
|
||||
mangoGroup
|
||||
.getPrice(m.marketIndex, mangoCache)
|
||||
.toNumber()
|
||||
)
|
||||
: null}
|
||||
</span>
|
||||
{/* <span className="text-th-green text-right w-20">
|
||||
+2.44%
|
||||
</span>
|
||||
<span className="text-th-fgd-3 text-right w-20">
|
||||
$233m
|
||||
</span> */}
|
||||
<ChevronRightIcon className="ml-1 h-4 w-5 text-th-fgd-2" />
|
||||
</div>
|
||||
</a>
|
||||
</Link>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(MarketsModal)
|
|
@ -0,0 +1,452 @@
|
|||
import { useEffect, useMemo } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { formatUsdValue, perpContractPrecision, usdFormatter } from '../utils'
|
||||
import { Table, Td, Th, TrBody, TrHead } from './TableElements'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from './TradePageGrid'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import { FavoriteMarketButton } from './TradeNavMenu'
|
||||
import { useSortableData } from '../hooks/useSortableData'
|
||||
import { LinkButton } from './Button'
|
||||
import { ArrowSmDownIcon } from '@heroicons/react/solid'
|
||||
import { useRouter } from 'next/router'
|
||||
import { AreaChart, Area, XAxis, YAxis } from 'recharts'
|
||||
import { useTheme } from 'next-themes'
|
||||
|
||||
const MarketsTable = ({
|
||||
isPerpMarket,
|
||||
markets,
|
||||
}: {
|
||||
isPerpMarket?: boolean
|
||||
markets: any[]
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const { width } = useViewport()
|
||||
const isMobile = width ? width < breakpoints.md : false
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
const coingeckoPrices = useMangoStore((s) => s.coingeckoPrices.data)
|
||||
const loadingCoingeckoPrices = useMangoStore((s) => s.coingeckoPrices.loading)
|
||||
const router = useRouter()
|
||||
|
||||
useEffect(() => {
|
||||
if (coingeckoPrices.length === 0) {
|
||||
actions.fetchCoingeckoPrices()
|
||||
}
|
||||
}, [coingeckoPrices])
|
||||
|
||||
const { items, requestSort, sortConfig } = useSortableData(markets)
|
||||
|
||||
return !isMobile ? (
|
||||
<div className={`md:overflow-x-auto`}>
|
||||
<div className={`inline-block min-w-full align-middle`}>
|
||||
<Table>
|
||||
<thead>
|
||||
<TrHead>
|
||||
<Th>
|
||||
<LinkButton
|
||||
className="flex items-center font-normal no-underline"
|
||||
onClick={() => requestSort('name')}
|
||||
>
|
||||
<span className="text-left font-normal text-th-fgd-3">
|
||||
{t('market')}
|
||||
</span>
|
||||
<ArrowSmDownIcon
|
||||
className={`default-transition ml-1 h-4 w-4 flex-shrink-0 ${
|
||||
sortConfig?.key === 'name'
|
||||
? sortConfig.direction === 'ascending'
|
||||
? 'rotate-180 transform'
|
||||
: 'rotate-360 transform'
|
||||
: null
|
||||
}`}
|
||||
/>
|
||||
</LinkButton>
|
||||
</Th>
|
||||
<Th>
|
||||
<LinkButton
|
||||
className="flex items-center font-normal no-underline"
|
||||
onClick={() => requestSort('last')}
|
||||
>
|
||||
<span className="text-left font-normal text-th-fgd-3">
|
||||
{t('price')}
|
||||
</span>
|
||||
<ArrowSmDownIcon
|
||||
className={`default-transition ml-1 h-4 w-4 flex-shrink-0 ${
|
||||
sortConfig?.key === 'last'
|
||||
? sortConfig.direction === 'ascending'
|
||||
? 'rotate-180 transform'
|
||||
: 'rotate-360 transform'
|
||||
: null
|
||||
}`}
|
||||
/>
|
||||
</LinkButton>
|
||||
</Th>
|
||||
<Th>
|
||||
<LinkButton
|
||||
className="flex items-center font-normal no-underline"
|
||||
onClick={() => requestSort('change24h')}
|
||||
>
|
||||
<span className="text-left font-normal text-th-fgd-3">
|
||||
{t('rolling-change')}
|
||||
</span>
|
||||
<ArrowSmDownIcon
|
||||
className={`default-transition ml-1 h-4 w-4 flex-shrink-0 ${
|
||||
sortConfig?.key === 'change24h'
|
||||
? sortConfig.direction === 'ascending'
|
||||
? 'rotate-180 transform'
|
||||
: 'rotate-360 transform'
|
||||
: null
|
||||
}`}
|
||||
/>
|
||||
</LinkButton>
|
||||
</Th>
|
||||
<Th>
|
||||
<LinkButton
|
||||
className="flex items-center font-normal no-underline"
|
||||
onClick={() => requestSort('volumeUsd24h')}
|
||||
>
|
||||
<span className="text-left font-normal text-th-fgd-3">
|
||||
{t('daily-volume')}
|
||||
</span>
|
||||
<ArrowSmDownIcon
|
||||
className={`default-transition ml-1 h-4 w-4 flex-shrink-0 ${
|
||||
sortConfig?.key === 'volumeUsd24h'
|
||||
? sortConfig.direction === 'ascending'
|
||||
? 'rotate-180 transform'
|
||||
: 'rotate-360 transform'
|
||||
: null
|
||||
}`}
|
||||
/>
|
||||
</LinkButton>
|
||||
</Th>
|
||||
{isPerpMarket ? (
|
||||
<>
|
||||
<Th>
|
||||
<LinkButton
|
||||
className="flex items-center font-normal no-underline"
|
||||
onClick={() => requestSort('funding1h')}
|
||||
>
|
||||
<span className="text-left font-normal text-th-fgd-3">
|
||||
{t('average-funding')}
|
||||
</span>
|
||||
<ArrowSmDownIcon
|
||||
className={`default-transition ml-1 h-4 w-4 flex-shrink-0 ${
|
||||
sortConfig?.key === 'funding1h'
|
||||
? sortConfig.direction === 'ascending'
|
||||
? 'rotate-180 transform'
|
||||
: 'rotate-360 transform'
|
||||
: null
|
||||
}`}
|
||||
/>
|
||||
</LinkButton>
|
||||
</Th>
|
||||
<Th>
|
||||
<LinkButton
|
||||
className="flex items-center no-underline"
|
||||
onClick={() => requestSort('openInterestUsd')}
|
||||
>
|
||||
<span className="text-left font-normal text-th-fgd-3">
|
||||
{t('open-interest')}
|
||||
</span>
|
||||
<ArrowSmDownIcon
|
||||
className={`default-transition ml-1 h-4 w-4 flex-shrink-0 ${
|
||||
sortConfig?.key === 'openInterestUsd'
|
||||
? sortConfig.direction === 'ascending'
|
||||
? 'rotate-180 transform'
|
||||
: 'rotate-360 transform'
|
||||
: null
|
||||
}`}
|
||||
/>
|
||||
</LinkButton>
|
||||
</Th>
|
||||
</>
|
||||
) : null}
|
||||
<Th>
|
||||
<span className="flex justify-end">{t('favorite')}</span>
|
||||
</Th>
|
||||
</TrHead>
|
||||
</thead>
|
||||
<tbody>
|
||||
{items.map((market) => {
|
||||
const {
|
||||
baseSymbol,
|
||||
change24h,
|
||||
funding1h,
|
||||
last,
|
||||
name,
|
||||
openInterest,
|
||||
openInterestUsd,
|
||||
volumeUsd24h,
|
||||
} = market
|
||||
const fundingApr = funding1h
|
||||
? (funding1h * 24 * 365).toFixed(2)
|
||||
: '-'
|
||||
const coingeckoData = coingeckoPrices.find(
|
||||
(asset) => asset.symbol === baseSymbol
|
||||
)
|
||||
const chartData = coingeckoData ? coingeckoData.prices : undefined
|
||||
|
||||
return (
|
||||
<TrBody key={name}>
|
||||
<Td>
|
||||
<Link href={`/?name=${name}`} shallow={true}>
|
||||
<a className="hover:cursor-pointer">
|
||||
<div className="flex h-full items-center text-th-fgd-2 hover:text-th-primary">
|
||||
<img
|
||||
alt=""
|
||||
width="20"
|
||||
height="20"
|
||||
src={`/assets/icons/${baseSymbol.toLowerCase()}.svg`}
|
||||
className={`mr-2.5`}
|
||||
/>
|
||||
<span className="default-transition">{name}</span>
|
||||
</div>
|
||||
</a>
|
||||
</Link>
|
||||
</Td>
|
||||
<Td className="flex items-center">
|
||||
<div className="w-20">
|
||||
{last ? (
|
||||
formatUsdValue(last)
|
||||
) : (
|
||||
<span className="text-th-fgd-4">
|
||||
{t('unavailable')}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="pl-6">
|
||||
{!loadingCoingeckoPrices ? (
|
||||
chartData !== undefined ? (
|
||||
<PriceChart
|
||||
name={name}
|
||||
change24h={change24h}
|
||||
data={chartData}
|
||||
height={40}
|
||||
width={104}
|
||||
/>
|
||||
) : (
|
||||
t('unavailable')
|
||||
)
|
||||
) : (
|
||||
<div className="h-10 w-[104px] animate-pulse rounded bg-th-bkg-3" />
|
||||
)}
|
||||
</div>
|
||||
</Td>
|
||||
<Td>
|
||||
<span
|
||||
className={
|
||||
change24h >= 0 ? 'text-th-green' : 'text-th-red'
|
||||
}
|
||||
>
|
||||
{change24h || change24h === 0 ? (
|
||||
`${(change24h * 100).toFixed(2)}%`
|
||||
) : (
|
||||
<span className="text-th-fgd-4">
|
||||
{t('unavailable')}
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
</Td>
|
||||
<Td>
|
||||
{volumeUsd24h ? (
|
||||
usdFormatter(volumeUsd24h, 0)
|
||||
) : (
|
||||
<span className="text-th-fgd-4">{t('unavailable')}</span>
|
||||
)}
|
||||
</Td>
|
||||
{isPerpMarket ? (
|
||||
<>
|
||||
<Td>
|
||||
{funding1h ? (
|
||||
<>
|
||||
<span>{`${funding1h.toFixed(4)}%`}</span>{' '}
|
||||
<span className="text-xs text-th-fgd-3">{`(${fundingApr}% APR)`}</span>
|
||||
</>
|
||||
) : (
|
||||
<span className="text-th-fgd-4">
|
||||
{t('unavailable')}
|
||||
</span>
|
||||
)}
|
||||
</Td>
|
||||
<Td>
|
||||
{openInterestUsd ? (
|
||||
<>
|
||||
<span>{usdFormatter(openInterestUsd, 0)}</span>{' '}
|
||||
{openInterest ? (
|
||||
<div className="text-xs text-th-fgd-4">
|
||||
{openInterest.toLocaleString(undefined, {
|
||||
maximumFractionDigits:
|
||||
perpContractPrecision[baseSymbol],
|
||||
})}{' '}
|
||||
{baseSymbol}
|
||||
</div>
|
||||
) : null}
|
||||
</>
|
||||
) : (
|
||||
<span className="text-th-fgd-4">
|
||||
{t('unavailable')}
|
||||
</span>
|
||||
)}
|
||||
</Td>
|
||||
</>
|
||||
) : null}
|
||||
<Td>
|
||||
<div className="flex justify-end">
|
||||
<FavoriteMarketButton market={market} />
|
||||
</div>
|
||||
</Td>
|
||||
</TrBody>
|
||||
)
|
||||
})}
|
||||
</tbody>
|
||||
</Table>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
{items.map((market) => {
|
||||
const { baseSymbol, change24h, funding1h, last, name } = market
|
||||
const fundingApr = funding1h ? (funding1h * 24 * 365).toFixed(2) : '-'
|
||||
const coingeckoData = coingeckoPrices.find(
|
||||
(asset) => asset.symbol === baseSymbol
|
||||
)
|
||||
const chartData = coingeckoData ? coingeckoData.prices : undefined
|
||||
|
||||
return (
|
||||
<Link href={`/?name=${name}`} shallow={true} key={name}>
|
||||
<a
|
||||
className="default-transition mb-2 block w-full rounded-lg border border-th-bkg-3 p-4 pb-2.5 hover:bg-th-bkg-2"
|
||||
onClick={() =>
|
||||
router.push(`/?name=${name}`, undefined, {
|
||||
shallow: true,
|
||||
})
|
||||
}
|
||||
>
|
||||
<div className="mb-1 flex justify-between">
|
||||
<div>
|
||||
<div className="mb-2 flex items-center font-bold text-th-fgd-2">
|
||||
<img
|
||||
alt=""
|
||||
width="20"
|
||||
height="20"
|
||||
src={`/assets/icons/${baseSymbol.toLowerCase()}.svg`}
|
||||
className="mr-2"
|
||||
/>
|
||||
|
||||
{name}
|
||||
<div
|
||||
className={`ml-3 ${
|
||||
change24h >= 0 ? 'text-th-green' : 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{change24h || change24h === 0 ? (
|
||||
`${(change24h * 100).toFixed(2)}%`
|
||||
) : (
|
||||
<span className="text-th-fgd-4">
|
||||
{t('unavailable')}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{!loadingCoingeckoPrices ? (
|
||||
chartData !== undefined ? (
|
||||
<PriceChart
|
||||
name={name}
|
||||
change24h={change24h}
|
||||
data={chartData}
|
||||
height={48}
|
||||
width={128}
|
||||
/>
|
||||
) : (
|
||||
t('unavailable')
|
||||
)
|
||||
) : (
|
||||
<div className="h-12 w-[128px] animate-pulse rounded bg-th-bkg-4" />
|
||||
)}
|
||||
</div>
|
||||
<div className="text-right">
|
||||
<p className="text-xl font-bold leading-none text-th-fgd-2">
|
||||
{last ? (
|
||||
formatUsdValue(last)
|
||||
) : (
|
||||
<span className="text-th-fgd-4">{t('unavailable')}</span>
|
||||
)}
|
||||
</p>
|
||||
{isPerpMarket ? (
|
||||
funding1h ? (
|
||||
<div className="mt-1 justify-end text-th-fgd-3">
|
||||
<div className="text-[10px] leading-tight text-th-fgd-4">
|
||||
{t('average-funding')}
|
||||
</div>
|
||||
<span className="text-xs">{`${fundingApr}% APR`}</span>
|
||||
</div>
|
||||
) : (
|
||||
<span className="text-th-fgd-4">{t('unavailable')}</span>
|
||||
)
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
</Link>
|
||||
)
|
||||
})}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
const COLORS = {
|
||||
GREEN: { Mango: '#AFD803', Dark: '#5EBF4d', Light: '#5EBF4d' },
|
||||
RED: { Mango: '#F84638', Dark: '#CC2929', Light: '#CC2929' },
|
||||
}
|
||||
|
||||
const PriceChart = ({ data, width, height, change24h, name }) => {
|
||||
const { theme } = useTheme()
|
||||
|
||||
const color = change24h >= 0 ? COLORS.GREEN[theme] : COLORS.RED[theme]
|
||||
|
||||
return (
|
||||
<AreaChart width={width} height={height} data={data}>
|
||||
<defs>
|
||||
<linearGradient id={`gradientArea-${name}`} x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0%" stopColor={color} stopOpacity={0.6} />
|
||||
<stop offset="100%" stopColor={color} stopOpacity={0} />
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<Area
|
||||
isAnimationActive={false}
|
||||
type="monotone"
|
||||
dataKey="1"
|
||||
stroke={color}
|
||||
fill={`url(#gradientArea-${name})`}
|
||||
/>
|
||||
<XAxis dataKey="0" hide />
|
||||
<YAxis domain={['dataMin', 'dataMax']} dataKey="1" hide />
|
||||
</AreaChart>
|
||||
)
|
||||
}
|
||||
|
||||
export const SpotMarketsTable = () => {
|
||||
const marketsInfo = useMangoStore((s) => s.marketsInfo)
|
||||
|
||||
const spotMarketsInfo = useMemo(
|
||||
() =>
|
||||
marketsInfo
|
||||
.filter((mkt) => mkt?.name.includes('USDC'))
|
||||
.sort((a, b) => b.volumeUsd24h - a.volumeUsd24h),
|
||||
[marketsInfo]
|
||||
)
|
||||
return <MarketsTable markets={spotMarketsInfo} />
|
||||
}
|
||||
|
||||
export const PerpMarketsTable = () => {
|
||||
const marketsInfo = useMangoStore((s) => s.marketsInfo)
|
||||
const perpMarketsInfo = useMemo(
|
||||
() =>
|
||||
marketsInfo
|
||||
.filter((mkt) => mkt?.name.includes('PERP'))
|
||||
.sort((a, b) => b.volumeUsd24h - a.volumeUsd24h),
|
||||
[marketsInfo]
|
||||
)
|
||||
|
||||
return <MarketsTable isPerpMarket markets={perpMarketsInfo} />
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
import { useRouter } from 'next/router'
|
||||
import Link from 'next/link'
|
||||
import { ChevronRightIcon } from '@heroicons/react/solid'
|
||||
|
||||
const MenuItem = ({ href, children, newWindow = false }) => {
|
||||
const { asPath } = useRouter()
|
||||
|
||||
return (
|
||||
<Link href={href} shallow={true}>
|
||||
<a
|
||||
className={`flex h-full items-center justify-between border-b border-th-bkg-4 p-3 font-bold text-th-fgd-1 transition-none hover:text-th-primary md:border-none md:py-0
|
||||
${asPath === href ? `text-th-primary` : `border-transparent`}
|
||||
`}
|
||||
target={newWindow ? '_blank' : ''}
|
||||
rel={newWindow ? 'noopener noreferrer' : ''}
|
||||
>
|
||||
{children}
|
||||
<ChevronRightIcon className="h-5 w-5 md:hidden" />
|
||||
</a>
|
||||
</Link>
|
||||
)
|
||||
}
|
||||
|
||||
export default MenuItem
|
|
@ -0,0 +1,75 @@
|
|||
import React from 'react'
|
||||
import { Portal } from 'react-portal'
|
||||
import { XIcon } from '@heroicons/react/solid'
|
||||
|
||||
const Modal: any = React.forwardRef<any, any>((props, ref) => {
|
||||
const {
|
||||
isOpen,
|
||||
onClose,
|
||||
children,
|
||||
hideClose = false,
|
||||
noPadding = false,
|
||||
alignTop = false,
|
||||
className = '',
|
||||
} = props
|
||||
|
||||
return (
|
||||
<Portal>
|
||||
<div
|
||||
className="fixed inset-0 z-40 overflow-y-auto"
|
||||
aria-labelledby="modal-title"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
>
|
||||
<div className="flex min-h-screen items-center text-center sm:block sm:p-0">
|
||||
{isOpen ? (
|
||||
<div
|
||||
className="fixed inset-0 bg-black bg-opacity-50 transition-opacity"
|
||||
aria-hidden="true"
|
||||
onClick={onClose}
|
||||
></div>
|
||||
) : null}
|
||||
|
||||
{alignTop ? null : (
|
||||
<span
|
||||
className="hidden sm:inline-block sm:h-screen sm:align-middle"
|
||||
aria-hidden="true"
|
||||
>
|
||||
​
|
||||
</span>
|
||||
)}
|
||||
|
||||
{isOpen ? (
|
||||
<div
|
||||
className={`inline-block min-h-screen border border-th-bkg-3 bg-th-bkg-1 text-left
|
||||
sm:min-h-full sm:rounded-lg ${
|
||||
noPadding ? '' : 'px-8 pt-6 pb-6'
|
||||
} w-full transform align-middle shadow-lg transition-all sm:max-w-md ${className}`}
|
||||
ref={ref}
|
||||
>
|
||||
{!hideClose ? (
|
||||
<div className="">
|
||||
<button
|
||||
onClick={onClose}
|
||||
className={`absolute right-4 top-4 text-th-fgd-4 focus:outline-none md:right-2 md:top-2 md:hover:text-th-primary`}
|
||||
>
|
||||
<XIcon className={`h-5 w-5`} />
|
||||
</button>
|
||||
</div>
|
||||
) : null}
|
||||
{children}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</Portal>
|
||||
)
|
||||
})
|
||||
|
||||
const Header = ({ children }) => {
|
||||
return <div className={`flex flex-col items-center pb-2`}>{children}</div>
|
||||
}
|
||||
|
||||
Modal.Header = Header
|
||||
|
||||
export default Modal
|
|
@ -0,0 +1,70 @@
|
|||
import { Fragment } from 'react'
|
||||
import { ChevronDownIcon } from '@heroicons/react/solid'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { Popover, Transition } from '@headlessui/react'
|
||||
import Checkbox from './Checkbox'
|
||||
|
||||
const MultiSelectDropdown = ({ options, selected, toggleOption }) => {
|
||||
const { t } = useTranslation('common')
|
||||
return (
|
||||
<Popover className="relative w-full min-w-[120px]">
|
||||
{({ open }) => (
|
||||
<div className="flex flex-col">
|
||||
<Popover.Button
|
||||
className={`default-transition rounded-md bg-th-bkg-1 p-2.5 text-th-fgd-1 ring-1 ring-inset ring-th-bkg-4 hover:ring-th-fgd-4 ${
|
||||
open ? 'ring-th-fgd-4' : 'ring-th-bkg-4'
|
||||
}`}
|
||||
>
|
||||
<div className={`flex items-center justify-between`}>
|
||||
<span>
|
||||
{selected.length === 1
|
||||
? selected[0]
|
||||
: t('filters-selected', { selectedFilters: selected.length })}
|
||||
</span>
|
||||
<ChevronDownIcon
|
||||
className={`default-transition ml-0.5 h-5 w-5 ${
|
||||
open ? 'rotate-180 transform' : 'rotate-360 transform'
|
||||
}`}
|
||||
aria-hidden="true"
|
||||
/>
|
||||
</div>
|
||||
</Popover.Button>
|
||||
<Transition
|
||||
appear={true}
|
||||
show={open}
|
||||
as={Fragment}
|
||||
enter="transition-all ease-in duration-200"
|
||||
enterFrom="opacity-0 transform scale-75"
|
||||
enterTo="opacity-100 transform scale-100"
|
||||
leave="transition ease-out duration-200"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<Popover.Panel className="absolute top-12 z-10 h-72 w-full overflow-y-auto">
|
||||
<div className="relative space-y-2.5 rounded-md bg-th-bkg-3 p-3">
|
||||
{options.map((option) => {
|
||||
const name = option.name ? option.name : option
|
||||
const isSelected = selected.includes(name)
|
||||
return (
|
||||
<Checkbox
|
||||
checked={isSelected}
|
||||
className="mr-2"
|
||||
key={name}
|
||||
onChange={() =>
|
||||
toggleOption(option.name ? { id: option.name } : option)
|
||||
}
|
||||
>
|
||||
{name}
|
||||
</Checkbox>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</Popover.Panel>
|
||||
</Transition>
|
||||
</div>
|
||||
)}
|
||||
</Popover>
|
||||
)
|
||||
}
|
||||
|
||||
export default MultiSelectDropdown
|
|
@ -0,0 +1,222 @@
|
|||
import React, { FunctionComponent, useState } from 'react'
|
||||
import {
|
||||
ExclamationCircleIcon,
|
||||
InformationCircleIcon,
|
||||
} from '@heroicons/react/solid'
|
||||
import Input, { Label } from './Input'
|
||||
import AccountSelect from './AccountSelect'
|
||||
import { ElementTitle } from './styles'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import {
|
||||
getSymbolForTokenMintAddress,
|
||||
trimDecimals,
|
||||
sleep,
|
||||
} from '../utils/index'
|
||||
import Loading from './Loading'
|
||||
import Button from './Button'
|
||||
import Tooltip from './Tooltip'
|
||||
import { notify } from '../utils/notifications'
|
||||
import { deposit } from '../utils/mango'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import ButtonGroup from './ButtonGroup'
|
||||
import InlineNotification from './InlineNotification'
|
||||
import Modal from './Modal'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
|
||||
interface NewAccountProps {
|
||||
onAccountCreation: (x?) => void
|
||||
}
|
||||
|
||||
const NewAccount: FunctionComponent<NewAccountProps> = ({
|
||||
onAccountCreation,
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const [inputAmount, setInputAmount] = useState<string>('')
|
||||
const [submitting, setSubmitting] = useState(false)
|
||||
const [invalidAmountMessage, setInvalidAmountMessage] = useState('')
|
||||
const [depositPercentage, setDepositPercentage] = useState('')
|
||||
const [invalidNameMessage, setInvalidNameMessage] = useState('')
|
||||
const [name, setName] = useState('')
|
||||
const { wallet } = useWallet()
|
||||
const walletTokens = useMangoStore((s) => s.wallet.tokens)
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
|
||||
const [selectedAccount, setSelectedAccount] = useState(walletTokens[0])
|
||||
|
||||
const symbol = getSymbolForTokenMintAddress(
|
||||
selectedAccount?.account?.mint.toString()
|
||||
)
|
||||
|
||||
const handleAccountSelect = (account) => {
|
||||
setInputAmount('')
|
||||
setDepositPercentage('')
|
||||
setInvalidAmountMessage('')
|
||||
setSelectedAccount(account)
|
||||
}
|
||||
|
||||
const handleNewAccountDeposit = () => {
|
||||
if (!wallet) return
|
||||
validateAmountInput(inputAmount)
|
||||
if (inputAmount) {
|
||||
setSubmitting(true)
|
||||
deposit({
|
||||
amount: parseFloat(inputAmount),
|
||||
fromTokenAcc: selectedAccount.account,
|
||||
accountName: name,
|
||||
wallet,
|
||||
})
|
||||
.then(async (response) => {
|
||||
await sleep(1000)
|
||||
actions.fetchWalletTokens(wallet)
|
||||
actions.fetchAllMangoAccounts(wallet)
|
||||
if (response && response.length > 0) {
|
||||
onAccountCreation(response[0])
|
||||
notify({
|
||||
title: 'Mango Account Created',
|
||||
txid: response[1],
|
||||
})
|
||||
}
|
||||
setSubmitting(false)
|
||||
})
|
||||
.catch((e) => {
|
||||
setSubmitting(false)
|
||||
console.error(e)
|
||||
notify({
|
||||
title: t('init-error'),
|
||||
description: e.message,
|
||||
type: 'error',
|
||||
})
|
||||
onAccountCreation()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const validateAmountInput = (amount) => {
|
||||
if (Number(amount) <= 0) {
|
||||
setInvalidAmountMessage(t('enter-amount'))
|
||||
}
|
||||
if (Number(amount) > selectedAccount.uiBalance) {
|
||||
setInvalidAmountMessage(t('insufficient-balance-deposit'))
|
||||
}
|
||||
}
|
||||
|
||||
const onChangeAmountInput = (amount) => {
|
||||
setInputAmount(amount)
|
||||
setDepositPercentage('')
|
||||
setInvalidAmountMessage('')
|
||||
}
|
||||
|
||||
const onChangeAmountButtons = async (percentage) => {
|
||||
setDepositPercentage(percentage)
|
||||
|
||||
if (!selectedAccount) {
|
||||
setInvalidAmountMessage(t('supported-assets'))
|
||||
return
|
||||
}
|
||||
|
||||
const max = selectedAccount.uiBalance
|
||||
const amount = ((parseInt(percentage) / 100) * max).toString()
|
||||
if (percentage === '100') {
|
||||
setInputAmount(amount)
|
||||
} else {
|
||||
setInputAmount(trimDecimals(amount, 6).toString())
|
||||
}
|
||||
setInvalidAmountMessage('')
|
||||
validateAmountInput(amount)
|
||||
}
|
||||
|
||||
const validateNameInput = () => {
|
||||
if (name.length >= 33) {
|
||||
setInvalidNameMessage(t('character-limit'))
|
||||
}
|
||||
}
|
||||
|
||||
const onChangeNameInput = (name) => {
|
||||
setName(name)
|
||||
if (invalidNameMessage) {
|
||||
setInvalidNameMessage('')
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Modal.Header>
|
||||
<ElementTitle noMarginBottom>{t('create-account')}</ElementTitle>
|
||||
<div className="my-2">
|
||||
<InlineNotification type="info" desc={t('insufficient-sol')} />
|
||||
</div>
|
||||
</Modal.Header>
|
||||
<div className="mb-4 border-b border-th-bkg-4 pb-6">
|
||||
<Label className="flex items-center">
|
||||
{t('account-name')}{' '}
|
||||
<span className="ml-1 text-th-fgd-3">{t('optional')}</span>
|
||||
<Tooltip content={t('tooltip-name-onchain')}>
|
||||
<InformationCircleIcon className="ml-2 h-5 w-5 text-th-fgd-4" />
|
||||
</Tooltip>
|
||||
</Label>
|
||||
<Input
|
||||
type="text"
|
||||
error={!!invalidNameMessage}
|
||||
placeholder="e.g. Calypso"
|
||||
value={name}
|
||||
onBlur={validateNameInput}
|
||||
onChange={(e) => onChangeNameInput(e.target.value)}
|
||||
/>
|
||||
{invalidNameMessage ? (
|
||||
<div className="flex items-center pt-1.5 text-th-red">
|
||||
<ExclamationCircleIcon className="mr-1.5 h-4 w-4" />
|
||||
{invalidNameMessage}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
<h3 className="mb-1 text-center">{t('initial-deposit')}</h3>
|
||||
<AccountSelect
|
||||
accounts={walletTokens}
|
||||
selectedAccount={selectedAccount}
|
||||
onSelectAccount={handleAccountSelect}
|
||||
/>
|
||||
<Label className="mt-4">{t('amount')}</Label>
|
||||
<Input
|
||||
type="number"
|
||||
min="0"
|
||||
placeholder="0.00"
|
||||
error={!!invalidAmountMessage}
|
||||
onBlur={(e) => validateAmountInput(e.target.value)}
|
||||
value={inputAmount || ''}
|
||||
onChange={(e) => onChangeAmountInput(e.target.value)}
|
||||
suffix={symbol}
|
||||
/>
|
||||
{invalidAmountMessage ? (
|
||||
<div className="flex items-center py-1.5 text-th-red">
|
||||
<ExclamationCircleIcon className="mr-1.5 h-4 w-4" />
|
||||
{invalidAmountMessage}
|
||||
</div>
|
||||
) : null}
|
||||
<div className="pt-1">
|
||||
<ButtonGroup
|
||||
activeValue={depositPercentage}
|
||||
onChange={(v) => onChangeAmountButtons(v)}
|
||||
unit="%"
|
||||
values={['25', '50', '75', '100']}
|
||||
/>
|
||||
</div>
|
||||
<div className={`flex justify-center pt-6`}>
|
||||
<Button
|
||||
disabled={
|
||||
parseFloat(inputAmount) <= 0 ||
|
||||
parseFloat(inputAmount) > selectedAccount?.uiBalance
|
||||
}
|
||||
onClick={handleNewAccountDeposit}
|
||||
className="w-full"
|
||||
>
|
||||
<div className={`flex items-center justify-center`}>
|
||||
{submitting && <Loading className="-ml-1 mr-3" />}
|
||||
{t('lets-go')}
|
||||
</div>
|
||||
</Button>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default NewAccount
|
|
@ -0,0 +1,192 @@
|
|||
import { useState, useEffect } from 'react'
|
||||
import { notify } from 'utils/notifications'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
import { PhotographIcon } from '@heroicons/react/solid'
|
||||
import Modal from './Modal'
|
||||
import { ElementTitle } from './styles'
|
||||
import Button from './Button'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { LinkButton } from 'components'
|
||||
import bs58 from 'bs58'
|
||||
|
||||
const ImgWithLoader = (props) => {
|
||||
const [isLoading, setIsLoading] = useState(true)
|
||||
return (
|
||||
<div className="relative">
|
||||
{isLoading && (
|
||||
<PhotographIcon className="absolute left-1/2 top-1/2 z-10 h-1/4 w-1/4 -translate-x-1/2 -translate-y-1/2 transform animate-pulse text-th-fgd-4" />
|
||||
)}
|
||||
<img {...props} onLoad={() => setIsLoading(false)} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const NftProfilePicModal = ({ isOpen, onClose }) => {
|
||||
const { t } = useTranslation(['common', 'profile'])
|
||||
const { publicKey, signMessage } = useWallet()
|
||||
const nfts = useMangoStore((s) => s.wallet.nfts.data)
|
||||
const nftsLoading = useMangoStore((s) => s.wallet.nfts.loading)
|
||||
const [selectedProfile, setSelectedProfile] = useState<string>('')
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
const profile = useMangoStore((s) => s.profile.details)
|
||||
|
||||
useEffect(() => {
|
||||
if (publicKey) {
|
||||
actions.fetchNfts(publicKey)
|
||||
}
|
||||
}, [publicKey])
|
||||
|
||||
useEffect(() => {
|
||||
if (profile.profile_image_url) {
|
||||
setSelectedProfile(profile.profile_image_url)
|
||||
}
|
||||
}, [profile])
|
||||
|
||||
const saveProfileImage = async () => {
|
||||
const name = profile.profile_name.toLowerCase()
|
||||
const traderCategory = profile.trader_category
|
||||
try {
|
||||
if (!publicKey) throw new Error('Wallet not connected!')
|
||||
if (!signMessage)
|
||||
throw new Error('Wallet does not support message signing!')
|
||||
|
||||
const messageString = JSON.stringify({
|
||||
profile_name: name,
|
||||
trader_category: traderCategory,
|
||||
profile_image_url: selectedProfile,
|
||||
})
|
||||
const message = new TextEncoder().encode(messageString)
|
||||
const signature = await signMessage(message)
|
||||
|
||||
const requestOptions = {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
wallet_pk: publicKey.toString(),
|
||||
message: messageString,
|
||||
signature: bs58.encode(signature),
|
||||
}),
|
||||
}
|
||||
const response = await fetch(
|
||||
'https://mango-transaction-log.herokuapp.com/v3/user-data/profile-details',
|
||||
requestOptions
|
||||
)
|
||||
if (response.status === 200) {
|
||||
await actions.fetchProfileDetails(publicKey.toString())
|
||||
onClose()
|
||||
notify({
|
||||
type: 'success',
|
||||
title: t('profile:profile-pic-success'),
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
notify({
|
||||
type: 'success',
|
||||
title: t('profile:profile:profile-pic-failure'),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const removeProfileImage = async () => {
|
||||
const name = profile.profile_name.toLowerCase()
|
||||
const traderCategory = profile.trader_category
|
||||
try {
|
||||
if (!publicKey) throw new Error('Wallet not connected!')
|
||||
if (!signMessage)
|
||||
throw new Error('Wallet does not support message signing!')
|
||||
|
||||
const messageString = JSON.stringify({
|
||||
profile_name: name,
|
||||
trader_category: traderCategory,
|
||||
profile_image_url: '',
|
||||
})
|
||||
const message = new TextEncoder().encode(messageString)
|
||||
const signature = await signMessage(message)
|
||||
|
||||
const requestOptions = {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
wallet_pk: publicKey.toString(),
|
||||
message: messageString,
|
||||
signature: bs58.encode(signature),
|
||||
}),
|
||||
}
|
||||
const response = await fetch(
|
||||
'https://mango-transaction-log.herokuapp.com/v3/user-data/profile-details',
|
||||
requestOptions
|
||||
)
|
||||
if (response.status === 200) {
|
||||
await actions.fetchProfileDetails(publicKey.toString())
|
||||
onClose()
|
||||
notify({
|
||||
type: 'success',
|
||||
title: t('profile:profile-pic-remove-success'),
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
notify({
|
||||
type: 'success',
|
||||
title: t('profile:profile-pic-remove-failure'),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal isOpen={isOpen} onClose={onClose}>
|
||||
<Modal.Header>
|
||||
<div className="mb-3 flex w-full flex-col items-center sm:mt-3 sm:flex-row sm:justify-between">
|
||||
<ElementTitle noMarginBottom>
|
||||
{t('profile:choose-profile')}
|
||||
</ElementTitle>
|
||||
<div className="mt-3 flex items-center space-x-4 sm:mt-0">
|
||||
<Button disabled={!selectedProfile} onClick={saveProfileImage}>
|
||||
{t('save')}
|
||||
</Button>
|
||||
{profile.profile_image_url ? (
|
||||
<LinkButton className="text-xs" onClick={removeProfileImage}>
|
||||
{t('profile:remove')}
|
||||
</LinkButton>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</Modal.Header>
|
||||
{nfts.length > 0 ? (
|
||||
<div className="flex flex-col items-center">
|
||||
<div className="mb-4 grid w-full grid-flow-row grid-cols-3 gap-4">
|
||||
{nfts.map((n) => (
|
||||
<button
|
||||
className={`default-transitions col-span-1 flex items-center justify-center rounded-md border bg-th-bkg-3 py-3 sm:py-4 md:hover:bg-th-bkg-4 ${
|
||||
selectedProfile === n.image
|
||||
? 'border-th-primary'
|
||||
: 'border-th-bkg-3'
|
||||
}`}
|
||||
key={n.image}
|
||||
onClick={() => setSelectedProfile(n.image)}
|
||||
>
|
||||
<ImgWithLoader
|
||||
className="h-16 w-16 flex-shrink-0 rounded-full sm:h-20 sm:w-20"
|
||||
src={n.image}
|
||||
/>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
) : nftsLoading ? (
|
||||
<div className="mb-4 grid w-full grid-flow-row grid-cols-3 gap-4">
|
||||
{[...Array(9)].map((i) => (
|
||||
<div
|
||||
className="col-span-1 h-[90px] animate-pulse rounded-md bg-th-bkg-3 sm:h-28"
|
||||
key={i}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-center">{t('profile:no-nfts')}</p>
|
||||
)}
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default NftProfilePicModal
|
|
@ -0,0 +1,210 @@
|
|||
import { Fragment, useEffect } from 'react'
|
||||
import {
|
||||
CheckCircleIcon,
|
||||
ExternalLinkIcon,
|
||||
InformationCircleIcon,
|
||||
XCircleIcon,
|
||||
} from '@heroicons/react/solid'
|
||||
import useMangoStore, {
|
||||
CLIENT_TX_TIMEOUT,
|
||||
CLUSTER,
|
||||
} from '../stores/useMangoStore'
|
||||
import { Notification, notify } from '../utils/notifications'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import Loading from './Loading'
|
||||
import { Transition } from '@headlessui/react'
|
||||
|
||||
const NotificationList = () => {
|
||||
const { t } = useTranslation('common')
|
||||
const notifications = useMangoStore((s) => s.notifications)
|
||||
const walletTokens = useMangoStore((s) => s.wallet.tokens)
|
||||
const notEnoughSoLMessage = t('not-enough-sol')
|
||||
|
||||
// if a notification is shown with {"InstructionError":[0,{"Custom":1}]} then
|
||||
// add a notification letting the user know they may not have enough SOL
|
||||
useEffect(() => {
|
||||
if (notifications.length) {
|
||||
const customErrorNotification = notifications.find(
|
||||
(n) => n.description && n.description.includes('"Custom":1')
|
||||
)
|
||||
const notEnoughSolNotification = notifications.find(
|
||||
(n) => n.title && n.title.includes(notEnoughSoLMessage)
|
||||
)
|
||||
const solBalance = walletTokens.find(
|
||||
(t) => t.config.symbol === 'SOL'
|
||||
)?.uiBalance
|
||||
|
||||
if (
|
||||
customErrorNotification &&
|
||||
solBalance < 0.04 &&
|
||||
!notEnoughSolNotification
|
||||
) {
|
||||
notify({
|
||||
title: notEnoughSoLMessage,
|
||||
type: 'info',
|
||||
})
|
||||
}
|
||||
}
|
||||
}, [notifications, walletTokens])
|
||||
|
||||
const reversedNotifications = [...notifications].reverse()
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`pointer-events-none fixed inset-0 z-50 flex items-end px-4 py-6 text-th-fgd-1 sm:p-6`}
|
||||
>
|
||||
<div className={`flex w-full flex-col`}>
|
||||
{reversedNotifications.map((n) => (
|
||||
<Notification key={n.id} notification={n} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const Notification = ({ notification }: { notification: Notification }) => {
|
||||
const { t } = useTranslation('common')
|
||||
const setMangoStore = useMangoStore((s) => s.set)
|
||||
const { type, title, description, txid, show, id } = notification
|
||||
|
||||
// overwrite the title if of the error message if it is a time out error
|
||||
let parsedTitle
|
||||
if (description) {
|
||||
if (
|
||||
description?.includes('Timed out awaiting') ||
|
||||
description?.includes('was not confirmed')
|
||||
) {
|
||||
parsedTitle = 'Transaction status unknown'
|
||||
}
|
||||
}
|
||||
|
||||
// if the notification is a success, then hide the confirming tx notification with the same txid
|
||||
useEffect(() => {
|
||||
if ((type === 'error' || type === 'success') && txid) {
|
||||
setMangoStore((s) => {
|
||||
const newNotifications = s.notifications.map((n) =>
|
||||
n.txid === txid && n.type === 'confirm' ? { ...n, show: false } : n
|
||||
)
|
||||
s.notifications = newNotifications
|
||||
})
|
||||
}
|
||||
}, [type, txid])
|
||||
|
||||
const hideNotification = () => {
|
||||
setMangoStore((s) => {
|
||||
const newNotifications = s.notifications.map((n) =>
|
||||
n.id === id ? { ...n, show: false } : n
|
||||
)
|
||||
s.notifications = newNotifications
|
||||
})
|
||||
}
|
||||
|
||||
// auto hide a notification after 8 seconds unless it is a confirming or time out notification
|
||||
// if no status is provided for a tx notification after 90s, hide it
|
||||
useEffect(() => {
|
||||
const id = setTimeout(
|
||||
() => {
|
||||
if (show) {
|
||||
hideNotification()
|
||||
}
|
||||
},
|
||||
parsedTitle || type === 'confirm' || type === 'error'
|
||||
? CLIENT_TX_TIMEOUT
|
||||
: 8000
|
||||
)
|
||||
|
||||
return () => {
|
||||
clearInterval(id)
|
||||
}
|
||||
})
|
||||
|
||||
return (
|
||||
<Transition
|
||||
show={show}
|
||||
as={Fragment}
|
||||
appear={true}
|
||||
enter="transform ease-out duration-500 transition"
|
||||
enterFrom="translate-y-2 opacity-0 sm:translate-y-0 sm:-translate-x-48"
|
||||
enterTo="translate-y-0 opacity-100 sm:translate-x-0"
|
||||
leave="transform ease-in duration-200 transition"
|
||||
leaveFrom="translate-y-0 sm:translate-x-0"
|
||||
leaveTo="-translate-y-2 sm:translate-y-0 sm:-translate-x-48"
|
||||
>
|
||||
<div
|
||||
className={`pointer-events-auto mt-2 w-full max-w-sm overflow-hidden rounded-md border border-th-bkg-4 bg-th-bkg-3 shadow-lg ring-1 ring-black ring-opacity-5`}
|
||||
>
|
||||
<div className={`relative flex items-center px-2 py-2.5`}>
|
||||
<div className={`flex-shrink-0`}>
|
||||
{type === 'success' ? (
|
||||
<CheckCircleIcon className={`mr-1 h-7 w-7 text-th-green`} />
|
||||
) : null}
|
||||
{type === 'info' && (
|
||||
<InformationCircleIcon
|
||||
className={`mr-1 h-7 w-7 text-th-primary`}
|
||||
/>
|
||||
)}
|
||||
{type === 'error' && (
|
||||
<XCircleIcon className={`mr-1 h-7 w-7 text-th-red`} />
|
||||
)}
|
||||
{type === 'confirm' && (
|
||||
<Loading className="mr-1 h-7 w-7 text-th-fgd-3" />
|
||||
)}
|
||||
</div>
|
||||
<div className={`ml-2 flex-1`}>
|
||||
<div className={`text-normal font-bold text-th-fgd-1`}>
|
||||
{parsedTitle || title}
|
||||
</div>
|
||||
{description ? (
|
||||
<p className={`mb-0 mt-0.5 leading-tight text-th-fgd-3`}>
|
||||
{description}
|
||||
</p>
|
||||
) : null}
|
||||
{txid ? (
|
||||
<a
|
||||
href={
|
||||
'https://explorer.solana.com/tx/' +
|
||||
txid +
|
||||
'?cluster=' +
|
||||
CLUSTER
|
||||
}
|
||||
className="mt-1 flex items-center text-sm"
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
<div className="flex-1 break-all text-xs">
|
||||
{type === 'error'
|
||||
? txid
|
||||
: `${txid.slice(0, 14)}...${txid.slice(txid.length - 14)}`}
|
||||
</div>
|
||||
<ExternalLinkIcon className="mb-0.5 ml-1 h-4 w-4" />
|
||||
</a>
|
||||
) : null}
|
||||
</div>
|
||||
<div className={`absolute right-2 top-2 flex-shrink-0`}>
|
||||
<button
|
||||
onClick={hideNotification}
|
||||
className={`text-th-fgd-4 focus:outline-none md:hover:text-th-primary`}
|
||||
>
|
||||
<span className={`sr-only`}>{t('close')}</span>
|
||||
<svg
|
||||
className={`h-5 w-5`}
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 20 20"
|
||||
fill="currentColor"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Transition>
|
||||
)
|
||||
}
|
||||
|
||||
export default NotificationList
|
|
@ -0,0 +1,627 @@
|
|||
import { useMemo, useState } from 'react'
|
||||
import { PencilIcon, TrashIcon, XIcon } from '@heroicons/react/solid'
|
||||
import Link from 'next/link'
|
||||
import { useRouter } from 'next/router'
|
||||
import Button, { IconButton } from './Button'
|
||||
import Loading from './Loading'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import { notify } from '../utils/notifications'
|
||||
import SideBadge from './SideBadge'
|
||||
import { Order, Market } from '@project-serum/serum/lib/market'
|
||||
import {
|
||||
PerpOrder,
|
||||
PerpMarket,
|
||||
MarketConfig,
|
||||
} from '@blockworks-foundation/mango-client'
|
||||
import { formatUsdValue, getDecimalCount, usdFormatter } from '../utils'
|
||||
import { Table, Td, Th, TrBody, TrHead } from './TableElements'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from './TradePageGrid'
|
||||
import { Row } from './TableElements'
|
||||
import { PerpTriggerOrder } from '../@types/types'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import Input, { Label } from './Input'
|
||||
import { useWallet, Wallet } from '@solana/wallet-adapter-react'
|
||||
|
||||
const DesktopTable = ({
|
||||
cancelledOrderId,
|
||||
editOrderIndex,
|
||||
handleCancelOrder,
|
||||
handleCancelAllOrders,
|
||||
handleModifyOrder,
|
||||
modifiedOrderId,
|
||||
openOrders,
|
||||
setEditOrderIndex,
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const { asPath } = useRouter()
|
||||
const { wallet } = useWallet()
|
||||
const [modifiedOrderSize, setModifiedOrderSize] = useState('')
|
||||
const [modifiedOrderPrice, setModifiedOrderPrice] = useState('')
|
||||
|
||||
const showEditOrderForm = (index, order) => {
|
||||
setEditOrderIndex(index)
|
||||
setModifiedOrderSize(order.size)
|
||||
setModifiedOrderPrice(order.price)
|
||||
}
|
||||
|
||||
const renderMarketName = (market: MarketConfig) => {
|
||||
const location =
|
||||
market.kind === 'spot'
|
||||
? `/?name=${market.baseSymbol}%2FUSDC`
|
||||
: `/?name=${market.name}`
|
||||
if (!asPath.includes(location)) {
|
||||
return (
|
||||
<Link href={location} shallow={true}>
|
||||
<a className="text-th-fgd-1 underline hover:text-th-fgd-1 hover:no-underline">
|
||||
{market.name}
|
||||
</a>
|
||||
</Link>
|
||||
)
|
||||
} else {
|
||||
return <span>{market.name}</span>
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Table>
|
||||
<thead>
|
||||
<TrHead>
|
||||
<Th>{t('market')}</Th>
|
||||
<Th>{t('side')}</Th>
|
||||
<Th>{t('size')}</Th>
|
||||
<Th>{t('price')}</Th>
|
||||
<Th>{t('value')}</Th>
|
||||
<Th>{t('condition')}</Th>
|
||||
<Th>
|
||||
<span className={`sr-only`}>{t('edit')}</span>
|
||||
</Th>
|
||||
</TrHead>
|
||||
</thead>
|
||||
<tbody>
|
||||
{openOrders.map(({ order, market }, index) => {
|
||||
const decimals = getDecimalCount(market.account.tickSize)
|
||||
const editThisOrder = editOrderIndex === index
|
||||
return (
|
||||
<TrBody key={`${order.orderId}${order.side}`}>
|
||||
<Td className="w-[14.286%]">
|
||||
<div className="flex items-center">
|
||||
<img
|
||||
alt=""
|
||||
width="20"
|
||||
height="20"
|
||||
src={`/assets/icons/${market.config.baseSymbol.toLowerCase()}.svg`}
|
||||
className={`mr-2.5`}
|
||||
/>
|
||||
<span className="whitespace-nowrap">
|
||||
{renderMarketName(market.config)}
|
||||
</span>
|
||||
</div>
|
||||
</Td>
|
||||
<Td className="w-[14.286%]">
|
||||
<SideBadge side={order.side} />
|
||||
</Td>
|
||||
{editOrderIndex !== index ? (
|
||||
<>
|
||||
<Td className="w-[14.286%]">
|
||||
{order.size.toLocaleString(undefined, {
|
||||
maximumFractionDigits: 4,
|
||||
})}
|
||||
</Td>
|
||||
<Td className="w-[14.286%]">
|
||||
{usdFormatter(order.price, decimals)}
|
||||
</Td>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Td className="w-[14.286%]">
|
||||
<Input
|
||||
className="default-transition h-7 rounded-none border-b-2 border-l-0 border-r-0 border-t-0 border-th-fgd-4 bg-transparent px-0 hover:border-th-fgd-3 focus:border-th-fgd-3 focus:outline-none"
|
||||
type="number"
|
||||
value={modifiedOrderSize}
|
||||
onChange={(e) => setModifiedOrderSize(e.target.value)}
|
||||
/>
|
||||
</Td>
|
||||
<Td className="w-[14.286%]">
|
||||
<Input
|
||||
autoFocus
|
||||
className="default-transition h-7 rounded-none border-b-2 border-l-0 border-r-0 border-t-0 border-th-fgd-4 bg-transparent px-0 hover:border-th-fgd-3 focus:border-th-fgd-3 focus:outline-none"
|
||||
type="number"
|
||||
value={modifiedOrderPrice}
|
||||
onChange={(e) => setModifiedOrderPrice(e.target.value)}
|
||||
/>
|
||||
</Td>
|
||||
</>
|
||||
)}
|
||||
<Td className="w-[14.286%]">
|
||||
{editThisOrder ? '' : formatUsdValue(order.price * order.size)}
|
||||
</Td>
|
||||
<Td className="w-[14.286%]">
|
||||
{order.perpTrigger &&
|
||||
`${t(order.orderType)} ${t(
|
||||
order.triggerCondition
|
||||
)} $${order.triggerPrice.toFixed(2)}`}
|
||||
</Td>
|
||||
<Td className="w-[14.286%]">
|
||||
<div className={`flex justify-end space-x-2`}>
|
||||
{editOrderIndex !== index ? (
|
||||
<>
|
||||
{!order.perpTrigger ? (
|
||||
<Button
|
||||
onClick={() => showEditOrderForm(index, order)}
|
||||
className="-my-1 h-7 pt-0 pb-0 pl-3 pr-3 text-xs"
|
||||
>
|
||||
{t('edit')}
|
||||
</Button>
|
||||
) : null}
|
||||
<Button
|
||||
onClick={() => handleCancelOrder(order, market.account)}
|
||||
className="-my-1 h-7 pt-0 pb-0 pl-3 pr-3 text-xs"
|
||||
primary={false}
|
||||
>
|
||||
{cancelledOrderId + '' === order.orderId + '' ? (
|
||||
<Loading />
|
||||
) : (
|
||||
<span>{t('cancel')}</span>
|
||||
)}
|
||||
</Button>
|
||||
{openOrders.filter(
|
||||
(o) => o.market.config.name === market.config.name
|
||||
).length > 1 ? (
|
||||
<Button
|
||||
onClick={() => handleCancelAllOrders(market.account)}
|
||||
className="-my-1 h-7 pt-0 pb-0 pl-3 pr-3 text-xs text-th-red"
|
||||
primary={false}
|
||||
>
|
||||
{t('cancel-all') + ' ' + market.config.name}
|
||||
</Button>
|
||||
) : null}
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Button
|
||||
className="h-7 pt-0 pb-0 pl-3 pr-3 text-xs"
|
||||
onClick={() =>
|
||||
handleModifyOrder(
|
||||
order,
|
||||
market.account,
|
||||
modifiedOrderPrice || order.price,
|
||||
modifiedOrderSize || order.size,
|
||||
wallet
|
||||
)
|
||||
}
|
||||
>
|
||||
{modifiedOrderId + '' === order.orderId + '' ? (
|
||||
<Loading />
|
||||
) : (
|
||||
<span>{t('save')}</span>
|
||||
)}
|
||||
</Button>
|
||||
<Button
|
||||
className="h-7 pt-0 pb-0 pl-3 pr-3 text-xs"
|
||||
onClick={() => setEditOrderIndex(null)}
|
||||
>
|
||||
Cancel Edit
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</Td>
|
||||
</TrBody>
|
||||
)
|
||||
})}
|
||||
</tbody>
|
||||
</Table>
|
||||
)
|
||||
}
|
||||
|
||||
const MobileTable = ({
|
||||
cancelledOrderId,
|
||||
editOrderIndex,
|
||||
handleCancelOrder,
|
||||
handleCancelAllOrders,
|
||||
handleModifyOrder,
|
||||
modifiedOrderId,
|
||||
openOrders,
|
||||
setEditOrderIndex,
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const { wallet } = useWallet()
|
||||
const [modifiedOrderSize, setModifiedOrderSize] = useState('')
|
||||
const [modifiedOrderPrice, setModifiedOrderPrice] = useState('')
|
||||
|
||||
const showEditOrderForm = (index) => {
|
||||
setEditOrderIndex(index)
|
||||
setModifiedOrderSize('')
|
||||
setModifiedOrderPrice('')
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="border-b border-th-bkg-3">
|
||||
{openOrders.map(({ market, order }, index) => {
|
||||
const editThisOrder = editOrderIndex === index
|
||||
return (
|
||||
<Row key={`${order.orderId}${order.side}`}>
|
||||
<div className="text-fgd-1 col-span-12 flex items-center justify-between text-left">
|
||||
<div className="flex items-center">
|
||||
<img
|
||||
alt=""
|
||||
width="20"
|
||||
height="20"
|
||||
src={`/assets/icons/${market.config.baseSymbol.toLowerCase()}.svg`}
|
||||
className={`mr-2.5`}
|
||||
/>
|
||||
<div>
|
||||
<div className="mb-0.5">{market.config.name}</div>
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
<span
|
||||
className={`mr-1
|
||||
${
|
||||
order.side === 'buy'
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}
|
||||
`}
|
||||
>
|
||||
{order.side.toUpperCase()}
|
||||
</span>
|
||||
{order.perpTrigger
|
||||
? `${order.size.toLocaleString(undefined, {
|
||||
maximumFractionDigits: 4,
|
||||
})} ${order.triggerCondition} ${formatUsdValue(
|
||||
order.triggerPrice
|
||||
)}`
|
||||
: `${order.size.toLocaleString(undefined, {
|
||||
maximumFractionDigits: 4,
|
||||
})} at ${formatUsdValue(order.price)}`}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex space-x-2">
|
||||
{!order.perpTrigger ? (
|
||||
<IconButton
|
||||
className={index % 2 === 0 ? 'bg-th-bkg-4' : 'bg-th-bkg-3'}
|
||||
onClick={() =>
|
||||
editThisOrder
|
||||
? setEditOrderIndex(null)
|
||||
: showEditOrderForm(index)
|
||||
}
|
||||
>
|
||||
{editThisOrder ? (
|
||||
<XIcon className="h-5 w-5" />
|
||||
) : (
|
||||
<PencilIcon className="h-5 w-5" />
|
||||
)}
|
||||
</IconButton>
|
||||
) : null}
|
||||
<IconButton
|
||||
className={index % 2 === 0 ? 'bg-th-bkg-4' : 'bg-th-bkg-3'}
|
||||
onClick={() => handleCancelOrder(order, market.account)}
|
||||
>
|
||||
{cancelledOrderId + '' === order.orderId + '' ? (
|
||||
<Loading />
|
||||
) : (
|
||||
<TrashIcon className="h-5 w-5" />
|
||||
)}
|
||||
</IconButton>
|
||||
{openOrders.filter(
|
||||
(o) => o.market.config.name === market.config.name
|
||||
).length > 1 ? (
|
||||
<IconButton
|
||||
onClick={() => handleCancelAllOrders(market.account)}
|
||||
>
|
||||
<TrashIcon className="h-5 w-5 text-th-red" />
|
||||
</IconButton>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
{editThisOrder ? (
|
||||
<div className="flex flex-col pt-4 sm:flex-row sm:space-x-3">
|
||||
<div className="pb-3">
|
||||
<Label className="text-xs">{t('size')}</Label>
|
||||
<Input
|
||||
className="default-transition h-7 w-full rounded-none border-b-2 border-l-0 border-r-0 border-t-0 border-th-fgd-4 bg-transparent px-0 hover:border-th-fgd-3 focus:border-th-fgd-3 focus:outline-none"
|
||||
type="number"
|
||||
value={modifiedOrderSize || order.size}
|
||||
onChange={(e) => setModifiedOrderSize(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Label className="text-xs">{t('price')}</Label>
|
||||
<Input
|
||||
autoFocus
|
||||
className="default-transition h-7 w-full rounded-none border-b-2 border-l-0 border-r-0 border-t-0 border-th-fgd-4 bg-transparent px-0 hover:border-th-fgd-3 focus:border-th-fgd-3 focus:outline-none"
|
||||
type="number"
|
||||
value={modifiedOrderPrice || order.price}
|
||||
onChange={(e) => setModifiedOrderPrice(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
className={`mt-4 h-8 pt-0 pb-0 pl-3 pr-3 text-xs`}
|
||||
onClick={() =>
|
||||
handleModifyOrder(
|
||||
order,
|
||||
market.account,
|
||||
modifiedOrderPrice || order.price,
|
||||
modifiedOrderSize || order.size,
|
||||
wallet
|
||||
)
|
||||
}
|
||||
>
|
||||
{modifiedOrderId + '' === order.orderId + '' ? (
|
||||
<Loading />
|
||||
) : (
|
||||
<span>{t('save')}</span>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
) : null}
|
||||
</Row>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const OpenOrdersTable = () => {
|
||||
const { t } = useTranslation('common')
|
||||
const { asPath } = useRouter()
|
||||
const { wallet } = useWallet()
|
||||
const openOrders = useMangoStore((s) => s.selectedMangoAccount.openOrders)
|
||||
const [cancelId, setCancelId] = useState<any>(null)
|
||||
const [modifyId, setModifyId] = useState<any>(null)
|
||||
const [editOrderIndex, setEditOrderIndex] = useState(null)
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
const { width } = useViewport()
|
||||
const isMobile = width ? width < breakpoints.md : false
|
||||
|
||||
const handleCancelAllOrders = async (market: PerpMarket | Market) => {
|
||||
const selectedMangoGroup =
|
||||
useMangoStore.getState().selectedMangoGroup.current
|
||||
const selectedMangoAccount =
|
||||
useMangoStore.getState().selectedMangoAccount.current
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
try {
|
||||
if (!selectedMangoGroup || !selectedMangoAccount || !wallet) return
|
||||
|
||||
if (market instanceof PerpMarket) {
|
||||
const txids = await mangoClient.cancelAllPerpOrders(
|
||||
selectedMangoGroup,
|
||||
[market],
|
||||
selectedMangoAccount,
|
||||
wallet.adapter
|
||||
)
|
||||
if (txids) {
|
||||
for (const txid of txids) {
|
||||
notify({
|
||||
title: t('cancel-all-success'),
|
||||
description: '',
|
||||
txid,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
notify({
|
||||
title: t('cancel-all-error'),
|
||||
description: t('transaction-failed'),
|
||||
})
|
||||
}
|
||||
actions.reloadOrders()
|
||||
} else if (market instanceof Market) {
|
||||
const txid = await mangoClient.cancelAllSpotOrders(
|
||||
selectedMangoGroup,
|
||||
selectedMangoAccount,
|
||||
market,
|
||||
wallet.adapter,
|
||||
20
|
||||
)
|
||||
if (txid) {
|
||||
notify({
|
||||
title: t('cancel-all-success'),
|
||||
description: '',
|
||||
txid,
|
||||
})
|
||||
} else {
|
||||
notify({
|
||||
title: t('cancel-all-error'),
|
||||
description: t('transaction-failed'),
|
||||
})
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
notify({
|
||||
title: t('cancel-all-error'),
|
||||
description: e.message,
|
||||
txid: e.txid,
|
||||
type: 'error',
|
||||
})
|
||||
console.log('error', `${e}`)
|
||||
} finally {
|
||||
actions.reloadMangoAccount()
|
||||
actions.updateOpenOrders()
|
||||
}
|
||||
}
|
||||
|
||||
const handleCancelOrder = async (
|
||||
order: Order | PerpOrder | PerpTriggerOrder,
|
||||
market: Market | PerpMarket
|
||||
) => {
|
||||
const selectedMangoGroup =
|
||||
useMangoStore.getState().selectedMangoGroup.current
|
||||
const selectedMangoAccount =
|
||||
useMangoStore.getState().selectedMangoAccount.current
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
setCancelId(order.orderId)
|
||||
let txid
|
||||
try {
|
||||
if (!selectedMangoGroup || !selectedMangoAccount || !wallet) return
|
||||
if (market instanceof Market) {
|
||||
txid = await mangoClient.cancelSpotOrder(
|
||||
selectedMangoGroup,
|
||||
selectedMangoAccount,
|
||||
wallet.adapter,
|
||||
market,
|
||||
order as Order
|
||||
)
|
||||
actions.reloadOrders()
|
||||
} else if (market instanceof PerpMarket) {
|
||||
// TODO: this is not ideal
|
||||
if (order['triggerCondition']) {
|
||||
txid = await mangoClient.removeAdvancedOrder(
|
||||
selectedMangoGroup,
|
||||
selectedMangoAccount,
|
||||
wallet.adapter,
|
||||
(order as PerpTriggerOrder).orderId
|
||||
)
|
||||
actions.reloadOrders()
|
||||
} else {
|
||||
txid = await mangoClient.cancelPerpOrder(
|
||||
selectedMangoGroup,
|
||||
selectedMangoAccount,
|
||||
wallet.adapter,
|
||||
market,
|
||||
order as PerpOrder,
|
||||
false
|
||||
)
|
||||
}
|
||||
}
|
||||
notify({ title: t('cancel-success'), txid })
|
||||
} catch (e) {
|
||||
notify({
|
||||
title: t('cancel-error'),
|
||||
description: e.message,
|
||||
txid: e.txid,
|
||||
type: 'error',
|
||||
})
|
||||
console.log('error', `${e}`)
|
||||
} finally {
|
||||
actions.reloadMangoAccount()
|
||||
actions.updateOpenOrders()
|
||||
setCancelId(null)
|
||||
}
|
||||
}
|
||||
|
||||
const handleModifyOrder = async (
|
||||
order: Order | PerpOrder,
|
||||
market: Market | PerpMarket,
|
||||
price: number,
|
||||
size: number,
|
||||
wallet: Wallet
|
||||
) => {
|
||||
const mangoAccount = useMangoStore.getState().selectedMangoAccount.current
|
||||
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
const marketConfig = useMangoStore.getState().selectedMarket.config
|
||||
const askInfo =
|
||||
useMangoStore.getState().accountInfos[marketConfig.asksKey.toString()]
|
||||
const bidInfo =
|
||||
useMangoStore.getState().accountInfos[marketConfig.bidsKey.toString()]
|
||||
const referrerPk = useMangoStore.getState().referrerPk
|
||||
|
||||
if (!wallet || !mangoGroup || !mangoAccount || !market) return
|
||||
setModifyId(order.orderId)
|
||||
try {
|
||||
const orderPrice = price
|
||||
|
||||
if (!orderPrice) {
|
||||
notify({
|
||||
title: t('price-unavailable'),
|
||||
description: t('try-again'),
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
const orderType = 'limit'
|
||||
let txid
|
||||
if (market instanceof Market) {
|
||||
txid = await mangoClient.modifySpotOrder(
|
||||
mangoGroup,
|
||||
mangoAccount,
|
||||
mangoGroup.mangoCache,
|
||||
market,
|
||||
wallet?.adapter,
|
||||
order as Order,
|
||||
order.side,
|
||||
orderPrice,
|
||||
size,
|
||||
orderType
|
||||
)
|
||||
} else {
|
||||
txid = await mangoClient.modifyPerpOrder(
|
||||
mangoGroup,
|
||||
mangoAccount,
|
||||
mangoGroup.mangoCache,
|
||||
market,
|
||||
wallet?.adapter,
|
||||
order as PerpOrder,
|
||||
order.side,
|
||||
orderPrice,
|
||||
size,
|
||||
orderType,
|
||||
0,
|
||||
order.side === 'buy' ? askInfo : bidInfo,
|
||||
false,
|
||||
referrerPk ? referrerPk : undefined
|
||||
)
|
||||
}
|
||||
notify({ title: t('successfully-placed'), txid })
|
||||
} catch (e) {
|
||||
console.log('error: ', e.message, e.txid, e)
|
||||
|
||||
notify({
|
||||
title: t('order-error'),
|
||||
description: e.message,
|
||||
txid: e.txid,
|
||||
type: 'error',
|
||||
})
|
||||
} finally {
|
||||
actions.reloadMangoAccount()
|
||||
actions.reloadOrders()
|
||||
actions.updateOpenOrders()
|
||||
setModifyId(null)
|
||||
setEditOrderIndex(null)
|
||||
}
|
||||
}
|
||||
|
||||
const sortedOpenOrders = useMemo(() => {
|
||||
return [...openOrders].sort((a, b) => b.price - a.price)
|
||||
}, [openOrders])
|
||||
|
||||
const tableProps = {
|
||||
openOrders: sortedOpenOrders,
|
||||
cancelledOrderId: cancelId,
|
||||
editOrderIndex,
|
||||
handleCancelOrder,
|
||||
handleCancelAllOrders,
|
||||
handleModifyOrder,
|
||||
modifiedOrderId: modifyId,
|
||||
setEditOrderIndex,
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={`flex flex-col sm:pb-4`}>
|
||||
<div className={`overflow-x-auto sm:-mx-6 lg:-mx-8`}>
|
||||
<div className={`inline-block min-w-full align-middle sm:px-6 lg:px-8`}>
|
||||
{openOrders && openOrders.length > 0 ? (
|
||||
!isMobile ? (
|
||||
<DesktopTable {...tableProps} />
|
||||
) : (
|
||||
<MobileTable {...tableProps} />
|
||||
)
|
||||
) : (
|
||||
<div
|
||||
className={`w-full rounded-md border border-th-bkg-3 py-6 text-center text-th-fgd-3`}
|
||||
>
|
||||
{t('no-orders')}
|
||||
{asPath === '/account' ? (
|
||||
<Link href={'/'} shallow={true}>
|
||||
<a className={`ml-2 inline-flex py-0`}>{t('make-trade')}</a>
|
||||
</Link>
|
||||
) : null}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default OpenOrdersTable
|
|
@ -0,0 +1,786 @@
|
|||
import React, { useRef, useEffect, useState, useMemo } from 'react'
|
||||
import Big from 'big.js'
|
||||
import isEqualLodash from 'lodash/isEqual'
|
||||
import useInterval from '../hooks/useInterval'
|
||||
import usePrevious from '../hooks/usePrevious'
|
||||
import {
|
||||
isEqual,
|
||||
getDecimalCount,
|
||||
getPrecisionDigits,
|
||||
usdFormatter,
|
||||
} from '../utils/'
|
||||
import {
|
||||
ArrowUpIcon,
|
||||
ArrowDownIcon,
|
||||
SwitchHorizontalIcon,
|
||||
} from '@heroicons/react/solid'
|
||||
import { CumulativeSizeIcon, StepSizeIcon } from './icons'
|
||||
import useMarkPrice from '../hooks/useMarkPrice'
|
||||
import { ElementTitle } from './styles'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import Tooltip from './Tooltip'
|
||||
import GroupSize from './GroupSize'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from './TradePageGrid'
|
||||
import {
|
||||
FlipCard,
|
||||
FlipCardBack,
|
||||
FlipCardFront,
|
||||
FlipCardInner,
|
||||
} from './FlipCard'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import FloatingElement from './FloatingElement'
|
||||
import useLocalStorageState from '../hooks/useLocalStorageState'
|
||||
import { ORDERBOOK_FLASH_KEY } from './SettingsModal'
|
||||
import {
|
||||
mangoGroupConfigSelector,
|
||||
marketConfigSelector,
|
||||
marketSelector,
|
||||
setStoreSelector,
|
||||
} from '../stores/selectors'
|
||||
import { Market } from '@project-serum/serum'
|
||||
import { PerpMarket } from '@blockworks-foundation/mango-client'
|
||||
|
||||
const Line = (props) => {
|
||||
return (
|
||||
<div
|
||||
className={`${props.className}`}
|
||||
style={{
|
||||
textAlign: props.invert ? 'left' : 'right',
|
||||
height: '100%',
|
||||
width: `${props['data-width'] ? props['data-width'] : ''}`,
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const groupBy = (ordersArray, market, grouping: number, isBids: boolean) => {
|
||||
if (!ordersArray || !market || !grouping || grouping == market?.tickSize) {
|
||||
return ordersArray || []
|
||||
}
|
||||
const groupFloors = {}
|
||||
for (let i = 0; i < ordersArray.length; i++) {
|
||||
if (typeof ordersArray[i] == 'undefined') {
|
||||
break
|
||||
}
|
||||
const bigGrouping = Big(grouping)
|
||||
const bigOrder = Big(ordersArray[i][0])
|
||||
|
||||
const floor = isBids
|
||||
? bigOrder.div(bigGrouping).round(0, Big.roundDown).times(bigGrouping)
|
||||
: bigOrder.div(bigGrouping).round(0, Big.roundUp).times(bigGrouping)
|
||||
if (typeof groupFloors[floor] == 'undefined') {
|
||||
groupFloors[floor] = ordersArray[i][1]
|
||||
} else {
|
||||
groupFloors[floor] = ordersArray[i][1] + groupFloors[floor]
|
||||
}
|
||||
}
|
||||
const sortedGroups = Object.entries(groupFloors)
|
||||
.map((entry) => {
|
||||
return [
|
||||
+parseFloat(entry[0]).toFixed(getDecimalCount(grouping)),
|
||||
entry[1],
|
||||
]
|
||||
})
|
||||
.sort(function (a: number[], b: number[]) {
|
||||
if (!a || !b) {
|
||||
return -1
|
||||
}
|
||||
return isBids ? b[0] - a[0] : a[0] - b[0]
|
||||
})
|
||||
return sortedGroups
|
||||
}
|
||||
|
||||
const getCumulativeOrderbookSide = (
|
||||
orders,
|
||||
totalSize,
|
||||
maxSize,
|
||||
depth,
|
||||
backwards = false
|
||||
) => {
|
||||
let cumulative = orders
|
||||
.slice(0, depth)
|
||||
.reduce((cumulative, [price, size], i) => {
|
||||
const cumulativeSize = (cumulative[i - 1]?.cumulativeSize || 0) + size
|
||||
cumulative.push({
|
||||
price,
|
||||
size,
|
||||
cumulativeSize,
|
||||
sizePercent: Math.round((cumulativeSize / (totalSize || 1)) * 100),
|
||||
maxSizePercent: Math.round((size / (maxSize || 1)) * 100),
|
||||
})
|
||||
return cumulative
|
||||
}, [])
|
||||
if (backwards) {
|
||||
cumulative = cumulative.reverse()
|
||||
}
|
||||
return cumulative
|
||||
}
|
||||
|
||||
const hasOpenOrderForPriceGroup = (openOrderPrices, price, grouping) => {
|
||||
return !!openOrderPrices.find((ooPrice) => {
|
||||
return ooPrice >= parseFloat(price) && ooPrice < price + grouping
|
||||
})
|
||||
}
|
||||
|
||||
export default function Orderbook({ depth = 8 }) {
|
||||
const { t } = useTranslation('common')
|
||||
const groupConfig = useMangoStore(mangoGroupConfigSelector)
|
||||
const marketConfig = useMangoStore(marketConfigSelector)
|
||||
const market = useMangoStore(marketSelector)
|
||||
const markPrice = useMarkPrice()
|
||||
|
||||
const { width } = useViewport()
|
||||
const isMobile = width ? width < breakpoints.sm : false
|
||||
|
||||
const currentOrderbookData = useRef<any>(null)
|
||||
const nextOrderbookData = useRef<any>(null)
|
||||
const previousDepth = usePrevious(depth)
|
||||
|
||||
const [openOrderPrices, setOpenOrderPrices] = useState<any[]>([])
|
||||
const [orderbookData, setOrderbookData] = useState<any | null>(null)
|
||||
const [defaultLayout, setDefaultLayout] = useState(true)
|
||||
const [displayCumulativeSize, setDisplayCumulativeSize] = useState(false)
|
||||
const [grouping, setGrouping] = useState(0.01)
|
||||
const [tickSize, setTickSize] = useState(0)
|
||||
const previousGrouping = usePrevious(grouping)
|
||||
|
||||
useEffect(() => {
|
||||
if (market && market.tickSize !== tickSize) {
|
||||
setTickSize(market.tickSize)
|
||||
setGrouping(market.tickSize)
|
||||
}
|
||||
}, [market])
|
||||
|
||||
useInterval(() => {
|
||||
const orderbook = useMangoStore.getState().selectedMarket.orderBook
|
||||
if (
|
||||
nextOrderbookData?.current &&
|
||||
(!isEqualLodash(
|
||||
currentOrderbookData.current,
|
||||
nextOrderbookData.current
|
||||
) ||
|
||||
previousDepth !== depth ||
|
||||
previousGrouping !== grouping)
|
||||
) {
|
||||
// check if user has open orders so we can highlight them on orderbook
|
||||
const openOrders =
|
||||
useMangoStore.getState().selectedMangoAccount.openOrders
|
||||
const newOpenOrderPrices = openOrders?.length
|
||||
? openOrders
|
||||
.filter(({ market }) =>
|
||||
market.account.publicKey.equals(marketConfig.publicKey)
|
||||
)
|
||||
.map(({ order }) => order.price)
|
||||
: []
|
||||
if (!isEqualLodash(newOpenOrderPrices, openOrderPrices)) {
|
||||
setOpenOrderPrices(newOpenOrderPrices)
|
||||
}
|
||||
|
||||
// updated orderbook data
|
||||
const bids = groupBy(orderbook?.bids, market, grouping, true) || []
|
||||
const asks = groupBy(orderbook?.asks, market, grouping, false) || []
|
||||
|
||||
const sum = (total, [, size], index) =>
|
||||
index < depth ? total + size : total
|
||||
const totalSize = bids.reduce(sum, 0) + asks.reduce(sum, 0)
|
||||
const maxSize =
|
||||
Math.max(
|
||||
...asks.map(function (a) {
|
||||
return a[1]
|
||||
})
|
||||
) +
|
||||
Math.max(
|
||||
...bids.map(function (b) {
|
||||
return b[1]
|
||||
})
|
||||
)
|
||||
|
||||
const bidsToDisplay = defaultLayout
|
||||
? getCumulativeOrderbookSide(bids, totalSize, maxSize, depth, false)
|
||||
: getCumulativeOrderbookSide(bids, totalSize, maxSize, depth / 2, false)
|
||||
const asksToDisplay = defaultLayout
|
||||
? getCumulativeOrderbookSide(asks, totalSize, maxSize, depth, false)
|
||||
: getCumulativeOrderbookSide(
|
||||
asks,
|
||||
totalSize,
|
||||
maxSize,
|
||||
(depth + 1) / 2,
|
||||
true
|
||||
)
|
||||
|
||||
currentOrderbookData.current = {
|
||||
bids: orderbook?.bids,
|
||||
asks: orderbook?.asks,
|
||||
}
|
||||
if (bidsToDisplay[0] || asksToDisplay[0]) {
|
||||
const bid = bidsToDisplay[0]?.price
|
||||
const ask = defaultLayout
|
||||
? asksToDisplay[0]?.price
|
||||
: asksToDisplay[asksToDisplay.length - 1]?.price
|
||||
let spread = 0,
|
||||
spreadPercentage = 0
|
||||
if (bid && ask) {
|
||||
spread = ask - bid
|
||||
spreadPercentage = (spread / ask) * 100
|
||||
}
|
||||
|
||||
setOrderbookData({
|
||||
bids: bidsToDisplay,
|
||||
asks: isMobile ? asksToDisplay.reverse() : asksToDisplay,
|
||||
spread,
|
||||
spreadPercentage,
|
||||
})
|
||||
} else {
|
||||
setOrderbookData(null)
|
||||
}
|
||||
}
|
||||
}, 400)
|
||||
|
||||
useEffect(
|
||||
() =>
|
||||
useMangoStore.subscribe(
|
||||
(state) =>
|
||||
(nextOrderbookData.current = {
|
||||
bids: state.selectedMarket.orderBook?.bids,
|
||||
asks: state.selectedMarket.orderBook?.asks,
|
||||
})
|
||||
),
|
||||
[]
|
||||
)
|
||||
|
||||
const handleLayoutChange = () => {
|
||||
setDefaultLayout(!defaultLayout)
|
||||
setOrderbookData((prevState) => ({
|
||||
...orderbookData,
|
||||
asks: prevState.asks.reverse(),
|
||||
}))
|
||||
}
|
||||
|
||||
const onGroupSizeChange = (groupSize) => {
|
||||
setGrouping(groupSize)
|
||||
}
|
||||
|
||||
return !isMobile ? (
|
||||
<FlipCard>
|
||||
<FlipCardInner flip={defaultLayout}>
|
||||
{defaultLayout ? (
|
||||
<FlipCardFront>
|
||||
<FloatingElement className="fadein-floating-element h-full">
|
||||
<div className="flex items-center justify-between pb-2.5">
|
||||
<div className="relative flex">
|
||||
<Tooltip
|
||||
content={
|
||||
displayCumulativeSize
|
||||
? t('tooltip-display-step')
|
||||
: t('tooltip-display-cumulative')
|
||||
}
|
||||
className="py-1 text-xs"
|
||||
>
|
||||
<button
|
||||
onClick={() => {
|
||||
setDisplayCumulativeSize(!displayCumulativeSize)
|
||||
}}
|
||||
className="flex h-7 w-7 items-center justify-center rounded-full bg-th-bkg-4 focus:outline-none md:hover:text-th-primary"
|
||||
>
|
||||
{displayCumulativeSize ? (
|
||||
<StepSizeIcon className="h-4 w-4" />
|
||||
) : (
|
||||
<CumulativeSizeIcon className="h-4 w-4" />
|
||||
)}
|
||||
</button>
|
||||
</Tooltip>
|
||||
</div>
|
||||
<ElementTitle noMarginBottom>{t('orderbook')}</ElementTitle>
|
||||
<div className="relative flex">
|
||||
<Tooltip
|
||||
content={t('tooltip-switch-layout')}
|
||||
className="py-1 text-xs"
|
||||
>
|
||||
<button
|
||||
onClick={handleLayoutChange}
|
||||
className="flex h-7 w-7 items-center justify-center rounded-full bg-th-bkg-4 focus:outline-none md:hover:text-th-primary"
|
||||
>
|
||||
<SwitchHorizontalIcon className="h-4 w-4" />
|
||||
</button>
|
||||
</Tooltip>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mb-3 flex items-center justify-end">
|
||||
<MarkPriceComponent markPrice={markPrice} />
|
||||
<GroupSize
|
||||
tickSize={market?.tickSize}
|
||||
onChange={onGroupSizeChange}
|
||||
value={grouping}
|
||||
className="relative flex w-1/3 flex-col items-end"
|
||||
/>
|
||||
</div>
|
||||
<div
|
||||
className={`mb-2 flex justify-between text-xs text-th-fgd-4`}
|
||||
>
|
||||
<div className={`text-left`}>
|
||||
{displayCumulativeSize ? 'Cumulative ' : ''}
|
||||
{t('size')} ({marketConfig.baseSymbol})
|
||||
</div>
|
||||
<div className={`text-center`}>
|
||||
{`${t('price')} (${groupConfig.quoteSymbol})`}
|
||||
</div>
|
||||
<div className={`text-right`}>
|
||||
{displayCumulativeSize ? 'Cumulative ' : ''}
|
||||
{t('size')} ({marketConfig.baseSymbol})
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex">
|
||||
<div className="w-1/2">
|
||||
{orderbookData?.bids.map(
|
||||
({
|
||||
price,
|
||||
size,
|
||||
cumulativeSize,
|
||||
sizePercent,
|
||||
maxSizePercent,
|
||||
}) => (
|
||||
<OrderbookRow
|
||||
market={market}
|
||||
hasOpenOrder={hasOpenOrderForPriceGroup(
|
||||
openOrderPrices,
|
||||
price,
|
||||
grouping
|
||||
)}
|
||||
key={price + ''}
|
||||
price={price}
|
||||
size={displayCumulativeSize ? cumulativeSize : size}
|
||||
side="buy"
|
||||
sizePercent={
|
||||
displayCumulativeSize ? maxSizePercent : sizePercent
|
||||
}
|
||||
grouping={grouping}
|
||||
/>
|
||||
)
|
||||
)}
|
||||
</div>
|
||||
<div className="w-1/2">
|
||||
{orderbookData?.asks.map(
|
||||
({
|
||||
price,
|
||||
size,
|
||||
cumulativeSize,
|
||||
sizePercent,
|
||||
maxSizePercent,
|
||||
}) => (
|
||||
<OrderbookRow
|
||||
market={market}
|
||||
hasOpenOrder={hasOpenOrderForPriceGroup(
|
||||
openOrderPrices,
|
||||
price,
|
||||
grouping
|
||||
)}
|
||||
invert
|
||||
key={price + ''}
|
||||
price={price}
|
||||
size={displayCumulativeSize ? cumulativeSize : size}
|
||||
side="sell"
|
||||
sizePercent={
|
||||
displayCumulativeSize ? maxSizePercent : sizePercent
|
||||
}
|
||||
grouping={grouping}
|
||||
/>
|
||||
)
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<OrderbookSpread orderbookData={orderbookData} />
|
||||
</FloatingElement>
|
||||
</FlipCardFront>
|
||||
) : (
|
||||
<FlipCardBack>
|
||||
<FloatingElement className="fadein-floating-element h-full">
|
||||
<div className="flex items-center justify-between pb-2.5">
|
||||
<div className="relative flex">
|
||||
<Tooltip
|
||||
content={
|
||||
displayCumulativeSize
|
||||
? t('tooltip-display-step')
|
||||
: t('tooltip-display-cumulative')
|
||||
}
|
||||
className="py-1 text-xs"
|
||||
>
|
||||
<button
|
||||
onClick={() => {
|
||||
setDisplayCumulativeSize(!displayCumulativeSize)
|
||||
}}
|
||||
className="flex h-8 w-8 items-center justify-center rounded-full bg-th-bkg-3 focus:outline-none md:hover:text-th-primary"
|
||||
>
|
||||
{displayCumulativeSize ? (
|
||||
<StepSizeIcon className="h-5 w-5" />
|
||||
) : (
|
||||
<CumulativeSizeIcon className="h-5 w-5" />
|
||||
)}
|
||||
</button>
|
||||
</Tooltip>
|
||||
</div>
|
||||
<ElementTitle noMarginBottom>{t('orderbook')}</ElementTitle>
|
||||
<div className="relative flex">
|
||||
<Tooltip
|
||||
content={t('tooltip-switch-layout')}
|
||||
className="py-1 text-xs"
|
||||
>
|
||||
<button
|
||||
onClick={handleLayoutChange}
|
||||
className="flex h-8 w-8 items-center justify-center rounded-full bg-th-bkg-3 focus:outline-none md:hover:text-th-primary"
|
||||
>
|
||||
<SwitchHorizontalIcon className="h-5 w-5" />
|
||||
</button>
|
||||
</Tooltip>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-row items-center justify-end pb-2">
|
||||
<MarkPriceComponent markPrice={markPrice} />
|
||||
<GroupSize
|
||||
tickSize={market?.tickSize}
|
||||
onChange={onGroupSizeChange}
|
||||
value={grouping}
|
||||
className="relative flex w-1/3 flex-col items-end"
|
||||
/>
|
||||
</div>
|
||||
<div className={`mb-2 flex justify-between text-th-fgd-4`}>
|
||||
<div className={`text-left text-xs`}>
|
||||
{displayCumulativeSize ? 'Cumulative ' : ''}
|
||||
{t('size')} ({marketConfig.baseSymbol})
|
||||
</div>
|
||||
<div className={`text-right text-xs`}>
|
||||
{`${t('price')} (${groupConfig.quoteSymbol})`}
|
||||
</div>
|
||||
</div>
|
||||
{orderbookData?.asks.map(
|
||||
({
|
||||
price,
|
||||
size,
|
||||
cumulativeSize,
|
||||
sizePercent,
|
||||
maxSizePercent,
|
||||
}) => (
|
||||
<OrderbookRow
|
||||
market={market}
|
||||
hasOpenOrder={hasOpenOrderForPriceGroup(
|
||||
openOrderPrices,
|
||||
price,
|
||||
grouping
|
||||
)}
|
||||
key={price + ''}
|
||||
price={price}
|
||||
size={displayCumulativeSize ? cumulativeSize : size}
|
||||
side="sell"
|
||||
sizePercent={
|
||||
displayCumulativeSize ? maxSizePercent : sizePercent
|
||||
}
|
||||
grouping={grouping}
|
||||
/>
|
||||
)
|
||||
)}
|
||||
<div className="my-2 flex justify-between rounded-md bg-th-bkg-2 p-2 text-xs">
|
||||
<div className="text-th-fgd-3">{t('spread')}</div>
|
||||
<div className="text-th-fgd-1">
|
||||
{orderbookData?.spread.toFixed(2)}
|
||||
</div>
|
||||
<div className="text-th-fgd-1">
|
||||
{orderbookData?.spreadPercentage.toFixed(2)}%
|
||||
</div>
|
||||
</div>
|
||||
{orderbookData?.bids.map(
|
||||
({
|
||||
price,
|
||||
size,
|
||||
cumulativeSize,
|
||||
sizePercent,
|
||||
maxSizePercent,
|
||||
}) => (
|
||||
<OrderbookRow
|
||||
market={market}
|
||||
hasOpenOrder={hasOpenOrderForPriceGroup(
|
||||
openOrderPrices,
|
||||
price,
|
||||
grouping
|
||||
)}
|
||||
key={price + ''}
|
||||
price={price}
|
||||
size={displayCumulativeSize ? cumulativeSize : size}
|
||||
side="buy"
|
||||
sizePercent={
|
||||
displayCumulativeSize ? maxSizePercent : sizePercent
|
||||
}
|
||||
grouping={grouping}
|
||||
/>
|
||||
)
|
||||
)}
|
||||
</FloatingElement>
|
||||
</FlipCardBack>
|
||||
)}
|
||||
</FlipCardInner>
|
||||
</FlipCard>
|
||||
) : (
|
||||
<div>
|
||||
<div className="flex items-center justify-between pb-2.5">
|
||||
<div className="relative flex">
|
||||
<button
|
||||
onClick={() => {
|
||||
setDisplayCumulativeSize(!displayCumulativeSize)
|
||||
}}
|
||||
className="flex h-8 w-8 items-center justify-center rounded-full bg-th-bkg-3 focus:outline-none md:hover:text-th-primary"
|
||||
>
|
||||
{displayCumulativeSize ? (
|
||||
<StepSizeIcon className="h-5 w-5" />
|
||||
) : (
|
||||
<CumulativeSizeIcon className="h-5 w-5" />
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<GroupSize
|
||||
tickSize={market?.tickSize}
|
||||
onChange={onGroupSizeChange}
|
||||
value={grouping}
|
||||
className="relative mb-1 flex w-1/3 flex-col items-end"
|
||||
/>
|
||||
</div>
|
||||
<div className={`flex justify-between text-th-fgd-4`}>
|
||||
<div className={`text-left text-xs`}>
|
||||
{displayCumulativeSize ? 'Cumulative ' : ''}
|
||||
{t('size')}
|
||||
</div>
|
||||
<div className={`text-right text-xs`}>{t('price')}</div>
|
||||
</div>
|
||||
{orderbookData?.asks.map(
|
||||
({ price, size, cumulativeSize, sizePercent, maxSizePercent }) => (
|
||||
<OrderbookRow
|
||||
market={market}
|
||||
hasOpenOrder={hasOpenOrderForPriceGroup(
|
||||
openOrderPrices,
|
||||
price,
|
||||
grouping
|
||||
)}
|
||||
key={price + ''}
|
||||
price={price}
|
||||
size={displayCumulativeSize ? cumulativeSize : size}
|
||||
side="sell"
|
||||
sizePercent={displayCumulativeSize ? maxSizePercent : sizePercent}
|
||||
grouping={grouping}
|
||||
/>
|
||||
)
|
||||
)}
|
||||
<OrderbookSpread orderbookData={orderbookData} />
|
||||
{orderbookData?.bids.map(
|
||||
({ price, size, cumulativeSize, sizePercent, maxSizePercent }) => (
|
||||
<OrderbookRow
|
||||
market={market}
|
||||
hasOpenOrder={hasOpenOrderForPriceGroup(
|
||||
openOrderPrices,
|
||||
price,
|
||||
grouping
|
||||
)}
|
||||
key={price + ''}
|
||||
price={price}
|
||||
size={displayCumulativeSize ? cumulativeSize : size}
|
||||
side="buy"
|
||||
sizePercent={displayCumulativeSize ? maxSizePercent : sizePercent}
|
||||
grouping={grouping}
|
||||
/>
|
||||
)
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const OrderbookSpread = ({ orderbookData }) => {
|
||||
const { t } = useTranslation('common')
|
||||
const selectedMarket = useMangoStore((s) => s.selectedMarket.current)
|
||||
const decimals = useMemo(() => {
|
||||
if (selectedMarket) {
|
||||
return getPrecisionDigits(selectedMarket?.tickSize)
|
||||
}
|
||||
return 2
|
||||
}, [selectedMarket])
|
||||
|
||||
return (
|
||||
<div className="my-2 flex justify-between rounded-md bg-th-bkg-2 p-2 text-xs">
|
||||
<div className="hidden text-th-fgd-3 sm:block">{t('spread')}</div>
|
||||
<div className="text-th-fgd-1">
|
||||
{orderbookData?.spread.toFixed(decimals)}
|
||||
</div>
|
||||
<div className="text-th-fgd-1">
|
||||
{orderbookData?.spreadPercentage.toFixed(2)}%
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const OrderbookRow = React.memo<any>(
|
||||
({
|
||||
side,
|
||||
price,
|
||||
size,
|
||||
sizePercent,
|
||||
invert,
|
||||
hasOpenOrder,
|
||||
market,
|
||||
grouping,
|
||||
}: {
|
||||
side: 'buy' | 'sell'
|
||||
price: number
|
||||
size: number
|
||||
sizePercent: number
|
||||
hasOpenOrder: boolean
|
||||
invert: boolean
|
||||
grouping: number
|
||||
market: Market | PerpMarket
|
||||
}) => {
|
||||
const element = useRef<HTMLDivElement>(null)
|
||||
const setMangoStore = useMangoStore(setStoreSelector)
|
||||
const [showOrderbookFlash] = useLocalStorageState(ORDERBOOK_FLASH_KEY, true)
|
||||
const flashClassName = side === 'sell' ? 'red-flash' : 'green-flash'
|
||||
|
||||
useEffect(() => {
|
||||
showOrderbookFlash &&
|
||||
!element.current?.classList.contains(flashClassName) &&
|
||||
element.current?.classList.add(flashClassName)
|
||||
const id = setTimeout(
|
||||
() =>
|
||||
element.current?.classList.contains(flashClassName) &&
|
||||
element.current?.classList.remove(flashClassName),
|
||||
250
|
||||
)
|
||||
return () => clearTimeout(id)
|
||||
}, [price, size])
|
||||
|
||||
const formattedSize =
|
||||
market?.minOrderSize && !isNaN(size)
|
||||
? Number(size).toFixed(getDecimalCount(market.minOrderSize))
|
||||
: size
|
||||
|
||||
const formattedPrice =
|
||||
market?.tickSize && !isNaN(price)
|
||||
? Number(price).toFixed(getDecimalCount(market.tickSize))
|
||||
: price
|
||||
|
||||
const handlePriceClick = () => {
|
||||
setMangoStore((state) => {
|
||||
state.tradeForm.price = Number(formattedPrice)
|
||||
})
|
||||
}
|
||||
|
||||
const handleSizeClick = () => {
|
||||
setMangoStore((state) => {
|
||||
state.tradeForm.baseSize = Number(formattedSize)
|
||||
})
|
||||
}
|
||||
|
||||
if (!market) return null
|
||||
|
||||
const groupingDecimalCount = getDecimalCount(grouping)
|
||||
const minOrderSizeDecimals = getDecimalCount(market.minOrderSize)
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`relative flex cursor-pointer justify-between text-sm leading-6`}
|
||||
ref={element}
|
||||
>
|
||||
{invert ? (
|
||||
<>
|
||||
<Line
|
||||
invert
|
||||
data-width={sizePercent + '%'}
|
||||
className={`absolute left-0 ${
|
||||
side === 'buy' ? `bg-th-green-muted` : `bg-th-red-muted`
|
||||
}`}
|
||||
/>
|
||||
<div className="flex w-full items-center justify-between hover:font-semibold">
|
||||
<div
|
||||
onClick={handlePriceClick}
|
||||
className={`z-10 text-xs leading-5 md:pl-5 md:leading-6 ${
|
||||
side === 'buy'
|
||||
? `text-th-green`
|
||||
: `text-th-red brightness-125`
|
||||
}`}
|
||||
>
|
||||
{usdFormatter(formattedPrice, groupingDecimalCount, false)}
|
||||
</div>
|
||||
|
||||
<div
|
||||
className={`z-10 text-xs ${
|
||||
hasOpenOrder ? 'text-th-primary' : 'text-th-fgd-2'
|
||||
}`}
|
||||
onClick={handleSizeClick}
|
||||
>
|
||||
{usdFormatter(formattedSize, minOrderSizeDecimals, false)}
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<div className="flex w-full items-center justify-between hover:font-semibold">
|
||||
<div
|
||||
className={`z-10 text-xs leading-5 md:leading-6 ${
|
||||
hasOpenOrder ? 'text-th-primary' : 'text-th-fgd-2'
|
||||
}`}
|
||||
onClick={handleSizeClick}
|
||||
>
|
||||
{usdFormatter(formattedSize, minOrderSizeDecimals, false)}
|
||||
</div>
|
||||
<div
|
||||
className={`z-10 text-xs leading-5 md:pr-4 md:leading-6 ${
|
||||
side === 'buy'
|
||||
? `text-th-green`
|
||||
: `text-th-red brightness-125`
|
||||
}`}
|
||||
onClick={handlePriceClick}
|
||||
>
|
||||
{usdFormatter(formattedPrice, groupingDecimalCount, false)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Line
|
||||
className={`absolute right-0 ${
|
||||
side === 'buy' ? `bg-th-green-muted` : `bg-th-red-muted`
|
||||
}`}
|
||||
data-width={sizePercent + '%'}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
},
|
||||
(prevProps, nextProps) =>
|
||||
isEqual(prevProps, nextProps, [
|
||||
'price',
|
||||
'size',
|
||||
'sizePercent',
|
||||
'hasOpenOrder',
|
||||
])
|
||||
)
|
||||
|
||||
const MarkPriceComponent = React.memo<{ markPrice: number }>(
|
||||
({ markPrice }) => {
|
||||
const previousMarkPrice: number = usePrevious(markPrice)
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`flex items-center justify-center font-bold md:w-1/3 md:text-base ${
|
||||
markPrice > previousMarkPrice
|
||||
? `text-th-green`
|
||||
: markPrice < previousMarkPrice
|
||||
? `text-th-red`
|
||||
: `text-th-fgd-1`
|
||||
}`}
|
||||
>
|
||||
{markPrice > previousMarkPrice && (
|
||||
<ArrowUpIcon className={`mr-1 h-4 w-4 text-th-green`} />
|
||||
)}
|
||||
{markPrice < previousMarkPrice && (
|
||||
<ArrowDownIcon className={`mr-1 h-4 w-4 text-th-red`} />
|
||||
)}
|
||||
{markPrice || '----'}
|
||||
</div>
|
||||
)
|
||||
},
|
||||
(prevProps, nextProps) => isEqual(prevProps, nextProps, ['markPrice'])
|
||||
)
|
|
@ -0,0 +1,11 @@
|
|||
import { PageBodyWrapper } from './styles'
|
||||
|
||||
const PageBodyContainer = ({ children }) => (
|
||||
<PageBodyWrapper className="grid grid-cols-12 gap-4 pb-14">
|
||||
<div className="col-span-12 px-4 lg:px-10 xl:col-span-12 2xl:col-span-10 2xl:col-start-2">
|
||||
{children}
|
||||
</div>
|
||||
</PageBodyWrapper>
|
||||
)
|
||||
|
||||
export default PageBodyContainer
|
|
@ -0,0 +1,71 @@
|
|||
import {
|
||||
ChevronDoubleLeftIcon,
|
||||
ChevronDoubleRightIcon,
|
||||
ChevronLeftIcon,
|
||||
ChevronRightIcon,
|
||||
} from '@heroicons/react/solid'
|
||||
|
||||
export default function Pagination({
|
||||
page,
|
||||
totalPages,
|
||||
firstPage,
|
||||
lastPage,
|
||||
nextPage,
|
||||
previousPage,
|
||||
}) {
|
||||
return (
|
||||
<div className="mt-4 flex items-center justify-end">
|
||||
<div className="flex justify-center">
|
||||
<button
|
||||
onClick={firstPage}
|
||||
disabled={page === 1}
|
||||
className={`bg-th-bkg-4 px-1 py-1 ${
|
||||
page !== 1
|
||||
? 'md:hover:cursor-pointer md:hover:text-th-primary'
|
||||
: 'md:hover:cursor-not-allowed'
|
||||
} disabled:text-th-fgd-4`}
|
||||
>
|
||||
<ChevronDoubleLeftIcon className={`h-5 w-5`} />
|
||||
</button>
|
||||
<button
|
||||
onClick={previousPage}
|
||||
disabled={page === 1}
|
||||
className={`ml-2 bg-th-bkg-4 px-1 py-1 ${
|
||||
page !== 1
|
||||
? 'md:hover:cursor-pointer md:hover:text-th-primary'
|
||||
: 'md:hover:cursor-not-allowed'
|
||||
} disabled:text-th-fgd-4`}
|
||||
>
|
||||
<ChevronLeftIcon className={`h-5 w-5`} />
|
||||
</button>
|
||||
</div>
|
||||
<div className="ml-2">
|
||||
{page} / {totalPages}
|
||||
</div>
|
||||
<div className="flex justify-center">
|
||||
<button
|
||||
onClick={nextPage}
|
||||
disabled={page === totalPages}
|
||||
className={`ml-2 bg-th-bkg-4 px-1 py-1 ${
|
||||
page !== totalPages
|
||||
? 'md:hover:cursor-pointer md:hover:text-th-primary'
|
||||
: 'md:hover:cursor-not-allowed'
|
||||
} disabled:text-th-fgd-4`}
|
||||
>
|
||||
<ChevronRightIcon className={`h-5 w-5`} />
|
||||
</button>
|
||||
<button
|
||||
onClick={lastPage}
|
||||
disabled={page === totalPages}
|
||||
className={`ml-2 bg-th-bkg-4 px-1 py-1 ${
|
||||
page !== totalPages
|
||||
? 'md:hover:cursor-pointer md:hover:text-th-primary'
|
||||
: 'md:hover:cursor-not-allowed'
|
||||
} disabled:text-th-fgd-4`}
|
||||
>
|
||||
<ChevronDoubleRightIcon className={`h-5 w-5`} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
|
@ -0,0 +1,632 @@
|
|||
import React, { useCallback, useEffect, useMemo, useState } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
import Link from 'next/link'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import {
|
||||
ExclamationIcon,
|
||||
InformationCircleIcon,
|
||||
PencilIcon,
|
||||
} from '@heroicons/react/solid'
|
||||
import { ZERO_I80F48 } from '@blockworks-foundation/mango-client'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import Button, { LinkButton } from '../components/Button'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from './TradePageGrid'
|
||||
import { ExpandableRow, Table, Td, Th, TrBody, TrHead } from './TableElements'
|
||||
import {
|
||||
formatUsdValue,
|
||||
getPrecisionDigits,
|
||||
roundPerpSize,
|
||||
usdFormatter,
|
||||
} from '../utils'
|
||||
import Loading from './Loading'
|
||||
import MarketCloseModal from './MarketCloseModal'
|
||||
import PerpSideBadge from './PerpSideBadge'
|
||||
import PnlText from './PnlText'
|
||||
import { settlePnl } from './MarketPosition'
|
||||
import MobileTableHeader from './mobile/MobileTableHeader'
|
||||
import ShareModal from './ShareModal'
|
||||
import { TwitterIcon } from './icons'
|
||||
import { marketSelector } from '../stores/selectors'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
import RedeemButtons from './RedeemButtons'
|
||||
import Tooltip from './Tooltip'
|
||||
import useMangoAccount from 'hooks/useMangoAccount'
|
||||
import useLocalStorageState from 'hooks/useLocalStorageState'
|
||||
import EditTableColumnsModal from './EditTableColumnsModal'
|
||||
|
||||
const TABLE_COLUMNS = {
|
||||
market: true,
|
||||
side: true,
|
||||
'position-size': true,
|
||||
'notional-size': true,
|
||||
'average-entry': true,
|
||||
'break-even': true,
|
||||
'estimated-liq-price': true,
|
||||
'unrealized-pnl': true,
|
||||
'unsettled-balance': true,
|
||||
}
|
||||
|
||||
const PERP_TABLE_COLUMNS_KEY = 'perpPositionTableColumns'
|
||||
|
||||
const PositionsTable: React.FC = () => {
|
||||
const { t } = useTranslation('common')
|
||||
const [showShareModal, setShowShareModal] = useState(false)
|
||||
const [showMarketCloseModal, setShowMarketCloseModal] = useState(false)
|
||||
const [positionToClose, setPositionToClose] = useState<any>(null)
|
||||
const [positionToShare, setPositionToShare] = useState<any>(null)
|
||||
const [settleSinglePos, setSettleSinglePos] = useState(null)
|
||||
const market = useMangoStore(marketSelector)
|
||||
const { wallet } = useWallet()
|
||||
const price = useMangoStore((s) => s.tradeForm.price)
|
||||
const setMangoStore = useMangoStore((s) => s.set)
|
||||
const openPositions = useMangoStore(
|
||||
(s) => s.selectedMangoAccount.openPerpPositions
|
||||
)
|
||||
const unsettledPositions =
|
||||
useMangoStore.getState().selectedMangoAccount.unsettledPerpPositions
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
|
||||
const { mangoAccount } = useMangoAccount()
|
||||
const { width } = useViewport()
|
||||
const isMobile = width ? width < breakpoints.md : false
|
||||
const { asPath } = useRouter()
|
||||
const [tableColumnsToShow] = useLocalStorageState(
|
||||
PERP_TABLE_COLUMNS_KEY,
|
||||
TABLE_COLUMNS
|
||||
)
|
||||
const [showEditTableColumns, setShowEditTableColumns] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
if (positionToShare) {
|
||||
const updatedPosition = openPositions.find(
|
||||
(p) => p.marketConfig === positionToShare.marketConfig
|
||||
)
|
||||
setPositionToShare(updatedPosition)
|
||||
}
|
||||
}, [openPositions])
|
||||
|
||||
useEffect(() => {
|
||||
if (positionToClose) {
|
||||
const updatedPosition = openPositions.find(
|
||||
(p) => p.marketConfig === positionToClose.marketConfig
|
||||
)
|
||||
if (updatedPosition) {
|
||||
setPositionToClose(updatedPosition)
|
||||
}
|
||||
}
|
||||
}, [openPositions])
|
||||
|
||||
const handleCloseWarning = useCallback(() => {
|
||||
setShowMarketCloseModal(false)
|
||||
setPositionToClose(null)
|
||||
}, [])
|
||||
|
||||
const handleSizeClick = (size, indexPrice) => {
|
||||
const sizePrecisionDigits = getPrecisionDigits(market!.minOrderSize)
|
||||
const priceOrDefault = price ? price : indexPrice
|
||||
const roundedSize = parseFloat(Math.abs(size).toFixed(sizePrecisionDigits))
|
||||
const quoteSize = parseFloat((roundedSize * priceOrDefault).toFixed(2))
|
||||
setMangoStore((state) => {
|
||||
state.tradeForm.baseSize = roundedSize
|
||||
state.tradeForm.quoteSize = quoteSize
|
||||
state.tradeForm.side = size > 0 ? 'sell' : 'buy'
|
||||
})
|
||||
}
|
||||
|
||||
const handleCloseShare = useCallback(() => {
|
||||
setShowShareModal(false)
|
||||
setPositionToShare(null)
|
||||
}, [])
|
||||
|
||||
const handleShowShare = (position) => {
|
||||
setPositionToShare(position)
|
||||
setShowShareModal(true)
|
||||
}
|
||||
|
||||
const handleShowMarketCloseModal = (position) => {
|
||||
setPositionToClose(position)
|
||||
setShowMarketCloseModal(true)
|
||||
}
|
||||
|
||||
const handleSettlePnl = async (perpMarket, perpAccount, index) => {
|
||||
if (wallet) {
|
||||
setSettleSinglePos(index)
|
||||
await settlePnl(perpMarket, perpAccount, t, undefined, wallet)
|
||||
setSettleSinglePos(null)
|
||||
}
|
||||
}
|
||||
|
||||
const unsettledSum = useMemo(() => {
|
||||
if (unsettledPositions.length > 1) {
|
||||
return unsettledPositions.reduce((a, c) => a + c.unsettledPnl, 0)
|
||||
}
|
||||
return
|
||||
}, [unsettledPositions])
|
||||
|
||||
return (
|
||||
<div className="flex flex-col">
|
||||
{unsettledPositions.length > 0 ? (
|
||||
<div className="mb-6 rounded-lg border border-th-bkg-3 p-4 sm:p-6">
|
||||
<div className="flex items-start justify-between pb-4">
|
||||
<div className="flex items-center">
|
||||
<ExclamationIcon className="mr-2 h-6 w-6 flex-shrink-0 text-th-primary" />
|
||||
<h3>
|
||||
{t('unsettled-positions')}{' '}
|
||||
{unsettledSum ? (
|
||||
<div
|
||||
className={
|
||||
unsettledSum >= 0 ? 'text-th-green' : 'text-th-red'
|
||||
}
|
||||
>
|
||||
{formatUsdValue(unsettledSum)}
|
||||
</div>
|
||||
) : null}
|
||||
</h3>
|
||||
</div>
|
||||
|
||||
{unsettledPositions.length > 1 ? <RedeemButtons /> : null}
|
||||
</div>
|
||||
<div className="grid grid-flow-row grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
|
||||
{unsettledPositions.map((p, index) => {
|
||||
return (
|
||||
<div
|
||||
className="col-span-1 flex items-center justify-between rounded-full bg-th-bkg-2 px-5 py-3"
|
||||
key={p.marketConfig.baseSymbol}
|
||||
>
|
||||
<div className="flex space-x-2">
|
||||
<div className="flex items-center">
|
||||
<img
|
||||
alt=""
|
||||
width="20"
|
||||
height="20"
|
||||
src={`/assets/icons/${p.marketConfig.baseSymbol.toLowerCase()}.svg`}
|
||||
className={`mr-3`}
|
||||
/>
|
||||
<div>
|
||||
<p className="mb-0 text-xs text-th-fgd-1">
|
||||
{p.marketConfig.name}
|
||||
</p>
|
||||
<PnlText className="font-bold" pnl={p.unsettledPnl} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{settleSinglePos === index ? (
|
||||
<Loading />
|
||||
) : (
|
||||
<LinkButton
|
||||
className="text-xs"
|
||||
onClick={() =>
|
||||
handleSettlePnl(p.perpMarket, p.perpAccount, index)
|
||||
}
|
||||
>
|
||||
{t('redeem-pnl')}
|
||||
</LinkButton>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
<div className={`md:overflow-x-auto`}>
|
||||
<div className={`inline-block min-w-full align-middle`}>
|
||||
{openPositions.length ? (
|
||||
!isMobile ? (
|
||||
<Table>
|
||||
<thead>
|
||||
<TrHead>
|
||||
{Object.entries(tableColumnsToShow).map((entry) =>
|
||||
entry[1] ? (
|
||||
<Th key={entry[0]}>
|
||||
{entry[0] === 'estimated-liq-price' ? (
|
||||
<Tooltip content={t('tooltip-estimated-liq-price')}>
|
||||
<span className="flex items-center">
|
||||
{t('estimated-liq-price')}
|
||||
<InformationCircleIcon className="ml-1 h-4 w-4 flex-shrink-0 cursor-help text-th-fgd-4" />
|
||||
</span>
|
||||
</Tooltip>
|
||||
) : (
|
||||
t(entry[0])
|
||||
)}
|
||||
</Th>
|
||||
) : null
|
||||
)}
|
||||
<LinkButton
|
||||
className="flex w-full items-start justify-end"
|
||||
onClick={() => setShowEditTableColumns(true)}
|
||||
>
|
||||
<PencilIcon className="mr-1 h-3.5 w-3.5 flex-shrink-0" />
|
||||
{t('edit-columns')}
|
||||
</LinkButton>
|
||||
</TrHead>
|
||||
</thead>
|
||||
<tbody>
|
||||
{openPositions.map(
|
||||
(
|
||||
{
|
||||
marketConfig,
|
||||
perpMarket,
|
||||
perpAccount,
|
||||
basePosition,
|
||||
notionalSize,
|
||||
indexPrice,
|
||||
avgEntryPrice,
|
||||
breakEvenPrice,
|
||||
unrealizedPnl,
|
||||
unsettledPnl,
|
||||
},
|
||||
index
|
||||
) => {
|
||||
const basePositionUi = roundPerpSize(
|
||||
basePosition,
|
||||
marketConfig.baseSymbol
|
||||
)
|
||||
const liquidationPrice =
|
||||
mangoGroup &&
|
||||
mangoAccount &&
|
||||
marketConfig &&
|
||||
mangoGroup &&
|
||||
mangoCache
|
||||
? mangoAccount.getLiquidationPrice(
|
||||
mangoGroup,
|
||||
mangoCache,
|
||||
marketConfig.marketIndex
|
||||
)
|
||||
: undefined
|
||||
return (
|
||||
<TrBody key={`${marketConfig.marketIndex}`}>
|
||||
{tableColumnsToShow['market'] ? (
|
||||
<Td>
|
||||
<div className="flex items-center">
|
||||
<img
|
||||
alt=""
|
||||
width="20"
|
||||
height="20"
|
||||
src={`/assets/icons/${marketConfig.baseSymbol.toLowerCase()}.svg`}
|
||||
className={`mr-2.5`}
|
||||
/>
|
||||
{decodeURIComponent(asPath).includes(
|
||||
marketConfig.name
|
||||
) ? (
|
||||
<span>{marketConfig.name}</span>
|
||||
) : (
|
||||
<Link
|
||||
href={{
|
||||
pathname: '/',
|
||||
query: { name: marketConfig.name },
|
||||
}}
|
||||
shallow={true}
|
||||
>
|
||||
<a className="text-th-fgd-1 underline hover:text-th-fgd-1 hover:no-underline">
|
||||
{marketConfig.name}
|
||||
</a>
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
</Td>
|
||||
) : null}
|
||||
{tableColumnsToShow['side'] ? (
|
||||
<Td>
|
||||
<PerpSideBadge basePosition={basePosition} />
|
||||
</Td>
|
||||
) : null}
|
||||
{tableColumnsToShow['position-size'] ? (
|
||||
<Td>
|
||||
{basePosition &&
|
||||
asPath.includes(marketConfig.baseSymbol) ? (
|
||||
<span
|
||||
className="cursor-pointer underline hover:no-underline"
|
||||
onClick={() =>
|
||||
handleSizeClick(basePosition, indexPrice)
|
||||
}
|
||||
>
|
||||
{basePositionUi}
|
||||
</span>
|
||||
) : (
|
||||
<span>{basePositionUi}</span>
|
||||
)}
|
||||
</Td>
|
||||
) : null}
|
||||
{tableColumnsToShow['notional-size'] ? (
|
||||
<Td>{formatUsdValue(Math.abs(notionalSize))}</Td>
|
||||
) : null}
|
||||
{tableColumnsToShow['average-entry'] ? (
|
||||
<Td>
|
||||
{avgEntryPrice
|
||||
? formatUsdValue(avgEntryPrice)
|
||||
: '-'}
|
||||
</Td>
|
||||
) : null}
|
||||
{tableColumnsToShow['break-even'] ? (
|
||||
<Td>
|
||||
{breakEvenPrice
|
||||
? formatUsdValue(breakEvenPrice)
|
||||
: '-'}
|
||||
</Td>
|
||||
) : null}
|
||||
{tableColumnsToShow['estimated-liq-price'] ? (
|
||||
<Td>
|
||||
{liquidationPrice &&
|
||||
liquidationPrice.gt(ZERO_I80F48)
|
||||
? usdFormatter(liquidationPrice)
|
||||
: 'N/A'}
|
||||
</Td>
|
||||
) : null}
|
||||
{tableColumnsToShow['unrealized-pnl'] ? (
|
||||
<Td>
|
||||
{unrealizedPnl ? (
|
||||
<PnlText pnl={unrealizedPnl} />
|
||||
) : (
|
||||
'-'
|
||||
)}
|
||||
</Td>
|
||||
) : null}
|
||||
{tableColumnsToShow['unsettled-balance'] ? (
|
||||
<Td>
|
||||
{unsettledPnl ? (
|
||||
settleSinglePos === index ? (
|
||||
<Loading />
|
||||
) : (
|
||||
<Tooltip content={t('redeem-pnl')}>
|
||||
<LinkButton
|
||||
className={
|
||||
unsettledPnl >= 0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}
|
||||
onClick={() =>
|
||||
handleSettlePnl(
|
||||
perpMarket,
|
||||
perpAccount,
|
||||
index
|
||||
)
|
||||
}
|
||||
disabled={unsettledPnl === 0}
|
||||
>
|
||||
{formatUsdValue(unsettledPnl)}
|
||||
</LinkButton>
|
||||
</Tooltip>
|
||||
)
|
||||
) : (
|
||||
'--'
|
||||
)}
|
||||
</Td>
|
||||
) : null}
|
||||
<Td>
|
||||
<div className="flex items-center space-x-3">
|
||||
<Button
|
||||
className="h-8 pt-0 pb-0 pl-3 pr-3 text-xs"
|
||||
primary={false}
|
||||
onClick={() =>
|
||||
handleShowMarketCloseModal(
|
||||
openPositions[index]
|
||||
)
|
||||
}
|
||||
>
|
||||
{t('close')}
|
||||
</Button>
|
||||
<LinkButton
|
||||
onClick={() =>
|
||||
handleShowShare(openPositions[index])
|
||||
}
|
||||
disabled={!avgEntryPrice ? true : false}
|
||||
>
|
||||
<TwitterIcon className="h-4 w-4" />
|
||||
</LinkButton>
|
||||
</div>
|
||||
</Td>
|
||||
</TrBody>
|
||||
)
|
||||
}
|
||||
)}
|
||||
</tbody>
|
||||
</Table>
|
||||
) : (
|
||||
<div className="border-b border-th-bkg-4">
|
||||
<MobileTableHeader
|
||||
colOneHeader={t('market')}
|
||||
colTwoHeader={t('unrealized-pnl')}
|
||||
/>
|
||||
{openPositions.map(
|
||||
(
|
||||
{
|
||||
marketConfig,
|
||||
basePosition,
|
||||
notionalSize,
|
||||
avgEntryPrice,
|
||||
breakEvenPrice,
|
||||
perpAccount,
|
||||
perpMarket,
|
||||
unrealizedPnl,
|
||||
unsettledPnl,
|
||||
},
|
||||
index
|
||||
) => {
|
||||
const basePositionUi = roundPerpSize(
|
||||
basePosition,
|
||||
marketConfig.baseSymbol
|
||||
)
|
||||
const liquidationPrice =
|
||||
mangoGroup &&
|
||||
mangoAccount &&
|
||||
marketConfig &&
|
||||
mangoGroup &&
|
||||
mangoCache
|
||||
? mangoAccount.getLiquidationPrice(
|
||||
mangoGroup,
|
||||
mangoCache,
|
||||
marketConfig.marketIndex
|
||||
)
|
||||
: undefined
|
||||
return (
|
||||
<ExpandableRow
|
||||
buttonTemplate={
|
||||
<>
|
||||
<div className="text-fgd-1 flex w-full items-center justify-between">
|
||||
<div className="flex items-center">
|
||||
<img
|
||||
alt=""
|
||||
width="20"
|
||||
height="20"
|
||||
src={`/assets/icons/${marketConfig.baseSymbol.toLowerCase()}.svg`}
|
||||
className={`mr-2.5`}
|
||||
/>
|
||||
<div>
|
||||
<div className="mb-0.5 text-left">
|
||||
{marketConfig.name}
|
||||
</div>
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
<span
|
||||
className={`mr-1 ${
|
||||
basePosition > 0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{basePosition > 0
|
||||
? t('long').toUpperCase()
|
||||
: t('short').toUpperCase()}
|
||||
</span>
|
||||
{basePositionUi}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{breakEvenPrice ? (
|
||||
<PnlText pnl={unrealizedPnl} />
|
||||
) : (
|
||||
'--'
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
}
|
||||
key={`${index}`}
|
||||
panelTemplate={
|
||||
<div className="grid grid-flow-row grid-cols-2 gap-4">
|
||||
<div className="col-span-1 text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('average-entry')}
|
||||
</div>
|
||||
{avgEntryPrice
|
||||
? formatUsdValue(avgEntryPrice)
|
||||
: '--'}
|
||||
</div>
|
||||
<div className="col-span-1 text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('notional-size')}
|
||||
</div>
|
||||
{formatUsdValue(notionalSize)}
|
||||
</div>
|
||||
<div className="col-span-1 text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('break-even')}
|
||||
</div>
|
||||
{breakEvenPrice
|
||||
? formatUsdValue(breakEvenPrice)
|
||||
: '--'}
|
||||
</div>
|
||||
<div className="col-span-1 text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('unsettled-balance')}
|
||||
</div>
|
||||
{unsettledPnl ? (
|
||||
settleSinglePos === index ? (
|
||||
<Loading />
|
||||
) : (
|
||||
<Tooltip content={t('redeem-pnl')}>
|
||||
<LinkButton
|
||||
className={`font-bold ${
|
||||
unsettledPnl >= 0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}`}
|
||||
onClick={() =>
|
||||
handleSettlePnl(
|
||||
perpMarket,
|
||||
perpAccount,
|
||||
index
|
||||
)
|
||||
}
|
||||
disabled={unsettledPnl === 0}
|
||||
>
|
||||
{formatUsdValue(unsettledPnl)}
|
||||
</LinkButton>
|
||||
</Tooltip>
|
||||
)
|
||||
) : (
|
||||
'--'
|
||||
)}
|
||||
</div>
|
||||
<div className="col-span-1 text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
<Tooltip
|
||||
content={t('tooltip-estimated-liq-price')}
|
||||
>
|
||||
<span className="flex items-center">
|
||||
{t('estimated-liq-price')}
|
||||
<InformationCircleIcon className="ml-1 h-4 w-4 flex-shrink-0 text-th-fgd-4" />
|
||||
</span>
|
||||
</Tooltip>
|
||||
</div>
|
||||
{liquidationPrice &&
|
||||
liquidationPrice.gt(ZERO_I80F48)
|
||||
? usdFormatter(liquidationPrice)
|
||||
: 'N/A'}
|
||||
</div>
|
||||
<div className="col-span-2">
|
||||
<Button
|
||||
className="w-full"
|
||||
primary={false}
|
||||
onClick={() =>
|
||||
handleShowMarketCloseModal(
|
||||
openPositions[index]
|
||||
)
|
||||
}
|
||||
>
|
||||
{t('market-close')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
)
|
||||
}
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
) : (
|
||||
<div
|
||||
className={`w-full rounded-md border border-th-bkg-3 py-6 text-center text-th-fgd-3`}
|
||||
>
|
||||
{t('no-perp')}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{showShareModal ? (
|
||||
<ShareModal
|
||||
isOpen={showShareModal}
|
||||
onClose={handleCloseShare}
|
||||
position={positionToShare!}
|
||||
/>
|
||||
) : null}
|
||||
{showMarketCloseModal ? (
|
||||
<MarketCloseModal
|
||||
isOpen={showMarketCloseModal}
|
||||
onClose={handleCloseWarning}
|
||||
position={positionToClose!}
|
||||
/>
|
||||
) : null}
|
||||
{showEditTableColumns ? (
|
||||
<EditTableColumnsModal
|
||||
isOpen={showEditTableColumns}
|
||||
onClose={() => setShowEditTableColumns(false)}
|
||||
columns={tableColumnsToShow}
|
||||
storageKey={PERP_TABLE_COLUMNS_KEY}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default PositionsTable
|
|
@ -0,0 +1,13 @@
|
|||
import SideBadge from './SideBadge'
|
||||
|
||||
const PerpSideBadge = ({ basePosition }: { basePosition: number }) => (
|
||||
<>
|
||||
{basePosition !== 0 ? (
|
||||
<SideBadge side={basePosition > 0 ? 'long' : 'short'} />
|
||||
) : (
|
||||
'--'
|
||||
)}
|
||||
</>
|
||||
)
|
||||
|
||||
export default PerpSideBadge
|
|
@ -0,0 +1,19 @@
|
|||
import { formatUsdValue } from '../utils'
|
||||
|
||||
const PnlText = ({ className, pnl }: { className?: string; pnl?: number }) => (
|
||||
<>
|
||||
{pnl ? (
|
||||
<p
|
||||
className={`mb-0 ${className} text-xs ${
|
||||
pnl > 0 ? 'text-th-green' : 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{formatUsdValue(pnl)}
|
||||
</p>
|
||||
) : (
|
||||
'--'
|
||||
)}
|
||||
</>
|
||||
)
|
||||
|
||||
export default PnlText
|
|
@ -0,0 +1,41 @@
|
|||
import useMangoStore from 'stores/useMangoStore'
|
||||
import { ProfileIcon } from './icons'
|
||||
|
||||
const ProfileImage = ({
|
||||
imageSize,
|
||||
placeholderSize,
|
||||
imageUrl,
|
||||
isOwnerProfile,
|
||||
}: {
|
||||
imageSize: string
|
||||
placeholderSize: string
|
||||
imageUrl?: string
|
||||
isOwnerProfile?: boolean
|
||||
}) => {
|
||||
const profile = useMangoStore((s) => s.profile.details)
|
||||
|
||||
return imageUrl || (isOwnerProfile && profile.profile_image_url) ? (
|
||||
<img
|
||||
alt=""
|
||||
src={imageUrl ? imageUrl : profile.profile_image_url}
|
||||
className={`default-transition rounded-full`}
|
||||
style={{ width: `${imageSize}px`, height: `${imageSize}px` }}
|
||||
/>
|
||||
) : (
|
||||
<div
|
||||
className={`flex flex-shrink-0 items-center justify-center rounded-full bg-th-bkg-4`}
|
||||
style={{ width: `${imageSize}px`, height: `${imageSize}px` }}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
width: `${placeholderSize}px`,
|
||||
height: `${placeholderSize}px`,
|
||||
}}
|
||||
>
|
||||
<ProfileIcon className={`h-full w-full text-th-fgd-3`} />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ProfileImage
|
|
@ -0,0 +1,55 @@
|
|||
import { PencilIcon } from '@heroicons/react/outline'
|
||||
import { useCallback, useState } from 'react'
|
||||
import useMangoStore from 'stores/useMangoStore'
|
||||
import NftProfilePicModal from './NftProfilePicModal'
|
||||
import ProfileImage from './ProfileImage'
|
||||
|
||||
const ProfileImageButton = ({
|
||||
disabled,
|
||||
imageSize,
|
||||
placeholderSize,
|
||||
imageUrl,
|
||||
}: {
|
||||
disabled: boolean
|
||||
imageSize: string
|
||||
placeholderSize: string
|
||||
imageUrl?: string
|
||||
}) => {
|
||||
const [showProfilePicModal, setShowProfilePicModal] = useState(false)
|
||||
const profile = useMangoStore((s) => s.profile.details)
|
||||
|
||||
const handleCloseProfilePicModal = useCallback(() => {
|
||||
setShowProfilePicModal(false)
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
disabled={disabled}
|
||||
className="relative mb-2 mr-4 flex items-center justify-center rounded-full sm:mb-0"
|
||||
onClick={() => setShowProfilePicModal(true)}
|
||||
style={{ width: `${imageSize}px`, height: `${imageSize}px` }}
|
||||
>
|
||||
<ProfileImage
|
||||
imageSize={imageSize}
|
||||
placeholderSize={placeholderSize}
|
||||
imageUrl={(imageUrl !== profile.profile_image_url && imageUrl) || ''}
|
||||
isOwnerProfile={imageUrl === profile.profile_image_url}
|
||||
/>
|
||||
{!disabled ? (
|
||||
<div className="default-transition absolute bottom-0 top-0 left-0 right-0 flex h-full w-full cursor-pointer items-center justify-center rounded-full bg-[rgba(0,0,0,0.6)] opacity-0 hover:opacity-100">
|
||||
<PencilIcon className="h-5 w-5 text-th-fgd-1" />
|
||||
</div>
|
||||
) : null}
|
||||
</button>
|
||||
{showProfilePicModal ? (
|
||||
<NftProfilePicModal
|
||||
isOpen={showProfilePicModal}
|
||||
onClose={handleCloseProfilePicModal}
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default ProfileImageButton
|
|
@ -0,0 +1,137 @@
|
|||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { ChartTradeType } from '../@types/types'
|
||||
import useInterval from '../hooks/useInterval'
|
||||
import ChartApi from '../utils/chartDataConnector'
|
||||
import { ElementTitle } from './styles'
|
||||
import { getDecimalCount, isEqual, usdFormatter } from '../utils/index'
|
||||
import useMangoStore, { CLUSTER } from '../stores/useMangoStore'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from './TradePageGrid'
|
||||
import { ExpandableRow } from './TableElements'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
|
||||
export default function RecentMarketTrades() {
|
||||
const { t } = useTranslation('common')
|
||||
const mangoConfig = useMangoStore((s) => s.selectedMangoGroup.config)
|
||||
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
|
||||
const market = useMangoStore((s) => s.selectedMarket.current)
|
||||
const { width } = useViewport()
|
||||
const isMobile = width ? width < breakpoints.sm : false
|
||||
const [trades, setTrades] = useState<any[]>([])
|
||||
|
||||
const fetchTradesForChart = useCallback(async () => {
|
||||
if (!marketConfig) return
|
||||
|
||||
const newTrades = await ChartApi.getRecentTrades(
|
||||
marketConfig.publicKey.toString()
|
||||
)
|
||||
if (!newTrades) return null
|
||||
if (newTrades.length && trades.length === 0) {
|
||||
setTrades(newTrades)
|
||||
} else if (
|
||||
newTrades?.length &&
|
||||
!isEqual(newTrades[0], trades[0], Object.keys(newTrades[0]))
|
||||
) {
|
||||
setTrades(newTrades)
|
||||
}
|
||||
}, [marketConfig, trades])
|
||||
|
||||
useEffect(() => {
|
||||
if (CLUSTER === 'mainnet') {
|
||||
fetchTradesForChart()
|
||||
}
|
||||
}, [fetchTradesForChart])
|
||||
|
||||
useInterval(async () => {
|
||||
if (CLUSTER === 'mainnet') {
|
||||
fetchTradesForChart()
|
||||
}
|
||||
}, 2000)
|
||||
|
||||
return !isMobile ? (
|
||||
<>
|
||||
<ElementTitle>{t('recent-trades')}</ElementTitle>
|
||||
<div className={`mb-2 grid grid-cols-3 text-xs text-th-fgd-4`}>
|
||||
<div>{`${t('price')} (${mangoConfig.quoteSymbol})`} </div>
|
||||
<div className={`text-right`}>
|
||||
{t('size')} ({marketConfig.baseSymbol})
|
||||
</div>
|
||||
<div className={`text-right`}>{t('time')}</div>
|
||||
</div>
|
||||
{!!trades.length && (
|
||||
<div className="text-xs">
|
||||
{trades.map((trade: ChartTradeType, i: number) => (
|
||||
<div key={i} className={`grid grid-cols-3 leading-6`}>
|
||||
<div
|
||||
className={`${
|
||||
trade.side === 'buy' ? `text-th-green` : `text-th-red`
|
||||
}`}
|
||||
>
|
||||
{market?.tickSize && !isNaN(trade.price)
|
||||
? usdFormatter(
|
||||
trade.price,
|
||||
getDecimalCount(market.tickSize),
|
||||
false
|
||||
)
|
||||
: ''}
|
||||
</div>
|
||||
<div className={`text-right text-th-fgd-3`}>
|
||||
{market?.minOrderSize && !isNaN(trade.size)
|
||||
? Number(trade.size).toLocaleString(undefined, {
|
||||
maximumFractionDigits: getDecimalCount(
|
||||
market.minOrderSize
|
||||
),
|
||||
})
|
||||
: ''}
|
||||
</div>
|
||||
<div className={`text-right text-th-fgd-3`}>
|
||||
{trade.time && new Date(trade.time).toLocaleTimeString()}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
<div className="my-3 border-b border-th-bkg-3">
|
||||
<ExpandableRow
|
||||
buttonTemplate={
|
||||
<div className="flex w-full justify-between text-left">
|
||||
<div className="text-fgd-1 mb-0.5">{t('recent-trades')}</div>
|
||||
</div>
|
||||
}
|
||||
panelTemplate={
|
||||
!!trades.length && (
|
||||
<div className="col-span-2">
|
||||
{trades.map((trade: ChartTradeType, i: number) => (
|
||||
<div key={i} className={`grid grid-cols-3 text-xs leading-5`}>
|
||||
<div
|
||||
className={`${
|
||||
trade.side === 'buy' ? `text-th-green` : `text-th-red`
|
||||
}`}
|
||||
>
|
||||
{market?.tickSize && !isNaN(trade.price)
|
||||
? Number(trade.price).toFixed(
|
||||
getDecimalCount(market.tickSize)
|
||||
)
|
||||
: ''}
|
||||
</div>
|
||||
<div className={`text-right`}>
|
||||
{market?.minOrderSize && !isNaN(trade.size)
|
||||
? Number(trade.size).toFixed(
|
||||
getDecimalCount(market.minOrderSize)
|
||||
)
|
||||
: ''}
|
||||
</div>
|
||||
<div className={`text-right text-th-fgd-4`}>
|
||||
{trade.time && new Date(trade.time).toLocaleTimeString()}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
import React, { useMemo, useState } from 'react'
|
||||
import { settleAllPnl, settlePosPnl } from 'components/MarketPosition'
|
||||
import Button from 'components/Button'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import Loading from 'components/Loading'
|
||||
import useMangoStore from 'stores/useMangoStore'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
|
||||
const RedeemButtons: React.FC = () => {
|
||||
const { t } = useTranslation('common')
|
||||
const [settling, setSettling] = useState(false)
|
||||
const { wallet } = useWallet()
|
||||
const [settlingPosPnl, setSettlingPosPnl] = useState(false)
|
||||
const unsettledPositions =
|
||||
useMangoStore.getState().selectedMangoAccount.unsettledPerpPositions
|
||||
const unsettledPositivePositions = useMangoStore
|
||||
.getState()
|
||||
.selectedMangoAccount.unsettledPerpPositions?.filter(
|
||||
(p) => p.unsettledPnl > 0
|
||||
)
|
||||
|
||||
const handleSettleAll = async () => {
|
||||
if (!wallet) return
|
||||
setSettling(true)
|
||||
try {
|
||||
await settleAllPnl(
|
||||
unsettledPositions.map((p) => p.perpMarket),
|
||||
t,
|
||||
undefined,
|
||||
wallet
|
||||
)
|
||||
} finally {
|
||||
setSettling(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleSettlePosPnl = async () => {
|
||||
if (!wallet) return
|
||||
setSettlingPosPnl(true)
|
||||
try {
|
||||
await settlePosPnl(
|
||||
unsettledPositivePositions.map((p) => p.perpMarket),
|
||||
t,
|
||||
undefined,
|
||||
wallet
|
||||
)
|
||||
} finally {
|
||||
setSettlingPosPnl(false)
|
||||
}
|
||||
}
|
||||
|
||||
const showPosOnlyButton = useMemo(() => {
|
||||
return (
|
||||
unsettledPositions.find((pos) => pos.unsettledPnl > 0) &&
|
||||
unsettledPositions.find((pos) => pos.unsettledPnl < 0)
|
||||
)
|
||||
}, [unsettledPositions])
|
||||
|
||||
return unsettledPositions?.length ? (
|
||||
<div className="flex space-x-3">
|
||||
{showPosOnlyButton ? (
|
||||
<Button
|
||||
className="flex h-7 items-center justify-center pt-0 pb-0 pl-3 pr-3 text-xs"
|
||||
onClick={handleSettlePosPnl}
|
||||
>
|
||||
{settlingPosPnl ? <Loading /> : t('redeem-positive')}
|
||||
</Button>
|
||||
) : null}
|
||||
<Button
|
||||
className="flex h-7 items-center justify-center pt-0 pb-0 pl-3 pr-3 text-xs"
|
||||
onClick={handleSettleAll}
|
||||
primary={false}
|
||||
>
|
||||
{settling ? <Loading /> : t('redeem-all')}
|
||||
</Button>
|
||||
</div>
|
||||
) : null
|
||||
}
|
||||
|
||||
export default RedeemButtons
|
|
@ -0,0 +1,30 @@
|
|||
import { TemplateIcon } from '@heroicons/react/solid'
|
||||
import { defaultLayouts, GRID_LAYOUT_KEY } from './TradePageGrid'
|
||||
import useLocalStorageState from '../hooks/useLocalStorageState'
|
||||
import Tooltip from './Tooltip'
|
||||
import { IconButton } from './Button'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
|
||||
const ResetLayout = ({ className = '' }) => {
|
||||
const { t } = useTranslation('common')
|
||||
const [, setSavedLayouts] = useLocalStorageState(
|
||||
GRID_LAYOUT_KEY,
|
||||
defaultLayouts
|
||||
)
|
||||
|
||||
const handleResetLayout = () => {
|
||||
setSavedLayouts(defaultLayouts)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={`relative inline-flex ${className}`}>
|
||||
<Tooltip content={t('tooltip-reset-layout')} className="py-1 text-xs">
|
||||
<IconButton onClick={handleResetLayout}>
|
||||
<TemplateIcon className="h-4 w-4" />
|
||||
</IconButton>
|
||||
</Tooltip>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ResetLayout
|
|
@ -0,0 +1,66 @@
|
|||
import { Listbox } from '@headlessui/react'
|
||||
import { ChevronDownIcon } from '@heroicons/react/solid'
|
||||
|
||||
const Select = ({
|
||||
value,
|
||||
onChange,
|
||||
children,
|
||||
className = '',
|
||||
dropdownPanelClassName = '',
|
||||
placeholder = '',
|
||||
disabled = false,
|
||||
}) => {
|
||||
return (
|
||||
<div className={`relative ${className}`}>
|
||||
<Listbox value={value} onChange={onChange} disabled={disabled}>
|
||||
{({ open }) => (
|
||||
<>
|
||||
<Listbox.Button
|
||||
className={`h-full w-full rounded-md bg-th-bkg-1 font-normal ring-1 ring-inset ring-th-bkg-4 hover:ring-th-fgd-4 focus:border-th-fgd-4 focus:outline-none`}
|
||||
>
|
||||
<div
|
||||
style={{ minHeight: '2.5rem' }}
|
||||
className={`flex items-center justify-between space-x-2 p-2 text-th-fgd-1`}
|
||||
>
|
||||
{value ? value : placeholder}
|
||||
<ChevronDownIcon
|
||||
className={`default-transition h-5 w-5 flex-shrink-0 text-th-fgd-1 ${
|
||||
open ? 'rotate-180 transform' : 'rotate-360 transform'
|
||||
}`}
|
||||
/>
|
||||
</div>
|
||||
</Listbox.Button>
|
||||
{open ? (
|
||||
<Listbox.Options
|
||||
static
|
||||
className={`thin-scroll absolute left-0 z-20 mt-1 max-h-60 w-full origin-top-left overflow-auto rounded-md bg-th-bkg-2 p-2 text-th-fgd-1 outline-none ${dropdownPanelClassName}`}
|
||||
>
|
||||
{children}
|
||||
</Listbox.Options>
|
||||
) : null}
|
||||
</>
|
||||
)}
|
||||
</Listbox>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const Option = ({ value, children, className = '' }) => {
|
||||
return (
|
||||
<Listbox.Option className="mb-0" value={value}>
|
||||
{({ selected }) => (
|
||||
<div
|
||||
className={`default-transition rounded p-2 text-th-fgd-1 hover:cursor-pointer hover:bg-th-bkg-3 hover:text-th-primary ${
|
||||
selected ? 'text-th-primary' : ''
|
||||
} ${className}`}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
)}
|
||||
</Listbox.Option>
|
||||
)
|
||||
}
|
||||
|
||||
Select.Option = Option
|
||||
|
||||
export default Select
|
|
@ -0,0 +1,88 @@
|
|||
import { MangoAccount } from '@blockworks-foundation/mango-client'
|
||||
import { RadioGroup } from '@headlessui/react'
|
||||
import { CheckCircleIcon } from '@heroicons/react/outline'
|
||||
import useLocalStorageState from 'hooks/useLocalStorageState'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import useMangoStore, { LAST_ACCOUNT_KEY } from 'stores/useMangoStore'
|
||||
import MangoAccountCard from './MangoAccountCard'
|
||||
|
||||
const SelectMangoAccount = ({
|
||||
onClose,
|
||||
className,
|
||||
}: {
|
||||
onClose?: () => void
|
||||
className?: string
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const selectedMangoAccount = useMangoStore(
|
||||
(s) => s.selectedMangoAccount.current
|
||||
)
|
||||
const mangoAccounts = useMangoStore((s) => s.mangoAccounts)
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
const setMangoStore = useMangoStore((s) => s.set)
|
||||
const [, setLastAccountViewed] = useLocalStorageState(LAST_ACCOUNT_KEY)
|
||||
|
||||
const handleMangoAccountChange = (mangoAccount: MangoAccount) => {
|
||||
setLastAccountViewed(mangoAccount.publicKey.toString())
|
||||
setMangoStore((state) => {
|
||||
state.selectedMangoAccount.current = mangoAccount
|
||||
})
|
||||
|
||||
actions.fetchTradeHistory()
|
||||
if (onClose) {
|
||||
onClose()
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<RadioGroup
|
||||
value={selectedMangoAccount}
|
||||
onChange={(acc) => {
|
||||
if (acc) {
|
||||
handleMangoAccountChange(acc)
|
||||
}
|
||||
}}
|
||||
>
|
||||
<RadioGroup.Label className="sr-only">
|
||||
{t('select-account')}
|
||||
</RadioGroup.Label>
|
||||
<div className={`${className} space-y-2`}>
|
||||
{mangoAccounts.map((account) => (
|
||||
<RadioGroup.Option
|
||||
key={account.publicKey.toString()}
|
||||
value={account}
|
||||
className={({ checked }) =>
|
||||
`${
|
||||
checked
|
||||
? 'ring-1 ring-inset ring-th-green'
|
||||
: 'ring-1 ring-inset ring-th-fgd-4 hover:ring-th-fgd-2'
|
||||
}
|
||||
default-transition relative flex w-full cursor-pointer rounded-md px-3 py-3 focus:outline-none`
|
||||
}
|
||||
>
|
||||
{({ checked }) => (
|
||||
<>
|
||||
<div className="flex w-full items-center justify-between">
|
||||
<div className="flex items-center">
|
||||
<div className="text-sm">
|
||||
<RadioGroup.Label className="flex cursor-pointer items-center text-th-fgd-1">
|
||||
<MangoAccountCard mangoAccount={account} />
|
||||
</RadioGroup.Label>
|
||||
</div>
|
||||
</div>
|
||||
{checked && (
|
||||
<div className="flex-shrink-0 text-th-green">
|
||||
<CheckCircleIcon className="h-5 w-5" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</RadioGroup.Option>
|
||||
))}
|
||||
</div>
|
||||
</RadioGroup>
|
||||
)
|
||||
}
|
||||
|
||||
export default SelectMangoAccount
|
|
@ -0,0 +1,32 @@
|
|||
import { useEffect, useState } from 'react'
|
||||
import { CogIcon } from '@heroicons/react/solid'
|
||||
import SettingsModal from './SettingsModal'
|
||||
|
||||
const Settings = () => {
|
||||
const [mounted, setMounted] = useState(false)
|
||||
const [showSettingsModal, setShowSettingsModal] = useState(false)
|
||||
|
||||
// When mounted on client, now we can show the UI
|
||||
useEffect(() => setMounted(true), [])
|
||||
|
||||
return mounted ? (
|
||||
<>
|
||||
<button
|
||||
className="default-transition flex h-8 w-8 items-center justify-center rounded-full bg-th-bkg-4 text-th-fgd-1 focus:outline-none md:hover:text-th-primary"
|
||||
onClick={() => setShowSettingsModal(true)}
|
||||
>
|
||||
<CogIcon className="h-5 w-5" />
|
||||
</button>
|
||||
{showSettingsModal ? (
|
||||
<SettingsModal
|
||||
onClose={() => setShowSettingsModal(false)}
|
||||
isOpen={showSettingsModal}
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
) : (
|
||||
<div className="h-8 w-8 rounded-full bg-th-bkg-3" />
|
||||
)
|
||||
}
|
||||
|
||||
export default Settings
|
|
@ -0,0 +1,355 @@
|
|||
import React, { useMemo, useState } from 'react'
|
||||
import { ChevronLeftIcon, ChevronRightIcon } from '@heroicons/react/solid'
|
||||
import Modal from './Modal'
|
||||
import { ElementTitle } from './styles'
|
||||
import Button, { LinkButton } from './Button'
|
||||
import Input, { Label } from './Input'
|
||||
import useMangoStore, { ENDPOINTS } from '../stores/useMangoStore'
|
||||
import useLocalStorageState from '../hooks/useLocalStorageState'
|
||||
import Select from './Select'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import Switch from './Switch'
|
||||
import { MarketKind } from '@blockworks-foundation/mango-client'
|
||||
import { useTheme } from 'next-themes'
|
||||
import { useRouter } from 'next/router'
|
||||
import ButtonGroup from './ButtonGroup'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
require('dayjs/locale/en')
|
||||
require('dayjs/locale/es')
|
||||
require('dayjs/locale/ru')
|
||||
require('dayjs/locale/zh')
|
||||
require('dayjs/locale/zh-tw')
|
||||
|
||||
const RPC_URLS = [
|
||||
{
|
||||
label: 'Syndica',
|
||||
value:
|
||||
'https://solana-api.syndica.io/access-token/4ywEBJNxuwPLXXU9UlMK67fAMZBt1GLdwuXyXSYnoYPn5aXajT8my0R5klXhYRkk/rpc',
|
||||
},
|
||||
{
|
||||
label: 'Triton (RPC Pool)',
|
||||
value: 'https://mango-mango-d092.mainnet.rpcpool.com/',
|
||||
},
|
||||
{ label: 'Genesys Go', value: 'https://mango.genesysgo.net' },
|
||||
{
|
||||
label: 'Project Serum',
|
||||
value: 'https://solana-api.projectserum.com/',
|
||||
},
|
||||
{ label: 'Custom', value: '' },
|
||||
]
|
||||
|
||||
const THEMES = ['Light', 'Dark', 'Mango']
|
||||
|
||||
export const LANGS = [
|
||||
{ locale: 'en', name: 'english', description: 'english' },
|
||||
{ locale: 'es', name: 'spanish', description: 'spanish' },
|
||||
{ locale: 'ru', name: 'russian', description: 'russian' },
|
||||
{
|
||||
locale: 'zh_tw',
|
||||
name: 'chinese-traditional',
|
||||
description: 'traditional chinese',
|
||||
},
|
||||
{ locale: 'zh', name: 'chinese', description: 'simplified chinese' },
|
||||
]
|
||||
|
||||
const CUSTOM_RPC = RPC_URLS.find((n) => n.label === 'Custom')
|
||||
|
||||
export const RPC_URL_KEY = 'rpc-url-key-0.11'
|
||||
export const DEFAULT_MARKET_KEY = 'defaultMarket-0.3'
|
||||
export const ORDERBOOK_FLASH_KEY = 'showOrderbookFlash'
|
||||
export const DEFAULT_SPOT_MARGIN_KEY = 'defaultSpotMargin'
|
||||
export const initialMarket = {
|
||||
base: 'SOL',
|
||||
kind: 'perp' as MarketKind,
|
||||
name: 'SOL-PERP',
|
||||
path: '/?name=SOL-PERP',
|
||||
}
|
||||
|
||||
const SettingsModal = ({ isOpen, onClose }) => {
|
||||
const { t } = useTranslation('common')
|
||||
const [settingsView, setSettingsView] = useState('')
|
||||
const { theme } = useTheme()
|
||||
const [savedLanguage] = useLocalStorageState('language', '')
|
||||
const [rpcEndpointUrl] = useLocalStorageState(RPC_URL_KEY, ENDPOINTS[0].url)
|
||||
|
||||
const [defaultMarket] = useLocalStorageState(
|
||||
DEFAULT_MARKET_KEY,
|
||||
initialMarket
|
||||
)
|
||||
const [showOrderbookFlash, setShowOrderbookFlash] = useLocalStorageState(
|
||||
ORDERBOOK_FLASH_KEY,
|
||||
true
|
||||
)
|
||||
|
||||
const [defaultSpotMargin, setDefaultSpotMargin] = useLocalStorageState(
|
||||
DEFAULT_SPOT_MARGIN_KEY,
|
||||
false
|
||||
)
|
||||
|
||||
const rpcEndpoint =
|
||||
RPC_URLS.find((node) => node.value === rpcEndpointUrl) || CUSTOM_RPC
|
||||
|
||||
const savedLanguageName = useMemo(() => {
|
||||
const matchingLang = LANGS.find((l) => l.locale === savedLanguage)
|
||||
if (matchingLang) {
|
||||
return matchingLang.name
|
||||
}
|
||||
}, [savedLanguage])
|
||||
|
||||
return (
|
||||
<Modal isOpen={isOpen} onClose={onClose}>
|
||||
{settingsView !== '' ? (
|
||||
<LinkButton
|
||||
className="absolute left-2 top-3 flex items-center"
|
||||
onClick={() => setSettingsView('')}
|
||||
>
|
||||
<ChevronLeftIcon className="h-5 w-5" />
|
||||
<span>{t('back')}</span>
|
||||
</LinkButton>
|
||||
) : null}
|
||||
<Modal.Header>
|
||||
<ElementTitle noMarginBottom>{t('settings')}</ElementTitle>
|
||||
</Modal.Header>
|
||||
{!settingsView ? (
|
||||
<div className="border-b border-th-bkg-4">
|
||||
<button
|
||||
className="default-transition flex w-full items-center justify-between rounded-none border-t border-th-bkg-4 py-3 font-normal text-th-fgd-1 focus:outline-none md:hover:text-th-primary"
|
||||
onClick={() => setSettingsView('Default Market')}
|
||||
>
|
||||
<span>{t('default-market')}</span>
|
||||
<div className="flex items-center text-xs text-th-fgd-3">
|
||||
{defaultMarket.name}
|
||||
<ChevronRightIcon className="ml-1 h-5 w-5 text-th-fgd-1" />
|
||||
</div>
|
||||
</button>
|
||||
<button
|
||||
className="default-transition flex w-full items-center justify-between rounded-none border-t border-th-bkg-4 py-3 font-normal text-th-fgd-1 focus:outline-none md:hover:text-th-primary"
|
||||
onClick={() => setSettingsView('Theme')}
|
||||
>
|
||||
<span>{t('theme')}</span>
|
||||
<div className="flex items-center text-xs text-th-fgd-3">
|
||||
{theme}
|
||||
<ChevronRightIcon className="ml-1 h-5 w-5 text-th-fgd-1" />
|
||||
</div>
|
||||
</button>
|
||||
<button
|
||||
className="default-transition flex w-full items-center justify-between rounded-none border-t border-th-bkg-4 py-3 font-normal text-th-fgd-1 focus:outline-none md:hover:text-th-primary"
|
||||
onClick={() => setSettingsView('Language')}
|
||||
>
|
||||
<span>{t('language')}</span>
|
||||
{savedLanguageName ? (
|
||||
<div className="flex items-center text-xs text-th-fgd-3">
|
||||
{t(savedLanguageName)}
|
||||
<ChevronRightIcon className="ml-1 h-5 w-5 text-th-fgd-1" />
|
||||
</div>
|
||||
) : null}
|
||||
</button>
|
||||
<button
|
||||
className="default-transition flex w-full items-center justify-between rounded-none border-t border-th-bkg-4 py-3 font-normal text-th-fgd-1 focus:outline-none md:hover:text-th-primary"
|
||||
onClick={() => setSettingsView('RPC Endpoint')}
|
||||
>
|
||||
<span>{t('rpc-endpoint')}</span>
|
||||
<div className="flex items-center text-xs text-th-fgd-3">
|
||||
{rpcEndpoint?.label}
|
||||
<ChevronRightIcon className="ml-1 h-5 w-5 text-th-fgd-1" />
|
||||
</div>
|
||||
</button>
|
||||
<div className="flex items-center justify-between border-t border-th-bkg-4 py-3 text-th-fgd-1">
|
||||
<span>{t('orderbook-animation')}</span>
|
||||
<Switch
|
||||
checked={showOrderbookFlash}
|
||||
onChange={(checked) => setShowOrderbookFlash(checked)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between border-t border-th-bkg-4 py-3 text-th-fgd-1">
|
||||
<span>{t('default-spot-margin')}</span>
|
||||
<Switch
|
||||
checked={defaultSpotMargin}
|
||||
onChange={(checked) => setDefaultSpotMargin(checked)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
<SettingsContent
|
||||
settingsView={settingsView}
|
||||
setSettingsView={setSettingsView}
|
||||
/>
|
||||
{!settingsView ? (
|
||||
<div className="flex justify-center pt-6">
|
||||
<Button onClick={onClose}>{t('done')}</Button>
|
||||
</div>
|
||||
) : null}
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(SettingsModal)
|
||||
|
||||
const SettingsContent = ({ settingsView, setSettingsView }) => {
|
||||
switch (settingsView) {
|
||||
case 'Default Market':
|
||||
return <DefaultMarketSettings setSettingsView={setSettingsView} />
|
||||
case 'RPC Endpoint':
|
||||
return <RpcEndpointSettings setSettingsView={setSettingsView} />
|
||||
case 'Theme':
|
||||
return <ThemeSettings setSettingsView={setSettingsView} />
|
||||
case 'Language':
|
||||
return <LanguageSettings />
|
||||
default:
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
const DefaultMarketSettings = ({ setSettingsView }) => {
|
||||
const { t } = useTranslation('common')
|
||||
const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
|
||||
const allMarkets = groupConfig
|
||||
? [...groupConfig.spotMarkets, ...groupConfig.perpMarkets].sort((a, b) =>
|
||||
a.name.localeCompare(b.name)
|
||||
)
|
||||
: []
|
||||
const [defaultMarket, setDefaultMarket] = useLocalStorageState(
|
||||
DEFAULT_MARKET_KEY,
|
||||
{
|
||||
base: 'BTC',
|
||||
kind: 'perp',
|
||||
name: 'BTC-PERP',
|
||||
path: '/?name=BTC-PERP',
|
||||
}
|
||||
)
|
||||
const handleSetDefaultMarket = (market) => {
|
||||
const base = market.slice(0, -5)
|
||||
const kind = market.includes('PERP') ? 'perp' : 'spot'
|
||||
|
||||
setDefaultMarket({
|
||||
base: base,
|
||||
kind: kind,
|
||||
name: market,
|
||||
path: `/?name=${market}`,
|
||||
})
|
||||
}
|
||||
const parsedDefaultMarket = defaultMarket
|
||||
return (
|
||||
<div>
|
||||
<Label>{t('default-market')}</Label>
|
||||
<Select
|
||||
value={parsedDefaultMarket.name}
|
||||
onChange={(market) => handleSetDefaultMarket(market)}
|
||||
className="w-full"
|
||||
>
|
||||
{allMarkets.map((market) => (
|
||||
<Select.Option key={market.name} value={market.name}>
|
||||
<div className="flex w-full items-center justify-between">
|
||||
{market.name}
|
||||
</div>
|
||||
</Select.Option>
|
||||
))}
|
||||
</Select>
|
||||
<Button onClick={() => setSettingsView('')} className="mt-6 w-full">
|
||||
<div className={`flex items-center justify-center`}>{t('save')}</div>
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const RpcEndpointSettings = ({ setSettingsView }) => {
|
||||
const { t } = useTranslation('common')
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
const [rpcEndpointUrl, setRpcEndpointUrl] = useLocalStorageState(
|
||||
RPC_URL_KEY,
|
||||
ENDPOINTS[0].url
|
||||
)
|
||||
const rpcEndpoint =
|
||||
RPC_URLS.find((node) => node.value === rpcEndpointUrl) || CUSTOM_RPC
|
||||
|
||||
const handleSetEndpointUrl = (endpointUrl) => {
|
||||
setRpcEndpointUrl(endpointUrl)
|
||||
actions.updateConnection(endpointUrl)
|
||||
setSettingsView('')
|
||||
}
|
||||
|
||||
const handleSelectEndpointUrl = (url) => {
|
||||
setRpcEndpointUrl(url)
|
||||
}
|
||||
return (
|
||||
<div className="flex flex-col text-th-fgd-1">
|
||||
<Label>{t('rpc-endpoint')}</Label>
|
||||
<Select
|
||||
value={rpcEndpoint?.label}
|
||||
onChange={(url) => handleSelectEndpointUrl(url)}
|
||||
className="w-full"
|
||||
>
|
||||
{RPC_URLS.map((node) => (
|
||||
<Select.Option key={node.value} value={node.value}>
|
||||
<span>{node.label}</span>
|
||||
</Select.Option>
|
||||
))}
|
||||
</Select>
|
||||
{rpcEndpoint?.label === 'Custom' ? (
|
||||
<div className="pt-4">
|
||||
<Label>{t('node-url')}</Label>
|
||||
<Input
|
||||
type="text"
|
||||
value={rpcEndpointUrl}
|
||||
onChange={(e) => setRpcEndpointUrl(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
<Button
|
||||
onClick={() => handleSetEndpointUrl(rpcEndpointUrl)}
|
||||
className="mt-6 w-full"
|
||||
>
|
||||
<div className={`flex items-center justify-center`}>{t('save')}</div>
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const ThemeSettings = ({ setSettingsView }) => {
|
||||
const { theme, setTheme } = useTheme()
|
||||
const { t } = useTranslation('common')
|
||||
|
||||
return (
|
||||
<>
|
||||
<Label>{t('theme')}</Label>
|
||||
<ButtonGroup
|
||||
activeValue={theme}
|
||||
onChange={(t) => setTheme(t)}
|
||||
values={THEMES}
|
||||
/>
|
||||
<Button onClick={() => setSettingsView('')} className="mt-6 w-full">
|
||||
<div className={`flex items-center justify-center`}>{t('save')}</div>
|
||||
</Button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
const LanguageSettings = () => {
|
||||
const [savedLanguage, setSavedLanguage] = useLocalStorageState('language', '')
|
||||
const router = useRouter()
|
||||
const { pathname, asPath, query } = router
|
||||
const { t } = useTranslation('common')
|
||||
|
||||
const handleLangChange = () => {
|
||||
document.cookie = `NEXT_LOCALE=${savedLanguage}; max-age=31536000; path=/`
|
||||
router.push({ pathname, query }, asPath, { locale: savedLanguage })
|
||||
dayjs.locale(savedLanguage == 'zh_tw' ? 'zh-tw' : savedLanguage)
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Label>{t('language')}</Label>
|
||||
<ButtonGroup
|
||||
activeValue={savedLanguage}
|
||||
onChange={(l) => setSavedLanguage(l)}
|
||||
values={LANGS.map((val) => val.locale)}
|
||||
names={LANGS.map((val) => t(val.name))}
|
||||
/>
|
||||
<Button onClick={() => handleLangChange()} className="mt-6 w-full">
|
||||
<div className={`flex items-center justify-center`}>{t('save')}</div>
|
||||
</Button>
|
||||
</>
|
||||
)
|
||||
}
|
|
@ -0,0 +1,313 @@
|
|||
import {
|
||||
FunctionComponent,
|
||||
useEffect,
|
||||
useMemo,
|
||||
createRef,
|
||||
useState,
|
||||
} from 'react'
|
||||
import { MarketConfig } from '@blockworks-foundation/mango-client'
|
||||
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import Modal from './Modal'
|
||||
import { useScreenshot } from '../hooks/useScreenshot'
|
||||
import * as MonoIcons from './icons'
|
||||
import { TwitterIcon } from './icons'
|
||||
import QRCode from 'react-qr-code'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import useMangoAccount from '../hooks/useMangoAccount'
|
||||
import {
|
||||
mangoCacheSelector,
|
||||
mangoGroupConfigSelector,
|
||||
mangoGroupSelector,
|
||||
} from '../stores/selectors'
|
||||
import {
|
||||
getMarketIndexBySymbol,
|
||||
ReferrerIdRecord,
|
||||
} from '@blockworks-foundation/mango-client'
|
||||
import Button from './Button'
|
||||
import Switch from './Switch'
|
||||
import { roundPerpSize } from 'utils'
|
||||
|
||||
const calculatePositionPercentage = (position) => {
|
||||
if (position.basePosition > 0) {
|
||||
const returnsPercentage =
|
||||
(position.indexPrice / position.avgEntryPrice - 1) * 100
|
||||
return returnsPercentage
|
||||
} else {
|
||||
const returnsPercentage =
|
||||
(position.indexPrice / position.avgEntryPrice - 1) * -100
|
||||
return returnsPercentage
|
||||
}
|
||||
}
|
||||
|
||||
async function copyToClipboard(image) {
|
||||
try {
|
||||
image.toBlob((blob) => {
|
||||
navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })])
|
||||
}, 'image/png')
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
|
||||
interface ShareModalProps {
|
||||
onClose: () => void
|
||||
isOpen: boolean
|
||||
position: {
|
||||
indexPrice: number
|
||||
avgEntryPrice: number
|
||||
basePosition: number
|
||||
marketConfig: MarketConfig
|
||||
notionalSize: number
|
||||
}
|
||||
}
|
||||
|
||||
const ShareModal: FunctionComponent<ShareModalProps> = ({
|
||||
isOpen,
|
||||
onClose,
|
||||
position,
|
||||
}) => {
|
||||
const { t } = useTranslation(['common', 'share-modal'])
|
||||
const ref = createRef()
|
||||
const [copied, setCopied] = useState(false)
|
||||
const [showButton, setShowButton] = useState(true)
|
||||
const [image, takeScreenshot] = useScreenshot()
|
||||
const { mangoAccount } = useMangoAccount()
|
||||
const mangoCache = useMangoStore(mangoCacheSelector)
|
||||
const groupConfig = useMangoStore(mangoGroupConfigSelector)
|
||||
const client = useMangoStore.getState().connection.client
|
||||
const mangoGroup = useMangoStore(mangoGroupSelector)
|
||||
const [customRefLinks, setCustomRefLinks] = useState<ReferrerIdRecord[]>([])
|
||||
const [showSize, setShowSize] = useState(true)
|
||||
const mngoIndex = getMarketIndexBySymbol(groupConfig, 'MNGO')
|
||||
const hasRequiredMngo = useMemo(() => {
|
||||
return mangoGroup && mangoAccount && mangoCache
|
||||
? mangoAccount
|
||||
.getUiDeposit(
|
||||
mangoCache.rootBankCache[mngoIndex],
|
||||
mangoGroup,
|
||||
mngoIndex
|
||||
)
|
||||
.toNumber() >= 10000
|
||||
: false
|
||||
}, [mangoAccount, mangoGroup])
|
||||
const [showReferral, setShowReferral] = useState(
|
||||
hasRequiredMngo ? true : false
|
||||
)
|
||||
|
||||
const marketConfig = position.marketConfig
|
||||
|
||||
// const maxLeverage = useMemo(() => {
|
||||
// if (!mangoGroup) return 1
|
||||
|
||||
// const ws = getWeights(mangoGroup, marketConfig.marketIndex, 'Init')
|
||||
// return Math.round((100 * -1) / (ws.perpAssetWeight.toNumber() - 1)) / 100
|
||||
// }, [mangoGroup, marketConfig])
|
||||
|
||||
const positionPercentage = calculatePositionPercentage(position)
|
||||
|
||||
const side = position.basePosition > 0 ? 'long' : 'short'
|
||||
|
||||
useEffect(() => {
|
||||
if (image) {
|
||||
copyToClipboard(image)
|
||||
setCopied(true)
|
||||
setShowButton(true)
|
||||
}
|
||||
}, [image])
|
||||
|
||||
useEffect(() => {
|
||||
// if the button is hidden we are taking a screenshot
|
||||
if (!showButton) {
|
||||
takeScreenshot(ref.current as HTMLElement)
|
||||
}
|
||||
}, [showButton])
|
||||
|
||||
const handleCopyToClipboard = () => {
|
||||
setShowButton(false)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const fetchCustomReferralLinks = async (mangoAccount) => {
|
||||
const referrerIds = await client.getReferrerIdsForMangoAccount(
|
||||
mangoAccount
|
||||
)
|
||||
|
||||
setCustomRefLinks(referrerIds)
|
||||
}
|
||||
|
||||
if (mangoAccount && customRefLinks?.length === 0) {
|
||||
fetchCustomReferralLinks(mangoAccount)
|
||||
}
|
||||
}, [mangoAccount])
|
||||
|
||||
const isProfit = positionPercentage > 0
|
||||
|
||||
const iconName = `${marketConfig.baseSymbol.slice(
|
||||
0,
|
||||
1
|
||||
)}${marketConfig.baseSymbol.slice(1, 4).toLowerCase()}MonoIcon`
|
||||
|
||||
const SymbolIcon = MonoIcons[iconName]
|
||||
|
||||
return (
|
||||
<Modal
|
||||
isOpen={isOpen}
|
||||
onClose={onClose}
|
||||
className={`-mt-40 ${
|
||||
side === 'long'
|
||||
? isProfit
|
||||
? 'bg-long-profit'
|
||||
: 'bg-long-loss'
|
||||
: isProfit
|
||||
? 'bg-short-profit'
|
||||
: 'bg-short-loss'
|
||||
} h-[337.5px] w-[600px] bg-contain leading-[0.5] sm:max-w-7xl`}
|
||||
noPadding
|
||||
hideClose
|
||||
ref={ref}
|
||||
>
|
||||
<div
|
||||
id="share-image"
|
||||
className="relative z-20 flex h-full flex-col items-center justify-center space-y-4 drop-shadow-lg"
|
||||
>
|
||||
{hasRequiredMngo && showReferral ? (
|
||||
<div className="absolute right-4 top-4">
|
||||
<QRCode
|
||||
size={64}
|
||||
value={
|
||||
customRefLinks.length > 0
|
||||
? `https://trade.mango.markets?ref=${customRefLinks[0].referrerId}`
|
||||
: `https://trade.mango.markets?ref=${mangoAccount?.publicKey.toString()}`
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
<div className="flex items-center text-lg text-th-fgd-3">
|
||||
<SymbolIcon className="mr-2 h-6 w-auto" />
|
||||
<span
|
||||
className={`mr-2 ${
|
||||
!showButton ? 'inline-block h-full align-top leading-none' : ''
|
||||
}`}
|
||||
>
|
||||
{position.marketConfig.name}
|
||||
</span>
|
||||
<span
|
||||
className={`h-full rounded border px-1 ${
|
||||
position.basePosition > 0
|
||||
? 'border-th-green text-th-green'
|
||||
: 'border-th-red text-th-red'
|
||||
}`}
|
||||
>
|
||||
<span
|
||||
className={`${
|
||||
!showButton ? 'inline-block h-full align-top leading-none' : ''
|
||||
}`}
|
||||
>
|
||||
{t(side).toLocaleUpperCase()}
|
||||
</span>
|
||||
</span>
|
||||
</div>
|
||||
<div
|
||||
className={`text-center text-6xl font-bold ${
|
||||
isProfit ? 'text-th-green' : 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{positionPercentage > 0 ? '+' : null}
|
||||
{positionPercentage.toFixed(2)}%
|
||||
</div>
|
||||
<div className="w-1/2 space-y-1 pt-2 text-base text-th-fgd-1">
|
||||
{showSize ? (
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-th-fgd-2">{t('size')}</span>
|
||||
<span className="font-bold">
|
||||
{roundPerpSize(
|
||||
position.basePosition,
|
||||
position.marketConfig.baseSymbol
|
||||
)}
|
||||
</span>
|
||||
</div>
|
||||
) : null}
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-th-fgd-2">{t('average-entry')}</span>
|
||||
<span className="font-bold">
|
||||
$
|
||||
{position.avgEntryPrice.toLocaleString(undefined, {
|
||||
maximumFractionDigits: 2,
|
||||
minimumFractionDigits: 2,
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-th-fgd-2">{t('share-modal:mark-price')}</span>
|
||||
<span className="font-bold">
|
||||
$
|
||||
{position.indexPrice.toLocaleString(undefined, {
|
||||
maximumFractionDigits: 2,
|
||||
minimumFractionDigits: 2,
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
{/* <div className="flex items-center justify-between">
|
||||
<span className="text-th-fgd-2">
|
||||
{t('share-modal:max-leverage')}
|
||||
</span>
|
||||
<span className="font-bold">{maxLeverage}x</span>
|
||||
</div> */}
|
||||
</div>
|
||||
</div>
|
||||
<div className="absolute left-1/2 mt-3 w-[600px] -translate-x-1/2 transform rounded-md bg-th-bkg-2 p-4">
|
||||
<div className="flex flex-col items-center">
|
||||
{!copied ? (
|
||||
<div className="flex space-x-4 pb-4">
|
||||
<div className="flex items-center">
|
||||
<label className="mr-1.5 text-th-fgd-2">
|
||||
{t('share-modal:show-size')}
|
||||
</label>
|
||||
<Switch
|
||||
checked={showSize}
|
||||
onChange={(checked) => setShowSize(checked)}
|
||||
/>
|
||||
</div>
|
||||
{hasRequiredMngo ? (
|
||||
<div className="flex items-center">
|
||||
<label className="mr-1.5 text-th-fgd-2">
|
||||
{t('share-modal:show-referral-qr')}
|
||||
</label>
|
||||
<Switch
|
||||
checked={showReferral}
|
||||
onChange={(checked) => setShowReferral(checked)}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
{copied ? (
|
||||
<a
|
||||
className="block flex items-center justify-center rounded-full bg-th-bkg-button px-6 py-2 text-center font-bold text-th-fgd-1 hover:cursor-pointer hover:text-th-fgd-1 hover:brightness-[1.1]"
|
||||
href={`https://twitter.com/intent/tweet?text=I'm ${side.toUpperCase()} %24${
|
||||
position.marketConfig.baseSymbol
|
||||
} perp on %40mangomarkets%0A[PASTE IMAGE HERE]`}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
<TwitterIcon className="mr-1.5 h-4 w-4" />
|
||||
<div>{t('share-modal:tweet-position')}</div>
|
||||
</a>
|
||||
) : (
|
||||
<div>
|
||||
<Button onClick={handleCopyToClipboard}>
|
||||
<div className="flex items-center">
|
||||
<TwitterIcon className="mr-1.5 h-4 w-4" />
|
||||
{t('share-modal:copy-and-share')}
|
||||
</div>
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default ShareModal
|
|
@ -0,0 +1,25 @@
|
|||
import React, { FunctionComponent } from 'react'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
|
||||
type SideBadgeProps = {
|
||||
side: string
|
||||
}
|
||||
|
||||
const SideBadge: FunctionComponent<SideBadgeProps> = ({ side }) => {
|
||||
const { t } = useTranslation('common')
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`inline-block rounded uppercase ${
|
||||
side === 'buy' || side === 'long'
|
||||
? 'border border-th-green text-th-green'
|
||||
: 'border border-th-red text-th-red'
|
||||
}
|
||||
-my-0.5 px-1.5 py-0.5 text-xs uppercase`}
|
||||
>
|
||||
{t(side)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default SideBadge
|
|
@ -0,0 +1,431 @@
|
|||
import Link from 'next/link'
|
||||
import useLocalStorageState from 'hooks/useLocalStorageState'
|
||||
import { DEFAULT_MARKET_KEY, initialMarket } from './SettingsModal'
|
||||
import { BtcMonoIcon, TradeIcon, TrophyIcon } from './icons'
|
||||
import {
|
||||
CashIcon,
|
||||
ChartBarIcon,
|
||||
CurrencyDollarIcon,
|
||||
DotsHorizontalIcon,
|
||||
SwitchHorizontalIcon,
|
||||
CalculatorIcon,
|
||||
LibraryIcon,
|
||||
LightBulbIcon,
|
||||
UserAddIcon,
|
||||
ExternalLinkIcon,
|
||||
ChevronDownIcon,
|
||||
ReceiptTaxIcon,
|
||||
} from '@heroicons/react/solid'
|
||||
import { useRouter } from 'next/router'
|
||||
import AccountOverviewPopover from './AccountOverviewPopover'
|
||||
import useMangoAccount from 'hooks/useMangoAccount'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { Fragment, ReactNode, useEffect, useState } from 'react'
|
||||
import { Disclosure, Popover, Transition } from '@headlessui/react'
|
||||
import HealthHeart from './HealthHeart'
|
||||
import { abbreviateAddress } from 'utils'
|
||||
import { I80F48 } from '@blockworks-foundation/mango-client'
|
||||
import useMangoStore from 'stores/useMangoStore'
|
||||
|
||||
const I80F48_100 = I80F48.fromString('100')
|
||||
|
||||
const SideNav = ({ collapsed }) => {
|
||||
const { t } = useTranslation('common')
|
||||
const [defaultMarket] = useLocalStorageState(
|
||||
DEFAULT_MARKET_KEY,
|
||||
initialMarket
|
||||
)
|
||||
const router = useRouter()
|
||||
const { pathname } = router
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`flex flex-col justify-between transition-all duration-500 ease-in-out ${
|
||||
collapsed ? 'w-[64px]' : 'w-[220px]'
|
||||
} min-h-screen border-r border-th-bkg-3 bg-th-bkg-1`}
|
||||
>
|
||||
<div className="mb-4">
|
||||
<Link href={defaultMarket.path} shallow={true}>
|
||||
<div
|
||||
className={`flex h-14 w-full items-center justify-start border-b border-th-bkg-3 px-4`}
|
||||
>
|
||||
<div className={`flex flex-shrink-0 cursor-pointer items-center`}>
|
||||
<img
|
||||
className={`h-8 w-auto`}
|
||||
src="/assets/icons/logo.svg"
|
||||
alt="next"
|
||||
/>
|
||||
<Transition
|
||||
appear={true}
|
||||
show={!collapsed}
|
||||
as={Fragment}
|
||||
enter="transition-all ease-in duration-300"
|
||||
enterFrom="opacity-50"
|
||||
enterTo="opacity-100"
|
||||
leave="transition ease-out duration-300"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<span className="ml-2 text-lg font-bold text-th-fgd-1">
|
||||
Mango
|
||||
</span>
|
||||
</Transition>
|
||||
</div>
|
||||
</div>
|
||||
</Link>
|
||||
<div className={`flex flex-col items-start space-y-3.5 px-4 pt-4`}>
|
||||
<MenuItem
|
||||
active={pathname === '/'}
|
||||
collapsed={collapsed}
|
||||
icon={<TradeIcon className="h-5 w-5" />}
|
||||
title={t('trade')}
|
||||
pagePath="/"
|
||||
/>
|
||||
<MenuItem
|
||||
active={pathname === '/account'}
|
||||
collapsed={collapsed}
|
||||
icon={<CurrencyDollarIcon className="h-5 w-5" />}
|
||||
title={t('account')}
|
||||
pagePath="/account"
|
||||
/>
|
||||
<MenuItem
|
||||
active={pathname === '/markets'}
|
||||
collapsed={collapsed}
|
||||
icon={<BtcMonoIcon className="h-4 w-4" />}
|
||||
title={t('markets')}
|
||||
pagePath="/markets"
|
||||
/>
|
||||
<MenuItem
|
||||
active={pathname === '/borrow'}
|
||||
collapsed={collapsed}
|
||||
icon={<CashIcon className="h-5 w-5" />}
|
||||
title={t('borrow')}
|
||||
pagePath="/borrow"
|
||||
/>
|
||||
<MenuItem
|
||||
active={pathname === '/swap'}
|
||||
collapsed={collapsed}
|
||||
icon={<SwitchHorizontalIcon className="h-5 w-5" />}
|
||||
title={t('swap')}
|
||||
pagePath="/swap"
|
||||
/>
|
||||
<MenuItem
|
||||
active={pathname === '/stats'}
|
||||
collapsed={collapsed}
|
||||
icon={<ChartBarIcon className="h-5 w-5" />}
|
||||
title={t('stats')}
|
||||
pagePath="/stats"
|
||||
/>
|
||||
<MenuItem
|
||||
active={pathname === '/leaderboard'}
|
||||
collapsed={collapsed}
|
||||
icon={<TrophyIcon className="h-[18px] w-[18px]" />}
|
||||
title={t('leaderboard')}
|
||||
pagePath="/leaderboard"
|
||||
/>
|
||||
<ExpandableMenuItem
|
||||
collapsed={collapsed}
|
||||
icon={<DotsHorizontalIcon className="h-5 w-5" />}
|
||||
title={t('more')}
|
||||
>
|
||||
<MenuItem
|
||||
active={pathname === '/referral'}
|
||||
collapsed={false}
|
||||
icon={<UserAddIcon className="h-4 w-4" />}
|
||||
title={t('referrals')}
|
||||
pagePath="/referral"
|
||||
hideIconBg
|
||||
/>
|
||||
<MenuItem
|
||||
active={pathname === '/risk-calculator'}
|
||||
collapsed={false}
|
||||
icon={<CalculatorIcon className="h-4 w-4" />}
|
||||
title={t('calculator')}
|
||||
pagePath="/risk-calculator"
|
||||
hideIconBg
|
||||
/>
|
||||
<MenuItem
|
||||
active={pathname === '/fees'}
|
||||
collapsed={false}
|
||||
icon={<ReceiptTaxIcon className="h-4 w-4" />}
|
||||
title={t('fees')}
|
||||
pagePath="/fees"
|
||||
hideIconBg
|
||||
/>
|
||||
<MenuItem
|
||||
collapsed={false}
|
||||
icon={<LightBulbIcon className="h-4 w-4" />}
|
||||
title={t('learn')}
|
||||
pagePath="https://docs.mango.markets"
|
||||
hideIconBg
|
||||
isExternal
|
||||
/>
|
||||
<MenuItem
|
||||
collapsed={false}
|
||||
icon={<LibraryIcon className="h-4 w-4" />}
|
||||
title={t('governance')}
|
||||
pagePath="https://dao.mango.markets"
|
||||
hideIconBg
|
||||
isExternal
|
||||
/>
|
||||
</ExpandableMenuItem>
|
||||
</div>
|
||||
</div>
|
||||
<AccountSummaryPanel collapsed={collapsed} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default SideNav
|
||||
|
||||
const MenuItem = ({
|
||||
active,
|
||||
collapsed,
|
||||
icon,
|
||||
title,
|
||||
pagePath,
|
||||
hideIconBg,
|
||||
isExternal,
|
||||
}: {
|
||||
active?: boolean
|
||||
collapsed: boolean
|
||||
icon: ReactNode
|
||||
title: string
|
||||
pagePath: string
|
||||
hideIconBg?: boolean
|
||||
isExternal?: boolean
|
||||
}) => {
|
||||
return !isExternal ? (
|
||||
<Link href={pagePath} shallow={true}>
|
||||
<a
|
||||
className={`default-transition flex items-center hover:brightness-[1.1] ${
|
||||
active ? 'text-th-primary' : 'text-th-fgd-1'
|
||||
}`}
|
||||
>
|
||||
<div
|
||||
className={
|
||||
hideIconBg
|
||||
? ''
|
||||
: 'flex h-8 w-8 items-center justify-center rounded-full bg-th-bkg-3'
|
||||
}
|
||||
>
|
||||
{icon}
|
||||
</div>
|
||||
<Transition
|
||||
appear={true}
|
||||
show={!collapsed}
|
||||
as={Fragment}
|
||||
enter="transition-all ease-in duration-300"
|
||||
enterFrom="opacity-50"
|
||||
enterTo="opacity-100"
|
||||
leave="transition ease-out duration-300"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<span className="ml-2">{title}</span>
|
||||
</Transition>
|
||||
</a>
|
||||
</Link>
|
||||
) : (
|
||||
<a
|
||||
href={pagePath}
|
||||
className={`default-transition flex items-center justify-between hover:brightness-[1.1] ${
|
||||
active ? 'text-th-primary' : 'text-th-fgd-1'
|
||||
}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<div className="flex items-center">
|
||||
<div
|
||||
className={
|
||||
hideIconBg
|
||||
? ''
|
||||
: 'flex h-8 w-8 items-center justify-center rounded-full bg-th-bkg-3'
|
||||
}
|
||||
>
|
||||
{icon}
|
||||
</div>
|
||||
{!collapsed ? <span className="ml-2">{title}</span> : null}
|
||||
</div>
|
||||
<ExternalLinkIcon className="h-4 w-4" />
|
||||
</a>
|
||||
)
|
||||
}
|
||||
|
||||
const AccountSummaryPanel = ({ collapsed }) => {
|
||||
const { t } = useTranslation('common')
|
||||
const { mangoAccount } = useMangoAccount()
|
||||
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
|
||||
if (!mangoAccount) return null
|
||||
|
||||
const maintHealthRatio =
|
||||
mangoAccount && mangoGroup && mangoCache
|
||||
? mangoAccount.getHealthRatio(mangoGroup, mangoCache, 'Maint')
|
||||
: I80F48_100
|
||||
|
||||
return (
|
||||
<div className="flex min-h-[64px] w-full items-center border-t border-th-bkg-3 ">
|
||||
<ExpandableMenuItem
|
||||
collapsed={collapsed}
|
||||
icon={<HealthHeart health={Number(maintHealthRatio)} size={32} />}
|
||||
title={
|
||||
<div className="py-3 text-left">
|
||||
<p className="mb-0 whitespace-nowrap text-xs text-th-fgd-3">
|
||||
{t('account-summary')}
|
||||
</p>
|
||||
<p className="mb-0 font-bold text-th-fgd-1">
|
||||
{abbreviateAddress(mangoAccount.publicKey)}
|
||||
</p>
|
||||
</div>
|
||||
}
|
||||
hideIconBg
|
||||
alignBottom
|
||||
>
|
||||
<AccountOverviewPopover
|
||||
collapsed={collapsed}
|
||||
health={maintHealthRatio}
|
||||
/>
|
||||
</ExpandableMenuItem>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const ExpandableMenuItem = ({
|
||||
children,
|
||||
collapsed,
|
||||
icon,
|
||||
title,
|
||||
hideIconBg,
|
||||
alignBottom,
|
||||
}: {
|
||||
children: ReactNode
|
||||
collapsed: boolean
|
||||
icon: ReactNode
|
||||
title: string | ReactNode
|
||||
hideIconBg?: boolean
|
||||
alignBottom?: boolean
|
||||
}) => {
|
||||
const [showMenu, setShowMenu] = useState(false)
|
||||
|
||||
const onHoverMenu = (open, action) => {
|
||||
if (
|
||||
(!open && action === 'onMouseEnter') ||
|
||||
(open && action === 'onMouseLeave')
|
||||
) {
|
||||
setShowMenu(!showMenu)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (collapsed) {
|
||||
setShowMenu(false)
|
||||
}
|
||||
}, [collapsed])
|
||||
|
||||
return collapsed ? (
|
||||
<Popover>
|
||||
<div
|
||||
onMouseEnter={() => onHoverMenu(showMenu, 'onMouseEnter')}
|
||||
onMouseLeave={() => onHoverMenu(showMenu, 'onMouseLeave')}
|
||||
className="relative z-30"
|
||||
>
|
||||
<Popover.Button className="hover:text-th-primary">
|
||||
<div
|
||||
className={` ${
|
||||
hideIconBg
|
||||
? ''
|
||||
: 'flex h-8 w-8 items-center justify-center rounded-full bg-th-bkg-3'
|
||||
} ${
|
||||
alignBottom
|
||||
? 'default-transition flex h-16 w-16 items-center justify-center hover:bg-th-bkg-2'
|
||||
: ''
|
||||
}`}
|
||||
>
|
||||
{icon}
|
||||
</div>
|
||||
</Popover.Button>
|
||||
<Transition
|
||||
appear={true}
|
||||
show={showMenu}
|
||||
as={Fragment}
|
||||
enter="transition-all ease-in duration-300"
|
||||
enterFrom="opacity-0 transform scale-90"
|
||||
enterTo="opacity-100 transform scale-100"
|
||||
leave="transition ease-out duration-300"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<Popover.Panel
|
||||
className={`absolute w-56 space-y-2 rounded-md bg-th-bkg-2 p-4 ${
|
||||
alignBottom
|
||||
? 'bottom-0 left-14'
|
||||
: 'left-10 top-1/2 -translate-y-1/2 transform'
|
||||
}`}
|
||||
>
|
||||
{children}
|
||||
</Popover.Panel>
|
||||
</Transition>
|
||||
</div>
|
||||
</Popover>
|
||||
) : (
|
||||
<Disclosure>
|
||||
<div
|
||||
onClick={() => setShowMenu(!showMenu)}
|
||||
role="button"
|
||||
className={`w-full `}
|
||||
>
|
||||
<Disclosure.Button
|
||||
className={`flex w-full items-center justify-between rounded-none hover:text-th-primary ${
|
||||
alignBottom ? 'h-[64px] px-4 hover:bg-th-bkg-2' : ''
|
||||
}`}
|
||||
>
|
||||
<div className="flex items-center">
|
||||
<div
|
||||
className={
|
||||
hideIconBg
|
||||
? ''
|
||||
: 'flex h-8 w-8 items-center justify-center rounded-full bg-th-bkg-3'
|
||||
}
|
||||
>
|
||||
{icon}
|
||||
</div>
|
||||
<Transition
|
||||
appear={true}
|
||||
show={!collapsed}
|
||||
as={Fragment}
|
||||
enter="transition-all ease-in duration-300"
|
||||
enterFrom="opacity-50"
|
||||
enterTo="opacity-100"
|
||||
leave="transition ease-out duration-300"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<span className="ml-2">{title}</span>
|
||||
</Transition>
|
||||
</div>
|
||||
<ChevronDownIcon
|
||||
className={`${
|
||||
showMenu ? 'rotate-180 transform' : 'rotate-360 transform'
|
||||
} default-transition h-5 w-5 flex-shrink-0`}
|
||||
/>
|
||||
</Disclosure.Button>
|
||||
<Transition
|
||||
appear={true}
|
||||
show={showMenu}
|
||||
as={Fragment}
|
||||
enter="transition-all ease-in duration-500"
|
||||
enterFrom="opacity-100 max-h-0"
|
||||
enterTo="opacity-100 max-h-64"
|
||||
leave="transition-all ease-out duration-500"
|
||||
leaveFrom="opacity-100 max-h-64"
|
||||
leaveTo="opacity-0 max-h-0"
|
||||
>
|
||||
<Disclosure.Panel className="overflow-hidden">
|
||||
<div className="space-y-2 p-2">{children}</div>
|
||||
</Disclosure.Panel>
|
||||
</Transition>
|
||||
</div>
|
||||
</Disclosure>
|
||||
)
|
||||
}
|
|
@ -0,0 +1,178 @@
|
|||
export {}
|
||||
|
||||
// import { FunctionComponent, useEffect, useState } from 'react'
|
||||
// import tw from 'twin.macro'
|
||||
// import styled from '@emotion/styled'
|
||||
// import Slider from 'rc-slider'
|
||||
// import 'rc-slider/assets/index.css'
|
||||
|
||||
// type StyledSliderProps = {
|
||||
// enableTransition?: boolean
|
||||
// disabled?: boolean
|
||||
// }
|
||||
|
||||
// const StyledSlider = styled(Slider)<StyledSliderProps>`
|
||||
// .rc-slider-rail {
|
||||
// ${tw`bg-th-bkg-3 h-2.5 rounded-full`}
|
||||
// }
|
||||
// .rc-slider-track {
|
||||
// ${tw`bg-th-primary h-2.5 rounded-full ring-1 ring-th-primary ring-inset`}
|
||||
// ${({ enableTransition }) =>
|
||||
// enableTransition && tw`transition-all duration-500`}
|
||||
// }
|
||||
// .rc-slider-step {
|
||||
// ${tw`hidden`}
|
||||
// }
|
||||
// .rc-slider-handle {
|
||||
// ${tw`border-4 border-th-primary h-4 w-4`}
|
||||
// background: #fff;
|
||||
// box-shadow: 0px 0px 8px 0px rgba(0, 0, 0, 0.1);
|
||||
// margin-top: -3px;
|
||||
// ${({ enableTransition }) =>
|
||||
// enableTransition && tw`transition-all duration-500`}
|
||||
// ${({ disabled }) => disabled && tw`bg-th-fgd-3 border-th-fgd-4`}
|
||||
// }
|
||||
// ${({ disabled }) => disabled && 'background-color: transparent'}
|
||||
// `
|
||||
|
||||
// const StyledSliderButtonWrapper = styled.div`
|
||||
// ${tw`absolute left-0 top-5 w-full`}
|
||||
// `
|
||||
|
||||
// type StyledSliderButtonProps = {
|
||||
// disabled: boolean
|
||||
// styleValue: number
|
||||
// sliderValue: number
|
||||
// }
|
||||
|
||||
// const StyledSliderButton = styled.button<StyledSliderButtonProps>`
|
||||
// ${tw`bg-none text-th-fgd-3 transition-all duration-300 hover:text-th-primary focus:outline-none`}
|
||||
// font-size: 0.65rem;
|
||||
// position: absolute;
|
||||
// display: inline-block;
|
||||
// vertical-align: middle;
|
||||
// text-align: center;
|
||||
// left: 0%;
|
||||
// :nth-of-type(2) {
|
||||
// left: 23%;
|
||||
// transform: translateX(-23%);
|
||||
// }
|
||||
// :nth-of-type(3) {
|
||||
// left: 50%;
|
||||
// transform: translateX(-50%);
|
||||
// }
|
||||
// :nth-of-type(4) {
|
||||
// left: 76%;
|
||||
// transform: translateX(-76%);
|
||||
// }
|
||||
// :nth-of-type(5) {
|
||||
// left: 100%;
|
||||
// transform: translateX(-100%);
|
||||
// }
|
||||
// ${({ styleValue, sliderValue }) => styleValue < sliderValue && tw`opacity-40`}
|
||||
// ${({ styleValue, sliderValue }) =>
|
||||
// styleValue === sliderValue && tw`text-th-primary`}
|
||||
// ${({ disabled }) =>
|
||||
// disabled && tw`cursor-not-allowed text-th-fgd-4 hover:text-th-fgd-4`}
|
||||
// `
|
||||
|
||||
// type SliderProps = {
|
||||
// onChange: (x) => void
|
||||
// onAfterChange?: (x) => void
|
||||
// step: number
|
||||
// value: number
|
||||
// disabled?: boolean
|
||||
// max?: number
|
||||
// maxButtonTransition?: boolean
|
||||
// }
|
||||
|
||||
// const AmountSlider: FunctionComponent<SliderProps> = ({
|
||||
// onChange,
|
||||
// onAfterChange,
|
||||
// step,
|
||||
// value,
|
||||
// disabled,
|
||||
// max,
|
||||
// maxButtonTransition,
|
||||
// }) => {
|
||||
// const [enableTransition, setEnableTransition] = useState(false)
|
||||
|
||||
// useEffect(() => {
|
||||
// if (maxButtonTransition) {
|
||||
// setEnableTransition(true)
|
||||
// }
|
||||
// }, [maxButtonTransition])
|
||||
|
||||
// useEffect(() => {
|
||||
// if (enableTransition) {
|
||||
// const transitionTimer = setTimeout(() => {
|
||||
// setEnableTransition(false)
|
||||
// }, 500)
|
||||
// return () => clearTimeout(transitionTimer)
|
||||
// }
|
||||
// }, [enableTransition])
|
||||
|
||||
// const handleSliderButtonClick = (value) => {
|
||||
// onChange(value)
|
||||
// setEnableTransition(true)
|
||||
// }
|
||||
|
||||
// return (
|
||||
// <div className="relative">
|
||||
// <StyledSlider
|
||||
// min={0}
|
||||
// max={max}
|
||||
// value={value || 0}
|
||||
// onChange={onChange}
|
||||
// onAfterChange={onAfterChange}
|
||||
// step={step}
|
||||
// enableTransition={enableTransition}
|
||||
// disabled={disabled}
|
||||
// />
|
||||
// <StyledSliderButtonWrapper>
|
||||
// <StyledSliderButton
|
||||
// disabled={disabled}
|
||||
// onClick={() => handleSliderButtonClick(0)}
|
||||
// styleValue={0}
|
||||
// sliderValue={value}
|
||||
// >
|
||||
// 0%
|
||||
// </StyledSliderButton>
|
||||
// <StyledSliderButton
|
||||
// disabled={disabled}
|
||||
// onClick={() => handleSliderButtonClick(25)}
|
||||
// styleValue={25}
|
||||
// sliderValue={value}
|
||||
// >
|
||||
// 25%
|
||||
// </StyledSliderButton>
|
||||
// <StyledSliderButton
|
||||
// disabled={disabled}
|
||||
// onClick={() => handleSliderButtonClick(50)}
|
||||
// styleValue={50}
|
||||
// sliderValue={value}
|
||||
// >
|
||||
// 50%
|
||||
// </StyledSliderButton>
|
||||
// <StyledSliderButton
|
||||
// disabled={disabled}
|
||||
// onClick={() => handleSliderButtonClick(75)}
|
||||
// styleValue={75}
|
||||
// sliderValue={value}
|
||||
// >
|
||||
// 75%
|
||||
// </StyledSliderButton>
|
||||
// <StyledSliderButton
|
||||
// disabled={disabled}
|
||||
// onClick={() => handleSliderButtonClick(100)}
|
||||
// styleValue={100}
|
||||
// sliderValue={value}
|
||||
// >
|
||||
// 100%
|
||||
// </StyledSliderButton>
|
||||
// </StyledSliderButtonWrapper>
|
||||
// </div>
|
||||
// )
|
||||
// }
|
||||
|
||||
// export default AmountSlider
|
|
@ -0,0 +1,85 @@
|
|||
import { useEffect, useState } from 'react'
|
||||
import Modal from './Modal'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import Button from './Button'
|
||||
import ButtonGroup from './ButtonGroup'
|
||||
import Input, { Label } from './Input'
|
||||
import { LinkButton } from './Button'
|
||||
|
||||
const slippagePresets = ['0.1', '0.5', '1', '2']
|
||||
|
||||
const SwapSettingsModal = ({
|
||||
isOpen,
|
||||
onClose,
|
||||
slippage,
|
||||
setSlippage,
|
||||
}: {
|
||||
isOpen: boolean
|
||||
onClose?: () => void
|
||||
slippage: number
|
||||
setSlippage: (x) => void
|
||||
}) => {
|
||||
const { t } = useTranslation(['common', 'swap'])
|
||||
const [tempSlippage, setTempSlippage] = useState(slippage)
|
||||
const [inputValue, setInputValue] = useState(
|
||||
tempSlippage ? tempSlippage.toString() : ''
|
||||
)
|
||||
const [showCustomSlippageForm, setShowCustomSlippageForm] = useState(false)
|
||||
|
||||
const handleSetTempSlippage = (s) => {
|
||||
setTempSlippage(s)
|
||||
setInputValue('')
|
||||
}
|
||||
|
||||
const handleSave = () => {
|
||||
setSlippage(inputValue ? parseFloat(inputValue) : tempSlippage)
|
||||
onClose?.()
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!slippagePresets.includes(tempSlippage.toString())) {
|
||||
setShowCustomSlippageForm(true)
|
||||
}
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<Modal isOpen={isOpen} onClose={onClose} hideClose>
|
||||
<Modal.Header>
|
||||
<h2 className="text-lg font-bold text-th-fgd-1">
|
||||
{t('swap:slippage-settings')}
|
||||
</h2>
|
||||
</Modal.Header>
|
||||
<div className="flex justify-between">
|
||||
<Label>{t('swap:slippage')}</Label>
|
||||
<LinkButton
|
||||
className="mb-1.5"
|
||||
onClick={() => setShowCustomSlippageForm(!showCustomSlippageForm)}
|
||||
>
|
||||
{showCustomSlippageForm ? t('presets') : t('custom')}
|
||||
</LinkButton>
|
||||
</div>
|
||||
{showCustomSlippageForm ? (
|
||||
<Input
|
||||
type="text"
|
||||
placeholder="0.00"
|
||||
value={inputValue}
|
||||
onChange={(e) => setInputValue(e.target.value)}
|
||||
suffix="%"
|
||||
/>
|
||||
) : (
|
||||
<ButtonGroup
|
||||
activeValue={tempSlippage.toString()}
|
||||
className="h-10"
|
||||
onChange={(v) => handleSetTempSlippage(v)}
|
||||
unit="%"
|
||||
values={slippagePresets}
|
||||
/>
|
||||
)}
|
||||
<Button className="mt-6 w-full" onClick={handleSave}>
|
||||
{t('save')}
|
||||
</Button>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default SwapSettingsModal
|
|
@ -0,0 +1,809 @@
|
|||
import { FunctionComponent, useEffect, useMemo, useState } from 'react'
|
||||
import { ExternalLinkIcon, EyeOffIcon } from '@heroicons/react/solid'
|
||||
import dayjs from 'dayjs'
|
||||
import relativeTime from 'dayjs/plugin/relativeTime'
|
||||
import { AreaChart, Area, XAxis, YAxis, Tooltip } from 'recharts'
|
||||
import useDimensions from 'react-cool-dimensions'
|
||||
import { IconButton } from './Button'
|
||||
import { LineChartIcon } from './icons'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { ExpandableRow } from './TableElements'
|
||||
|
||||
dayjs.extend(relativeTime)
|
||||
|
||||
interface SwapTokenInfoProps {
|
||||
inputTokenId?: string
|
||||
outputTokenId?: string
|
||||
}
|
||||
|
||||
export const numberFormatter = Intl.NumberFormat('en', {
|
||||
minimumFractionDigits: 1,
|
||||
maximumFractionDigits: 5,
|
||||
})
|
||||
|
||||
export const numberCompacter = Intl.NumberFormat('en', {
|
||||
notation: 'compact',
|
||||
maximumFractionDigits: 2,
|
||||
})
|
||||
|
||||
const SwapTokenInfo: FunctionComponent<SwapTokenInfoProps> = ({
|
||||
inputTokenId,
|
||||
outputTokenId,
|
||||
}) => {
|
||||
const [chartData, setChartData] = useState([])
|
||||
const [hideChart, setHideChart] = useState(false)
|
||||
const [baseTokenId, setBaseTokenId] = useState('')
|
||||
const [quoteTokenId, setQuoteTokenId] = useState('')
|
||||
const [inputTokenInfo, setInputTokenInfo] = useState<any>(null)
|
||||
const [outputTokenInfo, setOutputTokenInfo] = useState<any>(null)
|
||||
const [mouseData, setMouseData] = useState<string | null>(null)
|
||||
const [daysToShow, setDaysToShow] = useState(1)
|
||||
const [topHolders, setTopHolders] = useState<any>(null)
|
||||
const { observe, width, height } = useDimensions()
|
||||
const { t } = useTranslation(['common', 'swap'])
|
||||
|
||||
const getTopHolders = async (inputMint, outputMint) => {
|
||||
const inputResponse = await fetch(
|
||||
`https://public-api.solscan.io/token/holders?tokenAddress=${inputMint}&offset=0&limit=10`
|
||||
)
|
||||
const outputResponse = await fetch(
|
||||
`https://public-api.solscan.io/token/holders?tokenAddress=${outputMint}&offset=0&limit=10`
|
||||
)
|
||||
const inputData = await inputResponse.json()
|
||||
const outputData = await outputResponse.json()
|
||||
|
||||
setTopHolders({
|
||||
inputHolders: inputData.data,
|
||||
outputHolders: outputData.data,
|
||||
})
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (inputTokenInfo && outputTokenInfo) {
|
||||
getTopHolders(
|
||||
inputTokenInfo.contract_address,
|
||||
outputTokenInfo.contract_address
|
||||
)
|
||||
}
|
||||
}, [inputTokenInfo, outputTokenInfo])
|
||||
|
||||
const handleMouseMove = (coords) => {
|
||||
if (coords.activePayload) {
|
||||
setMouseData(coords.activePayload[0].payload)
|
||||
}
|
||||
}
|
||||
|
||||
const handleMouseLeave = () => {
|
||||
setMouseData(null)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!inputTokenId || !outputTokenId) {
|
||||
return
|
||||
}
|
||||
if (['usd-coin', 'tether'].includes(inputTokenId)) {
|
||||
setBaseTokenId(outputTokenId)
|
||||
setQuoteTokenId(inputTokenId)
|
||||
} else {
|
||||
setBaseTokenId(inputTokenId)
|
||||
setQuoteTokenId(outputTokenId)
|
||||
}
|
||||
}, [inputTokenId, outputTokenId])
|
||||
|
||||
// Use ohlc data
|
||||
|
||||
const getChartData = async () => {
|
||||
const inputResponse = await fetch(
|
||||
`https://api.coingecko.com/api/v3/coins/${baseTokenId}/ohlc?vs_currency=usd&days=${daysToShow}`
|
||||
)
|
||||
const outputResponse = await fetch(
|
||||
`https://api.coingecko.com/api/v3/coins/${quoteTokenId}/ohlc?vs_currency=usd&days=${daysToShow}`
|
||||
)
|
||||
const inputData = await inputResponse.json()
|
||||
const outputData = await outputResponse.json()
|
||||
|
||||
let data: any[] = []
|
||||
if (Array.isArray(inputData)) {
|
||||
data = data.concat(inputData)
|
||||
}
|
||||
if (Array.isArray(outputData)) {
|
||||
data = data.concat(outputData)
|
||||
}
|
||||
|
||||
const formattedData = data.reduce((a, c) => {
|
||||
const found = a.find((price) => price.time === c[0])
|
||||
if (found) {
|
||||
if (['usd-coin', 'tether'].includes(quoteTokenId)) {
|
||||
found.price = found.inputPrice / c[4]
|
||||
} else {
|
||||
found.price = c[4] / found.inputPrice
|
||||
}
|
||||
} else {
|
||||
a.push({ time: c[0], inputPrice: c[4] })
|
||||
}
|
||||
return a
|
||||
}, [])
|
||||
formattedData[formattedData.length - 1].time = Date.now()
|
||||
setChartData(formattedData.filter((d) => d.price))
|
||||
}
|
||||
|
||||
// Alternative chart data. Needs a timestamp tolerance to get data points for each asset
|
||||
|
||||
// const getChartData = async () => {
|
||||
// const now = Date.now() / 1000
|
||||
// const inputResponse = await fetch(
|
||||
// `https://api.coingecko.com/api/v3/coins/${inputTokenId}/market_chart/range?vs_currency=usd&from=${
|
||||
// now - 1 * 86400
|
||||
// }&to=${now}`
|
||||
// )
|
||||
|
||||
// const outputResponse = await fetch(
|
||||
// `https://api.coingecko.com/api/v3/coins/${outputTokenId}/market_chart/range?vs_currency=usd&from=${
|
||||
// now - 1 * 86400
|
||||
// }&to=${now}`
|
||||
// )
|
||||
// const inputData = await inputResponse.json()
|
||||
// const outputData = await outputResponse.json()
|
||||
|
||||
// const data = inputData?.prices.concat(outputData?.prices)
|
||||
|
||||
// const formattedData = data.reduce((a, c) => {
|
||||
// const found = a.find(
|
||||
// (price) => c[0] >= price.time - 120000 && c[0] <= price.time + 120000
|
||||
// )
|
||||
// if (found) {
|
||||
// found.price = found.inputPrice / c[1]
|
||||
// } else {
|
||||
// a.push({ time: c[0], inputPrice: c[1] })
|
||||
// }
|
||||
// return a
|
||||
// }, [])
|
||||
// setChartData(formattedData.filter((d) => d.price))
|
||||
// }
|
||||
|
||||
const getInputTokenInfo = async () => {
|
||||
const response = await fetch(
|
||||
`https://api.coingecko.com/api/v3/coins/${inputTokenId}?localization=false&tickers=false&developer_data=false&sparkline=false
|
||||
`
|
||||
)
|
||||
const data = await response.json()
|
||||
setInputTokenInfo(data)
|
||||
}
|
||||
|
||||
const getOutputTokenInfo = async () => {
|
||||
const response = await fetch(
|
||||
`https://api.coingecko.com/api/v3/coins/${outputTokenId}?localization=false&tickers=false&developer_data=false&sparkline=false
|
||||
`
|
||||
)
|
||||
const data = await response.json()
|
||||
setOutputTokenInfo(data)
|
||||
}
|
||||
|
||||
useMemo(() => {
|
||||
if (baseTokenId && quoteTokenId) {
|
||||
getChartData()
|
||||
}
|
||||
}, [daysToShow, baseTokenId, quoteTokenId])
|
||||
|
||||
useMemo(() => {
|
||||
if (baseTokenId) {
|
||||
getInputTokenInfo()
|
||||
}
|
||||
if (quoteTokenId) {
|
||||
getOutputTokenInfo()
|
||||
}
|
||||
}, [baseTokenId, quoteTokenId])
|
||||
|
||||
const chartChange = chartData.length
|
||||
? ((chartData[chartData.length - 1]['price'] - chartData[0]['price']) /
|
||||
chartData[0]['price']) *
|
||||
100
|
||||
: 0
|
||||
|
||||
return (
|
||||
<div>
|
||||
{chartData.length && baseTokenId && quoteTokenId ? (
|
||||
<div className="pb-6">
|
||||
<div className="flex items-start justify-between">
|
||||
<div>
|
||||
{inputTokenInfo && outputTokenInfo ? (
|
||||
<div className="text-sm text-th-fgd-3">
|
||||
{`${outputTokenInfo?.symbol?.toUpperCase()}/${inputTokenInfo?.symbol?.toUpperCase()}`}
|
||||
</div>
|
||||
) : null}
|
||||
{mouseData ? (
|
||||
<>
|
||||
<div className="text-lg font-bold text-th-fgd-1">
|
||||
{numberFormatter.format(mouseData['price'])}
|
||||
<span
|
||||
className={`ml-2 text-xs ${
|
||||
chartChange >= 0 ? 'text-th-green' : 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{chartChange.toFixed(2)}%
|
||||
</span>
|
||||
</div>
|
||||
<div className="text-xs font-normal text-th-fgd-3">
|
||||
{dayjs(mouseData['time']).format('DD MMM YY, h:mma')}
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<div className="text-lg font-bold text-th-fgd-1">
|
||||
{numberFormatter.format(
|
||||
chartData[chartData.length - 1]['price']
|
||||
)}
|
||||
<span
|
||||
className={`ml-2 text-xs ${
|
||||
chartChange >= 0 ? 'text-th-green' : 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{chartChange.toFixed(2)}%
|
||||
</span>
|
||||
</div>
|
||||
<div className="text-xs font-normal text-th-fgd-3">
|
||||
{dayjs(chartData[chartData.length - 1]['time']).format(
|
||||
'DD MMM YY, h:mma'
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<IconButton onClick={() => setHideChart(!hideChart)}>
|
||||
{hideChart ? (
|
||||
<LineChartIcon className="h-4 w-4" />
|
||||
) : (
|
||||
<EyeOffIcon className="h-4 w-4" />
|
||||
)}
|
||||
</IconButton>
|
||||
</div>
|
||||
{!hideChart ? (
|
||||
<div className="mt-4 h-52 w-full" ref={observe}>
|
||||
<AreaChart
|
||||
width={width}
|
||||
height={height}
|
||||
data={chartData}
|
||||
onMouseMove={handleMouseMove}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
>
|
||||
<Tooltip
|
||||
cursor={{
|
||||
strokeOpacity: 0,
|
||||
}}
|
||||
content={<></>}
|
||||
/>
|
||||
<defs>
|
||||
<linearGradient id="gradientArea" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0%" stopColor="#ffba24" stopOpacity={0.9} />
|
||||
<stop offset="90%" stopColor="#ffba24" stopOpacity={0} />
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<Area
|
||||
isAnimationActive={true}
|
||||
type="monotone"
|
||||
dataKey="price"
|
||||
stroke="#ffba24"
|
||||
fill="url(#gradientArea)"
|
||||
/>
|
||||
<XAxis dataKey="time" hide />
|
||||
<YAxis
|
||||
dataKey="price"
|
||||
type="number"
|
||||
domain={['dataMin', 'dataMax']}
|
||||
hide
|
||||
/>
|
||||
</AreaChart>
|
||||
<div className="flex justify-end">
|
||||
<button
|
||||
className={`default-transition px-3 py-2 text-xs font-bold text-th-fgd-1 focus:outline-none md:hover:text-th-primary ${
|
||||
daysToShow === 1 && 'text-th-primary'
|
||||
}`}
|
||||
onClick={() => setDaysToShow(1)}
|
||||
>
|
||||
24H
|
||||
</button>
|
||||
<button
|
||||
className={`default-transition px-3 py-2 text-xs font-bold text-th-fgd-1 focus:outline-none md:hover:text-th-primary ${
|
||||
daysToShow === 7 && 'text-th-primary'
|
||||
}`}
|
||||
onClick={() => setDaysToShow(7)}
|
||||
>
|
||||
7D
|
||||
</button>
|
||||
<button
|
||||
className={`default-transition px-3 py-2 text-xs font-bold text-th-fgd-1 focus:outline-none md:hover:text-th-primary ${
|
||||
daysToShow === 30 && 'text-th-primary'
|
||||
}`}
|
||||
onClick={() => setDaysToShow(30)}
|
||||
>
|
||||
30D
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : (
|
||||
<div className="mt-4 rounded-md bg-th-bkg-3 p-4 text-center text-th-fgd-3 md:mt-0">
|
||||
<LineChartIcon className="mx-auto h-6 w-6 text-th-primary" />
|
||||
{t('swap:chart-not-available')}
|
||||
</div>
|
||||
)}
|
||||
<div className="pt-8">
|
||||
{inputTokenInfo && outputTokenInfo && baseTokenId ? (
|
||||
<ExpandableRow
|
||||
buttonTemplate={
|
||||
<div className="text-fgd-1 flex w-full items-center justify-between">
|
||||
<div className="flex items-center">
|
||||
{inputTokenInfo.image?.small ? (
|
||||
<img
|
||||
className="rounded-full"
|
||||
src={inputTokenInfo.image?.small}
|
||||
width="32"
|
||||
height="32"
|
||||
alt={inputTokenInfo.name}
|
||||
/>
|
||||
) : null}
|
||||
<div className="ml-2.5 text-left">
|
||||
<h2 className="text-base font-bold text-th-fgd-1">
|
||||
{inputTokenInfo?.symbol?.toUpperCase()}
|
||||
</h2>
|
||||
<div className="text-xs font-normal text-th-fgd-3">
|
||||
{inputTokenInfo.name}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center">
|
||||
<div className="flex items-center space-x-3">
|
||||
{inputTokenInfo.market_data?.current_price?.usd ? (
|
||||
<div className="font-normal text-th-fgd-1">
|
||||
$
|
||||
{numberFormatter.format(
|
||||
inputTokenInfo.market_data?.current_price.usd
|
||||
)}
|
||||
</div>
|
||||
) : null}
|
||||
{inputTokenInfo.market_data?.price_change_percentage_24h ? (
|
||||
<div
|
||||
className={`font-normal text-th-fgd-1 ${
|
||||
inputTokenInfo.market_data
|
||||
.price_change_percentage_24h >= 0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{inputTokenInfo.market_data.price_change_percentage_24h.toFixed(
|
||||
2
|
||||
)}
|
||||
%
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
panelTemplate={
|
||||
<div>
|
||||
<div className="m-1 mt-0 pb-2 text-base font-bold text-th-fgd-1">
|
||||
{t('market-data')}
|
||||
</div>
|
||||
<div className="grid grid-flow-row grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-2 xl:grid-cols-3">
|
||||
{inputTokenInfo.market_cap_rank ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:market-cap-rank')}
|
||||
</div>
|
||||
<div className="text-lg font-bold text-th-fgd-1">
|
||||
#{inputTokenInfo.market_cap_rank}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
{inputTokenInfo.market_data?.market_cap &&
|
||||
inputTokenInfo.market_data?.market_cap?.usd !== 0 ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:market-cap')}
|
||||
</div>
|
||||
<div className="text-lg font-bold text-th-fgd-1">
|
||||
$
|
||||
{numberCompacter.format(
|
||||
inputTokenInfo.market_data?.market_cap?.usd
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
{inputTokenInfo.market_data?.total_volume?.usd ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('daily-volume')}
|
||||
</div>
|
||||
<div className="text-lg font-bold text-th-fgd-1">
|
||||
$
|
||||
{numberCompacter.format(
|
||||
inputTokenInfo.market_data?.total_volume?.usd
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
{inputTokenInfo.market_data?.circulating_supply ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:token-supply')}
|
||||
</div>
|
||||
<div className="text-lg font-bold text-th-fgd-1">
|
||||
{numberCompacter.format(
|
||||
inputTokenInfo.market_data.circulating_supply
|
||||
)}
|
||||
</div>
|
||||
{inputTokenInfo.market_data?.max_supply ? (
|
||||
<div className="text-xs text-th-fgd-2">
|
||||
{t('swap:max-supply')}:{' '}
|
||||
{numberCompacter.format(
|
||||
inputTokenInfo.market_data.max_supply
|
||||
)}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
{inputTokenInfo.market_data?.ath?.usd ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:ath')}
|
||||
</div>
|
||||
<div className="flex">
|
||||
<div className="text-lg font-bold text-th-fgd-1">
|
||||
$
|
||||
{numberFormatter.format(
|
||||
inputTokenInfo.market_data.ath.usd
|
||||
)}
|
||||
</div>
|
||||
{inputTokenInfo.market_data?.ath_change_percentage
|
||||
?.usd ? (
|
||||
<div
|
||||
className={`ml-1.5 mt-2 text-xs ${
|
||||
inputTokenInfo.market_data?.ath_change_percentage
|
||||
?.usd >= 0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{(inputTokenInfo.market_data?.ath_change_percentage?.usd).toFixed(
|
||||
2
|
||||
)}
|
||||
%
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
{inputTokenInfo.market_data?.ath_date?.usd ? (
|
||||
<div className="text-xs text-th-fgd-2">
|
||||
{dayjs(
|
||||
inputTokenInfo.market_data.ath_date.usd
|
||||
).fromNow()}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
{inputTokenInfo.market_data?.atl?.usd ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:atl')}
|
||||
</div>
|
||||
<div className="flex">
|
||||
<div className="text-lg font-bold text-th-fgd-1">
|
||||
$
|
||||
{numberFormatter.format(
|
||||
inputTokenInfo.market_data.atl.usd
|
||||
)}
|
||||
</div>
|
||||
{inputTokenInfo.market_data?.atl_change_percentage
|
||||
?.usd ? (
|
||||
<div
|
||||
className={`ml-1.5 mt-2 text-xs ${
|
||||
inputTokenInfo.market_data?.atl_change_percentage
|
||||
?.usd >= 0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{(inputTokenInfo.market_data?.atl_change_percentage?.usd).toLocaleString(
|
||||
undefined,
|
||||
{
|
||||
minimumFractionDigits: 0,
|
||||
maximumFractionDigits: 2,
|
||||
}
|
||||
)}
|
||||
%
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
{inputTokenInfo.market_data?.atl_date?.usd ? (
|
||||
<div className="text-xs text-th-fgd-2">
|
||||
{dayjs(
|
||||
inputTokenInfo.market_data.atl_date.usd
|
||||
).fromNow()}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
{topHolders?.inputHolders ? (
|
||||
<div className="pt-4">
|
||||
<div className="m-1 pb-3 text-base font-bold text-th-fgd-1">
|
||||
{t('swap:top-ten')}
|
||||
</div>
|
||||
{topHolders.inputHolders.map((holder) => (
|
||||
<a
|
||||
className="default mx-1 flex justify-between border-t border-th-bkg-4 px-2 py-2.5 text-th-fgd-3 transition hover:bg-th-bkg-2"
|
||||
href={`https://explorer.solana.com/address/${holder.owner}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
key={holder.owner}
|
||||
>
|
||||
<div className="text-th-fgd-3">
|
||||
{holder.owner.slice(0, 5) +
|
||||
'…' +
|
||||
holder.owner.slice(-5)}
|
||||
</div>
|
||||
<div className="flex items-center">
|
||||
<div className="text-th-fgd-1">
|
||||
{numberFormatter.format(
|
||||
holder.amount / Math.pow(10, holder.decimals)
|
||||
)}
|
||||
</div>
|
||||
<ExternalLinkIcon className="ml-2 h-4 w-4" />
|
||||
</div>
|
||||
</a>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
) : (
|
||||
<div className="mt-3 rounded-md bg-th-bkg-3 p-4 text-center text-th-fgd-3">
|
||||
{t('swap:input-info-unavailable')}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{outputTokenInfo && quoteTokenId ? (
|
||||
<div className="w-full border-b border-th-bkg-4">
|
||||
<ExpandableRow
|
||||
buttonTemplate={
|
||||
<div className="text-fgd-1 flex w-full items-center justify-between">
|
||||
<div className="flex items-center">
|
||||
{outputTokenInfo.image?.small ? (
|
||||
<img
|
||||
className="rounded-full"
|
||||
src={outputTokenInfo.image?.small}
|
||||
width="32"
|
||||
height="32"
|
||||
alt={outputTokenInfo.name}
|
||||
/>
|
||||
) : null}
|
||||
<div className="ml-2.5 text-left">
|
||||
<h2 className="text-base font-bold text-th-fgd-1">
|
||||
{outputTokenInfo?.symbol?.toUpperCase()}
|
||||
</h2>
|
||||
<div className="text-xs font-normal text-th-fgd-3">
|
||||
{outputTokenInfo.name}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center">
|
||||
<div className="flex items-center space-x-3">
|
||||
{outputTokenInfo.market_data?.current_price?.usd ? (
|
||||
<div className="font-normal text-th-fgd-1">
|
||||
$
|
||||
{numberFormatter.format(
|
||||
outputTokenInfo.market_data?.current_price.usd
|
||||
)}
|
||||
</div>
|
||||
) : null}
|
||||
{outputTokenInfo.market_data
|
||||
?.price_change_percentage_24h ? (
|
||||
<div
|
||||
className={`font-normal text-th-fgd-1 ${
|
||||
outputTokenInfo.market_data
|
||||
.price_change_percentage_24h >= 0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{outputTokenInfo.market_data.price_change_percentage_24h.toFixed(
|
||||
2
|
||||
)}
|
||||
%
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
panelTemplate={
|
||||
<div>
|
||||
<div className="m-1 mt-0 pb-2 text-base font-bold text-th-fgd-1">
|
||||
{t('market-data')}
|
||||
</div>
|
||||
<div className="grid grid-flow-row grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-2 xl:grid-cols-3">
|
||||
{outputTokenInfo.market_cap_rank ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:market-cap-rank')}
|
||||
</div>
|
||||
<div className="text-lg font-bold text-th-fgd-1">
|
||||
#{outputTokenInfo.market_cap_rank}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
{outputTokenInfo.market_data?.market_cap &&
|
||||
outputTokenInfo.market_data?.market_cap?.usd !== 0 ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:market-cap')}
|
||||
</div>
|
||||
<div className="text-lg font-bold text-th-fgd-1">
|
||||
$
|
||||
{numberCompacter.format(
|
||||
outputTokenInfo.market_data?.market_cap?.usd
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
{outputTokenInfo.market_data?.total_volume?.usd ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('daily-volume')}
|
||||
</div>
|
||||
<div className="text-lg font-bold text-th-fgd-1">
|
||||
$
|
||||
{numberCompacter.format(
|
||||
outputTokenInfo.market_data?.total_volume?.usd
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
{outputTokenInfo.market_data?.circulating_supply ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:token-supply')}
|
||||
</div>
|
||||
<div className="text-lg font-bold text-th-fgd-1">
|
||||
{numberCompacter.format(
|
||||
outputTokenInfo.market_data.circulating_supply
|
||||
)}
|
||||
</div>
|
||||
{outputTokenInfo.market_data?.max_supply ? (
|
||||
<div className="text-xs text-th-fgd-2">
|
||||
{t('swap:max-supply')}:{' '}
|
||||
{numberCompacter.format(
|
||||
outputTokenInfo.market_data.max_supply
|
||||
)}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
{outputTokenInfo.market_data?.ath?.usd ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:ath')}
|
||||
</div>
|
||||
<div className="flex">
|
||||
<div className="text-lg font-bold text-th-fgd-1">
|
||||
$
|
||||
{numberFormatter.format(
|
||||
outputTokenInfo.market_data.ath.usd
|
||||
)}
|
||||
</div>
|
||||
{outputTokenInfo.market_data?.ath_change_percentage
|
||||
?.usd ? (
|
||||
<div
|
||||
className={`ml-1.5 mt-2 text-xs ${
|
||||
outputTokenInfo.market_data
|
||||
?.ath_change_percentage?.usd >= 0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{(outputTokenInfo.market_data?.ath_change_percentage?.usd).toFixed(
|
||||
2
|
||||
)}
|
||||
%
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
{outputTokenInfo.market_data?.ath_date?.usd ? (
|
||||
<div className="text-xs text-th-fgd-2">
|
||||
{dayjs(
|
||||
outputTokenInfo.market_data.ath_date.usd
|
||||
).fromNow()}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
{outputTokenInfo.market_data?.atl?.usd ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:atl')}
|
||||
</div>
|
||||
<div className="flex">
|
||||
<div className="text-lg font-bold text-th-fgd-1">
|
||||
$
|
||||
{numberFormatter.format(
|
||||
outputTokenInfo.market_data.atl.usd
|
||||
)}
|
||||
</div>
|
||||
{outputTokenInfo.market_data?.atl_change_percentage
|
||||
?.usd ? (
|
||||
<div
|
||||
className={`ml-1.5 mt-2 text-xs ${
|
||||
outputTokenInfo.market_data
|
||||
?.atl_change_percentage?.usd >= 0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{(outputTokenInfo.market_data?.atl_change_percentage?.usd).toLocaleString(
|
||||
undefined,
|
||||
{
|
||||
minimumFractionDigits: 0,
|
||||
maximumFractionDigits: 2,
|
||||
}
|
||||
)}
|
||||
%
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
{outputTokenInfo.market_data?.atl_date?.usd ? (
|
||||
<div className="text-xs text-th-fgd-2">
|
||||
{dayjs(
|
||||
outputTokenInfo.market_data.atl_date.usd
|
||||
).fromNow()}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
{topHolders?.inputHolders ? (
|
||||
<div className="pt-4">
|
||||
<div className="m-1 pb-3 text-base font-bold text-th-fgd-1">
|
||||
{t('swap:top-ten')}
|
||||
</div>
|
||||
{topHolders.inputHolders.map((holder) => (
|
||||
<a
|
||||
className="default mx-1 flex justify-between border-t border-th-bkg-4 px-2 py-2.5 text-th-fgd-3 transition hover:bg-th-bkg-2"
|
||||
href={`https://explorer.solana.com/address/${holder.owner}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
key={holder.owner}
|
||||
>
|
||||
<div className="text-th-fgd-3">
|
||||
{holder.owner.slice(0, 5) +
|
||||
'…' +
|
||||
holder.owner.slice(-5)}
|
||||
</div>
|
||||
<div className="flex items-center">
|
||||
<div className="text-th-fgd-1">
|
||||
{numberFormatter.format(
|
||||
holder.amount / Math.pow(10, holder.decimals)
|
||||
)}
|
||||
</div>
|
||||
<ExternalLinkIcon className="ml-2 h-4 w-4" />
|
||||
</div>
|
||||
</a>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<div className="mt-3 rounded-md bg-th-bkg-3 p-4 text-center text-th-fgd-3">
|
||||
{t('swap:output-info-unavailable')}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default SwapTokenInfo
|
|
@ -0,0 +1,392 @@
|
|||
import { useEffect, useState } from 'react'
|
||||
import { PublicKey } from '@solana/web3.js'
|
||||
import dayjs from 'dayjs'
|
||||
import relativeTime from 'dayjs/plugin/relativeTime'
|
||||
import ButtonGroup from './ButtonGroup'
|
||||
import { numberCompacter, numberFormatter } from './SwapTokenInfo'
|
||||
import Button, { IconButton } from './Button'
|
||||
import Input from './Input'
|
||||
import { SearchIcon, XIcon } from '@heroicons/react/solid'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { ExpandableRow } from './TableElements'
|
||||
|
||||
const filterByVals = ['change-percent', '24h-volume']
|
||||
const timeFrameVals = ['24h', '7d', '30d']
|
||||
const insightTypeVals = ['best', 'worst']
|
||||
|
||||
dayjs.extend(relativeTime)
|
||||
|
||||
const SwapTokenInsights = ({ formState, jupiterTokens, setOutputToken }) => {
|
||||
const [tokenInsights, setTokenInsights] = useState<any>([])
|
||||
const [filteredTokenInsights, setFilteredTokenInsights] = useState<any>([])
|
||||
const [insightType, setInsightType] = useState(insightTypeVals[0])
|
||||
const [filterBy, setFilterBy] = useState(filterByVals[0])
|
||||
const [timeframe, setTimeframe] = useState(timeFrameVals[0])
|
||||
const [textFilter, setTextFilter] = useState('')
|
||||
const [showSearch, setShowSearch] = useState(false)
|
||||
const [loading, setLoading] = useState(false)
|
||||
const { t } = useTranslation(['common', 'swap'])
|
||||
|
||||
const getTokenInsights = async () => {
|
||||
setLoading(true)
|
||||
const ids = jupiterTokens
|
||||
.filter((token) => token?.extensions?.coingeckoId)
|
||||
.map((token) => token.extensions.coingeckoId)
|
||||
const response = await fetch(
|
||||
`https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=${ids.toString()}&order=market_cap_desc&sparkline=false&price_change_percentage=24h,7d,30d`
|
||||
)
|
||||
const data = await response.json()
|
||||
const filterMicroVolume = data.filter((token) => token.total_volume > 10000)
|
||||
setLoading(false)
|
||||
setTokenInsights(filterMicroVolume)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (filterBy === filterByVals[0] && textFilter === '') {
|
||||
//filter by 'change %'
|
||||
setFilteredTokenInsights(
|
||||
tokenInsights
|
||||
.sort((a, b) =>
|
||||
insightType === insightTypeVals[0] //insight type 'best'
|
||||
? b[`price_change_percentage_${timeframe}_in_currency`] -
|
||||
a[`price_change_percentage_${timeframe}_in_currency`]
|
||||
: a[`price_change_percentage_${timeframe}_in_currency`] -
|
||||
b[`price_change_percentage_${timeframe}_in_currency`]
|
||||
)
|
||||
.slice(0, 10)
|
||||
)
|
||||
}
|
||||
if (filterBy === filterByVals[1] && textFilter === '') {
|
||||
//filter by 24h vol
|
||||
setFilteredTokenInsights(
|
||||
tokenInsights
|
||||
.sort((a, b) =>
|
||||
insightType === insightTypeVals[0] //insight type 'best'
|
||||
? b.total_volume - a.total_volume
|
||||
: a.total_volume - b.total_volume
|
||||
)
|
||||
.slice(0, 10)
|
||||
)
|
||||
}
|
||||
if (textFilter !== '') {
|
||||
setFilteredTokenInsights(
|
||||
tokenInsights.filter(
|
||||
(token) =>
|
||||
token.name.includes(textFilter) || token.symbol.includes(textFilter)
|
||||
)
|
||||
)
|
||||
}
|
||||
}, [filterBy, insightType, textFilter, timeframe, tokenInsights])
|
||||
|
||||
useEffect(() => {
|
||||
if (jupiterTokens) {
|
||||
getTokenInsights()
|
||||
}
|
||||
}, [])
|
||||
|
||||
const handleToggleSearch = () => {
|
||||
setShowSearch(!showSearch)
|
||||
setTextFilter('')
|
||||
}
|
||||
|
||||
return filteredTokenInsights ? (
|
||||
<div>
|
||||
<div className="mb-3 flex items-end space-x-2">
|
||||
{!showSearch ? (
|
||||
<div className="flex w-full flex-col lg:flex-row lg:items-center lg:justify-between">
|
||||
<div className="mb-2 w-44 lg:mb-0">
|
||||
<ButtonGroup
|
||||
activeValue={filterBy}
|
||||
className="h-10"
|
||||
onChange={(t) => setFilterBy(t)}
|
||||
values={filterByVals}
|
||||
names={filterByVals.map((val) => t(`swap:${val}`))}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex space-x-2">
|
||||
{filterBy === filterByVals[0] ? ( //filter by change %
|
||||
<div className="w-36">
|
||||
<ButtonGroup
|
||||
activeValue={timeframe}
|
||||
className="h-10"
|
||||
onChange={(t) => setTimeframe(t)}
|
||||
values={timeFrameVals}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
<div className="w-28">
|
||||
<ButtonGroup
|
||||
activeValue={insightType}
|
||||
className="h-10"
|
||||
onChange={(t) => setInsightType(t)}
|
||||
values={insightTypeVals}
|
||||
names={insightTypeVals.map((val) => t(`swap:${val}`))}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="w-full">
|
||||
<Input
|
||||
type="text"
|
||||
placeholder="Search tokens..."
|
||||
value={textFilter}
|
||||
onChange={(e) => setTextFilter(e.target.value)}
|
||||
prefix={<SearchIcon className="h-4 w-4 text-th-fgd-3" />}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<IconButton
|
||||
className="h-10 w-10 flex-shrink-0"
|
||||
onClick={() => handleToggleSearch()}
|
||||
>
|
||||
{showSearch ? (
|
||||
<XIcon className="h-4 w-4" />
|
||||
) : (
|
||||
<SearchIcon className="h-4 w-4" />
|
||||
)}
|
||||
</IconButton>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div className="space-y-2">
|
||||
<div className="h-12 w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-12 w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
<div className="h-12 w-full animate-pulse rounded-md bg-th-bkg-3" />
|
||||
</div>
|
||||
) : filteredTokenInsights.length > 0 ? (
|
||||
<div className="border-b border-th-bkg-4">
|
||||
{filteredTokenInsights.map((insight) => {
|
||||
if (!insight) {
|
||||
return null
|
||||
}
|
||||
const jupToken = jupiterTokens.find(
|
||||
(t) => t?.extensions?.coingeckoId === insight.id
|
||||
)
|
||||
return (
|
||||
<>
|
||||
<ExpandableRow
|
||||
buttonTemplate={
|
||||
<div className="flex w-full items-center">
|
||||
<div className="flex w-1/2 items-center space-x-3">
|
||||
<div
|
||||
className={`min-w-[48px] text-xs ${
|
||||
timeframe === timeFrameVals[0] //timeframe 24h
|
||||
? insight.price_change_percentage_24h_in_currency >=
|
||||
0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
: timeframe === timeFrameVals[1] //timeframe 7d
|
||||
? insight.price_change_percentage_7d_in_currency >=
|
||||
0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
: insight.price_change_percentage_30d_in_currency >=
|
||||
0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{timeframe === timeFrameVals[0] //timeframe 24h
|
||||
? insight.price_change_percentage_24h_in_currency
|
||||
? `${insight.price_change_percentage_24h_in_currency.toFixed(
|
||||
1
|
||||
)}%`
|
||||
: '?'
|
||||
: timeframe === timeFrameVals[1] //timeframe 7d
|
||||
? insight.price_change_percentage_7d_in_currency
|
||||
? `${insight.price_change_percentage_7d_in_currency.toFixed(
|
||||
1
|
||||
)}%`
|
||||
: '?'
|
||||
: insight.price_change_percentage_30d_in_currency
|
||||
? `${insight.price_change_percentage_30d_in_currency.toFixed(
|
||||
1
|
||||
)}%`
|
||||
: '?'}
|
||||
</div>
|
||||
{insight.image ? (
|
||||
<img
|
||||
src={insight.image}
|
||||
width="24"
|
||||
height="24"
|
||||
alt={insight.name}
|
||||
className="hidden rounded-full lg:block"
|
||||
/>
|
||||
) : (
|
||||
<div className="inline-flex h-6 w-6 items-center justify-center rounded-full bg-th-bkg-3 text-xs text-th-fgd-3">
|
||||
?
|
||||
</div>
|
||||
)}
|
||||
<div className="text-left">
|
||||
<div className="font-bold">
|
||||
{insight?.symbol?.toUpperCase()}
|
||||
</div>
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{insight.name}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex w-1/2 items-center justify-end space-x-3 pl-2 text-right text-xs">
|
||||
<div>
|
||||
<div className="mb-[4px] text-th-fgd-4">
|
||||
{t('price')}
|
||||
</div>
|
||||
<div className="text-th-fgd-3">
|
||||
$
|
||||
{insight.current_price.toLocaleString(undefined, {
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 6,
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
<div className="border-l border-th-bkg-4" />
|
||||
<div>
|
||||
<div className="mb-[4px] text-th-fgd-4">
|
||||
{t('swap:24h-vol')}
|
||||
</div>
|
||||
<div className="text-th-fgd-3">
|
||||
{insight.total_volume > 0
|
||||
? `$${numberCompacter.format(
|
||||
insight.total_volume
|
||||
)}`
|
||||
: '?'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
className="ml-3 hidden pl-3 pr-3 text-xs lg:block"
|
||||
onClick={() =>
|
||||
setOutputToken({
|
||||
...formState,
|
||||
outputMint: new PublicKey(jupToken.address),
|
||||
})
|
||||
}
|
||||
>
|
||||
{t('buy')}
|
||||
</Button>
|
||||
</div>
|
||||
}
|
||||
panelTemplate={
|
||||
<div className="grid grid-flow-row grid-cols-1 sm:grid-cols-2 xl:grid-cols-3">
|
||||
{insight.market_cap_rank ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:market-cap-rank')}
|
||||
</div>
|
||||
<div className="font-bold text-th-fgd-1">
|
||||
#{insight.market_cap_rank}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
{insight?.market_cap && insight?.market_cap !== 0 ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:market-cap')}
|
||||
</div>
|
||||
<div className="font-bold text-th-fgd-1">
|
||||
${numberCompacter.format(insight.market_cap)}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
{insight?.circulating_supply ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:token-supply')}
|
||||
</div>
|
||||
<div className="font-bold text-th-fgd-1">
|
||||
{numberCompacter.format(insight.circulating_supply)}
|
||||
</div>
|
||||
{insight?.max_supply ? (
|
||||
<div className="text-xs text-th-fgd-2">
|
||||
{t('swap:max-supply')}
|
||||
{': '}
|
||||
{numberCompacter.format(insight.max_supply)}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
{insight?.ath ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:ath')}
|
||||
</div>
|
||||
<div className="flex">
|
||||
<div className="font-bold text-th-fgd-1">
|
||||
${numberFormatter.format(insight.ath)}
|
||||
</div>
|
||||
{insight?.ath_change_percentage ? (
|
||||
<div
|
||||
className={`ml-1.5 mt-0.5 text-xs ${
|
||||
insight?.ath_change_percentage >= 0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{insight.ath_change_percentage.toFixed(2)}%
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
{insight?.ath_date ? (
|
||||
<div className="text-xs text-th-fgd-2">
|
||||
{dayjs(insight.ath_date).fromNow()}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
{insight?.atl ? (
|
||||
<div className="m-1 rounded-md border border-th-bkg-4 p-3">
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
{t('swap:atl')}
|
||||
</div>
|
||||
<div className="flex">
|
||||
<div className="font-bold text-th-fgd-1">
|
||||
${numberFormatter.format(insight.atl)}
|
||||
</div>
|
||||
{insight?.atl_change_percentage ? (
|
||||
<div
|
||||
className={`ml-1.5 mt-0.5 text-xs ${
|
||||
insight?.atl_change_percentage >= 0
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}`}
|
||||
>
|
||||
{(insight?.atl_change_percentage).toLocaleString(
|
||||
undefined,
|
||||
{
|
||||
minimumFractionDigits: 0,
|
||||
maximumFractionDigits: 2,
|
||||
}
|
||||
)}
|
||||
%
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
{insight?.atl_date ? (
|
||||
<div className="text-xs text-th-fgd-2">
|
||||
{dayjs(insight.atl_date).fromNow()}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
) : (
|
||||
<div className="mt-3 rounded-md bg-th-bkg-3 p-4 text-center text-th-fgd-3">
|
||||
{t('swap:no-tokens-found')}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div className="mt-3 rounded-md bg-th-bkg-3 p-4 text-center text-th-fgd-3">
|
||||
{t('swap:insights-not-available')}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default SwapTokenInsights
|
|
@ -0,0 +1,194 @@
|
|||
import { memo, useMemo, useState, PureComponent, useEffect } from 'react'
|
||||
import { SearchIcon } from '@heroicons/react/solid'
|
||||
import Modal from './Modal'
|
||||
import { FixedSizeList } from 'react-window'
|
||||
import { Token } from '../@types/types'
|
||||
|
||||
const generateSearchTerm = (item: Token, searchValue: string) => {
|
||||
const normalizedSearchValue = searchValue.toLowerCase()
|
||||
const values = `${item.symbol} ${item.name}`.toLowerCase()
|
||||
|
||||
const isMatchingWithSymbol =
|
||||
item.symbol.toLowerCase().indexOf(normalizedSearchValue) >= 0
|
||||
const matchingSymbolPercent = isMatchingWithSymbol
|
||||
? normalizedSearchValue.length / item.symbol.length
|
||||
: 0
|
||||
|
||||
return {
|
||||
token: item,
|
||||
matchingIdx: values.indexOf(normalizedSearchValue),
|
||||
matchingSymbolPercent,
|
||||
}
|
||||
}
|
||||
|
||||
const startSearch = (items: Token[], searchValue: string) => {
|
||||
return items
|
||||
.map((item) => generateSearchTerm(item, searchValue))
|
||||
.filter((item) => item.matchingIdx >= 0)
|
||||
.sort((i1, i2) => i1.matchingIdx - i2.matchingIdx)
|
||||
.sort((i1, i2) => i2.matchingSymbolPercent - i1.matchingSymbolPercent)
|
||||
.map((item) => item.token)
|
||||
}
|
||||
|
||||
type ItemRendererProps = {
|
||||
data: any
|
||||
index: number
|
||||
style: any
|
||||
}
|
||||
|
||||
class ItemRenderer extends PureComponent<ItemRendererProps> {
|
||||
render() {
|
||||
// Access the items array using the "data" prop:
|
||||
const tokenInfo = this.props.data.items[this.props.index]
|
||||
|
||||
return (
|
||||
<div style={this.props.style}>
|
||||
<button
|
||||
key={tokenInfo?.address}
|
||||
className="flex w-full cursor-pointer items-center justify-between rounded-none py-4 px-6 font-normal focus:bg-th-bkg-3 focus:outline-none md:hover:bg-th-bkg-4"
|
||||
onClick={() => this.props.data.onSubmit(tokenInfo)}
|
||||
>
|
||||
<div className="flex items-center">
|
||||
<img
|
||||
src={tokenInfo?.logoURI}
|
||||
width="24"
|
||||
height="24"
|
||||
alt={tokenInfo?.symbol}
|
||||
/>
|
||||
<div className="ml-4">
|
||||
<div className="text-left text-th-fgd-2">
|
||||
{tokenInfo?.symbol || 'unknown'}
|
||||
</div>
|
||||
<div className="text-left text-th-fgd-4">
|
||||
{tokenInfo?.name || 'unknown'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const SwapTokenSelect = ({
|
||||
isOpen,
|
||||
sortedTokenMints,
|
||||
onClose,
|
||||
onTokenSelect,
|
||||
walletTokens,
|
||||
}: {
|
||||
isOpen: boolean
|
||||
sortedTokenMints: Token[]
|
||||
onClose?: (x?) => void
|
||||
onTokenSelect?: (x?) => void
|
||||
walletTokens?: Array<any>
|
||||
}) => {
|
||||
const [search, setSearch] = useState('')
|
||||
|
||||
const popularTokenSymbols = ['USDC', 'SOL', 'USDT', 'MNGO', 'BTC', 'ETH']
|
||||
|
||||
const popularTokens = useMemo(() => {
|
||||
return walletTokens?.length
|
||||
? sortedTokenMints.filter((token) => {
|
||||
const walletMints = walletTokens.map((tok) =>
|
||||
tok.account.mint.toString()
|
||||
)
|
||||
return !token?.name || !token?.symbol
|
||||
? false
|
||||
: popularTokenSymbols.includes(token.symbol) &&
|
||||
walletMints.includes(token.address)
|
||||
})
|
||||
: sortedTokenMints.filter((token) => {
|
||||
return !token?.name || !token?.symbol
|
||||
? false
|
||||
: popularTokenSymbols.includes(token.symbol)
|
||||
})
|
||||
}, [walletTokens])
|
||||
|
||||
useEffect(() => {
|
||||
function onEscape(e) {
|
||||
if (e.keyCode === 27) {
|
||||
onClose?.()
|
||||
}
|
||||
}
|
||||
window.addEventListener('keydown', onEscape)
|
||||
return () => window.removeEventListener('keydown', onEscape)
|
||||
}, [])
|
||||
|
||||
const tokenInfos = useMemo(() => {
|
||||
if (sortedTokenMints?.length) {
|
||||
const filteredTokens = sortedTokenMints.filter((token) => {
|
||||
return !token?.name || !token?.symbol ? false : true
|
||||
})
|
||||
if (walletTokens?.length) {
|
||||
const walletMints = walletTokens.map((tok) =>
|
||||
tok.account.mint.toString()
|
||||
)
|
||||
return filteredTokens.sort(
|
||||
(a, b) =>
|
||||
walletMints.indexOf(b.address) - walletMints.indexOf(a.address)
|
||||
)
|
||||
} else {
|
||||
return filteredTokens
|
||||
}
|
||||
} else {
|
||||
return []
|
||||
}
|
||||
}, [sortedTokenMints])
|
||||
|
||||
const handleUpdateSearch = (e) => {
|
||||
setSearch(e.target.value)
|
||||
}
|
||||
|
||||
const sortedTokens = search ? startSearch(tokenInfos, search) : tokenInfos
|
||||
|
||||
return (
|
||||
<Modal isOpen={isOpen} onClose={onClose} hideClose noPadding alignTop>
|
||||
<div className="flex flex-col pb-2 md:h-2/3">
|
||||
<div className="flex items-center p-6 text-lg text-th-fgd-4">
|
||||
<SearchIcon className="h-8 w-8" />
|
||||
<input
|
||||
type="text"
|
||||
className="ml-4 flex-1 bg-th-bkg-1 focus:outline-none"
|
||||
placeholder="Search by token or paste address"
|
||||
autoFocus
|
||||
value={search}
|
||||
onChange={handleUpdateSearch}
|
||||
/>
|
||||
</div>
|
||||
{popularTokens.length && onTokenSelect ? (
|
||||
<div className="flex flex-wrap px-4">
|
||||
{popularTokens.map((token) => (
|
||||
<button
|
||||
className="mx-1 mb-2 flex items-center rounded-md border border-th-fgd-4 bg-th-bkg-1 py-1 px-3 hover:border-th-fgd-3 focus:border-th-fgd-2"
|
||||
onClick={() => onTokenSelect(token)}
|
||||
key={token.address}
|
||||
>
|
||||
<img
|
||||
alt=""
|
||||
width="16"
|
||||
height="16"
|
||||
src={`/assets/icons/${token.symbol.toLowerCase()}.svg`}
|
||||
className={`mr-1.5`}
|
||||
/>
|
||||
<span className="text-th-fgd-1">{token.symbol}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
<FixedSizeList
|
||||
width="100%"
|
||||
height={403}
|
||||
itemData={{ items: sortedTokens, onSubmit: onTokenSelect }}
|
||||
itemCount={sortedTokens.length}
|
||||
itemSize={72}
|
||||
className="thin-scroll"
|
||||
>
|
||||
{ItemRenderer}
|
||||
</FixedSizeList>
|
||||
</div>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default memo(SwapTokenSelect)
|
|
@ -0,0 +1,46 @@
|
|||
import { FunctionComponent } from 'react'
|
||||
|
||||
interface SwitchProps {
|
||||
checked: boolean
|
||||
className?: string
|
||||
onChange: (x) => void
|
||||
}
|
||||
|
||||
const Switch: FunctionComponent<SwitchProps> = ({
|
||||
checked = false,
|
||||
className = '',
|
||||
children,
|
||||
onChange,
|
||||
}) => {
|
||||
const handleClick = () => {
|
||||
onChange(!checked)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={`flex items-center ${className}`}>
|
||||
<button
|
||||
type="button"
|
||||
className={`${
|
||||
checked ? 'bg-th-primary' : 'bg-th-bkg-button'
|
||||
} relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer rounded-full
|
||||
border-2 border-transparent transition-colors duration-200 ease-in-out
|
||||
focus:outline-none`}
|
||||
role="switch"
|
||||
aria-checked={checked}
|
||||
onClick={handleClick}
|
||||
>
|
||||
<span className="sr-only">{children}</span>
|
||||
<span
|
||||
aria-hidden="true"
|
||||
className={`${
|
||||
checked ? 'translate-x-5' : 'translate-x-0'
|
||||
} pointer-events-none inline-block h-5 w-5 transform rounded-full
|
||||
bg-white shadow ring-0 transition duration-200 ease-in-out`}
|
||||
></span>
|
||||
</button>
|
||||
<span className={children ? 'ml-2' : ''}>{children}</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Switch
|
|
@ -0,0 +1,172 @@
|
|||
import { Fragment, useCallback, useMemo, useRef, useState } from 'react'
|
||||
import { Popover, Transition } from '@headlessui/react'
|
||||
import { ChevronDownIcon, SearchIcon } from '@heroicons/react/solid'
|
||||
import Input from './Input'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import MarketNavItem from './MarketNavItem'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
|
||||
const SwitchMarketDropdown = () => {
|
||||
const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
|
||||
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
|
||||
const baseSymbol = marketConfig.baseSymbol
|
||||
const isPerpMarket = marketConfig.kind === 'perp'
|
||||
|
||||
const marketsInfo = useMangoStore((s) => s.marketsInfo)
|
||||
|
||||
const perpMarketsInfo = useMemo(
|
||||
() =>
|
||||
marketsInfo
|
||||
.filter((mkt) => mkt?.name.includes('PERP'))
|
||||
.sort((a, b) => b.volumeUsd24h - a.volumeUsd24h),
|
||||
[marketsInfo]
|
||||
)
|
||||
|
||||
const spotMarketsInfo = useMemo(
|
||||
() =>
|
||||
marketsInfo
|
||||
.filter((mkt) => mkt?.name.includes('USDC'))
|
||||
.sort((a, b) => b.volumeUsd24h - a.volumeUsd24h),
|
||||
[marketsInfo]
|
||||
)
|
||||
|
||||
const [suggestions, setSuggestions] = useState<any[]>([])
|
||||
const [searchString, setSearchString] = useState('')
|
||||
const buttonRef = useRef(null)
|
||||
const { t } = useTranslation('common')
|
||||
const filteredMarkets = marketsInfo
|
||||
.filter((m) => m.name.toLowerCase().includes(searchString.toLowerCase()))
|
||||
.sort((a, b) => a.name.localeCompare(b.name))
|
||||
|
||||
const onSearch = (searchString) => {
|
||||
if (searchString.length > 0) {
|
||||
const newSuggestions = suggestions.filter((v) =>
|
||||
v.name?.toLowerCase().includes(searchString.toLowerCase())
|
||||
)
|
||||
setSuggestions(newSuggestions)
|
||||
}
|
||||
setSearchString(searchString)
|
||||
}
|
||||
|
||||
const callbackRef = useCallback((inputElement) => {
|
||||
if (inputElement) {
|
||||
const timer = setTimeout(() => inputElement.focus(), 200)
|
||||
return () => clearTimeout(timer)
|
||||
}
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<Popover>
|
||||
{({ open }) => (
|
||||
<div className="relative flex flex-col">
|
||||
<Popover.Button
|
||||
className={`default-transition border border-th-fgd-4 p-0.5 transition-none hover:border-th-fgd-3 focus:border-th-fgd-4 focus:outline-none ${
|
||||
open && 'border-th-fgd-4'
|
||||
}`}
|
||||
ref={buttonRef}
|
||||
>
|
||||
<div className="flex items-center pl-2">
|
||||
<img
|
||||
alt=""
|
||||
width="24"
|
||||
height="24"
|
||||
src={`/assets/icons/${baseSymbol?.toLowerCase()}.svg`}
|
||||
className={`mr-2.5`}
|
||||
/>
|
||||
|
||||
<div className="pr-0.5 text-xl font-semibold">{baseSymbol}</div>
|
||||
<span className="text-xl text-th-fgd-4">
|
||||
{isPerpMarket ? '-' : '/'}
|
||||
</span>
|
||||
<div className="pl-0.5 text-xl font-semibold">
|
||||
{isPerpMarket ? 'PERP' : groupConfig?.quoteSymbol}
|
||||
</div>
|
||||
<div
|
||||
className={`flex h-10 w-8 items-center justify-center rounded-none`}
|
||||
>
|
||||
<ChevronDownIcon
|
||||
className={`default-transition h-6 w-6 ${
|
||||
open ? 'rotate-180 transform' : 'rotate-360 transform'
|
||||
}`}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</Popover.Button>
|
||||
<Transition
|
||||
as={Fragment}
|
||||
enter="transition-all ease-in duration-200"
|
||||
enterFrom="opacity-0 transform scale-75"
|
||||
enterTo="opacity-100 transform scale-100"
|
||||
leave="transition ease-out duration-200"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<Popover.Panel
|
||||
className="thin-scroll absolute left-0 top-14 z-10 max-h-[50vh] w-72 transform overflow-y-auto rounded-b-md rounded-tl-md bg-th-bkg-2 p-4 sm:max-h-[75vh]"
|
||||
tabIndex={-1}
|
||||
>
|
||||
<div className="hidden pb-2.5 sm:block">
|
||||
<Input
|
||||
onChange={(e) => onSearch(e.target.value)}
|
||||
prefix={<SearchIcon className="h-4 w-4 text-th-fgd-3" />}
|
||||
ref={callbackRef}
|
||||
type="text"
|
||||
value={searchString}
|
||||
/>
|
||||
</div>
|
||||
{searchString.length > 0 ? (
|
||||
<div className="pt-1.5">
|
||||
{filteredMarkets.length > 0 ? (
|
||||
filteredMarkets.map((mkt) => (
|
||||
<MarketNavItem
|
||||
buttonRef={buttonRef}
|
||||
onClick={() => setSearchString('')}
|
||||
market={mkt}
|
||||
key={mkt.name}
|
||||
/>
|
||||
))
|
||||
) : (
|
||||
<p className="mb-0 text-center">{t('no-markets')}</p>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div className="">
|
||||
<div className="flex justify-between py-1.5">
|
||||
<h4 className="text-xs font-normal">{t('futures')}</h4>
|
||||
<p className="mb-0 hidden text-xs text-th-fgd-3 sm:block">
|
||||
{t('favorite')}
|
||||
</p>
|
||||
</div>
|
||||
{perpMarketsInfo.map((mkt) => (
|
||||
<MarketNavItem
|
||||
buttonRef={buttonRef}
|
||||
onClick={() => setSearchString('')}
|
||||
market={mkt}
|
||||
key={mkt.name}
|
||||
/>
|
||||
))}
|
||||
<div className="flex justify-between py-1.5">
|
||||
<h4 className="text-xs font-normal">{t('spot')}</h4>
|
||||
<p className="mb-0 hidden text-xs text-th-fgd-3 sm:block">
|
||||
{t('favorite')}
|
||||
</p>
|
||||
</div>
|
||||
{spotMarketsInfo.map((mkt) => (
|
||||
<MarketNavItem
|
||||
buttonRef={buttonRef}
|
||||
onClick={() => setSearchString('')}
|
||||
market={mkt}
|
||||
key={mkt.name}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</Popover.Panel>
|
||||
</Transition>
|
||||
</div>
|
||||
)}
|
||||
</Popover>
|
||||
)
|
||||
}
|
||||
|
||||
export default SwitchMarketDropdown
|
|
@ -0,0 +1,829 @@
|
|||
import { useEffect, useRef, useState, useMemo } from 'react'
|
||||
import { useTheme } from 'next-themes'
|
||||
import {
|
||||
widget,
|
||||
ChartingLibraryWidgetOptions,
|
||||
IChartingLibraryWidget,
|
||||
ResolutionString,
|
||||
} from '../public/charting_library'
|
||||
import { CHART_DATA_FEED } from '../utils/chartDataConnector'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from './TradePageGrid'
|
||||
import { Order, Market } from '@project-serum/serum/lib/market'
|
||||
import { PerpOrder, PerpMarket } from '@blockworks-foundation/mango-client'
|
||||
import { notify } from '../utils/notifications'
|
||||
import { sleep, formatUsdValue, usdFormatter, roundPerpSize } from '../utils'
|
||||
import { PerpTriggerOrder } from '../@types/types'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import useLocalStorageState from '../hooks/useLocalStorageState'
|
||||
import { useWallet, Wallet } from '@solana/wallet-adapter-react'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
export interface ChartContainerProps {
|
||||
container: ChartingLibraryWidgetOptions['container']
|
||||
symbol: ChartingLibraryWidgetOptions['symbol']
|
||||
interval: ChartingLibraryWidgetOptions['interval']
|
||||
datafeedUrl: string
|
||||
libraryPath: ChartingLibraryWidgetOptions['library_path']
|
||||
chartsStorageUrl: ChartingLibraryWidgetOptions['charts_storage_url']
|
||||
chartsStorageApiVersion: ChartingLibraryWidgetOptions['charts_storage_api_version']
|
||||
clientId: ChartingLibraryWidgetOptions['client_id']
|
||||
userId: ChartingLibraryWidgetOptions['user_id']
|
||||
fullscreen: ChartingLibraryWidgetOptions['fullscreen']
|
||||
autosize: ChartingLibraryWidgetOptions['autosize']
|
||||
studiesOverrides: ChartingLibraryWidgetOptions['studies_overrides']
|
||||
theme: string
|
||||
}
|
||||
|
||||
const SHOW_ORDER_LINES_KEY = 'showOrderLines-0.1'
|
||||
const TRADE_EXECUTION_LIMIT = 100
|
||||
|
||||
const TVChartContainer = () => {
|
||||
const { t } = useTranslation(['common', 'tv-chart'])
|
||||
const { theme } = useTheme()
|
||||
const { width } = useViewport()
|
||||
const { wallet, publicKey, connected } = useWallet()
|
||||
const [chartReady, setChartReady] = useState(false)
|
||||
const [showOrderLinesLocalStorage, toggleShowOrderLinesLocalStorage] =
|
||||
useLocalStorageState(SHOW_ORDER_LINES_KEY, true)
|
||||
const [showOrderLines, toggleShowOrderLines] = useState(
|
||||
showOrderLinesLocalStorage
|
||||
)
|
||||
const [showTradeExecutions, toggleShowTradeExecutions] = useState(false)
|
||||
|
||||
const setMangoStore = useMangoStore.getState().set
|
||||
const mangoAccount = useMangoStore.getState().selectedMangoAccount.current
|
||||
const selectedMarketConfig = useMangoStore((s) => s.selectedMarket.config)
|
||||
const actions = useMangoStore((s) => s.actions)
|
||||
const isMobile = width ? width < breakpoints.sm : false
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
const selectedMarketName = selectedMarketConfig.name
|
||||
const tradeExecutions = useMangoStore((s) => s.tradingView.tradeExecutions)
|
||||
const tradeHistoryAndLiquidations = useMangoStore(
|
||||
(s) => s.tradeHistory.parsed
|
||||
)
|
||||
const tradeHistory = tradeHistoryAndLiquidations.filter(
|
||||
(t) => !('liqor' in t)
|
||||
)
|
||||
const [cachedTradeHistory, setCachedTradeHistory] = useState(tradeHistory)
|
||||
|
||||
// @ts-ignore
|
||||
const defaultProps: ChartContainerProps = useMemo(
|
||||
() => ({
|
||||
symbol: selectedMarketConfig.name,
|
||||
interval: '60' as ResolutionString,
|
||||
theme: 'Dark',
|
||||
container: 'tv_chart_container',
|
||||
datafeedUrl: CHART_DATA_FEED,
|
||||
libraryPath: '/charting_library/',
|
||||
chartsStorageUrl: 'https://trading-view-backend.herokuapp.com',
|
||||
chartsStorageApiVersion: '1.1',
|
||||
clientId: 'mango.markets',
|
||||
userId: '',
|
||||
fullscreen: false,
|
||||
autosize: true,
|
||||
studiesOverrides: {
|
||||
'volume.volume.color.0': theme === 'Mango' ? '#E54033' : '#CC2929',
|
||||
'volume.volume.color.1': theme === 'Mango' ? '#AFD803' : '#5EBF4D',
|
||||
'volume.precision': 4,
|
||||
},
|
||||
}),
|
||||
[selectedMarketConfig.name, theme]
|
||||
)
|
||||
|
||||
const tvWidgetRef = useRef<IChartingLibraryWidget | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (showOrderLines !== showOrderLinesLocalStorage) {
|
||||
toggleShowOrderLinesLocalStorage(showOrderLines)
|
||||
}
|
||||
}, [showOrderLines])
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
chartReady &&
|
||||
tvWidgetRef.current &&
|
||||
selectedMarketConfig.name !== tvWidgetRef.current?.activeChart()?.symbol()
|
||||
) {
|
||||
tvWidgetRef.current.setSymbol(
|
||||
selectedMarketConfig.name,
|
||||
tvWidgetRef.current.activeChart().resolution(),
|
||||
() => {
|
||||
if (showOrderLines) {
|
||||
const openOrders =
|
||||
useMangoStore.getState().selectedMangoAccount.openOrders
|
||||
deleteLines()
|
||||
drawLinesForMarket(openOrders)
|
||||
}
|
||||
if (showTradeExecutions) {
|
||||
setCachedTradeHistory(tradeHistory)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}, [selectedMarketConfig.name, chartReady])
|
||||
|
||||
let chartStyleOverrides = {
|
||||
'paneProperties.background': 'rgba(0,0,0,0)',
|
||||
'paneProperties.backgroundType': 'solid',
|
||||
'paneProperties.legendProperties.showBackground': false,
|
||||
'paneProperties.vertGridProperties.color': 'rgba(0,0,0,0)',
|
||||
'paneProperties.horzGridProperties.color': 'rgba(0,0,0,0)',
|
||||
'paneProperties.legendProperties.showStudyTitles': false,
|
||||
'scalesProperties.showStudyLastValue': false,
|
||||
}
|
||||
|
||||
const mainSeriesProperties = [
|
||||
'candleStyle',
|
||||
'hollowCandleStyle',
|
||||
'haStyle',
|
||||
'barStyle',
|
||||
]
|
||||
mainSeriesProperties.forEach((prop) => {
|
||||
chartStyleOverrides = {
|
||||
...chartStyleOverrides,
|
||||
[`mainSeriesProperties.${prop}.barColorsOnPrevClose`]: true,
|
||||
[`mainSeriesProperties.${prop}.drawWick`]: true,
|
||||
[`mainSeriesProperties.${prop}.drawBorder`]: true,
|
||||
[`mainSeriesProperties.${prop}.upColor`]:
|
||||
theme === 'Mango' ? '#AFD803' : '#5EBF4D',
|
||||
[`mainSeriesProperties.${prop}.downColor`]:
|
||||
theme === 'Mango' ? '#E54033' : '#CC2929',
|
||||
[`mainSeriesProperties.${prop}.borderColor`]:
|
||||
theme === 'Mango' ? '#AFD803' : '#5EBF4D',
|
||||
[`mainSeriesProperties.${prop}.borderUpColor`]:
|
||||
theme === 'Mango' ? '#AFD803' : '#5EBF4D',
|
||||
[`mainSeriesProperties.${prop}.borderDownColor`]:
|
||||
theme === 'Mango' ? '#E54033' : '#CC2929',
|
||||
[`mainSeriesProperties.${prop}.wickUpColor`]:
|
||||
theme === 'Mango' ? '#AFD803' : '#5EBF4D',
|
||||
[`mainSeriesProperties.${prop}.wickDownColor`]:
|
||||
theme === 'Mango' ? '#E54033' : '#CC2929',
|
||||
}
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
const widgetOptions: ChartingLibraryWidgetOptions = {
|
||||
// debug: true,
|
||||
symbol: selectedMarketConfig.name,
|
||||
// BEWARE: no trailing slash is expected in feed URL
|
||||
// tslint:disable-next-line:no-any
|
||||
datafeed: new (window as any).Datafeeds.UDFCompatibleDatafeed(
|
||||
defaultProps.datafeedUrl
|
||||
),
|
||||
interval:
|
||||
defaultProps.interval as ChartingLibraryWidgetOptions['interval'],
|
||||
container:
|
||||
defaultProps.container as ChartingLibraryWidgetOptions['container'],
|
||||
library_path: defaultProps.libraryPath as string,
|
||||
locale: 'en',
|
||||
enabled_features: [
|
||||
'hide_left_toolbar_by_default',
|
||||
publicKey ? 'study_templates' : '',
|
||||
],
|
||||
disabled_features: [
|
||||
'use_localstorage_for_settings',
|
||||
'timeframes_toolbar',
|
||||
isMobile ? 'left_toolbar' : '',
|
||||
'show_logo_on_all_charts',
|
||||
'caption_buttons_text_if_possible',
|
||||
'header_settings',
|
||||
// 'header_chart_type',
|
||||
'header_compare',
|
||||
'compare_symbol',
|
||||
'header_screenshot',
|
||||
// 'header_widget_dom_node',
|
||||
// 'header_widget',
|
||||
!publicKey ? 'header_saveload' : '',
|
||||
'header_undo_redo',
|
||||
'header_interval_dialog_button',
|
||||
'show_interval_dialog_on_key_press',
|
||||
'header_symbol_search',
|
||||
],
|
||||
// load_last_chart: true,
|
||||
charts_storage_url: defaultProps.chartsStorageUrl,
|
||||
charts_storage_api_version: defaultProps.chartsStorageApiVersion,
|
||||
client_id: defaultProps.clientId,
|
||||
user_id: publicKey ? publicKey.toString() : defaultProps.userId,
|
||||
fullscreen: defaultProps.fullscreen,
|
||||
autosize: defaultProps.autosize,
|
||||
studies_overrides: defaultProps.studiesOverrides,
|
||||
theme: theme === 'Light' ? 'Light' : 'Dark',
|
||||
custom_css_url: '/tradingview-chart.css',
|
||||
loading_screen: {
|
||||
backgroundColor:
|
||||
theme === 'Dark' ? '#101012' : theme === 'Light' ? '#fff' : '#141026',
|
||||
},
|
||||
overrides: {
|
||||
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||
|
||||
...chartStyleOverrides,
|
||||
},
|
||||
}
|
||||
|
||||
const tvWidget = new widget(widgetOptions)
|
||||
tvWidgetRef.current = tvWidget
|
||||
|
||||
// Create order lines and trade executions buttons
|
||||
tvWidgetRef.current.onChartReady(function () {
|
||||
createOLButton()
|
||||
createTEButton()
|
||||
setChartReady(true)
|
||||
})
|
||||
//eslint-disable-next-line
|
||||
}, [theme, isMobile, publicKey])
|
||||
|
||||
const createOLButton = () => {
|
||||
const button = tvWidgetRef?.current?.createButton()
|
||||
if (!button) {
|
||||
return
|
||||
}
|
||||
button.textContent = 'OL'
|
||||
if (showOrderLinesLocalStorage) {
|
||||
button.style.color =
|
||||
theme === 'Dark' || theme === 'Mango'
|
||||
? 'rgb(242, 201, 76)'
|
||||
: 'rgb(255, 156, 36)'
|
||||
} else {
|
||||
button.style.color =
|
||||
theme === 'Dark' || theme === 'Mango'
|
||||
? 'rgb(138, 138, 138)'
|
||||
: 'rgb(138, 138, 138)'
|
||||
}
|
||||
button.setAttribute('title', t('tv-chart:toggle-order-line'))
|
||||
button.addEventListener('click', toggleOrderLines)
|
||||
}
|
||||
const createTEButton = () => {
|
||||
const button = tvWidgetRef?.current?.createButton()
|
||||
if (!button) {
|
||||
return
|
||||
}
|
||||
button.textContent = 'TE'
|
||||
if (showTradeExecutions) {
|
||||
button.style.color =
|
||||
theme === 'Dark' || theme === 'Mango'
|
||||
? 'rgb(242, 201, 76)'
|
||||
: 'rgb(255, 156, 36)'
|
||||
} else {
|
||||
button.style.color =
|
||||
theme === 'Dark' || theme === 'Mango'
|
||||
? 'rgb(138, 138, 138)'
|
||||
: 'rgb(138, 138, 138)'
|
||||
}
|
||||
button.setAttribute('title', t('tv-chart:toggle-trade-executions'))
|
||||
button.addEventListener('click', toggleTradeExecutions)
|
||||
}
|
||||
|
||||
function cycleShowTradeExecutions() {
|
||||
toggleShowTradeExecutions((prevState) => !prevState)
|
||||
sleep(1000).then(() => {
|
||||
toggleShowTradeExecutions((prevState) => !prevState)
|
||||
})
|
||||
}
|
||||
|
||||
function toggleTradeExecutions() {
|
||||
toggleShowTradeExecutions((prevState) => !prevState)
|
||||
if (
|
||||
this.style.color === 'rgb(255, 156, 36)' ||
|
||||
this.style.color === 'rgb(242, 201, 76)'
|
||||
) {
|
||||
this.style.color =
|
||||
theme === 'Dark' || theme === 'Mango'
|
||||
? 'rgb(138, 138, 138)'
|
||||
: 'rgb(138, 138, 138)'
|
||||
} else {
|
||||
this.style.color =
|
||||
theme === 'Dark' || theme === 'Mango'
|
||||
? 'rgb(242, 201, 76)'
|
||||
: 'rgb(255, 156, 36)'
|
||||
}
|
||||
}
|
||||
|
||||
function toggleOrderLines() {
|
||||
toggleShowOrderLines((prevState) => !prevState)
|
||||
if (
|
||||
this.style.color === 'rgb(255, 156, 36)' ||
|
||||
this.style.color === 'rgb(242, 201, 76)'
|
||||
) {
|
||||
this.style.color =
|
||||
theme === 'Dark' || theme === 'Mango'
|
||||
? 'rgb(138, 138, 138)'
|
||||
: 'rgb(138, 138, 138)'
|
||||
} else {
|
||||
this.style.color =
|
||||
theme === 'Dark' || theme === 'Mango'
|
||||
? 'rgb(242, 201, 76)'
|
||||
: 'rgb(255, 156, 36)'
|
||||
}
|
||||
}
|
||||
|
||||
const handleCancelOrder = async (
|
||||
order: Order | PerpOrder | PerpTriggerOrder,
|
||||
market: Market | PerpMarket,
|
||||
wallet: Wallet
|
||||
) => {
|
||||
const selectedMangoGroup =
|
||||
useMangoStore.getState().selectedMangoGroup.current
|
||||
const selectedMangoAccount =
|
||||
useMangoStore.getState().selectedMangoAccount.current
|
||||
const mangoClient = useMangoStore.getState().connection.client
|
||||
let txid
|
||||
try {
|
||||
if (!selectedMangoGroup || !selectedMangoAccount) return
|
||||
if (market instanceof Market) {
|
||||
txid = await mangoClient.cancelSpotOrder(
|
||||
selectedMangoGroup,
|
||||
selectedMangoAccount,
|
||||
wallet?.adapter,
|
||||
// @ts-ignore
|
||||
market,
|
||||
order as Order
|
||||
)
|
||||
} else if (market instanceof PerpMarket) {
|
||||
if (order['triggerCondition']) {
|
||||
txid = await mangoClient.removeAdvancedOrder(
|
||||
selectedMangoGroup,
|
||||
selectedMangoAccount,
|
||||
wallet?.adapter,
|
||||
(order as PerpTriggerOrder).orderId
|
||||
)
|
||||
} else {
|
||||
txid = await mangoClient.cancelPerpOrder(
|
||||
selectedMangoGroup,
|
||||
selectedMangoAccount,
|
||||
wallet?.adapter,
|
||||
market,
|
||||
order as PerpOrder,
|
||||
false
|
||||
)
|
||||
}
|
||||
}
|
||||
notify({ title: t('cancel-success'), txid })
|
||||
} catch (e) {
|
||||
notify({
|
||||
title: t('cancel-error'),
|
||||
description: e.message,
|
||||
txid: e.txid,
|
||||
type: 'error',
|
||||
})
|
||||
console.log('error', `${e}`)
|
||||
} finally {
|
||||
actions.reloadMangoAccount()
|
||||
actions.reloadOrders()
|
||||
}
|
||||
}
|
||||
|
||||
const handleModifyOrder = async (
|
||||
order: Order | PerpOrder,
|
||||
market: Market | PerpMarket,
|
||||
price: number,
|
||||
wallet: Wallet
|
||||
) => {
|
||||
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
|
||||
const marketConfig = useMangoStore.getState().selectedMarket.config
|
||||
const askInfo =
|
||||
useMangoStore.getState().accountInfos[marketConfig.asksKey.toString()]
|
||||
const bidInfo =
|
||||
useMangoStore.getState().accountInfos[marketConfig.bidsKey.toString()]
|
||||
const referrerPk = useMangoStore.getState().referrerPk
|
||||
|
||||
if (!wallet || !mangoGroup || !mangoAccount || !market) return
|
||||
|
||||
try {
|
||||
const orderPrice = price
|
||||
|
||||
if (!orderPrice) {
|
||||
notify({
|
||||
title: t('price-unavailable'),
|
||||
description: t('try-again'),
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
const orderType = 'limit'
|
||||
let txid
|
||||
if (market instanceof Market) {
|
||||
txid = await mangoClient.modifySpotOrder(
|
||||
mangoGroup,
|
||||
mangoAccount,
|
||||
mangoGroup.mangoCache,
|
||||
// @ts-ignore
|
||||
market,
|
||||
wallet?.adapter,
|
||||
order as Order,
|
||||
order.side,
|
||||
orderPrice,
|
||||
order.size,
|
||||
orderType
|
||||
)
|
||||
} else {
|
||||
txid = await mangoClient.modifyPerpOrder(
|
||||
mangoGroup,
|
||||
mangoAccount,
|
||||
mangoGroup.mangoCache,
|
||||
market,
|
||||
wallet?.adapter,
|
||||
order as PerpOrder,
|
||||
order.side,
|
||||
orderPrice,
|
||||
order.size,
|
||||
orderType,
|
||||
0,
|
||||
order.side === 'buy' ? askInfo : bidInfo,
|
||||
false,
|
||||
referrerPk ? referrerPk : undefined
|
||||
)
|
||||
}
|
||||
|
||||
notify({ title: t('successfully-placed'), txid })
|
||||
} catch (e) {
|
||||
notify({
|
||||
title: t('order-error'),
|
||||
description: e.message,
|
||||
txid: e.txid,
|
||||
type: 'error',
|
||||
})
|
||||
} finally {
|
||||
sleep(1000).then(() => {
|
||||
actions.reloadMangoAccount()
|
||||
actions.reloadOrders()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function drawLine(order, market) {
|
||||
const orderSizeUi = roundPerpSize(order.size, market.config.baseSymbol)
|
||||
if (!tvWidgetRef?.current?.chart() || !wallet) return
|
||||
return tvWidgetRef.current
|
||||
.chart()
|
||||
.createOrderLine({ disableUndo: false })
|
||||
.onMove(function () {
|
||||
const currentOrderPrice = order.price
|
||||
const updatedOrderPrice = this.getPrice()
|
||||
const selectedMarketPrice =
|
||||
useMangoStore.getState().selectedMarket.markPrice
|
||||
if (!order.perpTrigger?.clientOrderId) {
|
||||
if (
|
||||
(order.side === 'buy' &&
|
||||
updatedOrderPrice > 1.05 * selectedMarketPrice) ||
|
||||
(order.side === 'sell' &&
|
||||
updatedOrderPrice < 0.95 * selectedMarketPrice)
|
||||
) {
|
||||
tvWidgetRef.current?.showNoticeDialog({
|
||||
title: t('tv-chart:outside-range'),
|
||||
body:
|
||||
t('tv-chart:slippage-warning', {
|
||||
updatedOrderPrice: formatUsdValue(updatedOrderPrice),
|
||||
aboveBelow: order.side == 'buy' ? t('above') : t('below'),
|
||||
selectedMarketPrice: formatUsdValue(selectedMarketPrice),
|
||||
}) +
|
||||
'<p><p>' +
|
||||
t('tv-chart:slippage-accept'),
|
||||
callback: () => {
|
||||
this.setPrice(currentOrderPrice)
|
||||
},
|
||||
})
|
||||
} else {
|
||||
tvWidgetRef.current?.showConfirmDialog({
|
||||
title: t('tv-chart:modify-order'),
|
||||
body: t('tv-chart:modify-order-details', {
|
||||
orderSize: orderSizeUi,
|
||||
baseSymbol: market.config.baseSymbol,
|
||||
orderSide: t(order.side),
|
||||
currentOrderPrice: currentOrderPrice,
|
||||
updatedOrderPrice: updatedOrderPrice,
|
||||
}),
|
||||
callback: (res) => {
|
||||
if (res) {
|
||||
handleModifyOrder(
|
||||
order,
|
||||
market.account,
|
||||
updatedOrderPrice,
|
||||
wallet
|
||||
)
|
||||
} else {
|
||||
this.setPrice(currentOrderPrice)
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
} else {
|
||||
tvWidgetRef.current?.showNoticeDialog({
|
||||
title: t('tv-chart:advanced-order'),
|
||||
body: t('tv-chart:advanced-order-details'),
|
||||
callback: () => {
|
||||
this.setPrice(currentOrderPrice)
|
||||
},
|
||||
})
|
||||
}
|
||||
})
|
||||
.onCancel(function () {
|
||||
tvWidgetRef.current?.showConfirmDialog({
|
||||
title: t('tv-chart:cancel-order'),
|
||||
body: t('tv-chart:cancel-order-details', {
|
||||
orderSize: orderSizeUi,
|
||||
baseSymbol: market.config.baseSymbol,
|
||||
orderSide: t(order.side),
|
||||
orderPrice: order.price,
|
||||
}),
|
||||
callback: (res) => {
|
||||
if (res) {
|
||||
handleCancelOrder(order, market.account, wallet)
|
||||
}
|
||||
},
|
||||
})
|
||||
})
|
||||
.setPrice(order.price)
|
||||
.setQuantity(orderSizeUi)
|
||||
.setText(getLineText(order, market))
|
||||
.setTooltip(
|
||||
order.perpTrigger?.clientOrderId
|
||||
? `${order.orderType} Order #: ${order.orderId}`
|
||||
: `Order #: ${order.orderId}`
|
||||
)
|
||||
.setBodyTextColor(
|
||||
theme === 'Dark' ? '#F2C94C' : theme === 'Light' ? '#FF9C24' : '#F2C94C'
|
||||
)
|
||||
.setQuantityTextColor(
|
||||
theme === 'Dark' ? '#F2C94C' : theme === 'Light' ? '#FF9C24' : '#F2C94C'
|
||||
)
|
||||
.setCancelButtonIconColor(
|
||||
theme === 'Dark' ? '#F2C94C' : theme === 'Light' ? '#FF9C24' : '#F2C94C'
|
||||
)
|
||||
.setBodyBorderColor(
|
||||
order.perpTrigger?.clientOrderId
|
||||
? '#FF9C24'
|
||||
: order.side == 'buy'
|
||||
? '#4BA53B'
|
||||
: '#AA2222'
|
||||
)
|
||||
.setQuantityBorderColor(
|
||||
order.perpTrigger?.clientOrderId
|
||||
? '#FF9C24'
|
||||
: order.side == 'buy'
|
||||
? '#4BA53B'
|
||||
: '#AA2222'
|
||||
)
|
||||
.setCancelButtonBorderColor(
|
||||
order.perpTrigger?.clientOrderId
|
||||
? '#FF9C24'
|
||||
: order.side == 'buy'
|
||||
? '#4BA53B'
|
||||
: '#AA2222'
|
||||
)
|
||||
.setBodyBackgroundColor(
|
||||
theme === 'Dark' ? '#1B1B1F' : theme === 'Light' ? '#fff' : '#1D1832'
|
||||
)
|
||||
.setQuantityBackgroundColor(
|
||||
theme === 'Dark' ? '#1B1B1F' : theme === 'Light' ? '#fff' : '#1D1832'
|
||||
)
|
||||
.setCancelButtonBackgroundColor(
|
||||
theme === 'Dark' ? '#1B1B1F' : theme === 'Light' ? '#fff' : '#1D1832'
|
||||
)
|
||||
.setLineColor(
|
||||
order.perpTrigger?.clientOrderId
|
||||
? '#FF9C24'
|
||||
: order.side == 'buy'
|
||||
? '#4BA53B'
|
||||
: '#AA2222'
|
||||
)
|
||||
.setLineLength(3)
|
||||
.setLineWidth(2)
|
||||
.setLineStyle(1)
|
||||
}
|
||||
|
||||
function getLineText(order, market) {
|
||||
const orderSideTranslated = t(order.side)
|
||||
if (order.perpTrigger?.clientOrderId) {
|
||||
const triggerPrice =
|
||||
order.perpTrigger.triggerPrice *
|
||||
Math.pow(10, market.config.baseDecimals - market.config.quoteDecimals)
|
||||
const orderTypeTranslated = t(order.orderType)
|
||||
const triggerConditionTranslated = t(order.perpTrigger.triggerCondition)
|
||||
if (order.side === 'buy') {
|
||||
if (order.perpTrigger.triggerCondition === 'above') {
|
||||
return (
|
||||
(order.orderType === 'market' ? t('stop-loss') : t('stop-limit')) +
|
||||
t('tv-chart:order-details', {
|
||||
orderType: orderTypeTranslated,
|
||||
orderSide: orderSideTranslated,
|
||||
triggerCondition: triggerConditionTranslated,
|
||||
triggerPrice: usdFormatter(triggerPrice),
|
||||
})
|
||||
)
|
||||
} else {
|
||||
return (
|
||||
t('take-profit') +
|
||||
t('tv-chart:order-details', {
|
||||
orderType: orderTypeTranslated,
|
||||
orderSide: orderSideTranslated,
|
||||
triggerCondition: triggerConditionTranslated,
|
||||
triggerPrice: usdFormatter(triggerPrice),
|
||||
})
|
||||
)
|
||||
}
|
||||
} else {
|
||||
if (order.perpTrigger.triggerCondition === 'below') {
|
||||
return (
|
||||
(order.orderType === 'market' ? t('stop-loss') : t('stop-limit')) +
|
||||
t('tv-chart:order-details', {
|
||||
orderType: orderTypeTranslated,
|
||||
orderSide: orderSideTranslated,
|
||||
triggerCondition: triggerConditionTranslated,
|
||||
triggerPrice: usdFormatter(triggerPrice),
|
||||
})
|
||||
)
|
||||
} else {
|
||||
return (
|
||||
t('take-profit') +
|
||||
t('tv-chart:order-details', {
|
||||
orderType: orderTypeTranslated,
|
||||
orderSide: orderSideTranslated,
|
||||
triggerCondition: triggerConditionTranslated,
|
||||
triggerPrice: usdFormatter(triggerPrice),
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return `${orderSideTranslated} ${market.config.baseSymbol}`.toUpperCase()
|
||||
}
|
||||
}
|
||||
|
||||
const drawLinesForMarket = (openOrders) => {
|
||||
const newOrderLines = new Map()
|
||||
if (openOrders?.length) {
|
||||
for (const { order, market } of openOrders) {
|
||||
if (market.config.name == selectedMarketName) {
|
||||
newOrderLines.set(order.orderId.toString(), drawLine(order, market))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setMangoStore((state) => {
|
||||
state.tradingView.orderLines = newOrderLines
|
||||
})
|
||||
}
|
||||
|
||||
const deleteLines = () => {
|
||||
const orderLines = useMangoStore.getState().tradingView.orderLines
|
||||
|
||||
if (orderLines.size > 0) {
|
||||
orderLines?.forEach((value, key) => {
|
||||
orderLines.get(key)?.remove()
|
||||
})
|
||||
|
||||
setMangoStore((state) => {
|
||||
state.tradingView.orderLines = new Map()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// delete order lines if showOrderLines button is toggled
|
||||
useEffect(() => {
|
||||
if (!showOrderLines) {
|
||||
deleteLines()
|
||||
}
|
||||
}, [showOrderLines])
|
||||
|
||||
// updated order lines if a user's open orders change
|
||||
useEffect(() => {
|
||||
let subscription
|
||||
if (chartReady && tvWidgetRef?.current) {
|
||||
subscription = useMangoStore.subscribe(
|
||||
(state) => state.selectedMangoAccount.openOrders,
|
||||
(openOrders) => {
|
||||
const orderLines = useMangoStore.getState().tradingView.orderLines
|
||||
tvWidgetRef.current?.onChartReady(() => {
|
||||
let matchingOrderLines = 0
|
||||
let openOrdersForMarket = 0
|
||||
|
||||
for (const [key] of orderLines) {
|
||||
openOrders?.forEach(({ order }) => {
|
||||
if (order.orderId == key) {
|
||||
matchingOrderLines += 1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
openOrders?.forEach(({ market }) => {
|
||||
if (market.config.name == selectedMarketName) {
|
||||
openOrdersForMarket += 1
|
||||
}
|
||||
})
|
||||
|
||||
tvWidgetRef.current?.activeChart().dataReady(() => {
|
||||
if (
|
||||
(showOrderLines &&
|
||||
matchingOrderLines !== openOrdersForMarket) ||
|
||||
orderLines?.size != matchingOrderLines
|
||||
) {
|
||||
deleteLines()
|
||||
drawLinesForMarket(openOrders)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
return subscription
|
||||
}, [chartReady, showOrderLines, selectedMarketName])
|
||||
|
||||
const drawTradeExecutions = (trades) => {
|
||||
const newTradeExecutions = new Map()
|
||||
trades
|
||||
.filter((trade) => {
|
||||
return trade.marketName === selectedMarketName
|
||||
})
|
||||
.slice(0, TRADE_EXECUTION_LIMIT)
|
||||
.forEach((trade) => {
|
||||
try {
|
||||
const arrowID = tvWidgetRef
|
||||
.current!.chart()
|
||||
.createExecutionShape()
|
||||
.setTime(dayjs(trade.loadTimestamp).unix())
|
||||
.setDirection(trade.side)
|
||||
.setArrowHeight(6)
|
||||
.setArrowColor(
|
||||
trade.side === 'buy'
|
||||
? theme === 'Mango'
|
||||
? '#AFD803'
|
||||
: '#5EBF4D'
|
||||
: theme === 'Mango'
|
||||
? '#E54033'
|
||||
: '#CC2929'
|
||||
)
|
||||
if (arrowID) {
|
||||
try {
|
||||
newTradeExecutions.set(
|
||||
`${trade.seqNum}${trade.marketName}`,
|
||||
arrowID
|
||||
)
|
||||
} catch (error) {
|
||||
console.log('couldnt set newTradeExecution')
|
||||
}
|
||||
} else {
|
||||
console.log(
|
||||
`Could not create execution shape for trade ${trade.seqNum}${trade.marketName}`
|
||||
)
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`could not draw arrow: ${error}`)
|
||||
}
|
||||
})
|
||||
return newTradeExecutions
|
||||
}
|
||||
|
||||
const removeTradeExecutions = (tradeExecutions) => {
|
||||
if (chartReady && tvWidgetRef?.current) {
|
||||
for (const val of tradeExecutions.values()) {
|
||||
try {
|
||||
val.remove()
|
||||
} catch (error) {
|
||||
console.log(
|
||||
`arrow ${val.seqNum}${val.marketName} could not be removed`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
setMangoStore((s) => {
|
||||
s.tradingView.tradeExecutions = new Map()
|
||||
})
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
showTradeExecutions ? cycleShowTradeExecutions() : null
|
||||
}, [selectedMarketName, mangoAccount?.publicKey])
|
||||
|
||||
useEffect(() => {
|
||||
if (tvWidgetRef && tvWidgetRef.current && chartReady) {
|
||||
setCachedTradeHistory(tradeHistory)
|
||||
}
|
||||
}, [connected, showTradeExecutions])
|
||||
|
||||
useEffect(() => {
|
||||
if (cachedTradeHistory.length !== tradeHistory.length) {
|
||||
setCachedTradeHistory(tradeHistory)
|
||||
}
|
||||
}, [mangoAccount?.publicKey, tradeHistory])
|
||||
|
||||
useEffect(() => {
|
||||
removeTradeExecutions(tradeExecutions)
|
||||
if (
|
||||
showTradeExecutions &&
|
||||
tvWidgetRef &&
|
||||
tvWidgetRef.current &&
|
||||
chartReady
|
||||
) {
|
||||
setMangoStore((s) => {
|
||||
s.tradingView.tradeExecutions = drawTradeExecutions(cachedTradeHistory)
|
||||
})
|
||||
}
|
||||
}, [cachedTradeHistory, selectedMarketName])
|
||||
|
||||
return (
|
||||
<div id={defaultProps.container as string} className="tradingview-chart" />
|
||||
)
|
||||
}
|
||||
|
||||
export default TVChartContainer
|
|
@ -0,0 +1,44 @@
|
|||
import * as MonoIcons from './icons'
|
||||
import { QuestionMarkCircleIcon } from '@heroicons/react/solid'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
|
||||
const TabButtons = ({
|
||||
tabs,
|
||||
activeTab,
|
||||
showSymbolIcon,
|
||||
onClick,
|
||||
}: {
|
||||
tabs: Array<{ label: string; key: string }>
|
||||
activeTab: string
|
||||
showSymbolIcon?: boolean
|
||||
onClick: (x) => void
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const renderSymbolIcon = (s) => {
|
||||
const iconName = `${s.slice(0, 1)}${s.slice(1, 4).toLowerCase()}MonoIcon`
|
||||
const SymbolIcon = MonoIcons[iconName] || QuestionMarkCircleIcon
|
||||
return <SymbolIcon className="mr-1.5 h-3.5 w-auto" />
|
||||
}
|
||||
return (
|
||||
<div className="flex flex-wrap">
|
||||
{tabs.map((tab) => (
|
||||
<div
|
||||
className={`default-transition mb-2 mr-2 flex cursor-pointer items-center rounded-full px-3 py-2 font-bold leading-none ring-1 ring-inset ${
|
||||
tab.key === activeTab
|
||||
? `text-th-primary ring-th-primary`
|
||||
: `text-th-fgd-4 ring-th-fgd-4 hover:text-th-fgd-3 hover:ring-th-fgd-3`
|
||||
} ${showSymbolIcon ? 'uppercase' : ''}
|
||||
`}
|
||||
onClick={() => onClick(tab.key)}
|
||||
role="button"
|
||||
key={tab.key}
|
||||
>
|
||||
{showSymbolIcon ? renderSymbolIcon(tab.label) : null}
|
||||
{t(tab.label.toLowerCase().replace(/\s/g, '-'))}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default TabButtons
|
|
@ -0,0 +1,117 @@
|
|||
import { Disclosure, Transition } from '@headlessui/react'
|
||||
import { ChevronDownIcon } from '@heroicons/react/solid'
|
||||
import { Fragment, ReactNode } from 'react'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
export const Table = ({ children }) => (
|
||||
<table className="min-w-full">{children}</table>
|
||||
)
|
||||
|
||||
export const TrHead = ({ children }) => (
|
||||
<tr className="text-xxs leading-tight text-th-fgd-2">{children}</tr>
|
||||
)
|
||||
|
||||
export const Th = ({ children }) => (
|
||||
<th className="px-4 pb-2 text-left font-normal" scope="col">
|
||||
{children}
|
||||
</th>
|
||||
)
|
||||
|
||||
export const TrBody = ({ children, className = '' }) => (
|
||||
<tr className={`border-b border-th-bkg-3 ${className}`}>{children}</tr>
|
||||
)
|
||||
|
||||
export const Td = ({
|
||||
children,
|
||||
className,
|
||||
}: {
|
||||
children: ReactNode
|
||||
className?: string
|
||||
}) => (
|
||||
<td className={`h-14 px-4 text-xs text-th-fgd-2 ${className}`}>{children}</td>
|
||||
)
|
||||
|
||||
type ExpandableRowProps = {
|
||||
buttonTemplate: React.ReactNode
|
||||
panelTemplate: React.ReactNode
|
||||
rounded?: boolean
|
||||
}
|
||||
|
||||
export const ExpandableRow = ({
|
||||
buttonTemplate,
|
||||
panelTemplate,
|
||||
rounded,
|
||||
}: ExpandableRowProps) => {
|
||||
return (
|
||||
<Disclosure>
|
||||
{({ open }) => (
|
||||
<>
|
||||
<Disclosure.Button
|
||||
className={`default-transition flex w-full items-center justify-between border-t border-th-bkg-3 p-4 text-left text-xs font-normal text-th-fgd-1 hover:bg-th-bkg-4 focus:outline-none ${
|
||||
rounded
|
||||
? open
|
||||
? 'rounded-b-none'
|
||||
: 'rounded-md'
|
||||
: 'rounded-none'
|
||||
}`}
|
||||
>
|
||||
{buttonTemplate}
|
||||
<div className="flex items-center justify-end pl-4">
|
||||
<ChevronDownIcon
|
||||
className={`${
|
||||
open ? 'rotate-180 transform' : 'rotate-360 transform'
|
||||
} default-transition h-5 w-5 flex-shrink-0 text-th-fgd-1`}
|
||||
/>
|
||||
</div>
|
||||
</Disclosure.Button>
|
||||
<Transition
|
||||
appear={true}
|
||||
show={open}
|
||||
as={Fragment}
|
||||
enter="transition-all ease-in duration-200"
|
||||
enterFrom="opacity-0"
|
||||
enterTo="opacity-100"
|
||||
leave="transition ease-out"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<Disclosure.Panel>
|
||||
<div className="px-4 pb-4 pt-2 text-xs">{panelTemplate}</div>
|
||||
</Disclosure.Panel>
|
||||
</Transition>
|
||||
</>
|
||||
)}
|
||||
</Disclosure>
|
||||
)
|
||||
}
|
||||
|
||||
type RowProps = {
|
||||
children: React.ReactNode
|
||||
}
|
||||
|
||||
export const Row = ({ children }: RowProps) => {
|
||||
return (
|
||||
<div
|
||||
className={`default-transition w-full rounded-none border-t border-th-bkg-3 p-4 font-normal text-th-fgd-1`}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export const TableDateDisplay = ({
|
||||
date,
|
||||
showSeconds,
|
||||
}: {
|
||||
date: string | number
|
||||
showSeconds?: boolean
|
||||
}) => (
|
||||
<>
|
||||
<p className="mb-0 text-xs text-th-fgd-2">
|
||||
{dayjs(date).format('DD MMM YYYY')}
|
||||
</p>
|
||||
<p className="mb-0 text-xs">
|
||||
{dayjs(date).format(showSeconds ? 'h:mm:ssa' : 'h:mma')}
|
||||
</p>
|
||||
</>
|
||||
)
|
|
@ -0,0 +1,72 @@
|
|||
import { FunctionComponent } from 'react'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
|
||||
interface TabsProps {
|
||||
activeTab: string
|
||||
onChange: (x) => void
|
||||
showCount?: Array<ShowCount>
|
||||
tabs: Array<string>
|
||||
}
|
||||
|
||||
interface ShowCount {
|
||||
tabName: string
|
||||
count: number
|
||||
}
|
||||
|
||||
const Tabs: FunctionComponent<TabsProps> = ({
|
||||
activeTab,
|
||||
onChange,
|
||||
showCount,
|
||||
tabs,
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
|
||||
return (
|
||||
<div className={`relative mb-6 border-b border-th-bkg-4`}>
|
||||
<div
|
||||
className={`default-transition absolute bottom-[-1px] left-0 h-0.5 bg-th-primary`}
|
||||
style={{
|
||||
maxWidth: '176px',
|
||||
transform: `translateX(${
|
||||
tabs.findIndex((v) => v === activeTab) * 100
|
||||
}%)`,
|
||||
width: `${100 / tabs.length}%`,
|
||||
}}
|
||||
/>
|
||||
<nav className="-mb-px flex" aria-label="Tabs">
|
||||
{tabs.map((tabName) => {
|
||||
const tabCount = showCount
|
||||
? showCount.find((e) => e.tabName === tabName)
|
||||
: null
|
||||
return (
|
||||
<a
|
||||
key={tabName}
|
||||
onClick={() => onChange(tabName)}
|
||||
className={`default-transition relative flex cursor-pointer justify-center whitespace-nowrap pb-4 font-bold hover:opacity-100
|
||||
${
|
||||
activeTab === tabName
|
||||
? `text-th-primary`
|
||||
: `text-th-fgd-4 hover:text-th-primary`
|
||||
}
|
||||
`}
|
||||
style={{ width: `${100 / tabs.length}%`, maxWidth: '176px' }}
|
||||
>
|
||||
{t(tabName.toLowerCase().replace(/\s/g, '-'))}
|
||||
{tabCount && tabCount.count > 0 ? (
|
||||
<Count count={tabCount.count} />
|
||||
) : null}
|
||||
</a>
|
||||
)
|
||||
})}
|
||||
</nav>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Tabs
|
||||
|
||||
const Count = ({ count }) => (
|
||||
<span className="ml-2 inline-flex h-5 w-5 items-center justify-center rounded-full bg-th-bkg-4 p-1 text-xxs text-th-fgd-2">
|
||||
{count}
|
||||
</span>
|
||||
)
|
|
@ -0,0 +1,55 @@
|
|||
import React, { ReactNode } from 'react'
|
||||
import Tippy from '@tippyjs/react'
|
||||
import 'tippy.js/animations/scale.css'
|
||||
|
||||
type TooltipProps = {
|
||||
content: ReactNode
|
||||
placement?: any
|
||||
className?: string
|
||||
children?: ReactNode
|
||||
delay?: number
|
||||
}
|
||||
|
||||
const Tooltip = ({
|
||||
children,
|
||||
content,
|
||||
className,
|
||||
placement = 'top',
|
||||
delay = 0,
|
||||
}: TooltipProps) => {
|
||||
return (
|
||||
<Tippy
|
||||
animation="scale"
|
||||
placement={placement}
|
||||
appendTo={() => document.body}
|
||||
maxWidth="20rem"
|
||||
interactive
|
||||
delay={delay}
|
||||
content={
|
||||
content ? (
|
||||
<div
|
||||
className={`rounded bg-th-bkg-3 p-2.5 text-xs leading-4 text-th-fgd-3 shadow-md outline-none focus:outline-none ${className}`}
|
||||
>
|
||||
{content}
|
||||
</div>
|
||||
) : null
|
||||
}
|
||||
>
|
||||
<div className="outline-none focus:outline-none">{children}</div>
|
||||
</Tippy>
|
||||
)
|
||||
}
|
||||
|
||||
const Content = ({ className = '', children }) => {
|
||||
return (
|
||||
<div
|
||||
className={`default-transition inline-block cursor-help border-b border-dashed border-th-fgd-3 border-opacity-20 hover:border-th-bkg-2 ${className}`}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Tooltip.Content = Content
|
||||
|
||||
export default Tooltip
|
|
@ -0,0 +1,312 @@
|
|||
import { FunctionComponent, useEffect, useMemo, useState } from 'react'
|
||||
import { RefreshIcon } from '@heroicons/react/solid'
|
||||
import Input, { Label } from './Input'
|
||||
import Button, { LinkButton } from './Button'
|
||||
import Modal from './Modal'
|
||||
import { ElementTitle } from './styles'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import dayjs from 'dayjs'
|
||||
import DateRangePicker from './DateRangePicker'
|
||||
import useMangoStore from 'stores/useMangoStore'
|
||||
import MultiSelectDropdown from './MultiSelectDropdown'
|
||||
import InlineNotification from './InlineNotification'
|
||||
|
||||
interface TradeHistoryFilterModalProps {
|
||||
filters: any
|
||||
setFilters: any
|
||||
isOpen: boolean
|
||||
onClose: () => void
|
||||
showApiWarning: boolean
|
||||
}
|
||||
|
||||
const TradeHistoryFilterModal: FunctionComponent<
|
||||
TradeHistoryFilterModalProps
|
||||
> = ({ filters, setFilters, isOpen, onClose, showApiWarning }) => {
|
||||
const { t } = useTranslation('common')
|
||||
const [newFilters, setNewFilters] = useState({ ...filters })
|
||||
const [dateFrom, setDateFrom] = useState<Date | null>(null)
|
||||
const [dateTo, setDateTo] = useState<Date | null>(null)
|
||||
const [sizeFrom, setSizeFrom] = useState(filters?.size?.values?.from || '')
|
||||
const [sizeTo, setSizeTo] = useState(filters?.size?.values?.to || '')
|
||||
const [valueFrom, setValueFrom] = useState(filters?.value?.values?.from || '')
|
||||
const [valueTo, setValueTo] = useState(filters?.value?.values?.to || '')
|
||||
const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
|
||||
const markets = useMemo(
|
||||
() =>
|
||||
groupConfig
|
||||
? [...groupConfig.perpMarkets, ...groupConfig.spotMarkets].sort(
|
||||
(a, b) => a.name.localeCompare(b.name)
|
||||
)
|
||||
: [],
|
||||
[groupConfig]
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (filters?.loadTimestamp?.values?.from) {
|
||||
setDateFrom(new Date(filters?.loadTimestamp?.values?.from))
|
||||
}
|
||||
|
||||
if (filters?.loadTimestamp?.values?.to) {
|
||||
setDateTo(new Date(filters?.loadTimestamp?.values?.to))
|
||||
}
|
||||
}, [])
|
||||
|
||||
const handleUpdateFilterButtons = (key: string, value: any) => {
|
||||
const updatedFilters = { ...newFilters }
|
||||
if (Object.prototype.hasOwnProperty.call(updatedFilters, key)) {
|
||||
updatedFilters[key].includes(value)
|
||||
? (updatedFilters[key] = updatedFilters[key].filter((v) => v !== value))
|
||||
: updatedFilters[key].push(value)
|
||||
} else {
|
||||
updatedFilters[key] = [value]
|
||||
}
|
||||
setNewFilters(updatedFilters)
|
||||
}
|
||||
|
||||
const toggleOption = ({ id }) => {
|
||||
setNewFilters((prevSelected) => {
|
||||
const newSelections = prevSelected.marketName
|
||||
? [...prevSelected.marketName]
|
||||
: []
|
||||
if (newSelections.includes(id)) {
|
||||
return {
|
||||
...prevSelected,
|
||||
marketName: newSelections.filter((item) => item != id),
|
||||
}
|
||||
} else {
|
||||
newSelections.push(id)
|
||||
return { ...prevSelected, marketName: newSelections }
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (sizeFrom && sizeTo) {
|
||||
// filter should still work if users get from/to backwards
|
||||
const from = sizeFrom < sizeTo ? sizeFrom : sizeTo
|
||||
const to = sizeTo > sizeFrom ? sizeTo : sizeFrom
|
||||
setNewFilters((prevSelected) => {
|
||||
return {
|
||||
...prevSelected,
|
||||
size: {
|
||||
condition: (size) => size >= from && size <= to,
|
||||
values: { from: from, to: to },
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
}, [sizeFrom, sizeTo])
|
||||
|
||||
useEffect(() => {
|
||||
if (valueFrom && valueTo) {
|
||||
// filter should still work if users get from/to backwards
|
||||
const from = valueFrom < valueTo ? valueFrom : valueTo
|
||||
const to = valueTo > valueFrom ? valueTo : valueFrom
|
||||
setNewFilters((prevSelected) => {
|
||||
return {
|
||||
...prevSelected,
|
||||
value: {
|
||||
condition: (value) => value >= from && value <= to,
|
||||
values: { from: from, to: to },
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
}, [valueFrom, valueTo])
|
||||
|
||||
useEffect(() => {
|
||||
if (dateFrom && dateTo) {
|
||||
const dateFromTimestamp = dayjs(dateFrom).unix() * 1000
|
||||
const dateToTimestamp = (dayjs(dateTo).unix() + 86399) * 1000
|
||||
// filter should still work if users get from/to backwards
|
||||
const from =
|
||||
dateFromTimestamp < dateToTimestamp
|
||||
? dateFromTimestamp
|
||||
: dateToTimestamp
|
||||
const to =
|
||||
dateToTimestamp > dateFromTimestamp
|
||||
? dateToTimestamp
|
||||
: dateFromTimestamp
|
||||
setNewFilters((prevSelected) => {
|
||||
return {
|
||||
...prevSelected,
|
||||
loadTimestamp: {
|
||||
condition: (date) => {
|
||||
const timestamp = dayjs(date).unix() * 1000
|
||||
return timestamp >= from && timestamp <= to
|
||||
},
|
||||
values: { from: from, to: to },
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
}, [dateFrom, dateTo])
|
||||
|
||||
const handleResetFilters = () => {
|
||||
setFilters({})
|
||||
setNewFilters({})
|
||||
setDateFrom(null)
|
||||
setDateTo(null)
|
||||
setSizeFrom('')
|
||||
setSizeTo('')
|
||||
setValueFrom('')
|
||||
setValueTo('')
|
||||
}
|
||||
|
||||
const updateFilters = (filters) => {
|
||||
setFilters(filters)
|
||||
onClose()
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal onClose={onClose} isOpen={isOpen}>
|
||||
<Modal.Header>
|
||||
<div className="flex w-full items-start justify-between pt-2">
|
||||
<ElementTitle noMarginBottom>
|
||||
{t('filter-trade-history')}
|
||||
</ElementTitle>
|
||||
<LinkButton
|
||||
className="flex items-center text-th-primary"
|
||||
onClick={() => handleResetFilters()}
|
||||
>
|
||||
<RefreshIcon className="mr-1.5 h-4 w-4" />
|
||||
{t('reset')}
|
||||
</LinkButton>
|
||||
</div>
|
||||
</Modal.Header>
|
||||
{showApiWarning ? (
|
||||
<div className="mt-1 mb-3">
|
||||
<InlineNotification
|
||||
type="warning"
|
||||
desc={t('trade-history-api-warning')}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
<div className="pb-4">
|
||||
<p className="font-bold text-th-fgd-1">{t('date')}</p>
|
||||
<div className="flex items-center space-x-2">
|
||||
<div className="w-full">
|
||||
<DateRangePicker
|
||||
startDate={dateFrom}
|
||||
setStartDate={setDateFrom}
|
||||
endDate={dateTo}
|
||||
setEndDate={setDateTo}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="pb-4">
|
||||
<Label>{t('markets')}</Label>
|
||||
<MultiSelectDropdown
|
||||
options={markets}
|
||||
selected={newFilters.marketName || []}
|
||||
toggleOption={toggleOption}
|
||||
/>
|
||||
</div>
|
||||
<div className="mb-4 flex items-center justify-between border-y border-th-bkg-4 py-3">
|
||||
<p className="mb-0 font-bold text-th-fgd-1">{t('side')}</p>
|
||||
<div className="flex space-x-2">
|
||||
<FilterButton
|
||||
filters={newFilters}
|
||||
filterKey="side"
|
||||
onClick={() => handleUpdateFilterButtons('side', 'buy')}
|
||||
value="buy"
|
||||
/>
|
||||
<FilterButton
|
||||
filters={newFilters}
|
||||
filterKey="side"
|
||||
onClick={() => handleUpdateFilterButtons('side', 'sell')}
|
||||
value="sell"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="pb-4">
|
||||
<p className="font-bold text-th-fgd-1">{t('size')}</p>
|
||||
<div className="flex items-center space-x-2">
|
||||
<div className="w-1/2">
|
||||
<Label>{t('from')}</Label>
|
||||
<Input
|
||||
type="number"
|
||||
min="0"
|
||||
placeholder="0.00"
|
||||
value={sizeFrom || ''}
|
||||
onChange={(e) => setSizeFrom(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="w-1/2">
|
||||
<Label>{t('to')}</Label>
|
||||
<Input
|
||||
type="number"
|
||||
min="0"
|
||||
placeholder="0.00"
|
||||
value={sizeTo || ''}
|
||||
onChange={(e) => setSizeTo(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="pb-4">
|
||||
<p className="font-bold text-th-fgd-1">{t('value')}</p>
|
||||
<div className="flex items-center space-x-2">
|
||||
<div className="w-1/2">
|
||||
<Label>{t('from')}</Label>
|
||||
<Input
|
||||
type="number"
|
||||
min="0"
|
||||
placeholder="0.00"
|
||||
value={valueFrom || ''}
|
||||
onChange={(e) => setValueFrom(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="w-1/2">
|
||||
<Label>{t('to')}</Label>
|
||||
<Input
|
||||
type="number"
|
||||
min="0"
|
||||
placeholder="0.00"
|
||||
value={valueTo || ''}
|
||||
onChange={(e) => setValueTo(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mb-6 flex items-center justify-between border-y border-th-bkg-4 py-3">
|
||||
<p className="mb-0 font-bold text-th-fgd-1">{t('liquidity')}</p>
|
||||
<div className="flex space-x-2">
|
||||
<FilterButton
|
||||
filters={newFilters}
|
||||
filterKey="liquidity"
|
||||
onClick={() => handleUpdateFilterButtons('liquidity', 'Maker')}
|
||||
value="Maker"
|
||||
/>
|
||||
<FilterButton
|
||||
filters={newFilters}
|
||||
filterKey="liquidity"
|
||||
onClick={() => handleUpdateFilterButtons('liquidity', 'Taker')}
|
||||
value="Taker"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<Button className="w-full" onClick={() => updateFilters(newFilters)}>
|
||||
{Object.keys(filters).length > 0 ? t('update-filters') : t('filter')}
|
||||
</Button>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
const FilterButton = ({ filters, filterKey, value, onClick }) => {
|
||||
const { t } = useTranslation('common')
|
||||
return (
|
||||
<button
|
||||
className={`default-transitions rounded-full border border-th-fgd-3 px-3 py-1 text-xs text-th-fgd-1 ${
|
||||
filters[filterKey]?.includes(value) &&
|
||||
'border-th-primary bg-th-primary text-th-bkg-1 md:hover:text-th-bkg-1'
|
||||
} md:hover:border-th-primary md:hover:text-th-primary`}
|
||||
onClick={onClick}
|
||||
>
|
||||
{t(value.toLowerCase())}
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
export default TradeHistoryFilterModal
|
|
@ -0,0 +1,630 @@
|
|||
import { ArrowSmDownIcon } from '@heroicons/react/solid'
|
||||
import BN from 'bn.js'
|
||||
import Link from 'next/link'
|
||||
import { useRouter } from 'next/router'
|
||||
import SideBadge from './SideBadge'
|
||||
import Button, { LinkButton } from './Button'
|
||||
import { useSortableData } from '../hooks/useSortableData'
|
||||
import { useViewport } from '../hooks/useViewport'
|
||||
import { breakpoints } from './TradePageGrid'
|
||||
import {
|
||||
Table,
|
||||
TableDateDisplay,
|
||||
Td,
|
||||
Th,
|
||||
TrBody,
|
||||
TrHead,
|
||||
} from './TableElements'
|
||||
import { ExpandableRow } from './TableElements'
|
||||
import { formatUsdValue } from '../utils'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import Pagination from './Pagination'
|
||||
import usePagination from '../hooks/usePagination'
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { useFilteredData } from '../hooks/useFilteredData'
|
||||
import TradeHistoryFilterModal from './TradeHistoryFilterModal'
|
||||
import {
|
||||
FilterIcon,
|
||||
InformationCircleIcon,
|
||||
RefreshIcon,
|
||||
SaveIcon,
|
||||
} from '@heroicons/react/solid'
|
||||
import { fetchHourlyPerformanceStats } from './account_page/AccountOverview'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
import Loading from './Loading'
|
||||
import { exportDataToCSV } from '../utils/export'
|
||||
import Tooltip from './Tooltip'
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
|
||||
const formatTradeDateTime = (timestamp: BN | string) => {
|
||||
// don't compare to BN because of npm maddness
|
||||
// prototypes can be different due to multiple versions being imported
|
||||
if (typeof timestamp === 'string') {
|
||||
return timestamp
|
||||
} else {
|
||||
return timestamp.toNumber() * 1000
|
||||
}
|
||||
}
|
||||
|
||||
const TradeHistoryTable = ({
|
||||
numTrades,
|
||||
showActions,
|
||||
}: {
|
||||
numTrades?: number
|
||||
showActions?: boolean
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
|
||||
const { asPath } = useRouter()
|
||||
const { width } = useViewport()
|
||||
const tradeHistoryAndLiquidations = useMangoStore(
|
||||
(state) => state.tradeHistory.parsed
|
||||
)
|
||||
const tradeHistory = tradeHistoryAndLiquidations.filter(
|
||||
(t) => !('liqor' in t)
|
||||
)
|
||||
const isMobile = width ? width < breakpoints.md : false
|
||||
const [filters, setFilters] = useState({})
|
||||
const [showFiltersModal, setShowFiltersModal] = useState(false)
|
||||
const [loadExportData, setLoadExportData] = useState(false)
|
||||
|
||||
const filteredData = useFilteredData(tradeHistory, filters)
|
||||
const initialLoad = useMangoStore((s) => s.tradeHistory.initialLoad)
|
||||
const { publicKey } = useWallet()
|
||||
|
||||
const {
|
||||
paginatedData,
|
||||
totalPages,
|
||||
nextPage,
|
||||
previousPage,
|
||||
page,
|
||||
firstPage,
|
||||
lastPage,
|
||||
setData,
|
||||
data,
|
||||
} = usePagination(filteredData, { perPage: 100 })
|
||||
const { items, requestSort, sortConfig } = useSortableData(paginatedData)
|
||||
|
||||
useEffect(() => {
|
||||
if (data?.length !== filteredData?.length) {
|
||||
setData(filteredData)
|
||||
}
|
||||
}, [filteredData])
|
||||
|
||||
const renderMarketName = (trade: any) => {
|
||||
if (
|
||||
trade.marketName.includes('PERP') ||
|
||||
trade.marketName.includes('USDC')
|
||||
) {
|
||||
const location = `/?name=${trade.marketName}`
|
||||
if (asPath.includes(location)) {
|
||||
return <span>{trade.marketName}</span>
|
||||
} else {
|
||||
return (
|
||||
<Link href={location} shallow={true}>
|
||||
<a className="text-th-fgd-1 underline hover:text-th-fgd-1 hover:no-underline">
|
||||
{trade.marketName}
|
||||
</a>
|
||||
</Link>
|
||||
)
|
||||
}
|
||||
} else {
|
||||
return <span>{trade.marketName}</span>
|
||||
}
|
||||
}
|
||||
|
||||
const exportPerformanceDataToCSV = async () => {
|
||||
if (!mangoAccount) return
|
||||
setLoadExportData(true)
|
||||
const exportData = await fetchHourlyPerformanceStats(
|
||||
mangoAccount.publicKey.toString(),
|
||||
10000
|
||||
)
|
||||
const dataToExport = exportData.map((row) => {
|
||||
const timestamp = new Date(row.time)
|
||||
return {
|
||||
timestamp: `${timestamp.toLocaleDateString()} ${timestamp.toLocaleTimeString()}`,
|
||||
account_equity: row.account_equity,
|
||||
pnl: row.pnl,
|
||||
}
|
||||
})
|
||||
|
||||
const title = `${
|
||||
mangoAccount.name || mangoAccount.publicKey
|
||||
}-Performance-${new Date().toLocaleDateString()}`
|
||||
const headers = ['Timestamp', 'Account Equity', 'PNL']
|
||||
|
||||
exportDataToCSV(dataToExport, title, headers, t)
|
||||
setLoadExportData(false)
|
||||
}
|
||||
|
||||
const hasActiveFilter = useMemo(() => {
|
||||
return tradeHistory.length !== filteredData.length
|
||||
}, [data, filteredData])
|
||||
|
||||
const mangoAccountPk = useMemo(() => {
|
||||
if (mangoAccount) {
|
||||
return mangoAccount.publicKey.toString()
|
||||
}
|
||||
}, [mangoAccount])
|
||||
|
||||
const canWithdraw =
|
||||
mangoAccount && publicKey ? mangoAccount.owner.equals(publicKey) : false
|
||||
|
||||
return (
|
||||
<>
|
||||
{showActions ? (
|
||||
<div className="flex items-center justify-between pb-3">
|
||||
<div className="flex items-center">
|
||||
<h4 className="mb-0 flex items-center text-th-fgd-1">
|
||||
{data.length === 1
|
||||
? t('number-trade', {
|
||||
number: !initialLoad ? (
|
||||
<Loading className="mr-2" />
|
||||
) : (
|
||||
data.length
|
||||
),
|
||||
})
|
||||
: t('number-trades', {
|
||||
number: !initialLoad ? (
|
||||
<Loading className="mr-2" />
|
||||
) : (
|
||||
data.length
|
||||
),
|
||||
})}
|
||||
</h4>
|
||||
|
||||
{mangoAccount ? (
|
||||
<Tooltip
|
||||
content={
|
||||
<div className="mr-4 text-xs text-th-fgd-3">
|
||||
{t('delay-displaying-recent')} {t('use-explorer-one')}
|
||||
<a
|
||||
href={`https://explorer.solana.com/address/${mangoAccount.publicKey.toString()}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
{t('use-explorer-two')}
|
||||
</a>
|
||||
{t('use-explorer-three')}
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<InformationCircleIcon className="ml-1.5 h-5 w-5 cursor-pointer text-th-fgd-4" />
|
||||
</Tooltip>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<div className="flex items-center space-x-3">
|
||||
{hasActiveFilter ? (
|
||||
<LinkButton
|
||||
className="order-4 mt-3 flex items-center justify-end whitespace-nowrap text-xs sm:order-first sm:mt-0"
|
||||
onClick={() => setFilters({})}
|
||||
>
|
||||
<RefreshIcon className="mr-1.5 h-4 w-4 flex-shrink-0" />
|
||||
{t('reset-filters')}
|
||||
</LinkButton>
|
||||
) : null}
|
||||
{tradeHistory.length >= 15 && initialLoad ? (
|
||||
<Button
|
||||
className="order-3 flex h-8 items-center justify-center whitespace-nowrap pt-0 pb-0 pl-3 pr-3 text-xs sm:order-first"
|
||||
onClick={() => setShowFiltersModal(true)}
|
||||
>
|
||||
<FilterIcon className="mr-1.5 h-4 w-4" />
|
||||
{t('filter')}
|
||||
</Button>
|
||||
) : null}
|
||||
{canWithdraw && !isMobile ? (
|
||||
<Button
|
||||
className={`flex h-8 items-center justify-center whitespace-nowrap pt-0 pb-0 pl-3 pr-3 text-xs`}
|
||||
onClick={exportPerformanceDataToCSV}
|
||||
>
|
||||
{loadExportData ? (
|
||||
<Loading />
|
||||
) : (
|
||||
<div className={`flex items-center`}>
|
||||
<SaveIcon className={`mr-1.5 h-4 w-4`} />
|
||||
{t('export-pnl-csv')}
|
||||
</div>
|
||||
)}
|
||||
</Button>
|
||||
) : null}
|
||||
{canWithdraw && mangoAccount && !isMobile ? (
|
||||
<div className={`flex items-center`}>
|
||||
<a
|
||||
className={`default-transition flex h-8 w-full items-center justify-center whitespace-nowrap rounded-full bg-th-bkg-button pt-0 pb-0 pl-3 pr-3 text-xs font-bold text-th-fgd-1 hover:text-th-fgd-1 hover:brightness-[1.1]`}
|
||||
href={`https://event-history-api.herokuapp.com/all_trades_csv?mango_account=${mangoAccountPk}&open_orders=${mangoAccount.spotOpenOrders
|
||||
.filter(
|
||||
(e) => e.toString() !== '11111111111111111111111111111111'
|
||||
)
|
||||
.join(',')}`}
|
||||
download
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<SaveIcon className={`mr-1.5 h-4 w-4`} />
|
||||
{t('export-trades-csv')}
|
||||
<Tooltip content={t('trade-export-disclaimer')}>
|
||||
<InformationCircleIcon className="ml-1.5 h-5 w-5 cursor-help text-th-fgd-4" />
|
||||
</Tooltip>
|
||||
</a>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
<div className={`flex flex-col sm:pb-4`}>
|
||||
<div className={`overflow-x-auto sm:-mx-6 lg:-mx-8`}>
|
||||
<div
|
||||
className={`inline-block min-w-full align-middle sm:px-6 lg:px-8`}
|
||||
>
|
||||
{tradeHistory && paginatedData.length > 0 ? (
|
||||
!isMobile ? (
|
||||
<>
|
||||
<Table>
|
||||
<thead>
|
||||
<TrHead>
|
||||
<Th>
|
||||
<LinkButton
|
||||
className="flex items-center no-underline"
|
||||
onClick={() => requestSort('marketName')}
|
||||
>
|
||||
<span className="font-normal">{t('market')}</span>
|
||||
<ArrowSmDownIcon
|
||||
className={`default-transition ml-1 h-4 w-4 flex-shrink-0 ${
|
||||
sortConfig?.key === 'marketName'
|
||||
? sortConfig.direction === 'ascending'
|
||||
? 'rotate-180 transform'
|
||||
: 'rotate-360 transform'
|
||||
: null
|
||||
}`}
|
||||
/>
|
||||
</LinkButton>
|
||||
</Th>
|
||||
<Th>
|
||||
<LinkButton
|
||||
className="flex items-center no-underline"
|
||||
onClick={() => requestSort('side')}
|
||||
>
|
||||
<span className="font-normal">{t('side')}</span>
|
||||
<ArrowSmDownIcon
|
||||
className={`default-transition ml-1 h-4 w-4 flex-shrink-0 ${
|
||||
sortConfig?.key === 'side'
|
||||
? sortConfig.direction === 'ascending'
|
||||
? 'rotate-180 transform'
|
||||
: 'rotate-360 transform'
|
||||
: null
|
||||
}`}
|
||||
/>
|
||||
</LinkButton>
|
||||
</Th>
|
||||
<Th>
|
||||
<LinkButton
|
||||
className="flex items-center no-underline"
|
||||
onClick={() => requestSort('size')}
|
||||
>
|
||||
<span className="font-normal">{t('size')}</span>
|
||||
<ArrowSmDownIcon
|
||||
className={`default-transition ml-1 h-4 w-4 flex-shrink-0 ${
|
||||
sortConfig?.key === 'size'
|
||||
? sortConfig.direction === 'ascending'
|
||||
? 'rotate-180 transform'
|
||||
: 'rotate-360 transform'
|
||||
: null
|
||||
}`}
|
||||
/>
|
||||
</LinkButton>
|
||||
</Th>
|
||||
<Th>
|
||||
<LinkButton
|
||||
className="flex items-center no-underline"
|
||||
onClick={() => requestSort('price')}
|
||||
>
|
||||
<span className="font-normal">{t('price')}</span>
|
||||
<ArrowSmDownIcon
|
||||
className={`default-transition ml-1 h-4 w-4 flex-shrink-0 ${
|
||||
sortConfig?.key === 'price'
|
||||
? sortConfig.direction === 'ascending'
|
||||
? 'rotate-180 transform'
|
||||
: 'rotate-360 transform'
|
||||
: null
|
||||
}`}
|
||||
/>
|
||||
</LinkButton>
|
||||
</Th>
|
||||
<Th>
|
||||
<LinkButton
|
||||
className="flex items-center no-underline"
|
||||
onClick={() => requestSort('value')}
|
||||
>
|
||||
<span className="font-normal">{t('value')}</span>
|
||||
<ArrowSmDownIcon
|
||||
className={`default-transition ml-1 h-4 w-4 flex-shrink-0 ${
|
||||
sortConfig?.key === 'value'
|
||||
? sortConfig.direction === 'ascending'
|
||||
? 'rotate-180 transform'
|
||||
: 'rotate-360 transform'
|
||||
: null
|
||||
}`}
|
||||
/>
|
||||
</LinkButton>
|
||||
</Th>
|
||||
<Th>
|
||||
<LinkButton
|
||||
className="flex items-center no-underline"
|
||||
onClick={() => requestSort('liquidity')}
|
||||
>
|
||||
<span className="font-normal">
|
||||
{t('liquidity')}
|
||||
</span>
|
||||
<ArrowSmDownIcon
|
||||
className={`default-transition ml-1 h-4 w-4 flex-shrink-0 ${
|
||||
sortConfig?.key === 'liquidity'
|
||||
? sortConfig.direction === 'ascending'
|
||||
? 'rotate-180 transform'
|
||||
: 'rotate-360 transform'
|
||||
: null
|
||||
}`}
|
||||
/>
|
||||
</LinkButton>
|
||||
</Th>
|
||||
<Th>
|
||||
<LinkButton
|
||||
className="flex items-center no-underline"
|
||||
onClick={() => requestSort('feeCost')}
|
||||
>
|
||||
<span className="font-normal">{t('fee')}</span>
|
||||
<ArrowSmDownIcon
|
||||
className={`default-transition ml-1 h-4 w-4 flex-shrink-0 ${
|
||||
sortConfig?.key === 'feeCost'
|
||||
? sortConfig.direction === 'ascending'
|
||||
? 'rotate-180 transform'
|
||||
: 'rotate-360 transform'
|
||||
: null
|
||||
}`}
|
||||
/>
|
||||
</LinkButton>
|
||||
</Th>
|
||||
<Th>
|
||||
<LinkButton
|
||||
className="flex items-center no-underline"
|
||||
onClick={() => requestSort('loadTimestamp')}
|
||||
>
|
||||
<span className="font-normal">
|
||||
{t('approximate-time')}
|
||||
</span>
|
||||
<ArrowSmDownIcon
|
||||
className={`default-transition ml-1 h-4 w-4 flex-shrink-0 ${
|
||||
sortConfig?.key === 'loadTimestamp'
|
||||
? sortConfig.direction === 'ascending'
|
||||
? 'rotate-180 transform'
|
||||
: 'rotate-360 transform'
|
||||
: null
|
||||
}`}
|
||||
/>
|
||||
</LinkButton>
|
||||
</Th>
|
||||
</TrHead>
|
||||
</thead>
|
||||
<tbody>
|
||||
{items.map((trade: any) => {
|
||||
return (
|
||||
<TrBody key={`${trade.seqNum}${trade.marketName}`}>
|
||||
<Td className="!py-2 ">
|
||||
<div className="flex items-center">
|
||||
<img
|
||||
alt=""
|
||||
width="20"
|
||||
height="20"
|
||||
src={`/assets/icons/${trade.marketName
|
||||
.split(/-|\//)[0]
|
||||
.toLowerCase()}.svg`}
|
||||
className={`mr-2.5`}
|
||||
/>
|
||||
{renderMarketName(trade)}
|
||||
</div>
|
||||
</Td>
|
||||
<Td className="!py-2 ">
|
||||
<SideBadge side={trade.side} />
|
||||
</Td>
|
||||
<Td className="!py-2 ">{trade.size}</Td>
|
||||
<Td className="!py-2 ">
|
||||
{formatUsdValue(trade.price, trade.symbol)}
|
||||
</Td>
|
||||
<Td className="!py-2 ">
|
||||
{formatUsdValue(trade.value)}
|
||||
</Td>
|
||||
<Td className="!py-2 ">
|
||||
{t(trade.liquidity.toLowerCase())}
|
||||
</Td>
|
||||
<Td className="!py-2 ">
|
||||
{formatUsdValue(trade.feeCost)}
|
||||
</Td>
|
||||
<Td className="!py-2">
|
||||
{trade.loadTimestamp || trade.timestamp ? (
|
||||
<TableDateDisplay
|
||||
date={formatTradeDateTime(
|
||||
trade.loadTimestamp || trade.timestamp
|
||||
)}
|
||||
showSeconds
|
||||
/>
|
||||
) : (
|
||||
t('recent')
|
||||
)}
|
||||
</Td>
|
||||
<Td className="keep-break w-[0.1%] !py-2">
|
||||
{trade.marketName.includes('PERP') ? (
|
||||
<a
|
||||
className="text-xs text-th-fgd-4 underline underline-offset-4"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
href={`/account?pubkey=${
|
||||
trade.liquidity === 'Taker'
|
||||
? trade.maker
|
||||
: trade.taker
|
||||
}`}
|
||||
>
|
||||
{t('view-counterparty')}
|
||||
</a>
|
||||
) : null}
|
||||
</Td>
|
||||
</TrBody>
|
||||
)
|
||||
})}
|
||||
</tbody>
|
||||
</Table>
|
||||
{numTrades && items.length > numTrades ? (
|
||||
<div className="mt-4 flex items-center justify-center">
|
||||
<Link href="/account" shallow={true}>
|
||||
{t('view-all-trades')}
|
||||
</Link>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex items-center justify-end">
|
||||
<Pagination
|
||||
page={page}
|
||||
totalPages={totalPages}
|
||||
nextPage={nextPage}
|
||||
lastPage={lastPage}
|
||||
firstPage={firstPage}
|
||||
previousPage={previousPage}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
<div className="mb-6">
|
||||
<div className="border-b border-th-bkg-3">
|
||||
{paginatedData.map((trade: any, index) => (
|
||||
<ExpandableRow
|
||||
buttonTemplate={
|
||||
<>
|
||||
<div className="text-fgd-1 flex w-full items-center justify-between">
|
||||
<div className="text-left">
|
||||
{trade.loadTimestamp || trade.timestamp ? (
|
||||
<TableDateDisplay
|
||||
date={formatTradeDateTime(
|
||||
trade.loadTimestamp || trade.timestamp
|
||||
)}
|
||||
showSeconds
|
||||
/>
|
||||
) : (
|
||||
t('recent')
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-right">
|
||||
<div className="mb-0.5 flex items-center text-left">
|
||||
<img
|
||||
alt=""
|
||||
width="16"
|
||||
height="16"
|
||||
src={`/assets/icons/${trade.marketName
|
||||
.split(/-|\//)[0]
|
||||
.toLowerCase()}.svg`}
|
||||
className={`mr-1.5`}
|
||||
/>
|
||||
{trade.marketName}
|
||||
</div>
|
||||
<div className="text-xs text-th-fgd-3">
|
||||
<span
|
||||
className={`mr-1
|
||||
${
|
||||
trade.side === 'buy' || trade.side === 'long'
|
||||
? 'text-th-green'
|
||||
: 'text-th-red'
|
||||
}
|
||||
`}
|
||||
>
|
||||
{trade.side.toUpperCase()}
|
||||
</span>
|
||||
{trade.size.toLocaleString()}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
}
|
||||
key={`${index}`}
|
||||
panelTemplate={
|
||||
<div className="grid grid-flow-row grid-cols-2 gap-4">
|
||||
<div className="text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('price')}
|
||||
</div>
|
||||
{formatUsdValue(trade.price)}
|
||||
</div>
|
||||
<div className="text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('value')}
|
||||
</div>
|
||||
{formatUsdValue(trade.value)}
|
||||
</div>
|
||||
<div className="text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('liquidity')}
|
||||
</div>
|
||||
{trade.liquidity}
|
||||
</div>
|
||||
<div className="text-left">
|
||||
<div className="pb-0.5 text-xs text-th-fgd-3">
|
||||
{t('fee')}
|
||||
</div>
|
||||
{formatUsdValue(trade.feeCost)}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
{numTrades && items.length > numTrades ? (
|
||||
<div className="mt-4 flex items-center justify-center">
|
||||
<Link href="/account" shallow={true}>
|
||||
{t('view-all-trades')}
|
||||
</Link>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex items-center justify-center md:justify-end">
|
||||
<Pagination
|
||||
page={page}
|
||||
totalPages={totalPages}
|
||||
nextPage={nextPage}
|
||||
lastPage={lastPage}
|
||||
firstPage={firstPage}
|
||||
previousPage={previousPage}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
) : hasActiveFilter ? (
|
||||
<div className="w-full rounded-md border border-th-bkg-3 py-6 text-center text-th-fgd-3">
|
||||
{t('no-trades-found')}
|
||||
</div>
|
||||
) : (
|
||||
<div className="w-full rounded-md border border-th-bkg-3 py-6 text-center text-th-fgd-3">
|
||||
{t('no-history')}
|
||||
{asPath === '/account' ? (
|
||||
<Link href={'/'} shallow={true}>
|
||||
<a className="ml-2 inline-flex py-0">{t('make-trade')}</a>
|
||||
</Link>
|
||||
) : null}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{showFiltersModal ? (
|
||||
<TradeHistoryFilterModal
|
||||
filters={filters}
|
||||
setFilters={setFilters}
|
||||
isOpen={showFiltersModal}
|
||||
onClose={() => setShowFiltersModal(false)}
|
||||
showApiWarning={tradeHistory.length > 10000}
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default TradeHistoryTable
|
|
@ -0,0 +1,256 @@
|
|||
import {
|
||||
Fragment,
|
||||
FunctionComponent,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
} from 'react'
|
||||
import { Popover, Transition } from '@headlessui/react'
|
||||
import { useTranslation } from 'next-i18next'
|
||||
import { StarIcon } from '@heroicons/react/outline'
|
||||
import {
|
||||
ChevronDownIcon,
|
||||
StarIcon as FilledStarIcon,
|
||||
} from '@heroicons/react/solid'
|
||||
import useLocalStorageState from '../hooks/useLocalStorageState'
|
||||
import MarketNavItem from './MarketNavItem'
|
||||
import useMangoStore from '../stores/useMangoStore'
|
||||
|
||||
const initialMenuCategories = [
|
||||
{ name: 'Futures', desc: 'perp-desc' },
|
||||
{ name: 'Spot', desc: 'spot-desc' },
|
||||
]
|
||||
|
||||
export const FAVORITE_MARKETS_KEY = 'favoriteMarkets-0.1'
|
||||
|
||||
const TradeNavMenu = () => {
|
||||
const [favoriteMarkets] = useLocalStorageState(FAVORITE_MARKETS_KEY, [])
|
||||
const [activeMenuCategory, setActiveMenuCategory] = useState('Futures')
|
||||
const [menuCategories, setMenuCategories] = useState(initialMenuCategories)
|
||||
const buttonRef = useRef<HTMLButtonElement>(null)
|
||||
const { t } = useTranslation('common')
|
||||
|
||||
const marketsInfo = useMangoStore((s) => s.marketsInfo)
|
||||
|
||||
const perpMarketsInfo = useMemo(
|
||||
() =>
|
||||
marketsInfo
|
||||
.filter((mkt) => mkt?.name.includes('PERP'))
|
||||
.sort((a, b) => b.volumeUsd24h - a.volumeUsd24h),
|
||||
[marketsInfo]
|
||||
)
|
||||
|
||||
const spotMarketsInfo = useMemo(
|
||||
() =>
|
||||
marketsInfo
|
||||
.filter((mkt) => mkt?.name.includes('USDC'))
|
||||
.sort((a, b) => b.volumeUsd24h - a.volumeUsd24h),
|
||||
[marketsInfo]
|
||||
)
|
||||
|
||||
const markets = useMemo(
|
||||
() =>
|
||||
activeMenuCategory === 'Futures'
|
||||
? perpMarketsInfo
|
||||
: activeMenuCategory === 'Spot'
|
||||
? spotMarketsInfo
|
||||
: marketsInfo.filter((mkt) => favoriteMarkets.includes(mkt.name)),
|
||||
[activeMenuCategory, marketsInfo, favoriteMarkets]
|
||||
)
|
||||
|
||||
const handleMenuCategoryChange = (categoryName) => {
|
||||
setActiveMenuCategory(categoryName)
|
||||
}
|
||||
|
||||
const toggleMenu = () => {
|
||||
buttonRef?.current?.click()
|
||||
if (favoriteMarkets.length > 0) {
|
||||
setActiveMenuCategory('Favorites')
|
||||
} else {
|
||||
setActiveMenuCategory('Futures')
|
||||
}
|
||||
}
|
||||
|
||||
const onHoverMenu = (open, action) => {
|
||||
if (
|
||||
(!open && action === 'onMouseEnter') ||
|
||||
(open && action === 'onMouseLeave')
|
||||
) {
|
||||
toggleMenu()
|
||||
}
|
||||
}
|
||||
|
||||
const handleClickOutside = (event) => {
|
||||
if (buttonRef.current && !buttonRef.current.contains(event.target)) {
|
||||
event.stopPropagation()
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
document.addEventListener('mousedown', handleClickOutside)
|
||||
|
||||
return () => {
|
||||
document.removeEventListener('mousedown', handleClickOutside)
|
||||
}
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (favoriteMarkets.length > 0 && menuCategories.length === 2) {
|
||||
const newCategories = [{ name: 'Favorites', desc: '' }, ...menuCategories]
|
||||
setMenuCategories(newCategories)
|
||||
}
|
||||
if (favoriteMarkets.length === 0 && menuCategories.length === 3) {
|
||||
setMenuCategories(
|
||||
menuCategories.filter((cat) => cat.name !== 'Favorites')
|
||||
)
|
||||
if (activeMenuCategory === 'Favorites') {
|
||||
setActiveMenuCategory('Futures')
|
||||
}
|
||||
}
|
||||
}, [favoriteMarkets])
|
||||
|
||||
return (
|
||||
<Popover>
|
||||
{({ open }) => (
|
||||
<div
|
||||
onMouseEnter={() => onHoverMenu(open, 'onMouseEnter')}
|
||||
onMouseLeave={() => onHoverMenu(open, 'onMouseLeave')}
|
||||
className="relative z-50 flex flex-col"
|
||||
>
|
||||
<Popover.Button
|
||||
className={`-mr-3 rounded-none px-3 transition-none focus:bg-th-bkg-3 focus:outline-none ${
|
||||
open && 'bg-th-bkg-3'
|
||||
}`}
|
||||
ref={buttonRef}
|
||||
>
|
||||
<div
|
||||
className={`flex h-14 items-center rounded-none font-bold hover:text-th-primary`}
|
||||
>
|
||||
<span>{t('trade')}</span>
|
||||
<ChevronDownIcon
|
||||
className={`default-transition ml-0.5 h-5 w-5 ${
|
||||
open ? 'rotate-180 transform' : 'rotate-360 transform'
|
||||
}`}
|
||||
/>
|
||||
</div>
|
||||
</Popover.Button>
|
||||
<Transition
|
||||
appear={true}
|
||||
show={open}
|
||||
as={Fragment}
|
||||
enter="transition-all ease-in duration-100"
|
||||
enterFrom="opacity-0 transform scale-90"
|
||||
enterTo="opacity-100 transform scale-100"
|
||||
leave="transition ease-out duration-100"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<Popover.Panel className="absolute top-14 grid min-h-[235px] w-[760px] grid-cols-3 grid-rows-1">
|
||||
<div className="col-span-1 rounded-bl-lg bg-th-bkg-4">
|
||||
<MenuCategories
|
||||
activeCategory={activeMenuCategory}
|
||||
categories={menuCategories}
|
||||
onChange={handleMenuCategoryChange}
|
||||
/>
|
||||
</div>
|
||||
<div className="col-span-2 rounded-br-lg bg-th-bkg-3 p-4">
|
||||
<div className="grid grid-flow-row grid-cols-2 gap-x-6">
|
||||
{markets.map((mkt) => (
|
||||
<MarketNavItem
|
||||
buttonRef={buttonRef}
|
||||
market={mkt}
|
||||
key={mkt.name}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</Popover.Panel>
|
||||
</Transition>
|
||||
</div>
|
||||
)}
|
||||
</Popover>
|
||||
)
|
||||
}
|
||||
|
||||
export default TradeNavMenu
|
||||
|
||||
interface MenuCategoriesProps {
|
||||
activeCategory: string
|
||||
onChange: (x) => void
|
||||
categories: Array<any>
|
||||
}
|
||||
|
||||
const MenuCategories: FunctionComponent<MenuCategoriesProps> = ({
|
||||
activeCategory,
|
||||
onChange,
|
||||
categories,
|
||||
}) => {
|
||||
const { t } = useTranslation('common')
|
||||
|
||||
return (
|
||||
<div className={`relative`}>
|
||||
<div
|
||||
className={`default-transition absolute top-0 left-0 z-10 w-0.5 bg-th-primary`}
|
||||
style={{
|
||||
transform: `translateY(${
|
||||
categories.findIndex((cat) => cat.name === activeCategory) * 100
|
||||
}%)`,
|
||||
height: `${100 / categories.length}%`,
|
||||
}}
|
||||
/>
|
||||
{categories.map((cat) => {
|
||||
return (
|
||||
<button
|
||||
key={cat.name}
|
||||
onClick={() => onChange(cat.name)}
|
||||
onMouseEnter={() => onChange(cat.name)}
|
||||
className={`default-transition relative flex h-14 w-full cursor-pointer flex-col justify-center whitespace-nowrap rounded-none px-4 font-bold md:hover:bg-th-bkg-3 ${
|
||||
activeCategory === cat.name
|
||||
? `bg-th-bkg-3 text-th-primary`
|
||||
: `text-th-fgd-2 md:hover:text-th-primary`
|
||||
}
|
||||
`}
|
||||
>
|
||||
{t(cat.name.toLowerCase().replace(' ', '-'))}
|
||||
<div className="text-xs font-normal text-th-fgd-4">
|
||||
{t(cat.desc)}
|
||||
</div>
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export const FavoriteMarketButton = ({ market }) => {
|
||||
const [favoriteMarkets, setFavoriteMarkets] = useLocalStorageState(
|
||||
FAVORITE_MARKETS_KEY,
|
||||
[]
|
||||
)
|
||||
|
||||
const addToFavorites = (mkt) => {
|
||||
const newFavorites: any = [...favoriteMarkets, mkt]
|
||||
setFavoriteMarkets(newFavorites)
|
||||
}
|
||||
|
||||
const removeFromFavorites = (mkt) => {
|
||||
setFavoriteMarkets(favoriteMarkets.filter((m) => m !== mkt))
|
||||
}
|
||||
|
||||
return favoriteMarkets.find((mkt) => mkt === market.name) ? (
|
||||
<button
|
||||
className="default-transition flex items-center justify-center text-th-primary md:hover:text-th-fgd-3"
|
||||
onClick={() => removeFromFavorites(market.name)}
|
||||
>
|
||||
<FilledStarIcon className="h-5 w-5" />
|
||||
</button>
|
||||
) : (
|
||||
<button
|
||||
className="default-transition flex items-center justify-center text-th-fgd-4 md:hover:text-th-primary"
|
||||
onClick={() => addToFavorites(market.name)}
|
||||
>
|
||||
<StarIcon className="h-5 w-5" />
|
||||
</button>
|
||||
)
|
||||
}
|
|
@ -0,0 +1,155 @@
|
|||
import dynamic from "next/dynamic";
|
||||
import { Responsive, WidthProvider } from "react-grid-layout";
|
||||
import round from "lodash/round";
|
||||
import max from "lodash/max";
|
||||
import MobileTradePage from "./mobile/MobileTradePage";
|
||||
|
||||
const TVChartContainer = dynamic(() => import("./TVChartContainer"), {
|
||||
ssr: false,
|
||||
});
|
||||
import { useEffect, useState } from "react";
|
||||
import FloatingElement from "../components/FloatingElement";
|
||||
import UserInfo from "./UserInfo";
|
||||
import RecentMarketTrades from "./RecentMarketTrades";
|
||||
import useMangoStore from "../stores/useMangoStore";
|
||||
import useLocalStorageState from "../hooks/useLocalStorageState";
|
||||
import { useViewport } from "../hooks/useViewport";
|
||||
import MarketDetails from "./MarketDetails";
|
||||
|
||||
const ResponsiveGridLayout = WidthProvider(Responsive);
|
||||
|
||||
export const defaultLayouts = {
|
||||
xxl: [
|
||||
{ i: "tvChart", x: 0, y: 0, w: 8, h: 19 },
|
||||
{ i: "tradeForm", x: 8, y: 0, w: 2, h: 19 },
|
||||
{ i: "orderbook", x: 10, y: 0, w: 2, h: 25 },
|
||||
{ i: "marketTrades", x: 10, y: 1, w: 2, h: 13 },
|
||||
{ i: "userInfo", x: 0, y: 1, w: 10, h: 19 },
|
||||
],
|
||||
xl: [
|
||||
{ i: "tvChart", x: 0, y: 0, w: 6, h: 19, minW: 2 },
|
||||
{ i: "tradeForm", x: 6, y: 0, w: 3, h: 19, minW: 3 },
|
||||
{ i: "orderbook", x: 9, y: 0, w: 3, h: 25, minW: 2 },
|
||||
{ i: "marketTrades", x: 9, y: 0, w: 3, h: 13, minW: 2 },
|
||||
{ i: "userInfo", x: 0, y: 1, w: 9, h: 19, minW: 6 },
|
||||
],
|
||||
lg: [
|
||||
{ i: "tvChart", x: 0, y: 0, w: 6, h: 19, minW: 2 },
|
||||
{ i: "tradeForm", x: 6, y: 0, w: 3, h: 19, minW: 2 },
|
||||
{ i: "orderbook", x: 9, y: 0, w: 3, h: 25, minW: 2 },
|
||||
{ i: "marketTrades", x: 9, y: 1, w: 3, h: 13, minW: 2 },
|
||||
{ i: "userInfo", x: 0, y: 1, w: 9, h: 19, minW: 6 },
|
||||
],
|
||||
md: [
|
||||
{ i: "tvChart", x: 0, y: 0, w: 12, h: 16, minW: 2 },
|
||||
{ i: "tradeForm", x: 0, y: 1, w: 4, h: 22, minW: 3 },
|
||||
{ i: "orderbook", x: 4, y: 1, w: 4, h: 22, minW: 2 },
|
||||
{ i: "marketTrades", x: 8, y: 1, w: 4, h: 22, minW: 2 },
|
||||
{ i: "userInfo", x: 0, y: 2, w: 12, h: 19, minW: 6 },
|
||||
],
|
||||
sm: [
|
||||
{ i: "tvChart", x: 0, y: 0, w: 12, h: 20, minW: 6 },
|
||||
{ i: "tradeForm", x: 0, y: 1, w: 12, h: 17, minW: 6 },
|
||||
{ i: "orderbook", x: 0, y: 2, w: 6, h: 22, minW: 3 },
|
||||
{ i: "marketTrades", x: 6, y: 2, w: 6, h: 22, minW: 3 },
|
||||
{ i: "userInfo", x: 0, y: 3, w: 12, h: 19, minW: 6 },
|
||||
],
|
||||
};
|
||||
|
||||
export const GRID_LAYOUT_KEY = "mangoSavedLayouts-3.2.0";
|
||||
export const breakpoints = { xxl: 1600, xl: 1440, lg: 1170, md: 960, sm: 768 };
|
||||
|
||||
const getCurrentBreakpoint = () => {
|
||||
return Responsive.utils.getBreakpointFromWidth(
|
||||
breakpoints,
|
||||
window.innerWidth - 63
|
||||
);
|
||||
};
|
||||
|
||||
const TradePageGrid: React.FC = () => {
|
||||
const [mounted, setMounted] = useState(false);
|
||||
const { uiLocked } = useMangoStore((s) => s.settings);
|
||||
const [savedLayouts, setSavedLayouts] = useLocalStorageState(
|
||||
GRID_LAYOUT_KEY,
|
||||
defaultLayouts
|
||||
);
|
||||
const { width } = useViewport();
|
||||
const isMobile = width ? width < breakpoints.sm : false;
|
||||
|
||||
const onLayoutChange = (layouts) => {
|
||||
if (layouts) {
|
||||
setSavedLayouts(layouts);
|
||||
}
|
||||
};
|
||||
|
||||
const [orderbookDepth, setOrderbookDepth] = useState(10);
|
||||
const [currentBreakpoint, setCurrentBreakpoint] = useState<string | null>(
|
||||
null
|
||||
);
|
||||
|
||||
const onBreakpointChange = (newBreakpoint: string) => {
|
||||
setCurrentBreakpoint(newBreakpoint);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const adjustOrderBook = (layouts, breakpoint?: string | null) => {
|
||||
const bp = breakpoint ? breakpoint : getCurrentBreakpoint();
|
||||
const orderbookLayout = layouts[bp].find((obj) => {
|
||||
return obj.i === "orderbook";
|
||||
});
|
||||
let depth = orderbookLayout.h * 0.921 - 5;
|
||||
const maxNum = max([1, depth]);
|
||||
if (typeof maxNum === "number") {
|
||||
depth = round(maxNum);
|
||||
}
|
||||
setOrderbookDepth(depth);
|
||||
};
|
||||
|
||||
adjustOrderBook(savedLayouts, currentBreakpoint);
|
||||
}, [currentBreakpoint, savedLayouts]);
|
||||
|
||||
useEffect(() => setMounted(true), []);
|
||||
|
||||
if (!mounted) return null;
|
||||
|
||||
return !isMobile ? (
|
||||
<>
|
||||
<div className="pt-2">
|
||||
<MarketDetails />
|
||||
</div>
|
||||
<ResponsiveGridLayout
|
||||
layouts={savedLayouts ? savedLayouts : defaultLayouts}
|
||||
breakpoints={breakpoints}
|
||||
cols={{ xxl: 12, xl: 12, lg: 12, md: 12, sm: 12 }}
|
||||
rowHeight={15}
|
||||
isDraggable={!uiLocked}
|
||||
isResizable={!uiLocked}
|
||||
onBreakpointChange={(newBreakpoint) =>
|
||||
onBreakpointChange(newBreakpoint)
|
||||
}
|
||||
onLayoutChange={(layout, layouts) => onLayoutChange(layouts)}
|
||||
measureBeforeMount
|
||||
>
|
||||
<div key="tvChart">
|
||||
<FloatingElement className="h-full pl-0 md:pl-0 md:pr-1 md:pb-1 md:pt-2.5">
|
||||
<TVChartContainer />
|
||||
</FloatingElement>
|
||||
</div>
|
||||
<div key="marketTrades">
|
||||
<FloatingElement className="h-full">
|
||||
<RecentMarketTrades />
|
||||
</FloatingElement>
|
||||
</div>
|
||||
<div key="userInfo">
|
||||
<FloatingElement className="h-full">
|
||||
<UserInfo />
|
||||
</FloatingElement>
|
||||
</div>
|
||||
</ResponsiveGridLayout>
|
||||
</>
|
||||
) : (
|
||||
<MobileTradePage />
|
||||
);
|
||||
};
|
||||
|
||||
export default TradePageGrid;
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue